Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ 0c768d69

1 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 3782bd44 Leszek Koltunski
// 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 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 fcd5b990 Leszek Koltunski
package org.distorted.screens;
11 7289fd6c Leszek Koltunski
12 3f7a4363 Leszek Koltunski
import java.lang.ref.WeakReference;
13
14 7289fd6c Leszek Koltunski
import android.content.SharedPreferences;
15
import android.graphics.Bitmap;
16
import android.graphics.Canvas;
17
import android.graphics.Paint;
18 473611ee Leszek Koltunski
import android.graphics.PorterDuff;
19
import android.graphics.drawable.Drawable;
20 ca292407 Leszek Koltunski
import android.os.Bundle;
21 66e777b0 Leszek Koltunski
import androidx.core.content.ContextCompat;
22 7289fd6c Leszek Koltunski
import android.view.View;
23
import android.widget.ImageButton;
24
import android.widget.LinearLayout;
25
26 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
27 d8cf6f49 Leszek Koltunski
import org.distorted.objectlib.main.ObjectSignatures;
28 3f7a4363 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
29
30 ca292407 Leszek Koltunski
import org.distorted.dialogs.RubikDialogSolverError;
31 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
32 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34 d8cf6f49 Leszek Koltunski
import org.distorted.objectlib.shape.ShapeDiamond;
35
import org.distorted.objectlib.shape.ShapeDodecahedron;
36
import org.distorted.objectlib.shape.ShapeHexahedron;
37
import org.distorted.objectlib.shape.ShapeOctahedron;
38
import org.distorted.objectlib.shape.ShapeTetrahedron;
39 400ff34d Leszek Koltunski
import org.distorted.objects.RubikObjectList;
40 373fa45f Leszek Koltunski
import org.distorted.solvers.ImplementedSolversList;
41 a304ee64 Leszek Koltunski
import org.distorted.solvers.SolverMain;
42 7289fd6c Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 fcd5b990 Leszek Koltunski
public class RubikScreenSolver extends RubikScreenAbstract
46 7289fd6c Leszek Koltunski
  {
47
  private static Bitmap[] mBitmap;
48
  private ImageButton[] mColorButton;
49 dd874ae8 Leszek Koltunski
  private TransparentImageButton mBackButton, mSolveButton;
50 5660465b Leszek Koltunski
  private boolean mSolving;
51 1f9772f3 Leszek Koltunski
  private int mCurrentColor;
52 41748f19 Leszek Koltunski
  private int[] mFaceColors;
53 57dea1ac Leszek Koltunski
  private int mNumColors;
54 7ea57482 Leszek Koltunski
  private float mBitmapSize;
55 373fa45f Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
56
57 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
60 7289fd6c Leszek Koltunski
    {
61 9523ae28 Leszek Koltunski
    ObjectControl control = act.getControl();
62
    control.unsetLock();
63 7289fd6c Leszek Koltunski
    }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
68 7289fd6c Leszek Koltunski
    {
69 9523ae28 Leszek Koltunski
    ObjectControl control = act.getControl();
70
    control.setLock(false);
71
72 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
73 6d4d56cb Leszek Koltunski
    float heigh = act.getScreenHeightInPixels();
74 7ea57482 Leszek Koltunski
75 373fa45f Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
76 5660465b Leszek Koltunski
    mSolving = false;
77
78 67d7fb28 Leszek Koltunski
    int solverIndex= act.getSolverIndex();
79
    ImplementedSolversList currentSolver = ImplementedSolversList.getSolver(solverIndex);
80
    int currentObject = currentSolver.getObject();
81 b20e89d2 Leszek Koltunski
    act.changeIfDifferent(currentObject,control);
82 33f6abfd Leszek Koltunski
    control.solveOnly();
83 1088a219 Leszek Koltunski
    RubikObjectList.setCurrObject(currentObject);
84 7289fd6c Leszek Koltunski
85 d8cf6f49 Leszek Koltunski
    generateFaceColors(currentObject);
86 20931cf6 Leszek Koltunski
87 57dea1ac Leszek Koltunski
    final float BUTTON_RATIO = 0.75f;
88
    int sizeV = (int)(heigh*RubikActivity.RATIO_BAR*BUTTON_RATIO);
89
    int sizeH = (int)((width/mNumColors)*BUTTON_RATIO);
90
    mBitmapSize = Math.min(sizeV,sizeH);
91
92 7289fd6c Leszek Koltunski
    // TOP ////////////////////////////
93
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
94
    layoutTop.removeAllViews();
95
96 57dea1ac Leszek Koltunski
    if( mNumColors>0 )
97 41748f19 Leszek Koltunski
      {
98 7ebd72f7 Leszek Koltunski
      setupBitmaps();
99 ad0c8e0e Leszek Koltunski
      setupColorButtons(act,width);
100 41748f19 Leszek Koltunski
      markButton(act);
101
      }
102 473611ee Leszek Koltunski
103 7289fd6c Leszek Koltunski
    for(ImageButton button: mColorButton) layoutTop.addView(button);
104
105
    // BOT ////////////////////////////
106 57dea1ac Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
107
108
    LinearLayout layoutL = new LinearLayout(act);
109
    layoutL.setLayoutParams(params);
110
    LinearLayout layoutM = new LinearLayout(act);
111
    layoutM.setLayoutParams(params);
112
    LinearLayout layoutR = new LinearLayout(act);
113
    layoutR.setLayoutParams(params);
114
115 92843d3b Leszek Koltunski
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
116
    layoutBot.removeAllViews();
117 264af0ad Leszek Koltunski
118 dd874ae8 Leszek Koltunski
    setupSolveButton(act);
119
    setupBackButton(act);
120 7289fd6c Leszek Koltunski
121 57dea1ac Leszek Koltunski
    layoutL.addView(mSolveButton);
122
    layoutR.addView(mBackButton);
123 af133d41 Leszek Koltunski
124 57dea1ac Leszek Koltunski
    layoutBot.addView(layoutL);
125
    layoutBot.addView(layoutM);
126
    layoutBot.addView(layoutR);
127 7289fd6c Leszek Koltunski
    }
128
129 4ab3d91b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130 d8cf6f49 Leszek Koltunski
// This doesn't quite work in many cases, but in case of the solvers that will pop up in foreseeable
131
// future it should be ok.
132 4ab3d91b Leszek Koltunski
133 d8cf6f49 Leszek Koltunski
  public void generateFaceColors(int object)
134 4ab3d91b Leszek Koltunski
    {
135 d8cf6f49 Leszek Koltunski
    if( object== ObjectSignatures.PYRA_3 ||
136
        object== ObjectSignatures.PYRA_4 ||
137
        object== ObjectSignatures.PYRA_5 ||
138
        object== ObjectSignatures.PDUO_2 ||
139
        object== ObjectSignatures.JING_2 ||
140
        object== ObjectSignatures.MORP_2 ||
141
        object== ObjectSignatures.MORP_3 ||
142
        object== ObjectSignatures.MORP_4  )
143
      {
144 57dea1ac Leszek Koltunski
      mNumColors  = ShapeTetrahedron.NUM_FACES;
145 d8cf6f49 Leszek Koltunski
      mFaceColors = ShapeTetrahedron.FACE_COLORS;
146
      }
147
    else if( object== ObjectSignatures.DIAM_2 ||
148
        object== ObjectSignatures.DIAM_3 ||
149
        object== ObjectSignatures.DIAM_4 ||
150
        object== ObjectSignatures.TRAJ_3 ||
151
        object== ObjectSignatures.TRAJ_4 ||
152
        object== ObjectSignatures.PDIA_3  )
153
      {
154 57dea1ac Leszek Koltunski
      mNumColors  = ShapeOctahedron.NUM_FACES;
155 d8cf6f49 Leszek Koltunski
      mFaceColors = ShapeOctahedron.FACE_COLORS;
156
      }
157
    else if( object== ObjectSignatures.CRYS_3 ||
158
        object== ObjectSignatures.STAR_3 ||
159
        object== ObjectSignatures.PENT_2 ||
160
        object== ObjectSignatures.KILO_3 ||
161
        object== ObjectSignatures.KILO_5 ||
162
        object== ObjectSignatures.MEGA_3 ||
163
        object== ObjectSignatures.MEGA_5  )
164
      {
165 57dea1ac Leszek Koltunski
      mNumColors  = ShapeDodecahedron.NUM_FACES;
166 d8cf6f49 Leszek Koltunski
      mFaceColors = ShapeDodecahedron.FACE_COLORS;
167
      }
168
    else if( object== ObjectSignatures.BALL_4 )
169
      {
170 57dea1ac Leszek Koltunski
      mNumColors  = ShapeDiamond.NUM_FACES;
171 d8cf6f49 Leszek Koltunski
      mFaceColors = ShapeDiamond.FACE_COLORS;
172
      }
173
    else
174
      {
175 57dea1ac Leszek Koltunski
      mNumColors  = ShapeHexahedron.NUM_FACES;
176 d8cf6f49 Leszek Koltunski
      mFaceColors = ShapeHexahedron.FACE_COLORS;
177
      }
178 4ab3d91b Leszek Koltunski
    }
179
180 5660465b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181 7289fd6c Leszek Koltunski
182 7ebd72f7 Leszek Koltunski
  private void setupBitmaps()
183 7289fd6c Leszek Koltunski
    {
184 1bd07548 Leszek Koltunski
    final int SIZE = (int)mBitmapSize;
185
    final float R = SIZE*0.15f;
186
    final float M = SIZE*0.08f;
187 7289fd6c Leszek Koltunski
188 57dea1ac Leszek Koltunski
    mBitmap = new Bitmap[mNumColors];
189 7289fd6c Leszek Koltunski
190
    Paint paint = new Paint();
191
    paint.setColor(0xff008800);
192
    paint.setStyle(Paint.Style.FILL);
193
194
    paint.setAntiAlias(true);
195
    paint.setTextAlign(Paint.Align.CENTER);
196
    paint.setStyle(Paint.Style.FILL);
197
198 57dea1ac Leszek Koltunski
    for(int i=0; i<mNumColors; i++)
199 7289fd6c Leszek Koltunski
      {
200
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
201
      Canvas canvas = new Canvas(mBitmap[i]);
202
203
      paint.setColor(0xff000000);
204
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
205
206 41748f19 Leszek Koltunski
      paint.setColor(mFaceColors[i]);
207 7289fd6c Leszek Koltunski
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
208
      }
209
    }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213 ad0c8e0e Leszek Koltunski
  private void setupColorButtons(final RubikActivity act, final float width)
214 7289fd6c Leszek Koltunski
    {
215 57dea1ac Leszek Koltunski
    mColorButton = new ImageButton[mNumColors];
216 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
217 c651024f Leszek Koltunski
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
218 7289fd6c Leszek Koltunski
219 57dea1ac Leszek Koltunski
    for(int i=0; i<mNumColors; i++)
220 7289fd6c Leszek Koltunski
      {
221
      final int ii = i;
222 ad0c8e0e Leszek Koltunski
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
223
      params.topMargin    = margin;
224
      params.bottomMargin = margin;
225
      params.leftMargin   = margin;
226
      params.rightMargin  = margin;
227
228 7289fd6c Leszek Koltunski
      mColorButton[i] = new ImageButton(act);
229 ad0c8e0e Leszek Koltunski
      mColorButton[i].setLayoutParams(params);
230 7289fd6c Leszek Koltunski
      mColorButton[i].setPadding(padding,0,padding,0);
231
      mColorButton[i].setImageBitmap(mBitmap[i]);
232
233
      mColorButton[i].setOnClickListener( new View.OnClickListener()
234
        {
235
        @Override
236
        public void onClick(View view)
237
          {
238 473611ee Leszek Koltunski
          mCurrentColor = ii;
239
          markButton(act);
240 7289fd6c Leszek Koltunski
          }
241
        });
242
      }
243
    }
244
245 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247 dd874ae8 Leszek Koltunski
  private void setupSolveButton(final RubikActivity act)
248 264af0ad Leszek Koltunski
    {
249 23ac2462 Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_solve,R.drawable.ui_medium_solve, R.drawable.ui_big_solve, R.drawable.ui_huge_solve);
250 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
251
    mSolveButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
252 264af0ad Leszek Koltunski
253
    mSolveButton.setOnClickListener( new View.OnClickListener()
254
      {
255
      @Override
256
      public void onClick(View v)
257
        {
258 5660465b Leszek Koltunski
        if( !mSolving )
259
          {
260
          mSolving = true;
261 9c2f0c91 Leszek Koltunski
          TwistyObject object = act.getObject();
262 ecf3f149 Leszek Koltunski
          SolverMain solver = new SolverMain( act.getResources(), object );
263 f0336037 Leszek Koltunski
          solver.start();
264 5660465b Leszek Koltunski
          }
265 264af0ad Leszek Koltunski
        }
266
      });
267
    }
