Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 0fd3ac1f

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