Project

General

Profile

« Previous | Next » 

Revision e41e7dc3

Added by Leszek Koltunski about 4 years ago

Add the 'Solved' dialog.

View differences:

src/main/java/org/distorted/dialog/RubikDialogSolved.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.dialog;
21

  
22
import android.app.Dialog;
23
import android.content.DialogInterface;
24
import android.os.Bundle;
25
import android.support.annotation.NonNull;
26
import android.support.v4.app.FragmentActivity;
27
import android.support.v7.app.AlertDialog;
28
import android.support.v7.app.AppCompatDialogFragment;
29
import android.view.LayoutInflater;
30
import android.view.View;
31
import android.view.Window;
32
import android.view.WindowManager;
33
import android.widget.TextView;
34

  
35
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37
import org.distorted.uistate.RubikState;
38

  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

  
41
public class RubikDialogSolved extends AppCompatDialogFragment
42
  {
43
  @Override
44
  public void onStart()
45
    {
46
    super.onStart();
47

  
48
    Window window = getDialog().getWindow();
49
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
50
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
51
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
52
    }
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

  
56
  @NonNull
57
  @Override
58
  public Dialog onCreateDialog(Bundle savedInstanceState)
59
    {
60
    FragmentActivity act = getActivity();
61
    LayoutInflater inflater = act.getLayoutInflater();
62
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
63

  
64
    TextView tv = (TextView) inflater.inflate(R.layout.dialog_title, null);
65
    tv.setText(R.string.solved);
66
    builder.setCustomTitle(tv);
67

  
68
    builder.setCancelable(true);
69
    builder.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener()
70
      {
71
      @Override
72
      public void onClick(DialogInterface dialog, int which)
73
        {
74
        RubikActivity act = (RubikActivity)getActivity();
75
        RubikState.switchState(act,RubikState.PLAY);
76
        }
77
      });
78

  
79
    Bundle args = getArguments();
80
    long time;
81

  
82
    try
83
      {
84
      time = args.getLong("time");
85
      }
86
    catch(Exception e)
87
      {
88
      time = 0;
89
      }
90

  
91
    final View view = inflater.inflate(R.layout.dialog_solved, null);
92
    TextView text = view.findViewById(R.id.solved_time);
93
    text.setText(getString(R.string.ti_placeholder, (time/100)/10.0f));
94
    builder.setView(view);
95

  
96
    return builder.create();
97
    }
98
  }
src/main/java/org/distorted/magic/RubikActivity.java
79 79
      view.onResume();
80 80
      restorePreferences();
81 81
      RubikState.setState(this);
82
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
83 82

  
84 83
      if( mJustStarted )
85 84
        {
......
89 88
        scores.setCountry(this);
90 89
        }
91 90

  
91
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
92 92
      int object = play.getObject();
93 93
      int size   = play.getSize();
94 94
      RubikObjectList obj = RubikObjectList.getObject(object);
src/main/java/org/distorted/magic/RubikRenderer.java
21 21

  
22 22
import android.content.SharedPreferences;
23 23
import android.opengl.GLSurfaceView;
24
import android.os.Bundle;
24 25

  
26
import org.distorted.dialog.RubikDialogSolved;
25 27
import org.distorted.effect.BaseEffect;
26 28
import org.distorted.library.effect.VertexEffectSink;
27 29
import org.distorted.library.main.DistortedLibrary;
......
53 55
    private boolean mChangeObject, mSolveObject, mScrambleObject;
54 56
    private boolean mCanRotate, mCanDrag, mCanUI;
55 57
    private boolean mIsSolved;
58
    private boolean mIsNewRecord;
59
    private long mNewRecord;
56 60
    private RubikObject mOldObject, mNewObject;
57 61
    private int mScreenWidth, mScreenHeight;
58 62
    private SharedPreferences mPreferences;
......
265 269
         if( RubikState.getCurrentState()==RubikState.SOLV )
266 270
           {
267 271
           RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
268
           solving.stopCounting();
272
           mNewRecord = solving.stopCounting((RubikActivity)mView.getContext());
273

  
274
           if( mNewRecord< 0 )
275
             {
276
             mNewRecord = -mNewRecord;
277
             mIsNewRecord = false;
278
             }
279
           else
280
             {
281
             mIsNewRecord = true;
282
             }
269 283
           }
270 284

  
271 285
         mCanDrag   = true;
......
383 397
                 }
384 398
               });
385 399
             }
400

  
401
           if( i==BaseEffect.Type.WIN.ordinal() )
402
             {
403
             if( RubikState.getCurrentState()==RubikState.SOLV )
404
               {
405
               final RubikActivity act = (RubikActivity)mView.getContext();
406

  
407
               if( mIsNewRecord )
408
                 {
409
                 RubikDialogSolved dialog = new RubikDialogSolved();
410

  
411
                 Bundle bundle = new Bundle();
412
                 bundle.putLong("time", mNewRecord );
413
                 dialog.setArguments(bundle);
414
                 dialog.show( act.getSupportFragmentManager(), null);
415
                 }
416
               else
417
                 {
418
                 RubikDialogSolved dialog = new RubikDialogSolved();
419

  
420
                 Bundle bundle = new Bundle();
421
                 bundle.putLong("time", mNewRecord );
422
                 dialog.setArguments(bundle);
423
                 dialog.show( act.getSupportFragmentManager(), null);
424
                 }
425
               }
426
             }
427

  
386 428
           break;
387 429
           }
388 430
         }
