Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.magic;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.graphics.PorterDuff;
26
import android.graphics.drawable.Drawable;
27
import android.opengl.GLSurfaceView;
28
import android.support.v4.content.ContextCompat;
29
import android.util.AttributeSet;
30
import android.util.DisplayMetrics;
31
import android.view.MotionEvent;
32
import android.view.ViewGroup;
33
import android.widget.Button;
34
import android.widget.ImageButton;
35
import android.widget.LinearLayout;
36

    
37
import org.distorted.library.type.Static2D;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.object.RubikCube;
40
import org.distorted.object.RubikCubeMovement;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikSurfaceView extends GLSurfaceView
45
{
46
    // 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
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
50
    private final static int ROTATION_SENSITIVITY =  12;
51
    // 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
    private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
55

    
56
    private RubikRenderer mRenderer;
57
    private RubikCubeMovement mMovement;
58
    private boolean mInScrambleMode;
59
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
60
    private float mX, mY;
61
    private int mScreenWidth, mScreenHeight, mScreenMin;
62

    
63
    private static int mButton = RubikSize.SIZE3.ordinal();
64
    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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// return quat1*quat2
150

    
151
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
152
      {
153
      float qx = quat1.get0();
154
      float qy = quat1.get1();
155
      float qz = quat1.get2();
156
      float qw = quat1.get3();
157

    
158
      float rx = quat2.get0();
159
      float ry = quat2.get1();
160
      float rz = quat2.get2();
161
      float rw = quat2.get3();
162

    
163
      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

    
168
      return new Static4D(tx,ty,tz,tw);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
173

    
174
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
175
      {
176
      float qx = quat.get0();
177
      float qy = quat.get1();
178
      float qz = quat.get2();
179
      float qw = quat.get3();
180

    
181
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
182
      Static4D tmp = quatMultiply(quat,vector);
183

    
184
      return quatMultiply(tmp,quatInverted);
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
189

    
190
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
191
      {
192
      float qx = quat.get0();
193
      float qy = quat.get1();
194
      float qz = quat.get2();
195
      float qw = quat.get3();
196

    
197
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
198
      Static4D tmp = quatMultiply(quatInverted,vector);
199

    
200
      return quatMultiply(tmp,quat);
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    void markButton(int button)
206
      {
207
      mButton = button;
208
      RubikActivity act = (RubikActivity)getContext();
209

    
210
      for(int b=0; b<RubikSize.LENGTH; b++)
211
        {
212
        Drawable d = act.findViewById(b).getBackground();
213

    
214
        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
        }
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    void addSizeButtons(RubikActivity act)
228
      {
229
      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
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
    void enterScrambleMode()
252
      {
253
      if( !mInScrambleMode )
254
        {
255
        mInScrambleMode = true;
256

    
257
        android.util.Log.e("view", "entering scramble mode");
258

    
259
        RubikActivity act = (RubikActivity)getContext();
260

    
261
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
262

    
263
        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
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    void leaveScrambleMode()
278
      {
279
      if( mInScrambleMode )
280
        {
281
        mInScrambleMode = false;
282

    
283
        android.util.Log.e("view", "leaving scramble mode");
284

    
285
        RubikActivity act = (RubikActivity)getContext();
286

    
287
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
288

    
289
        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
        }
299
      }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
    void leaveScrambleModeNonUI()
304
      {
305
      RubikActivity act = (RubikActivity)getContext();
306

    
307
      act.runOnUiThread(new Runnable()
308
        {
309
        @Override
310
        public void run()
311
          {
312
          leaveScrambleMode();
313
          }
314
        });
315
      }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
// PUBLIC API
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
    public RubikSurfaceView(Context context, AttributeSet attrs)
322
      {
323
      super(context,attrs);
324

    
325
      if(!isInEditMode())
326
        {
327
        mInScrambleMode = false;
328
        mRenderer = new RubikRenderer(this);
329
        mMovement = new RubikCubeMovement();
330

    
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
      float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
345
      float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
346

    
347
      switch(action)
348
         {
349
         case MotionEvent.ACTION_DOWN: mX = x;
350
                                       mY = y;
351

    
352
                                       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
                                         {
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
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
372
                                         mRenderer.setQuatCurrentOnNextRender();
373

    
374
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
375
                                           {
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
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
387
                                           {
388
                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
389
                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
390

    
391
                                           Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
392
                                           RubikCube cube = mRenderer.getCube();
393

    
394
                                           cube.addNewRotation( (int)rot.get0(), (int)(cube.getSize()*rot.get1()) );
395

    
396
                                           mBeginningRotation = false;
397
                                           mContinuingRotation= true;
398
                                           }
399
                                         }
400
                                       else if( mContinuingRotation )
401
                                         {
402
                                         Static4D touchPoint3 = new Static4D(x, y, 0, 0);
403
                                         Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
404

    
405
                                         float angle = mMovement.continueRotation(rotatedTouchPoint3);
406
                                         mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
407
                                         }
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
                                         mRenderer.finishRotation();
420
                                         }
421
                                       break;
422
         }
423

    
424
      return true;
425
      }
426
}
427

    
(11-11/11)