Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikSurfaceView.java @ 123d6172

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

    
50
    // Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
51
    private final static int ROTATION_SENSITIVITY =  12;
52

    
53
    // Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
54
    private final static int DIRECTION_SENSITIVITY=  12;
55

    
56
    private RubikRenderer mRenderer;
57
    private RubikCubeMovement mMovement;
58

    
59
    private boolean mInScrambleMode;
60
    private int mButton = RubikSize.SIZE3.ordinal();
61

    
62
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
63
    private float mX, mY;
64
    private int mScreenWidth, mScreenHeight, mScreenMin;
65

    
66
    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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
// return quat1*quat2
73

    
74
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
75
      {
76
      float qx = quat1.get1();
77
      float qy = quat1.get2();
78
      float qz = quat1.get3();
79
      float qw = quat1.get4();
80

    
81
      float rx = quat2.get1();
82
      float ry = quat2.get2();
83
      float rz = quat2.get3();
84
      float rw = quat2.get4();
85

    
86
      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

    
91
      return new Static4D(tx,ty,tz,tw);
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
96

    
97
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
98
      {
99
      float qx = quat.get1();
100
      float qy = quat.get2();
101
      float qz = quat.get3();
102
      float qw = quat.get4();
103

    
104
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
105
      Static4D tmp = quatMultiply(quat,vector);
106

    
107
      return quatMultiply(tmp,quatInverted);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
112

    
113
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
114
      {
115
      float qx = quat.get1();
116
      float qy = quat.get2();
117
      float qz = quat.get3();
118
      float qw = quat.get4();
119

    
120
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
121
      Static4D tmp = quatMultiply(quatInverted,vector);
122

    
123
      return quatMultiply(tmp,quat);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    void setScreenSize(int width, int height)
129
      {
130
      mScreenWidth = width;
131
      mScreenHeight= height;
132

    
133
      mScreenMin = width<height ? width:height;
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
    RubikRenderer getRenderer()
139
      {
140
      return mRenderer;
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    int getRedButton()
146
      {
147
      return mButton;
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    void markButton(int button)
153
      {
154
      mButton = button;
155
      RubikActivity act = (RubikActivity)getContext();
156

    
157
      for(int b=0; b<RubikSize.LENGTH; b++)
158
        {
159
        Drawable d = act.findViewById(b).getBackground();
160

    
161
        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
        }
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    void addSizeButtons(RubikActivity act)
175
      {
176
      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
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    void enterScrambleMode()
199
      {
200
      if( !mInScrambleMode )
201
        {
202
        mInScrambleMode = true;
203

    
204
        android.util.Log.e("view", "entering scramble mode");
205

    
206
        RubikActivity act = (RubikActivity)getContext();
207

    
208
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
209

    
210
        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
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    void leaveScrambleMode()
225
      {
226
      if( mInScrambleMode )
227
        {
228
        mInScrambleMode = false;
229

    
230
        android.util.Log.e("view", "leaving scramble mode");
231

    
232
        RubikActivity act = (RubikActivity)getContext();
233

    
234
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
235

    
236
        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
        }
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
    void leaveScrambleModeNonUI()
251
      {
252
      RubikActivity act = (RubikActivity)getContext();
253

    
254
      act.runOnUiThread(new Runnable()
255
        {
256
        @Override
257
        public void run()
258
          {
259
          leaveScrambleMode();
260
          }
261
        });
262
      }
263

    
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
    private Static4D quatFromDrag(float dragX, float dragY)
295
      {
296
      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

    
301
      if( axisL>0 )
302
        {
303
        axisX /= axisL;
304
        axisY /= axisL;
305
        axisZ /= axisL;
306

    
307
        float ratio = axisL;
308
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
309

    
310
        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
      }
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
        mInScrambleMode = false;
330
        mRenderer = new RubikRenderer(this);
331
        mMovement = new RubikCubeMovement();
332

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

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

    
354
                                       if( mMovement.faceTouched(mQuatAccumulated, x, y) )
355
                                         {
356
                                         mDragging           = false;
357
                                         mBeginningRotation  = mRenderer.canRotate();
358
                                         mContinuingRotation = false;
359
                                         }
360
                                       else
361
                                         {
362
                                         mDragging           = mRenderer.canDrag();
363
                                         mBeginningRotation  = false;
364
                                         mContinuingRotation = false;
365
                                         }
366
                                       break;
367
         case MotionEvent.ACTION_MOVE: if( mDragging )
368
                                         {
369
                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
370
                                         mRenderer.setQuatCurrentOnNextRender();
371

    
372
                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
373
                                           {
374
                                           mX = x;
375
                                           mY = y;
376
                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
377
                                           mTempCurrent.set(0f, 0f, 0f, 1f);
378
                                           mRenderer.setQuatCurrentOnNextRender();
379
                                           mRenderer.setQuatAccumulatedOnNextRender();
380
                                           }
381
                                         }
382
                                       if( mBeginningRotation )
383
                                         {
384
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
385
                                           {
386
                                           Static2D rot = mMovement.newRotation(mQuatAccumulated, x, y);
387
                                           RubikCube cube = mRenderer.getCube();
388

    
389
                                           cube.addNewRotation( (int)rot.get1(), (int)(cube.getSize()*rot.get2()) );
390

    
391
                                           mBeginningRotation = false;
392
                                           mContinuingRotation= true;
393
                                           }
394
                                         }
395
                                       else if( mContinuingRotation )
396
                                         {
397
                                         float angle = mMovement.continueRotation(mQuatAccumulated, x, y);
398
                                         mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
399
                                         }
400
                                       break;
401
         case MotionEvent.ACTION_UP  : if( mDragging )
402
                                         {
403
                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
404
                                         mTempCurrent.set(0f, 0f, 0f, 1f);
405
                                         mRenderer.setQuatCurrentOnNextRender();
406
                                         mRenderer.setQuatAccumulatedOnNextRender();
407
                                         }
408

    
409
                                       if( mContinuingRotation )
410
                                         {
411
                                         mRenderer.finishRotation();
412
                                         }
413
                                       break;
414
         }
415

    
416
      return true;
417
      }
418
}
419

    
(10-10/10)