268
269 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
270
271 dd874ae8 Leszek Koltunski
  private void setupBackButton(final RubikActivity act)
272 7289fd6c Leszek Koltunski
    {
273 4fb1fc0d Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
274 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
275
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
276 7289fd6c Leszek Koltunski
277
    mBackButton.setOnClickListener( new View.OnClickListener()
278
      {
279
      @Override
280
      public void onClick(View v)
281
        {
282 2afc6754 Leszek Koltunski
        ObjectControl control = act.getControl();
283
        control.resetAllTextureMaps();
284 fcd5b990 Leszek Koltunski
        ScreenList.goBack(act);
285 7289fd6c Leszek Koltunski
        }
286
      });
287
    }
288
289 473611ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291
  private void markButton(RubikActivity act)
292
    {
293 57dea1ac Leszek Koltunski
    for(int b=0; b<mNumColors; b++)
294 473611ee Leszek Koltunski
      {
295
      Drawable d = mColorButton[b].getBackground();
296
297
      if( b==mCurrentColor )
298
        {
299
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
300
        }
301
      else
302
        {
303
        d.clearColorFilter();
304
        }
305
      }
306
    }
307
308 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
  public void savePreferences(SharedPreferences.Editor editor)
311
    {
312 1f9772f3 Leszek Koltunski
    editor.putInt("stateSolver_color", mCurrentColor);
313 7289fd6c Leszek Koltunski
    }
