Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ beb325a0

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.magic;
21
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25 beb325a0 Leszek Koltunski
import android.graphics.PorterDuff;
26
import android.graphics.drawable.Drawable;
27 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
28 beb325a0 Leszek Koltunski
import android.support.v4.content.ContextCompat;
29 0c52af30 Leszek Koltunski
import android.util.AttributeSet;
30 beb325a0 Leszek Koltunski
import android.util.DisplayMetrics;
31 0c52af30 Leszek Koltunski
import android.view.MotionEvent;
32 beb325a0 Leszek Koltunski
import android.view.ViewGroup;
33
import android.widget.Button;
34
import android.widget.ImageButton;
35
import android.widget.LinearLayout;
36 0c52af30 Leszek Koltunski
37
import org.distorted.library.type.Static4D;
38 beb325a0 Leszek Koltunski
import org.distorted.object.RubikCubeMovement;
39 0c52af30 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
43 0c52af30 Leszek Koltunski
{
44 34747dd1 Leszek Koltunski
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
45 47ba5ddc Leszek Koltunski
    private final static int ROTATION_SENSITIVITY =  12;
46 34747dd1 Leszek Koltunski
47 eac805bd Leszek Koltunski
    // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
48
    private final static int DIRECTION_SENSITIVITY=  12;
49
50 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
51
    private RubikCubeMovement mMovement;
52 0c52af30 Leszek Koltunski
53 beb325a0 Leszek Koltunski
    private boolean mInScrambleMode;
54
    private int mButton = RubikSize.SIZE3.ordinal();
55 0c52af30 Leszek Koltunski
56
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
57
    private int mX, mY;
58
    private int mLastTouchedFace;
59
    private float mCameraDistance;
60 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
61 0c52af30 Leszek Koltunski
62 45686da2 Leszek Koltunski
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
63
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
64
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
65
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
66 ee5c2ae1 Leszek Koltunski
67 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68 47ba5ddc Leszek Koltunski
// return quat1*quat2
69 0c52af30 Leszek Koltunski
70 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
71 45686da2 Leszek Koltunski
      {
72 47ba5ddc Leszek Koltunski
      float qx = quat1.get1();
73
      float qy = quat1.get2();
74
      float qz = quat1.get3();
75
      float qw = quat1.get4();
76 45686da2 Leszek Koltunski
77 47ba5ddc Leszek Koltunski
      float rx = quat2.get1();
78
      float ry = quat2.get2();
79
      float rz = quat2.get3();
80
      float rw = quat2.get4();
81 45686da2 Leszek Koltunski
82 47ba5ddc Leszek Koltunski
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
83
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
84
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
85
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
86 45686da2 Leszek Koltunski
87 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
88 45686da2 Leszek Koltunski
      }
89
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
92 45686da2 Leszek Koltunski
93 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
94 45686da2 Leszek Koltunski
      {
95 47ba5ddc Leszek Koltunski
      float qx = quat.get1();
96
      float qy = quat.get2();
97
      float qz = quat.get3();
98
      float qw = quat.get4();
99 45686da2 Leszek Koltunski
100 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
101 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
102 0c52af30 Leszek Koltunski
103 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
104 0c52af30 Leszek Koltunski
      }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
108 0c52af30 Leszek Koltunski
109 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
110 0c52af30 Leszek Koltunski
      {
111 47ba5ddc Leszek Koltunski
      float qx = quat.get1();
112
      float qy = quat.get2();
113
      float qz = quat.get3();
114
      float qw = quat.get4();
115 0c52af30 Leszek Koltunski
116 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
117 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
118 0c52af30 Leszek Koltunski
119 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
120 0c52af30 Leszek Koltunski
      }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124 beb325a0 Leszek Koltunski
    void setScreenSize(int width, int height)
125 0c52af30 Leszek Koltunski
      {
126 beb325a0 Leszek Koltunski
      mScreenWidth = width;
127
      mScreenHeight= height;
128 0c52af30 Leszek Koltunski
129 beb325a0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
130 0c52af30 Leszek Koltunski
      }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134 beb325a0 Leszek Koltunski
    void setCameraDist(float distance)
135 0c52af30 Leszek Koltunski
      {
136 beb325a0 Leszek Koltunski
      mCameraDistance = distance;
137 0c52af30 Leszek Koltunski
      }
138
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141 beb325a0 Leszek Koltunski
    RubikRenderer getRenderer()
142 0c52af30 Leszek Koltunski
      {
143 beb325a0 Leszek Koltunski
      return mRenderer;
144 0c52af30 Leszek Koltunski
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148 beb325a0 Leszek Koltunski
    int getRedButton()
149 0c52af30 Leszek Koltunski
      {
150 beb325a0 Leszek Koltunski
      return mButton;
151 0c52af30 Leszek Koltunski
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155 beb325a0 Leszek Koltunski
    void markButton(int button)
156 0c52af30 Leszek Koltunski
      {
157 beb325a0 Leszek Koltunski
      mButton = button;
158
      RubikActivity act = (RubikActivity)getContext();
159 0c52af30 Leszek Koltunski
160 beb325a0 Leszek Koltunski
      for(int b=0; b<RubikSize.LENGTH; b++)
161 0c52af30 Leszek Koltunski
        {
162 beb325a0 Leszek Koltunski
        Drawable d = act.findViewById(b).getBackground();
163 0c52af30 Leszek Koltunski
164 beb325a0 Leszek Koltunski
        if( b==button )
165
          {
166
          d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
167
          }
168
        else
169
          {
170
          d.clearColorFilter();
171
          }
172 0c52af30 Leszek Koltunski
        }
173
      }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177 beb325a0 Leszek Koltunski
    void addSizeButtons(RubikActivity act)
178 0c52af30 Leszek Koltunski
      {
179 beb325a0 Leszek Koltunski
      LinearLayout layout = act.findViewById(R.id.sizeLayout);
180
      DisplayMetrics metrics = getResources().getDisplayMetrics();
181
      float scale = metrics.density;
182
      int size = (int)(64*scale +0.5f);
183
      int padding = (int)(3*scale + 0.5f);
184
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
185
186
      for(int i=0; i<RubikSize.LENGTH; i++)
187
        {
188
        ImageButton button = new ImageButton(act);
189
        button.setLayoutParams(params);
190
        button.setId(i);
191
        button.setPadding(padding,0,padding,0);
192
        int iconID = RubikSize.getSize(i).getIconID();
193
        button.setImageResource(iconID);
194
        button.setOnClickListener(act);
195
        layout.addView(button);
196
        }
197 0c52af30 Leszek Koltunski
      }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 beb325a0 Leszek Koltunski
    void enterScrambleMode()
202 0c52af30 Leszek Koltunski
      {
203 beb325a0 Leszek Koltunski
      if( !mInScrambleMode )
204
        {
205
        mInScrambleMode = true;
206 0c52af30 Leszek Koltunski
207 beb325a0 Leszek Koltunski
        android.util.Log.e("view", "entering scramble mode");
208 0c52af30 Leszek Koltunski
209 beb325a0 Leszek Koltunski
        RubikActivity act = (RubikActivity)getContext();
210 0c52af30 Leszek Koltunski
211 beb325a0 Leszek Koltunski
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
212 0c52af30 Leszek Koltunski
213 beb325a0 Leszek Koltunski
        if( scrambleButt!=null )
214
          {
215
          scrambleButt.setClickable(false);
216
          scrambleButt.setVisibility(INVISIBLE);
217
          }
218
        else
219
          {
220
          android.util.Log.e("view", "button null!");
221
          }
222
        }
223 0c52af30 Leszek Koltunski
      }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227 beb325a0 Leszek Koltunski
    void leaveScrambleMode()
228 0c52af30 Leszek Koltunski
      {
229 beb325a0 Leszek Koltunski
      if( mInScrambleMode )
230
        {
231
        mInScrambleMode = false;
232 0c52af30 Leszek Koltunski
233 beb325a0 Leszek Koltunski
        android.util.Log.e("view", "leaving scramble mode");
234 0c52af30 Leszek Koltunski
235 beb325a0 Leszek Koltunski
        RubikActivity act = (RubikActivity)getContext();
236 0c52af30 Leszek Koltunski
237 beb325a0 Leszek Koltunski
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
238 0c52af30 Leszek Koltunski
239 beb325a0 Leszek Koltunski
        if( scrambleButt!=null )
240
          {
241
          scrambleButt.setClickable(true);
242
          scrambleButt.setVisibility(VISIBLE);
243
          }
244
        else
245
          {
246
          android.util.Log.e("view", "button null!");
247
          }
248 0c52af30 Leszek Koltunski
        }
249
      }
250
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253 beb325a0 Leszek Koltunski
    void leaveScrambleModeNonUI()
254 0c52af30 Leszek Koltunski
      {
255 beb325a0 Leszek Koltunski
      RubikActivity act = (RubikActivity)getContext();
256 0c52af30 Leszek Koltunski
257 beb325a0 Leszek Koltunski
      act.runOnUiThread(new Runnable()
258 0c52af30 Leszek Koltunski
        {
259 beb325a0 Leszek Koltunski
        @Override
260
        public void run()
261
          {
262
          leaveScrambleMode();
263
          }
264
        });
265 0c52af30 Leszek Koltunski
      }
266 47ba5ddc Leszek Koltunski
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269
    void setQuatAccumulated()
270
      {
271
      mQuatAccumulated.set(mTempAccumulated);
272
      }
273
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276
    void setQuatCurrent()
277
      {
278
      mQuatCurrent.set(mTempCurrent);
279
      }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
    Static4D getQuatAccumulated()
284
      {
285
      return mQuatAccumulated;
286
      }
287
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
    Static4D getQuatCurrent()
291
      {
292
      return mQuatCurrent;
293
      }
294
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
297 beb325a0 Leszek Koltunski
    private Static4D quatFromDrag(float dragX, float dragY)
298 47ba5ddc Leszek Koltunski
      {
299 beb325a0 Leszek Koltunski
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
300
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
301
      float axisZ = 0;
302
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
303 47ba5ddc Leszek Koltunski
304 beb325a0 Leszek Koltunski
      if( axisL>0 )
305
        {
306
        axisX /= axisL;
307
        axisY /= axisL;
308
        axisZ /= axisL;
309 47ba5ddc Leszek Koltunski
310 beb325a0 Leszek Koltunski
        float ratio = axisL/mScreenMin;
311
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
312 47ba5ddc Leszek Koltunski
313 beb325a0 Leszek Koltunski
        float cosA = (float)Math.cos(Math.PI*ratio);
314
        float sinA = (float)Math.sqrt(1-cosA*cosA);
315
316
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
317
        }
318
319
      return new Static4D(0f, 0f, 0f, 1f);
320 47ba5ddc Leszek Koltunski
      }
321
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324 beb325a0 Leszek Koltunski
    void setMovement(RubikCubeMovement movement)
325 47ba5ddc Leszek Koltunski
      {
326 beb325a0 Leszek Koltunski
      mMovement = movement;
327 47ba5ddc Leszek Koltunski
      }
328
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
// PUBLIC API
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
    public RubikSurfaceView(Context context, AttributeSet attrs)
334
      {
335
      super(context,attrs);
336
337
      if(!isInEditMode())
338
        {
339 beb325a0 Leszek Koltunski
        mInScrambleMode = false;
340 47ba5ddc Leszek Koltunski
        mRenderer = new RubikRenderer(this);
341
342
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
343
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
344
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
345
        setRenderer(mRenderer);
346
        }
347
      }
348
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
351
    @Override
352
    public boolean onTouchEvent(MotionEvent event)
353
      {
354
      int action = event.getAction();
355
      int x = (int)event.getX();
356
      int y = (int)event.getY();
357
358
      switch(action)
359
         {
360
         case MotionEvent.ACTION_DOWN: mX = x;
361
                                       mY = y;
362 beb325a0 Leszek Koltunski
                                       mLastTouchedFace = mMovement.faceTouched(mQuatAccumulated,mCameraDistance, x,y, mScreenWidth, mScreenHeight);
363 47ba5ddc Leszek Koltunski
364 beb325a0 Leszek Koltunski
                                       if( mLastTouchedFace != RubikCubeMovement.NONE )
365 47ba5ddc Leszek Koltunski
                                         {
366
                                         mDragging           = false;
367
                                         mBeginningRotation  = mRenderer.canRotate();
368
                                         mContinuingRotation = false;
369
                                         }
370
                                       else
371
                                         {
372
                                         mDragging           = mRenderer.canDrag();
373
                                         mBeginningRotation  = false;
374
                                         mContinuingRotation = false;
375
                                         }
376
                                       break;
377
         case MotionEvent.ACTION_MOVE: if( mDragging )
378
                                         {
379
                                         mTempCurrent.set(quatFromDrag(mX-x,mY-y));
380
                                         mRenderer.setQuatCurrentOnNextRender();
381
382
                                         int minimumDist = (mScreenMin*mScreenMin)/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY);
383
384
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > minimumDist )
385
                                           {
386
                                           mX = x;
387
                                           mY = y;
388
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
389
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
390
                                           mRenderer.setQuatCurrentOnNextRender();
391
                                           mRenderer.setQuatAccumulatedOnNextRender();
392
                                           }
393
                                         }
394
                                       if( mBeginningRotation )
395
                                         {
396
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY);
397
398
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
399
                                           {
400 beb325a0 Leszek Koltunski
                                           mMovement.addNewRotation(mQuatAccumulated,mLastTouchedFace, x,y, mScreenWidth, mScreenHeight);
401 47ba5ddc Leszek Koltunski
                                           mBeginningRotation = false;
402
                                           mContinuingRotation= true;
403
                                           }
404
                                         }
405
                                       else if( mContinuingRotation )
406
                                         {
407 beb325a0 Leszek Koltunski
                                         mMovement.continueRotation(mQuatAccumulated,mLastTouchedFace, x,y, mScreenWidth, mScreenHeight);
408 47ba5ddc Leszek Koltunski
                                         }
409
                                       break;
410
         case MotionEvent.ACTION_UP  : if( mDragging )
411
                                         {
412
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
413
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
414
                                         mRenderer.setQuatCurrentOnNextRender();
415
                                         mRenderer.setQuatAccumulatedOnNextRender();
416
                                         }
417
418
                                       if( mContinuingRotation )
419
                                         {
420 beb325a0 Leszek Koltunski
                                         mRenderer.finishRotation();
421 47ba5ddc Leszek Koltunski
                                         }
422
                                       break;
423
         }
424
425
      return true;
426
      }
427 0c52af30 Leszek Koltunski
}