src/main/java/org/distorted/scores/RubikScores.java
217 217

  
218 218
///////////////////////////////////////////////////////////////////////////////////////////////////
219 219

  
220
  public void setRecord(int object, int size, int scramble, long timeTaken)
220
  public boolean setRecord(int object, int size, int scramble, long timeTaken)
221 221
    {
222 222
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
223 223

  
......
226 226
      if( mRecords[object][size][scramble-1]> timeTaken )
227 227
        {
228 228
        mRecords[object][size][scramble-1] = timeTaken;
229
        android.util.Log.e("RubikScores","new record: ("+object+","+size+","+scramble+") ="+timeTaken);
229
        return true;
230 230
        }
231 231
      }
232

  
233
    return false;
232 234
    }
233 235

  
234 236
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/uistate/RubikStateSolving.java
42 42
  private long mStartTime;
43 43
  private boolean mRunning;
44 44
  private RubikScores mScores;
45
  private Button mBack;
45 46

  
46 47
///////////////////////////////////////////////////////////////////////////////////////////////////
47 48

  
......
54 55

  
55 56
  void leaveState(RubikActivity act)
56 57
    {
57
    stopCounting();
58
    stopCounting(act);
58 59
    }
59 60

  
60 61
///////////////////////////////////////////////////////////////////////////////////////////////////
......
88 89
    buttonL.setOnClickListener(act);
89 90
    layoutBot.addView(buttonL);
90 91

  
91
    Button buttonR = new Button(act);
92
    buttonR.setLayoutParams(params);
93
    buttonR.setId(BUTTON_ID_BACK);
94
    buttonR.setPadding(padding,0,padding,0);
95
    buttonR.setText(R.string.back);
96
    buttonR.setOnClickListener(act);
97
    layoutBot.addView(buttonR);
92
    mBack = new Button(act);
93
    mBack.setLayoutParams(params);
94
    mBack.setId(BUTTON_ID_BACK);
95
    mBack.setPadding(padding,0,padding,0);
96
    mBack.setText(R.string.back);
97
    mBack.setOnClickListener(act);
98
    layoutBot.addView(mBack);
98 99

  
99 100
    buttonL.setVisibility(android.view.View.INVISIBLE);
100 101
    }
......
144 145

  
145 146
///////////////////////////////////////////////////////////////////////////////////////////////////
146 147

  
147
  public long stopCounting()
148
  public long stopCounting(RubikActivity act)
148 149
    {
149 150
    if( mRunning )
150 151
      {
152
      act.runOnUiThread(new Runnable()
153
        {
154
        @Override
155
        public void run()
156
          {
157
          mTime.setText(R.string.solved);
158
          mBack.setClickable(false);
159
          }
160
        });
161

  
151 162
      if( mTimer!=null )
152 163
        {
153 164
        mTimer.cancel();
......
162 173
      int size    = play.getSize();
163 174
      int scramble= play.getPicker();
164 175

  
165
      mScores.setRecord(object, size, scramble, timeTaken);
166
      return timeTaken;
176
      boolean isNew = mScores.setRecord(object, size, scramble, timeTaken);
177

  
178
      return isNew ? timeTaken : -timeTaken;
167 179
      }
168 180

  
169 181
    return 0;
src/main/res/layout/dialog_solved.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="match_parent"
4
    android:layout_height="match_parent"
5
    android:gravity="center_horizontal"
6
    android:orientation="vertical">
7

  
8
    <LinearLayout
9
        android:layout_width="fill_parent"
10
        android:layout_height="fill_parent"
11
        android:gravity="center|fill_horizontal"
12
        android:layout_marginLeft="10dp"
13
        android:layout_marginRight="10dp"
14
        android:layout_marginTop="0dp"
15
        android:background="@color/grey"
16
        android:orientation="vertical">
17

  
18
        <TextView
19
            android:id="@+id/solved_time"
20
            android:layout_width="match_parent"
21
            android:layout_height="match_parent"
22
            android:gravity="center"
23
            android:textSize="24sp"
24
            android:layout_marginTop="10dp"
25
            android:layout_marginLeft="10dp"
26
            android:layout_marginRight="10dp"
27
            android:layout_marginBottom="10dp"/>
28

  
29
    </LinearLayout>
30
</LinearLayout>
src/main/res/values/strings.xml
9 9
    <string name="settings">Settings</string>
10 10
    <string name="scores">High Scores</string>
11 11
    <string name="about">About</string>
12
    <string name="solved">Solved</string>
12 13
    <string name="back">Back</string>
13 14
    <string name="save">SAVE</string>
14 15
    <string name="ok">OK</string>
......
19 20
    <string name="win_effect">Win Effect</string>
20 21
    <string name="duration">Duration:</string>
21 22
    <string name="type">Type:</string>
22
    <string name="downloading">Downloading...</string>
23
    <string name="downloading">Downloading</string>
23 24
    <string name="credits1">Open Source app developed using the Distorted graphics library. </string>
24 25
    <string name="credits2">Code, tutorials, learn how to write your own graphics effect: <a href="http://www.distorted.org/cube">Distorted.org</a></string>
25 26

  
......
27 28
    <string name="sc_placeholder">Scramble %1$d</string>
28 29
    <string name="tm_placeholder">%1$02d:%2$02d</string>
29 30
    <string name="ap_placeholder">%1$s %2$s</string>
31
    <string name="ti_placeholder">%1$.1f seconds</string>
30 32
</resources>

Also available in: Unified diff