314
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
317
  public void restorePreferences(SharedPreferences preferences)
318
    {
319 1f9772f3 Leszek Koltunski
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
320 7289fd6c Leszek Koltunski
    }
321 473611ee Leszek Koltunski
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324
  public int getCurrentColor()
325
    {
326
    return mCurrentColor;
327
    }
328 f0336037 Leszek Koltunski
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331 373fa45f Leszek Koltunski
  public void setSolved(final String moves)
332 f0336037 Leszek Koltunski
    {
333
    mSolving = false;
334 373fa45f Leszek Koltunski
    final RubikActivity act = mWeakAct.get();
335 f0336037 Leszek Koltunski
336 373fa45f Leszek Koltunski
    if( act!=null )
337 f0336037 Leszek Koltunski
      {
338 373fa45f Leszek Koltunski
      act.runOnUiThread(new Runnable()
339 f0336037 Leszek Koltunski
        {
340 373fa45f Leszek Koltunski
        @Override
341
        public void run()
342
          {
343 f5da732a Leszek Koltunski
          ScreenList.switchScreen(act, ScreenList.SOLU);
344
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
345 373fa45f Leszek Koltunski
          solution.setupMoves(act, moves);
346
          }
347
        });
348
      }
349 f0336037 Leszek Koltunski
    }
350
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352
353 c02fa107 Leszek Koltunski
  public void displayErrorDialog(String message)
354 f0336037 Leszek Koltunski
    {
355
    mSolving = false;
356 373fa45f Leszek Koltunski
    RubikActivity act = mWeakAct.get();
357 f0336037 Leszek Koltunski
358 373fa45f Leszek Koltunski
    if( act!=null )
359
      {
360
      RubikDialogSolverError dialog = new RubikDialogSolverError();
361
      Bundle bundle = new Bundle();
362 c02fa107 Leszek Koltunski
      bundle.putString("argument", message );
363 373fa45f Leszek Koltunski
      dialog.setArguments(bundle);
364
      dialog.show( act.getSupportFragmentManager(), null);
365
      }
366 f0336037 Leszek Koltunski
    }
367 7289fd6c Leszek Koltunski
  }