Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / ScreenSolutionSinglephased.java @ 36aa1f22

1 cb30e768 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 7e9d918b leszek
package org.distorted.solvers;
11 cb30e768 leszek
12
import android.content.SharedPreferences;
13
import android.util.TypedValue;
14
import android.view.Gravity;
15
import android.view.LayoutInflater;
16
import android.view.View;
17
import android.widget.LinearLayout;
18
import android.widget.TextView;
19
20
import org.distorted.helpers.TransparentImageButton;
21
import org.distorted.main.R;
22
import org.distorted.objectlib.helpers.MovesFinished;
23 302600e5 leszek
import org.distorted.objectlib.helpers.ObjectMove;
24 cb30e768 leszek
import org.distorted.objectlib.main.ObjectControl;
25
26 94ce8e53 leszek
import java.lang.ref.WeakReference;
27
28 cb30e768 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
29
30 a742d66b leszek
public class ScreenSolutionSinglephased extends ScreenAbstract implements MovesFinished
31 cb30e768 leszek
  {
32
  private static final int MILLIS_PER_DEGREE = 6;
33
34
  private TransparentImageButton mPrevButton, mNextButton, mBackButton;
35
  private TextView mMovesText;
36 302600e5 leszek
  private ObjectMove[] mMoves;
37 cb30e768 leszek
  private int mCurrMove, mNumMoves;
38
  private boolean mCanRotate;
39
  private float mButtonSize;
40 94ce8e53 leszek
  private WeakReference<SolverActivity> mAct;
41 cb30e768 leszek
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
  void leaveScreen(SolverActivity act)
45
    {
46
    ObjectControl control = act.getControl();
47
    control.solveOnly();
48
    }
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
  void enterScreen(final SolverActivity act)
53
    {
54 94ce8e53 leszek
    mAct = new WeakReference<>(act);
55
56 cb30e768 leszek
    float width = act.getScreenWidthInPixels();
57
    mButtonSize = width*SolverActivity.BUTTON_TEXT_SIZE;
58
    float titleSize  = width*SolverActivity.TITLE_TEXT_SIZE;
59
60
    LayoutInflater inflater = act.getLayoutInflater();
61
62
    // TOP ////////////////////////////
63
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
64
    layoutTop.removeAllViews();
65
66
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
67
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
68
    text.setText(R.string.solution);
69
    layoutTop.addView(text);
70
71
    // BOT ////////////////////////////
72
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
73
    layoutBot.removeAllViews();
74
75
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
76
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
77
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
78
79
    LinearLayout layoutLeft = new LinearLayout(act);
80
    layoutLeft.setLayoutParams(paramsL);
81
    LinearLayout layoutMid = new LinearLayout(act);
82
    layoutMid.setLayoutParams(paramsM);
83
    LinearLayout layoutRight = new LinearLayout(act);
84
    layoutRight.setLayoutParams(paramsR);
85
86
    setupPrevButton(act);
87
    setupNextButton(act);
88
    setupTextView(act,width);
89
90
    layoutLeft.addView(mPrevButton);
91
    layoutLeft.addView(mMovesText);
92
    layoutLeft.addView(mNextButton);
93
94
    setupBackButton(act);
95
96
    layoutRight.addView(mBackButton);
97
98
    layoutBot.addView(layoutLeft);
99
    layoutBot.addView(layoutMid);
100
    layoutBot.addView(layoutRight);
101
    }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
  private void setupPrevButton(final SolverActivity act)
106
    {
107
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
108
    mPrevButton = new TransparentImageButton(act,R.drawable.ui_left,params);
109
110
    mPrevButton.setOnClickListener( new View.OnClickListener()
111
      {
112
      @Override
113
      public void onClick(View v)
114
        {
115
        ObjectControl control = act.getControl();
116
        backMove(control);
117
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
118
        }
119
      });
120
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private void setupNextButton(final SolverActivity act)
125
    {
126
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
127
    mNextButton = new TransparentImageButton(act,R.drawable.ui_right,params);
128
129
    mNextButton.setOnClickListener( new View.OnClickListener()
130
      {
131
      @Override
132
      public void onClick(View v)
133
        {
134
        ObjectControl control = act.getControl();
135
        makeMove(control);
136
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
137
        }
138
      });
139
    }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
  private void setupTextView(final SolverActivity act, final float width)
144
    {
145
    int padding = (int)(width*SolverActivity.PADDING);
146
    int margin  = (int)(width*SolverActivity.SMALL_MARGIN);
147
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
148
    params.topMargin    = margin;
149
    params.bottomMargin = margin;
150
    params.leftMargin   = margin;
151
    params.rightMargin  = margin;
152
153
    mMovesText = new TextView(act);
154
    mMovesText.setTextSize(20);
155
    mMovesText.setLayoutParams(params);
156
    mMovesText.setPadding(padding,0,padding,0);
157
    mMovesText.setGravity(Gravity.CENTER);
158
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
159
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
160
    }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164
  private void setupBackButton(final SolverActivity act)
165
    {
166
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
167
    mBackButton = new TransparentImageButton(act,R.drawable.ui_back,params);
168
169
    mBackButton.setOnClickListener( new View.OnClickListener()
170
      {
171
      @Override
172
      public void onClick(View v)
173
        {
174
        ScreenList.goBack(act);
175
        }
176
      });
177
    }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
  private void makeMove(ObjectControl control)
182
    {
183
    if( mCanRotate )
184
      {
185
      mCurrMove++;
186
187
      if( mCurrMove>mNumMoves )
188
        {
189
        mCurrMove= 0;
190
        control.initializeObject(null);
191
        }
192
      else
193
        {
194 032657c3 leszek
        ObjectMove move = mMoves[mCurrMove-1];
195 cb30e768 leszek
196 032657c3 leszek
        if( move.isNonempty() )
197 cb30e768 leszek
          {
198
          mCanRotate = false;
199 032657c3 leszek
          control.addForwardRotation(this, move, MILLIS_PER_DEGREE);
200 cb30e768 leszek
          }
201
        else
202
          {
203
          android.util.Log.e("solution", "error: solution contains angle 0");
204
          }
205
        }
206
      }
207
    else
208
      {
209
      android.util.Log.e("solution", "failed to make move!");
210
      }
211
    }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
  private void backMove(ObjectControl control)
216
    {
217
    if( mCanRotate )
218
      {
219
      mCurrMove--;
220
221
      if( mCurrMove<0 )
222
        {
223
        mCurrMove=mNumMoves;
224
        control.initializeObject(mMoves);
225
        }
226
      else
227
        {
228 032657c3 leszek
        ObjectMove move = mMoves[mCurrMove];
229 cb30e768 leszek
230 032657c3 leszek
        if( move.isNonempty() )
231 cb30e768 leszek
          {
232
          mCanRotate = false;
233 032657c3 leszek
          control.addBackwardRotation(this, move, MILLIS_PER_DEGREE);
234 cb30e768 leszek
          }
235
        else
236
          {
237
          android.util.Log.e("solution", "error: solution contains angle 0");
238
          }
239
        }
240
      }
241
    else
242
      {
243
      android.util.Log.e("solution", "failed to back move!");
244
      }
245
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 302600e5 leszek
  void setSolution(ObjectMove[] moves)
250 cb30e768 leszek
    {
251
    mCanRotate= true;
252
    mCurrMove = 0;
253
    mNumMoves = moves==null ? 0 : moves.length;
254
    mMoves    = moves;
255
256 94ce8e53 leszek
    SolverActivity act = mAct.get();
257 cb30e768 leszek
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
258
    }
259
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
262 94ce8e53 leszek
  public void savePreferences(SharedPreferences.Editor editor) { }
263
  public void restorePreferences(SharedPreferences preferences) { }
264
  public void onActionFinished(final long effectID) { mCanRotate = true; }
265 cb30e768 leszek
  }