Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorView.java @ 9694d2d5

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
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 da56b12f Leszek Koltunski
import android.view.MotionEvent;
29 9530f6b0 Leszek Koltunski
30
import com.google.firebase.crashlytics.FirebaseCrashlytics;
31
32 903041cd Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
33
34 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36
public class BandagedCreatorView extends GLSurfaceView
37
{
38 9694d2d5 Leszek Koltunski
    private final static float DIRECTION_SENSITIVITY= 12.0f;
39
    public static final int INVALID_POINTER_ID = -1;
40 75173f81 Leszek Koltunski
41 9530f6b0 Leszek Koltunski
    private BandagedCreatorRenderer mRenderer;
42 ed32e32d Leszek Koltunski
    private BandagedCreatorTouchControl mTouchControl;
43 75173f81 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
44
    private int mTouchedIndex1, mTouchedIndex2;
45 9694d2d5 Leszek Koltunski
    private float mX1, mY1, mX2, mY2, mX, mY;
46
    private int mPointer1, mPointer2;
47
    private float mRotAngle, mInitDistance;
48 9530f6b0 Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// PUBLIC API
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
    public BandagedCreatorView(Context context, AttributeSet attrs)
54
      {
55
      super(context,attrs);
56
57 da56b12f Leszek Koltunski
      mX = -1;
58
      mY = -1;
59
60 550db260 Leszek Koltunski
      mTouchedIndex1 = -1;
61
      mTouchedIndex2 = -1;
62
63 9530f6b0 Leszek Koltunski
      if(!isInEditMode())
64
        {
65
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
66
        mRenderer = new BandagedCreatorRenderer(this);
67 903041cd Leszek Koltunski
        DistortedScreen screen = mRenderer.getScreen();
68 5d5ed376 Leszek Koltunski
        mTouchControl = new BandagedCreatorTouchControl(BandagedCreatorRenderer.SCREEN_RATIO, screen.getFOV() );
69 9530f6b0 Leszek Koltunski
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 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100
    public int getTouched()
101
      {
102
      return mTouchedIndex1;
103
      }
104
105 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107
    public BandagedCreatorRenderer getRenderer()
108
      {
109
      return mRenderer;
110
      }
111
112 5d5ed376 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 213c15de Leszek Koltunski
    public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
115 5d5ed376 Leszek Koltunski
      {
116 213c15de Leszek Koltunski
      mTouchControl.setCubits(cubits,x,y,z);
117 5d5ed376 Leszek Koltunski
      }
118
119 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
    public void resetCubits()
122
      {
123 6f3af598 Leszek Koltunski
      if( mTouchedIndex1>=0 ) mRenderer.untouchCubit(mTouchedIndex1);
124
      if( mTouchedIndex2>=0 ) mRenderer.untouchCubit(mTouchedIndex2);
125 e0b71e6e Leszek Koltunski
126
      mTouchedIndex1 = -1;
127
      mTouchedIndex2 = -1;
128
      }
129
130 da56b12f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 28cb1607 Leszek Koltunski
    public void setScreenSize(int width, int height)
133 903041cd Leszek Koltunski
      {
134
      mScreenWidth = width;
135
      mScreenHeight= height;
136 75173f81 Leszek Koltunski
      mScreenMin   = Math.min(width, height);
137 903041cd Leszek Koltunski
      }
138
139 9694d2d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141
    private float getAngle(float x1, float y1, float x2, float y2)
142
      {
143
      return (float) Math.atan2(y1-y2, x1-x2);
144
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
    private void actionDown(MotionEvent event)
149
      {
150
      mPointer1 = event.getPointerId(0);
151
      mX1 = event.getX();
152
      mY1 = event.getY();
153
      mPointer2 = INVALID_POINTER_ID;
154
155
      float x1 = (mX1 -mScreenWidth*0.5f)/mScreenMin;
156
      float y1 = (mScreenHeight*0.5f-mY1)/mScreenMin;
157
158
      int index = mTouchControl.cubitTouched(x1,y1,mRenderer.getQuatAccu() );
159
160
      if( index<0 )
161
        {
162
        mX = mX1;
163
        mY = mY1;
164
        }
165
      else
166
        {
167
        mX = -1;
168
        mY = -1;
169
170
        if( mTouchedIndex1<0 )
171
          {
172
          mTouchedIndex1 = index;
173
          mRenderer.touchCubit(mTouchedIndex1);
174
          }
175
        else
176
          {
177
          mTouchedIndex2 = index;
178
179
          if( mTouchedIndex1 != index )
180
            {
181
            mRenderer.touchCubit(mTouchedIndex2);
182
            }
183
          }
184
        }
185
      }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189
    private void actionMove(MotionEvent event)
190
      {
191
      int index1 = event.findPointerIndex(mPointer1);
192
193
      if( index1>=0 )
194
        {
195
        mX1 = event.getX(index1);
196
        mY1 = event.getY(index1);
197
        }
198
199
      int index2 = event.findPointerIndex(mPointer2);
200
201
      if( index2>=0 )
202
        {
203
        mX2 = event.getX(index2);
204
        mY2 = event.getY(index2);
205
        }
206
207
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
208
        {
209
        float angleNow = getAngle(mX1,mY1,mX2,mY2);
210
        float angleDiff = angleNow-mRotAngle;
211
        float sinA = (float)Math.sin(angleDiff);
212
        float cosA = (float)Math.cos(angleDiff);
213
        mRenderer.setQuatTemp(0,0,sinA,cosA);
214
        mRenderer.resetQuats();
215
        mRotAngle = angleNow;
216
217
/*
218
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
219
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
220
        mInitDistance = distNow;
221
        TwistyObject object = mPreRender.getObject();
222
        if( object!=null ) object.setObjectRatio(distQuot, mObjectNode.getMinSize() );
223
 */
224
        }
225
      else
226
        {
227
        float x = event.getX();
228
        float y = event.getY();
229
230
        if( mX>=0 && mY>= 0 )
231
          {
232
          float px = mY-y;
233
          float py = mX-x;
234
          float pz = 0;
235
          float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
236
237
          if( plen>0 )
238
            {
239
            px /= plen;
240
            py /= plen;
241
            pz /= plen;
242
243
            float cosA = (float)Math.cos(plen*3.14f/mScreenMin);
244
            float sinA = (float)Math.sqrt(1-cosA*cosA);
245
246
            mRenderer.setQuatTemp(px*sinA, py*sinA, pz*sinA, cosA);
247
            }
248
          }
249
        if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mScreenMin*mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
250
          {
251
          mX = x;
252
          mY = y;
253
          mRenderer.resetQuats();
254
          }
255
        }
256
      }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260
    private void actionUp()
261
      {
262
      mPointer1 = INVALID_POINTER_ID;
263
      mPointer2 = INVALID_POINTER_ID;
264
265
      if( mTouchedIndex2>=0 )
266
        {
267
        mRenderer.untouchCubit(mTouchedIndex1);
268
        mRenderer.untouchCubit(mTouchedIndex2);
269
        mRenderer.setConnecting(mTouchedIndex1,mTouchedIndex2);
270
        mTouchedIndex1 = -1;
271
        mTouchedIndex2 = -1;
272
        }
273
274
      mX = -1;
275
      mY = -1;
276
277
      mRenderer.resetQuats();
278
      }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
    private void actionPointerDown(MotionEvent event)
283
      {
284
      int index = event.getActionIndex();
285
286
      if( mPointer1==INVALID_POINTER_ID )
287
        {
288
        mPointer1 = event.getPointerId(index);
289
        mX1 = event.getX(index);
290
        mY1 = event.getY(index);
291
        }
292
      else if( mPointer2==INVALID_POINTER_ID )
293
        {
294
        mPointer2 = event.getPointerId(index);
295
        mX2 = event.getX(index);
296
        mY2 = event.getY(index);
297
        }
298
299
      mRotAngle = getAngle(mX1,mY1,mX2,mY2);
300
      mInitDistance = -1;
301
      mRenderer.resetQuats();
302
      }
303
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306
    private void actionPointerUp(MotionEvent event)
307
      {
308
      int index = event.getActionIndex();
309
310
      if( index==event.findPointerIndex(mPointer1) )
311
        {
312
        mPointer1 = INVALID_POINTER_ID;
313
        mX = mX2;
314
        mY = mY2;
315
        }
316
      else if( index==event.findPointerIndex(mPointer2) )
317
        {
318
        mPointer2 = INVALID_POINTER_ID;
319
        mX = mX1;
320
        mY = mY1;
321
        }
322
323
      mRenderer.resetQuats();
324
      }
325
326 903041cd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328
    @Override
329
    public boolean onTouchEvent(MotionEvent event)
330 da56b12f Leszek Koltunski
      {
331 e0b71e6e Leszek Koltunski
      if( mRenderer.isBusy() ) return true;
332
333 9694d2d5 Leszek Koltunski
      int action = event.getActionMasked();
334 da56b12f Leszek Koltunski
335
      switch(action)
336
         {
337 9694d2d5 Leszek Koltunski
         case MotionEvent.ACTION_DOWN        : actionDown(event)       ; break;
338
         case MotionEvent.ACTION_MOVE        : actionMove(event)       ; break;
339
         case MotionEvent.ACTION_UP          : actionUp()              ; break;
340
         case MotionEvent.ACTION_POINTER_DOWN: actionPointerDown(event); break;
341
         case MotionEvent.ACTION_POINTER_UP  : actionPointerUp  (event); break;
342 da56b12f Leszek Koltunski
         }
343
344
      return true;
345
      }
346 9530f6b0 Leszek Koltunski
}