Project

General

Profile

Download (4.69 KB) Statistics
| Branch: | Revision:

os-android / src / main / java / org / distorted / os / OSInterface.java @ e3023b62

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.os;
11

    
12
import android.app.Activity;
13
import android.util.DisplayMetrics;
14
import android.view.MotionEvent;
15

    
16
import java.lang.ref.WeakReference;
17
import org.distorted.objectlib.helpers.OperatingSystemInterface;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public class OSInterface implements OperatingSystemInterface
22
{
23

    
24
  private final WeakReference<Activity> mAct;
25
  private MotionEvent mEvent;
26
  private int mPointer1, mPointer2;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
  public OSInterface(Activity act)
31
    {
32
    mAct = new WeakReference<>(act);
33
    }
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
  public void setMotionEvent(MotionEvent event)
38
    {
39
    mEvent = event;
40
    }
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  public int getScreenDensity()
45
    {
46
    DisplayMetrics dm = new DisplayMetrics();
47
    mAct.get().getWindowManager().getDefaultDisplay().getMetrics(dm);
48
    return dm.densityDpi;
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public int getAction()
54
    {
55
    switch( mEvent.getActionMasked() )
56
      {
57
      case( MotionEvent.ACTION_DOWN        ): return ACTION_DOWN_1;
58
      case( MotionEvent.ACTION_UP          ): return ACTION_UP_1;
59
      case( MotionEvent.ACTION_POINTER_DOWN): return ACTION_DOWN_2;
60
      case( MotionEvent.ACTION_POINTER_UP  ): return ACTION_UP_2;
61
      case( MotionEvent.ACTION_MOVE        ): return ACTION_MOVE;
62
      }
63

    
64
    return 0;
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  public void upOneOfThem()
70
    {
71
    int index = mEvent.getActionIndex();
72

    
73
         if( index==mEvent.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
74
    else if( index==mEvent.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  public boolean isFirstPressed()
80
    {
81
    return mPointer1!=INVALID_POINTER_ID;
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  public boolean isSecondPressed()
87
    {
88
    return mPointer2!=INVALID_POINTER_ID;
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  public void pressFirst()
94
    {
95
    mPointer1 = mEvent.getPointerId(0);
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public void unpressFirst()
101
    {
102
    mPointer1 = INVALID_POINTER_ID;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void pressSecond()
108
    {
109
    int index = mEvent.getActionIndex();
110
    mPointer2 = mEvent.getPointerId(index);
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public void unpressSecond()
116
    {
117
    mPointer2 = INVALID_POINTER_ID;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  public float getFirstX()
123
    {
124
    return mEvent.getX();
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  public float getFirstY()
130
    {
131
    return mEvent.getY();
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public float getSecondX()
137
    {
138
    int index = mEvent.findPointerIndex(mPointer2);
139
    return mEvent.getX(index);
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public float getSecondY()
145
    {
146
    int index = mEvent.findPointerIndex(mPointer2);
147
    return mEvent.getY(index);
148
    }
149
}
    (1-1/1)