Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / ScreenSolving.java @ e9397ae9

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.playui;
11

    
12
import android.content.SharedPreferences;
13
import android.util.TypedValue;
14
import android.view.LayoutInflater;
15
import android.view.View;
16
import android.widget.LinearLayout;
17
import android.widget.TextView;
18

    
19
import org.distorted.dialogs.RubikDialogAbandon;
20
import org.distorted.external.RubikScores;
21
import org.distorted.helpers.TransparentImageButton;
22
import org.distorted.main.R;
23
import org.distorted.objects.RubikObjectList;
24

    
25
import java.util.Timer;
26
import java.util.TimerTask;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class ScreenSolving extends ScreenBase
31
  {
32
  private static final int MOVES_THRESHHOLD = 10;
33

    
34
  private TextView mTime;
35
  private Timer mTimer;
36
  private long mStartTime;
37
  private boolean mRunning;
38
  private final RubikScores mScores;
39
  private long mElapsed;
40
  private TransparentImageButton mBackButton;
41

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

    
44
  ScreenSolving()
45
    {
46
    mScores = RubikScores.getInstance();
47
    }
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  void leaveScreen(PlayActivity act)
52
    {
53
    stopCounting();
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  void enterScreen(final PlayActivity act)
59
    {
60
    float width = act.getScreenWidthInPixels();
61
    float titleSize  = width*PlayActivity.TITLE_TEXT_SIZE;
62

    
63
    startCounting(act);
64

    
65
    LayoutInflater inflater = act.getLayoutInflater();
66

    
67
    // TOP ////////////////////////////
68
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
69
    layoutTop.removeAllViews();
70
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
71
    int elapsed = (int)mElapsed/1000;
72
    mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
73
    mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
74
    layoutTop.addView(mTime);
75

    
76
    setupBackButton(act);
77
    createBottomPane(act,mBackButton);
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private void setupBackButton(final PlayActivity act)
83
    {
84
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
85
    mBackButton = new TransparentImageButton(act,R.drawable.ui_back,params);
86

    
87
    mBackButton.setOnClickListener( new View.OnClickListener()
88
      {
89
      @Override
90
      public void onClick(View v)
91
        {
92
        if( mMovesController.getNumMoves() > MOVES_THRESHHOLD )
93
          {
94
          RubikDialogAbandon abaDiag = new RubikDialogAbandon();
95
          abaDiag.show(act.getSupportFragmentManager(), null);
96
          }
97
        else
98
          {
99
          ScreenList.goBack(act);
100
          }
101
        }
102
      });
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void savePreferences(SharedPreferences.Editor editor)
108
    {
109
    stopCounting();
110

    
111
    mElapsed = System.currentTimeMillis()-mStartTime;
112
    editor.putLong("stateSolving_elapsed" , mElapsed);
113
    mScores.savePreferences(editor);
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  public void restorePreferences(SharedPreferences preferences)
119
    {
120
    mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
121
    mScores.restorePreferences(preferences);
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  private void startCounting(final PlayActivity act)
127
    {
128
    if( !mRunning )
129
      {
130
      mRunning = true;
131
      mStartTime = System.currentTimeMillis() - mElapsed;
132
      mTimer = new Timer();
133

    
134
      mTimer.scheduleAtFixedRate(new TimerTask()
135
        {
136
        @Override
137
        public void run()
138
          {
139
          act.runOnUiThread(new Runnable()
140
            {
141
            @Override
142
            public void run()
143
              {
144
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
145
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
146
              }
147
            });
148
          }
149
        }, 0, 1000);
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  private void stopCounting()
156
    {
157
    if( mTimer!=null )
158
      {
159
      mTimer.cancel();
160
      mTimer = null;
161
      }
162

    
163
    mRunning = false;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public int stopTimerAndGetRecord()
169
    {
170
    if( mRunning )
171
      {
172
      stopCounting();
173
      mElapsed = System.currentTimeMillis()-mStartTime;
174
      return (int)mElapsed;
175
      }
176

    
177
    return 0;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public int setRecord()
183
    {
184
    /*
185
    ScreenPlay play = (ScreenPlay) ScreenList.PLAY.getScreenClass();
186
    int level = play.getLevel()-1;
187
    */
188

    
189
    int level = 0;
190
    android.util.Log.e("D", "TODO: implement level!!");
191

    
192
    int object = RubikObjectList.getCurrObject();
193
    return mScores.setRecord(object, level, (int)mElapsed);
194
    }
195

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

    
198
  public void resetElapsed()
199
    {
200
    mElapsed = 0;
201
    }
202
  }
(12-12/12)