Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / ScreenSolutionSinglephased.java @ d34d025d

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