Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ f404152d

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.bandaged;
11

    
12
import android.annotation.SuppressLint;
13
import android.app.ActivityManager;
14
import android.content.Context;
15
import android.content.pm.ConfigurationInfo;
16
import android.opengl.GLES30;
17
import android.opengl.GLSurfaceView;
18
import android.util.AttributeSet;
19
import android.view.MotionEvent;
20

    
21
import com.google.firebase.crashlytics.FirebaseCrashlytics;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

    
25
public class BandagedCreatorView extends GLSurfaceView
26
{
27
    public static final int INVALID_POINTER_ID = -1;
28

    
29
    private BandagedCreatorRenderer mRenderer;
30
    private BandagedCreatorTouchControl mTouchControl;
31
    private int mScreenWidth, mScreenHeight, mScreenMin;
32
    private int mTouchedIndex1, mTouchedIndex2;
33
    private float mX1, mY1, mX2, mY2, mX, mY;
34
    private int mPointer1, mPointer2;
35
    private float mRotAngle, mInitDistance;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// PUBLIC API
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
    public BandagedCreatorView(Context context, AttributeSet attrs)
42
      {
43
      super(context,attrs);
44

    
45
      mX = -1;
46
      mY = -1;
47

    
48
      mTouchedIndex1 = -1;
49
      mTouchedIndex2 = -1;
50

    
51
      if(!isInEditMode())
52
        {
53
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
54
        mRenderer = new BandagedCreatorRenderer(this);
55
        mTouchControl = mRenderer.createTouchControl();
56

    
57
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
58

    
59
        try
60
          {
61
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
62
          int esVersion = configurationInfo.reqGlEsVersion>>16;
63
          setEGLContextClientVersion(esVersion);
64
          setRenderer(mRenderer);
65
          }
66
        catch(Exception ex)
67
          {
68
          act.OpenGLError();
69

    
70
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
71
          String version = GLES30.glGetString(GLES30.GL_VERSION);
72
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
73
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
74

    
75
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
76
          crashlytics.setCustomKey("GLSL Version"  , shading );
77
          crashlytics.setCustomKey("GL version"    , version );
78
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
79
          crashlytics.setCustomKey("GLSL renderer" , renderer);
80
          crashlytics.recordException(ex);
81
          }
82
        }
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
    public int getTouched()
88
      {
89
      return mTouchedIndex1;
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
    public BandagedCreatorTouchControl getTouchControl()
95
      {
96
      return mTouchControl;
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    public BandagedCreatorRenderer getRenderer()
102
      {
103
      return mRenderer;
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    public void resetCubits()
109
      {
110
      if( mTouchedIndex1>=0 ) mRenderer.untouchCubit(mTouchedIndex1);
111
      if( mTouchedIndex2>=0 ) mRenderer.untouchCubit(mTouchedIndex2);
112

    
113
      mTouchedIndex1 = -1;
114
      mTouchedIndex2 = -1;
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
    public void setScreenSize(int width, int height)
120
      {
121
      mScreenWidth = width;
122
      mScreenHeight= height;
123
      mScreenMin   = Math.min(width, height);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    private float getAngle(float x1, float y1, float x2, float y2)
129
      {
130
      return (float) Math.atan2(y1-y2, x1-x2);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    private void actionDown(MotionEvent event)
136
      {
137
      mPointer1 = event.getPointerId(0);
138
      mX1 = event.getX();
139
      mY1 = event.getY();
140
      mPointer2 = INVALID_POINTER_ID;
141

    
142
      float x1 = (mX1 -mScreenWidth*0.5f)/mScreenMin;
143
      float y1 = (mScreenHeight*0.5f-mY1)/mScreenMin;
144

    
145
      int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
146

    
147
      if( index<0 )
148
        {
149
        mX = mX1;
150
        mY = mY1;
151
        }
152
      else
153
        {
154
        mX = -1;
155
        mY = -1;
156

    
157
        if( mTouchedIndex1<0 )
158
          {
159
          mTouchedIndex1 = index;
160
          mRenderer.touchCubit(mTouchedIndex1);
161
          }
162
        else
163
          {
164
          mTouchedIndex2 = index;
165

    
166
          if( mTouchedIndex1 != index )
167
            {
168
            mRenderer.touchCubit(mTouchedIndex2);
169
            }
170
          }
171
        }
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    private void actionMove(MotionEvent event)
177
      {
178
      int index1 = event.findPointerIndex(mPointer1);
179

    
180
      if( index1>=0 )
181
        {
182
        mX1 = event.getX(index1);
183
        mY1 = event.getY(index1);
184
        }
185

    
186
      int index2 = event.findPointerIndex(mPointer2);
187

    
188
      if( index2>=0 )
189
        {
190
        mX2 = event.getX(index2);
191
        mY2 = event.getY(index2);
192
        }
193

    
194
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
195
        {
196
        float angleNow = getAngle(mX1,mY1,mX2,mY2);
197
        float angleDiff = angleNow-mRotAngle;
198
        float sinA = (float)Math.sin(angleDiff);
199
        float cosA = (float)Math.cos(angleDiff);
200
        mRenderer.setQuatTemp(0,0,sinA,cosA);
201
        mRenderer.resetQuats();
202
        mRotAngle = angleNow;
203

    
204
        float distNow  = (float)Math.sqrt( (mX1-mX2)*(mX1-mX2) + (mY1-mY2)*(mY1-mY2) );
205
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
206
        mInitDistance = distNow;
207
        mRenderer.mulObjectRatio(distQuot);
208
        }
209
      else
210
        {
211
        float x = event.getX();
212
        float y = event.getY();
213

    
214
        if( mX>=0 && mY>= 0 )
215
          {
216
          float px = mY-y;
217
          float py = mX-x;
218
          float pz = 0;
219
          float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
220

    
221
          if( plen>0 )
222
            {
223
            px /= plen;
224
            py /= plen;
225
            pz /= plen;
226

    
227
            float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
228
            float sinA = (float)Math.sqrt(1-cosA*cosA);
229

    
230
            mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
231
            }
232
          }
233

    
234
        mRenderer.resetQuats();
235
        mX = x;
236
        mY = y;
237
        }
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    private void actionUp()
243
      {
244
      mPointer1 = INVALID_POINTER_ID;
245
      mPointer2 = INVALID_POINTER_ID;
246

    
247
      if( mTouchedIndex2>=0 )
248
        {
249
        mRenderer.untouchCubit(mTouchedIndex1);
250

    
251
        if( mTouchedIndex2!=mTouchedIndex1 )
252
          {
253
          mRenderer.untouchCubit(mTouchedIndex2);
254
          mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
255
          }
256

    
257
        mTouchedIndex1 = -1;
258
        mTouchedIndex2 = -1;
259
        }
260

    
261
      mX = -1;
262
      mY = -1;
263

    
264
      mRenderer.resetQuats();
265
      }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    private void actionPointerDown(MotionEvent event)
270
      {
271
      int index = event.getActionIndex();
272

    
273
      if( mPointer1==INVALID_POINTER_ID )
274
        {
275
        mPointer1 = event.getPointerId(index);
276
        mX1 = event.getX(index);
277
        mY1 = event.getY(index);
278
        }
279
      else if( mPointer2==INVALID_POINTER_ID )
280
        {
281
        mPointer2 = event.getPointerId(index);
282
        mX2 = event.getX(index);
283
        mY2 = event.getY(index);
284
        }
285

    
286
      mRotAngle = getAngle(mX1,mY1,mX2,mY2);
287
      mInitDistance = -1;
288
      mRenderer.resetQuats();
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    private void actionPointerUp(MotionEvent event)
294
      {
295
      int index = event.getActionIndex();
296

    
297
      if( index==event.findPointerIndex(mPointer1) )
298
        {
299
        mPointer1 = INVALID_POINTER_ID;
300
        mX = mX2;
301
        mY = mY2;
302
        }
303
      else if( index==event.findPointerIndex(mPointer2) )
304
        {
305
        mPointer2 = INVALID_POINTER_ID;
306
        mX = mX1;
307
        mY = mY1;
308
        }
309

    
310
      mRenderer.resetQuats();
311
      }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
    @SuppressLint("ClickableViewAccessibility")
316
    @Override
317
    public boolean onTouchEvent(MotionEvent event)
318
      {
319
      if( mRenderer.isBusy() ) return true;
320

    
321
      int action = event.getActionMasked();
322

    
323
      switch(action)
324
         {
325
         case MotionEvent.ACTION_DOWN        : actionDown(event)       ; break;
326
         case MotionEvent.ACTION_MOVE        : actionMove(event)       ; break;
327
         case MotionEvent.ACTION_UP          : actionUp()              ; break;
328
         case MotionEvent.ACTION_POINTER_DOWN: actionPointerDown(event); break;
329
         case MotionEvent.ACTION_POINTER_UP  : actionPointerUp  (event); break;
330
         }
331

    
332
      return true;
333
      }
334
}
335

    
(6-6/14)