Project

General

Profile

« Previous | Next » 

Revision 4235de9b

Added by Leszek Koltunski over 4 years ago

Major rearrangement of the UI.

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;
24 25
import android.content.pm.ConfigurationInfo;
25 26
import android.graphics.PorterDuff;
26 27
import android.graphics.drawable.Drawable;
27 28
import android.opengl.GLSurfaceView;
29
import android.preference.PreferenceManager;
28 30
import android.support.v4.content.ContextCompat;
29 31
import android.util.AttributeSet;
30 32
import android.util.DisplayMetrics;
33
import android.view.LayoutInflater;
31 34
import android.view.MotionEvent;
35
import android.view.View;
32 36
import android.view.ViewGroup;
33 37
import android.widget.Button;
34 38
import android.widget.ImageButton;
35 39
import android.widget.LinearLayout;
36 40

  
41
import org.distorted.component.HorizontalNumberPicker;
42
import org.distorted.effect.BaseEffect;
37 43
import org.distorted.library.type.Static2D;
38 44
import org.distorted.library.type.Static4D;
39 45
import org.distorted.object.RubikCube;
......
43 49

  
44 50
public class RubikSurfaceView extends GLSurfaceView
45 51
{
52
    public static final int BUTTON_ID_BACK= 1023;
53
    public static final int BUTTON_ID_EXIT= 1022;
54

  
55
    public static final int MIN_SCRAMBLE =  1;
56
    public static final int DEF_SCRAMBLE =  1;
57
    public static final int MAX_SCRAMBLE = 18;
58

  
46 59
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
47 60
    // given face by SWIPING_SENSITIVITY/2 degrees.
48 61
    private final static int SWIPING_SENSITIVITY  = 240;
......
55 68

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

  
75
    private HorizontalNumberPicker mPicker;
76
    private int mScrambleValue;
77

  
63 78
    private static int mButton = RubikSize.SIZE3.ordinal();
64 79
    private static Static4D mQuatCurrent    = new Static4D(0,0,0,1);
65 80
    private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
66 81
    private static Static4D mTempCurrent    = new Static4D(0,0,0,1);
67 82
    private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
68 83

  
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

  
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

  
94
    void savePreferences()
95
      {
96
      RubikActivity act = (RubikActivity)getContext();
97
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
98
      SharedPreferences.Editor editor = preferences.edit();
99

  
100
      for (int i = 0; i< BaseEffect.Type.LENGTH; i++)
101
        {
102
        BaseEffect.Type.getType(i).savePreferences(editor);
103
        }
104

  
105
      editor.putInt("scramble", mScrambleValue );
106

  
107
      editor.apply();
108
      }
109

  
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

  
112
    void restorePreferences()
113
      {
114
      RubikActivity act = (RubikActivity)getContext();
115
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(act);
116

  
117
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
118
        {
119
        BaseEffect.Type.getType(i).restorePreferences(preferences);
120
        }
121

  
122
      mScrambleValue= preferences.getInt("scramble", DEF_SCRAMBLE);
123
      }
124

  
69 125
///////////////////////////////////////////////////////////////////////////////////////////////////
70 126

  
71 127
    void setScreenSize(int width, int height)
......
224 280

  
225 281
///////////////////////////////////////////////////////////////////////////////////////////////////
226 282

  
227
    void addSizeButtons(RubikActivity act)
283
    void enterMainState(RubikActivity act)
284
      {
285
      LayoutInflater inflater = act.getLayoutInflater();
286

  
287
      // TOP ////////////////////////////
288
      LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
289
      layoutTop.removeAllViews();
290
      final View viewTop = inflater.inflate(R.layout.main_title, null);
291
      layoutTop.addView(viewTop);
292

  
293
      // BOT ////////////////////////////
294
      LinearLayout layoutBot = act.findViewById(R.id.mainBar);
295
      layoutBot.removeAllViews();
296

  
297
      DisplayMetrics metrics = getResources().getDisplayMetrics();
298
      float scale = metrics.density;
299
      int size = (int)(60*scale +0.5f);
300
      int padding = (int)(5*scale + 0.5f);
301
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
302

  
303
      Button buttonL = new Button(act);
304
      buttonL.setLayoutParams(params);
305
      buttonL.setId(BUTTON_ID_EXIT);
306
      buttonL.setPadding(padding,0,padding,0);
307
      buttonL.setText(R.string.back);
308
      buttonL.setOnClickListener(act);
309
      layoutBot.addView(buttonL);
310

  
311
      Button buttonR = new Button(act);
312
      buttonR.setLayoutParams(params);
313
      buttonR.setId(BUTTON_ID_EXIT);
314
      buttonR.setPadding(padding,0,padding,0);
315
      buttonR.setText(R.string.exit);
316
      buttonR.setOnClickListener(act);
317
      layoutBot.addView(buttonR);
318

  
319
      buttonL.setVisibility(INVISIBLE);
320
      }
321

  
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

  
324
    void enterPlayState(RubikActivity act)
228 325
      {
229
      LinearLayout layout = act.findViewById(R.id.sizeLayout);
326
      LayoutInflater inflater = act.getLayoutInflater();
327

  
328
      // TOP ////////////////////////////
329
      LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
330
      layoutTop.removeAllViews();
331

  
332
      final View viewTop = inflater.inflate(R.layout.play_title, null);
333
      layoutTop.addView(viewTop);
334

  
335
      // BOT ////////////////////////////
336
      LinearLayout layoutBot = act.findViewById(R.id.mainBar);
337
      layoutBot.removeAllViews();
338

  
230 339
      DisplayMetrics metrics = getResources().getDisplayMetrics();
231 340
      float scale = metrics.density;
232
      int size = (int)(64*scale +0.5f);
341
      int size = (int)(60*scale +0.5f);
233 342
      int padding = (int)(3*scale + 0.5f);
234 343
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
235 344

  
......
242 351
        int iconID = RubikSize.getSize(i).getIconID();
243 352
        button.setImageResource(iconID);
244 353
        button.setOnClickListener(act);
245
        layout.addView(button);
354
        layoutBot.addView(button);
246 355
        }
247
      }
248

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

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

  
257
        android.util.Log.e("view", "entering scramble mode");
357
      ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
258 358

  
259
        RubikActivity act = (RubikActivity)getContext();
359
      Button button = new Button(act);
360
      button.setLayoutParams(params2);
361
      button.setId(BUTTON_ID_BACK);
362
      button.setPadding(padding,0,padding,0);
363
      button.setText(R.string.back);
364
      button.setOnClickListener(act);
365
      layoutBot.addView(button);
260 366

  
261
        Button scrambleButt = act.findViewById(R.id.rubikScramble);
367
      markButton(mButton);
262 368

  
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
      }
369
      mPicker = act.findViewById(R.id.rubikNumberPicker);
274 370

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

  
277
    void leaveScrambleMode()
278
      {
279
      if( mInScrambleMode )
371
      if( mPicker!=null )
280 372
        {
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
          }
373
        mPicker.setMin(MIN_SCRAMBLE);
374
        mPicker.setMax(MAX_SCRAMBLE);
375
        mPicker.setValue(mScrambleValue);
298 376
        }
299 377
      }
300 378

  
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 379
///////////////////////////////////////////////////////////////////////////////////////////////////
318 380
// PUBLIC API
319 381
///////////////////////////////////////////////////////////////////////////////////////////////////
......
324 386

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

  

Also available in: Unified diff