Project

General

Profile

« Previous | Next » 

Revision 211b48f2

Added by Leszek Koltunski over 4 years ago

Separate the States (and also Dialogues) into their own package.

View differences:

src/main/java/org/distorted/magic/RubikSurfaceView.java
21 21

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

  
41
import org.distorted.component.HorizontalNumberPicker;
42 29
import org.distorted.library.type.Static2D;
43 30
import org.distorted.library.type.Static4D;
44 31
import org.distorted.object.RubikCube;
......
48 35

  
49 36
public class RubikSurfaceView extends GLSurfaceView
50 37
{
51
    public static final int BUTTON_ID_BACK= 1023;
52

  
53
    public static final int MIN_SCRAMBLE =  1;
54
    public static final int DEF_SCRAMBLE =  1;
55
    public static final int MAX_SCRAMBLE = 18;
56

  
57 38
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
58 39
    // given face by SWIPING_SENSITIVITY/2 degrees.
59 40
    private final static int SWIPING_SENSITIVITY  = 240;
......
70 51
    private float mX, mY;
71 52
    private int mScreenWidth, mScreenHeight, mScreenMin;
72 53

  
73
    private HorizontalNumberPicker mPicker;
74
    private int mPickerValue;
75
    private RubikState mCurrentState;
76

  
77
    private static int mButton = RubikSize.SIZE3.ordinal();
78 54
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
79 55
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
80 56
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
81 57
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
82 58

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

  
85
    void scramble()
86
      {
87
      int scramble = mPicker.getValue();
88
      mRenderer.scrambleCube(scramble);
89
      }
90

  
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

  
93
    void savePreferences(SharedPreferences.Editor editor)
94
      {
95
      if( mPicker!=null )
96
        {
97
        editor.putInt("scramble", mPicker.getValue() );
98
        }
99
      }
100

  
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

  
103
    void restorePreferences(SharedPreferences preferences)
104
      {
105
      mPickerValue= preferences.getInt("scramble", DEF_SCRAMBLE);
106
      }
107

  
108 59
///////////////////////////////////////////////////////////////////////////////////////////////////
109 60

  
110 61
    void setScreenSize(int width, int height)
......
122 73
      return mRenderer;
123 74
      }
124 75

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

  
127
    static int getRedButton()
128
      {
129
      return mButton;
130
      }
131

  
132 76
///////////////////////////////////////////////////////////////////////////////////////////////////
133 77

  
134 78
    void setQuatAccumulated()
......
239 183
      return quatMultiply(tmp,quat);
240 184
      }
241 185

  
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

  
244
    void markButton(int button)
245
      {
246
      mButton = button;
247
      RubikActivity act = (RubikActivity)getContext();
248

  
249
      for(int b=0; b<RubikSize.LENGTH; b++)
250
        {
251
        Drawable d = act.findViewById(b).getBackground();
252

  
253
        if( b==button )
254
          {
255
          d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
256
          }
257
        else
258
          {
259
          d.clearColorFilter();
260
          }
261
        }
262
      }
263

  
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

  
266
    void switchState(RubikActivity act, RubikState state)
267
      {
268
      if( mCurrentState!=state )
269
        {
270
        switch(state)
271
          {
272
          case MAIN: enterMainState(act); break;
273
          case PLAY: enterPlayState(act); break;
274
          }
275

  
276
        if( mCurrentState!=null )
277
          {
278
          switch(mCurrentState)
279
            {
280
            case MAIN: leaveMainState(act); break;
281
            case PLAY: leavePlayState(act); break;
282
            }
283
          }
284

  
285
        mCurrentState = state;
286
        }
287
      else
288
        {
289
        android.util.Log.e("act", "trying to change into same state "+state);
290
        }
291
      }
292

  
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

  
295
    private void leaveMainState(RubikActivity act)
296
      {
297
      FragmentManager mana = act.getSupportFragmentManager();
298
      RubikDialogMain diag = (RubikDialogMain) mana.findFragmentByTag(RubikDialogMain.getDialogTag());
299

  
300
      if( diag!=null )
301
        {
302
        diag.dismiss();
303
        }
304
      else
305
        {
306
        android.util.Log.e("act", "cannot find main dialog!");
307
        }
308
      }
309

  
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

  
312
    private void leavePlayState(RubikActivity act)
313
      {
314
      mPickerValue = mPicker.getValue();
315
      }
316

  
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

  
319
    private void enterMainState(RubikActivity act)
