Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ 3b8f5220

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.bandaged;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLES30;
26
import android.opengl.GLSurfaceView;
27
import android.util.AttributeSet;
28
import android.view.MotionEvent;
29

    
30
import com.google.firebase.crashlytics.FirebaseCrashlytics;
31

    
32
import org.distorted.library.main.DistortedScreen;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class BandagedCreatorView extends GLSurfaceView
37
{
38
    private final static float DIRECTION_SENSITIVITY= 12.0f;
39
    public static final int INVALID_POINTER_ID = -1;
40

    
41
    private BandagedCreatorRenderer mRenderer;
42
    private BandagedCreatorTouchControl mTouchControl;
43
    private int mScreenWidth, mScreenHeight, mScreenMin;
44
    private int mTouchedIndex1, mTouchedIndex2;
45
    private float mX1, mY1, mX2, mY2, mX, mY;
46
    private int mPointer1, mPointer2;
47
    private float mRotAngle, mInitDistance;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// PUBLIC API
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    public BandagedCreatorView(Context context, AttributeSet attrs)
54
      {
55
      super(context,attrs);
56

    
57
      mX = -1;
58
      mY = -1;
59

    
60
      mTouchedIndex1 = -1;
61
      mTouchedIndex2 = -1;
62

    
63
      if(!isInEditMode())
64
        {
65
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
66
        mRenderer = new BandagedCreatorRenderer(this);
67
        DistortedScreen screen = mRenderer.getScreen();
68
        mTouchControl = new BandagedCreatorTouchControl( mRenderer.getObjectRatio() , screen.getFOV() );
69

    
70
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
71

    
72
        try
73
          {
74
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
75
          int esVersion = configurationInfo.reqGlEsVersion>>16;
76
          setEGLContextClientVersion(esVersion);
77
          setRenderer(mRenderer);
78
          }
79
        catch(Exception ex)
80
          {
81
          act.OpenGLError();
82

    
83
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
84
          String version = GLES30.glGetString(GLES30.GL_VERSION);
85
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
86
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
87

    
88
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
89
          crashlytics.setCustomKey("GLSL Version"  , shading );
90
          crashlytics.setCustomKey("GL version"    , version );
91
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
92
          crashlytics.setCustomKey("GLSL renderer" , renderer);
93
          crashlytics.recordException(ex);
94
          }
95
        }
96
      }
97

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

    
100
    public int getTouched()
101
      {
102
      return mTouchedIndex1;
103
      }
104

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

    
107
    public BandagedCreatorTouchControl getTouchControl()
108
      {
109
      return mTouchControl;
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    public BandagedCreatorRenderer getRenderer()
115
      {
116
      return mRenderer;
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
122
      {
123
      mTouchControl.setCubits(cubits,x,y,z);
124
      }
125

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

    
128
    public void resetCubits()
129
      {
130
      if( mTouchedIndex1>=0 ) mRenderer.untouchCubit(mTouchedIndex1);
131
      if( mTouchedIndex2>=0 ) mRenderer.untouchCubit(mTouchedIndex2);
132

    
133
      mTouchedIndex1 = -1;
134
      mTouchedIndex2 = -1;
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    public void setScreenSize(int width, int height)
140
      {
141
      mScreenWidth = width;
142
      mScreenHeight= height;
143
      mScreenMin   = Math.min(width, height);
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    private float getAngle(float x1, float y1, float x2, float y2)
149
      {
150
      return (float) Math.atan2(y1-y2, x1-x2);
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    private void actionDown(MotionEvent event)
156
      {
157
      mPointer1 = event.getPointerId(0);
158
      mX1 = event.getX();
159
      mY1 = event.getY();
160
      mPointer2 = INVALID_POINTER_ID;
161

    
162
      float x1 = (mX1 -mScreenWidth*0.5f)/mScreenMin;
163
      float y1 = (mScreenHeight*0.5f-mY1)/mScreenMin;
164

    
165
      int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
166

    
167
      if( index<0 )
168
        {
169
        mX = mX1;
170
        mY = mY1;
171
        }
172
      else
173
        {
174
        mX = -1;
175
        mY = -1;
176

    
177
        if( mTouchedIndex1<0 )
178
          {
179
          mTouchedIndex1 = index;
180
          mRenderer.touchCubit(mTouchedIndex1);
181
          }
182
        else
183
          {
184
          mTouchedIndex2 = index;
185

    
186
          if( mTouchedIndex1 != index )
187
            {
188
            mRenderer.touchCubit(mTouchedIndex2);
189
            }
190
          }
191
        }
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    private void actionMove(MotionEvent event)
197
      {
198
      int index1 = event.findPointerIndex(mPointer1);
199

    
200
      if( index1>=0 )
201
        {
202
        mX1 = event.getX(index1);
203
        mY1 = event.getY(index1);
204
        }
205

    
206
      int index2 = event.findPointerIndex(mPointer2);
207

    
208
      if( index2>=0 )
209
        {
210
        mX2 = event.getX(index2);
211
        mY2 = event.getY(index2);
212
        }
213

    
214
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
215
        {
216
        float angleNow = getAngle(mX1,mY1,mX2,mY2);
217
        float angleDiff = angleNow-mRotAngle;
218
        float sinA = (float)Math.sin(angleDiff);
219
        float cosA = (float)Math.cos(angleDiff);
220
        mRenderer.setQuatTemp(0,0,sinA,cosA);
221
        mRenderer.resetQuats();
222
        mRotAngle = angleNow;
223

    
224
        float distNow  = (float)Math.sqrt( (mX1-mX2)*(mX1-mX2) + (mY1-mY2)*(mY1-mY2) );
225
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
226
        mInitDistance = distNow;
227
        mRenderer.mulObjectRatio(distQuot);
228
        }
229
      else
230
        {
231
        float x = event.getX();
232
        float y = event.getY();
233

    
234
        if( mX>=0 && mY>= 0 )
235
          {
236
          float px = mY-y;
237
          float py = mX-x;
238
          float pz = 0;
239
          float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
240

    
241
          if( plen>0 )
242
            {
243
            px /= plen;
244
            py /= plen;
245
            pz /= plen;
246

    
247
            float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
248
            float sinA = (float)Math.sqrt(1-cosA*cosA);
249

    
250
            mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
251
            }
252
          }
253
        if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
254
          {
255
          mX = x;
256
          mY = y;
257
          mRenderer.resetQuats();
258
          }
259
        }
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
    private void actionUp()
265
      {
266
      mPointer1 = INVALID_POINTER_ID;
267
      mPointer2 = INVALID_POINTER_ID;
268

    
269
      if( mTouchedIndex2>=0 )
270
        {
271
        mRenderer.untouchCubit(mTouchedIndex1);
272

    
273
        if( mTouchedIndex2!=mTouchedIndex1 )
274
          {
275
          mRenderer.untouchCubit(mTouchedIndex2);
276
          mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
277
          }
278

    
279
        mTouchedIndex1 = -1;
280
        mTouchedIndex2 = -1;
281
        }
282

    
283
      mX = -1;
284
      mY = -1;
285

    
286
      mRenderer.resetQuats();
287
      }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    private void actionPointerDown(MotionEvent event)
292
      {
293
      int index = event.getActionIndex();
294

    
295
      if( mPointer1==INVALID_POINTER_ID )
296
        {
297
        mPointer1 = event.getPointerId(index);
298
        mX1 = event.getX(index);
299
        mY1 = event.getY(index);
300
        }
301
      else if( mPointer2==INVALID_POINTER_ID )
302
        {
303
        mPointer2 = event.getPointerId(index);
304
        mX2 = event.getX(index);
305
        mY2 = event.getY(index);
306
        }
307

    
308
      mRotAngle = getAngle(mX1,mY1,mX2,mY2);
309
      mInitDistance = -1;
310
      mRenderer.resetQuats();
311
      }
312

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

    
315
    private void actionPointerUp(MotionEvent event)
316
      {
317
      int index = event.getActionIndex();
318

    
319
      if( index==event.findPointerIndex(mPointer1) )
320
        {
321
        mPointer1 = INVALID_POINTER_ID;
322
        mX = mX2;
323
        mY = mY2;
324
        }
325
      else if( index==event.findPointerIndex(mPointer2) )
326
        {
327
        mPointer2 = INVALID_POINTER_ID;
328
        mX = mX1;
329
        mY = mY1;
330
        }
331

    
332
      mRenderer.resetQuats();
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
    @Override
338
    public boolean onTouchEvent(MotionEvent event)
339
      {
340
      if( mRenderer.isBusy() ) return true;
341

    
342
      int action = event.getActionMasked();
343

    
344
      switch(action)
345
         {
346
         case MotionEvent.ACTION_DOWN        : actionDown(event)       ; break;
347
         case MotionEvent.ACTION_MOVE        : actionMove(event)       ; break;
348
         case MotionEvent.ACTION_UP          : actionUp()              ; break;
349
         case MotionEvent.ACTION_POINTER_DOWN: actionPointerDown(event); break;
350
         case MotionEvent.ACTION_POINTER_UP  : actionPointerUp  (event); break;
351
         }
352

    
353
      return true;
354
      }
355
}
356

    
(6-6/13)