Project

General

Profile

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

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

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.content.res.Resources;
14
import android.util.DisplayMetrics;
15
import android.view.MotionEvent;
16

    
17
import java.io.File;
18
import java.io.FileInputStream;
19
import java.io.FileNotFoundException;
20
import java.io.InputStream;
21
import java.lang.ref.WeakReference;
22
import org.distorted.objectlib.helpers.OperatingSystemInterface;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class OSInterface implements OperatingSystemInterface
27
{
28

    
29
  private final WeakReference<Activity> mAct;
30
  private MotionEvent mEvent;
31
  private int mPointer1, mPointer2;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
  public OSInterface(Activity act)
36
    {
37
    mAct = new WeakReference<>(act);
38
    }
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
// SCREEN
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
// TOUCH CONTROL
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public void setMotionEvent(MotionEvent event)
56
    {
57
    mEvent = event;
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  public int getAction()
63
    {
64
    switch( mEvent.getActionMasked() )
65
      {
66
      case( MotionEvent.ACTION_DOWN        ): return ACTION_DOWN_1;
67
      case( MotionEvent.ACTION_UP          ): return ACTION_UP_1;
68
      case( MotionEvent.ACTION_POINTER_DOWN): return ACTION_DOWN_2;
69
      case( MotionEvent.ACTION_POINTER_UP  ): return ACTION_UP_2;
70
      case( MotionEvent.ACTION_MOVE        ): return ACTION_MOVE;
71
      }
72

    
73
    return 0;
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  public void upOneOfThem()
79
    {
80
    int index = mEvent.getActionIndex();
81

    
82
         if( index==mEvent.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
83
    else if( index==mEvent.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  public boolean isFirstPressed()
89
    {
90
    return mPointer1!=INVALID_POINTER_ID;
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  public boolean isSecondPressed()
96
    {
97
    return mPointer2!=INVALID_POINTER_ID;
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  public void pressFirst()
103
    {
104
    mPointer1 = mEvent.getPointerId(0);
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  public void unpressFirst()
110
    {
111
    mPointer1 = INVALID_POINTER_ID;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  public void pressSecond()
117
    {
118
    int index = mEvent.getActionIndex();
119
    mPointer2 = mEvent.getPointerId(index);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public void unpressSecond()
125
    {
126
    mPointer2 = INVALID_POINTER_ID;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  public float getFirstX()
132
    {
133
    return mEvent.getX();
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public float getFirstY()
139
    {
140
    return mEvent.getY();
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public float getSecondX()
146
    {
147
    int index = mEvent.findPointerIndex(mPointer2);
148
    return mEvent.getX(index);
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  public float getSecondY()
154
    {
155
    int index = mEvent.findPointerIndex(mPointer2);
156
    return mEvent.getY(index);
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// LOCAL FILES
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  public InputStream openLocalFile(String name)
164
    {
165
    Activity act = mAct.get();
166
    File file = new File(act.getFilesDir(), name);
167
    InputStream stream;
168

    
169
    try
170
      {
171
      stream = new FileInputStream(file);
172
      }
173
    catch(FileNotFoundException fnf)
174
      {
175
      stream = null;
176
      }
177

    
178
    return stream;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  public InputStream openLocalFile(int id)
184
    {
185
    Resources res = mAct.get().getResources();
186
    return res!=null ? res.openRawResource(id) : null;
187
    }
188

    
189
}
    (1-1/1)