320
      {
321
      FragmentManager mana = act.getSupportFragmentManager();
322
      RubikDialogMain diag = (RubikDialogMain) mana.findFragmentByTag(RubikDialogMain.getDialogTag());
323

  
324
      if( diag==null )
325
        {
326
        RubikDialogMain diag2 = new RubikDialogMain();
327
        diag2.show( mana, RubikDialogMain.getDialogTag() );
328
        }
329

  
330
      LayoutInflater inflater = act.getLayoutInflater();
331

  
332
      // TOP ////////////////////////////
333
      LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
334
      layoutTop.removeAllViews();
335
      final View viewTop = inflater.inflate(R.layout.main_title, null);
336
      layoutTop.addView(viewTop);
337

  
338
      // BOT ////////////////////////////
339
      LinearLayout layoutBot = act.findViewById(R.id.mainBar);
340
      layoutBot.removeAllViews();
341

  
342
      DisplayMetrics metrics = getResources().getDisplayMetrics();
343
      float scale = metrics.density;
344
      int size = (int)(60*scale +0.5f);
345
      int padding = (int)(5*scale + 0.5f);
346
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
347

  
348
      Button buttonL = new Button(act);
349
      buttonL.setLayoutParams(params);
350
      buttonL.setId(BUTTON_ID_BACK);
351
      buttonL.setPadding(padding,0,padding,0);
352
      buttonL.setText(R.string.back);
353
      buttonL.setOnClickListener(act);
354
      layoutBot.addView(buttonL);
355

  
356
      Button buttonR = new Button(act);
357
      buttonR.setLayoutParams(params);
358
      buttonR.setId(BUTTON_ID_BACK);
359
      buttonR.setPadding(padding,0,padding,0);
360
      buttonR.setText(R.string.exit);
361
      buttonR.setOnClickListener(act);
362
      layoutBot.addView(buttonR);
363

  
364
      buttonL.setVisibility(INVISIBLE);
365
      }
366

  
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

  
369
    private void enterPlayState(RubikActivity act)
370
      {
371
      LayoutInflater inflater = act.getLayoutInflater();
372

  
373
      // TOP ////////////////////////////
374
      LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
375
      layoutTop.removeAllViews();
376

  
377
      final View viewTop = inflater.inflate(R.layout.play_title, null);
378
      layoutTop.addView(viewTop);
379

  
380
      // BOT ////////////////////////////
381
      LinearLayout layoutBot = act.findViewById(R.id.mainBar);
382
      layoutBot.removeAllViews();
383

  
384
      DisplayMetrics metrics = getResources().getDisplayMetrics();
385
      float scale = metrics.density;
386
      int size = (int)(60*scale +0.5f);
387
      int padding = (int)(3*scale + 0.5f);
388
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
389

  
390
      for(int i=0; i<RubikSize.LENGTH; i++)
391
        {
392
        ImageButton button = new ImageButton(act);
393
        button.setLayoutParams(params);
394
        button.setId(i);
395
        button.setPadding(padding,0,padding,0);
396
        int iconID = RubikSize.getSize(i).getIconID();
397
        button.setImageResource(iconID);
398
        button.setOnClickListener(act);
399
        layoutBot.addView(button);
400
        }
401

  
402
      ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
403

  
404
      Button button = new Button(act);
405
      button.setLayoutParams(params2);
406
      button.setId(BUTTON_ID_BACK);
407
      button.setPadding(padding,0,padding,0);
408
      button.setText(R.string.back);
409
      button.setOnClickListener(act);
410
      layoutBot.addView(button);
411

  
412
      markButton(mButton);
413

  
414
      mPicker = act.findViewById(R.id.rubikNumberPicker);
415

  
416
      if( mPicker!=null )
417
        {
418
        mPicker.setMin(MIN_SCRAMBLE);
419
        mPicker.setMax(MAX_SCRAMBLE);
420
        mPicker.setValue(mPickerValue);
421
        }
422
      }
423

  
424 186
///////////////////////////////////////////////////////////////////////////////////////////////////
425 187
// PUBLIC API
426 188
///////////////////////////////////////////////////////////////////////////////////////////////////
......
434 196
        mRenderer = new RubikRenderer(this);
435 197
        mMovement = new RubikCubeMovement();
436 198

  
437
        mCurrentState = null;
438

  
439 199
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
440 200
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
441 201
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );

Also available in: Unified diff