Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 86c73a69

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