Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ 030f1bf4

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.screens;
11

    
12
import java.lang.ref.WeakReference;
13

    
14
import android.content.SharedPreferences;
15
import android.graphics.Bitmap;
16
import android.graphics.Canvas;
17
import android.graphics.Paint;
18
import android.graphics.PorterDuff;
19
import android.graphics.drawable.Drawable;
20
import android.os.Bundle;
21
import androidx.core.content.ContextCompat;
22
import android.view.View;
23
import android.widget.ImageButton;
24
import android.widget.LinearLayout;
25

    
26
import org.distorted.objectlib.main.ObjectControl;
27
import org.distorted.objectlib.main.ObjectSignatures;
28
import org.distorted.objectlib.main.TwistyObject;
29

    
30
import org.distorted.dialogs.RubikDialogSolverError;
31
import org.distorted.helpers.TransparentImageButton;
32
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34
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
import org.distorted.objects.RubikObjectList;
40
import org.distorted.solvers.ImplementedSolversList;
41
import org.distorted.solvers.SolverMain;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikScreenSolver extends RubikScreenAbstract
46
  {
47
  private static Bitmap[] mBitmap;
48
  private ImageButton[] mColorButton;
49
  private TransparentImageButton mBackButton, mSolveButton;
50
  private boolean mSolving;
51
  private int mCurrentColor;
52
  private int[] mFaceColors;
53
  private int mNumColors;
54
  private float mBitmapSize;
55
  private WeakReference<RubikActivity> mWeakAct;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  void leaveScreen(RubikActivity act)
60
    {
61
    ObjectControl control = act.getControl();
62
    control.unsetLock();
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  void enterScreen(final RubikActivity act)
68
    {
69
    ObjectControl control = act.getControl();
70
    control.setLock(false);
71

    
72
    float width = act.getScreenWidthInPixels();
73
    float heigh = act.getScreenHeightInPixels();
74

    
75
    mWeakAct = new WeakReference<>(act);
76
    mSolving = false;
77

    
78
    int solverIndex= act.getSolverIndex();
79
    ImplementedSolversList currentSolver = ImplementedSolversList.getSolver(solverIndex);
80
    int currentObject = currentSolver.getObject();
81
    act.changeIfDifferent(currentObject,control);
82
    control.solveOnly();
83
    RubikObjectList.setCurrObject(currentObject);
84

    
85
    generateFaceColors(currentObject);
86

    
87
    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
    // TOP ////////////////////////////
93
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
94
    layoutTop.removeAllViews();
95

    
96
    if( mNumColors>0 )
97
      {
98
      setupBitmaps();
99
      setupColorButtons(act,width);
100
      markButton(act);
101
      }
102

    
103
    for(ImageButton button: mColorButton) layoutTop.addView(button);
104

    
105
    // BOT ////////////////////////////
106
    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
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
116
    layoutBot.removeAllViews();
117

    
118
    setupSolveButton(act);
119
    setupBackButton(act);
120

    
121
    layoutL.addView(mSolveButton);
122
    layoutR.addView(mBackButton);
123

    
124
    layoutBot.addView(layoutL);
125
    layoutBot.addView(layoutM);
126
    layoutBot.addView(layoutR);
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// 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

    
133
  public void generateFaceColors(int object)
134
    {
135
    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
      mNumColors  = ShapeTetrahedron.NUM_FACES;
145
      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
      mNumColors  = ShapeOctahedron.NUM_FACES;
155
      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
      mNumColors  = ShapeDodecahedron.NUM_FACES;
166
      mFaceColors = ShapeDodecahedron.FACE_COLORS;
167
      }
168
    else if( object== ObjectSignatures.BALL_4 )
169
      {
170
      mNumColors  = ShapeDiamond.NUM_FACES;
171
      mFaceColors = ShapeDiamond.FACE_COLORS;
172
      }
173
    else
174
      {
175
      mNumColors  = ShapeHexahedron.NUM_FACES;
176
      mFaceColors = ShapeHexahedron.FACE_COLORS;
177
      }
178
    }
179

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

    
182
  private void setupBitmaps()
183
    {
184
    final int SIZE = (int)mBitmapSize;
185
    final float R = SIZE*0.15f;
186
    final float M = SIZE*0.08f;
187

    
188
    mBitmap = new Bitmap[mNumColors];
189

    
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
    for(int i=0; i<mNumColors; i++)
199
      {
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
      paint.setColor(mFaceColors[i]);
207
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  private void setupColorButtons(final RubikActivity act, final float width)
214
    {
215
    mColorButton = new ImageButton[mNumColors];
216
    int padding = (int)(width*RubikActivity.PADDING);
217
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
218

    
219
    for(int i=0; i<mNumColors; i++)
220
      {
221
      final int ii = i;
222
      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
      mColorButton[i] = new ImageButton(act);
229
      mColorButton[i].setLayoutParams(params);
230
      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
          mCurrentColor = ii;
239
          markButton(act);
240
          }
241
        });
242
      }
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  private void setupSolveButton(final RubikActivity act)
248
    {
249
    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
    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

    
253
    mSolveButton.setOnClickListener( new View.OnClickListener()
254
      {
255
      @Override
256
      public void onClick(View v)
257
        {
258
        if( !mSolving )
259
          {
260
          mSolving = true;
261
          TwistyObject object = act.getObject();
262
          SolverMain solver = new SolverMain( act.getResources(), object );
263
          solver.start();
264
          }
265
        }
266
      });
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  private void setupBackButton(final RubikActivity act)
272
    {
273
    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
    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

    
277
    mBackButton.setOnClickListener( new View.OnClickListener()
278
      {
279
      @Override
280
      public void onClick(View v)
281
        {
282
        ObjectControl control = act.getControl();
283
        control.resetAllTextureMaps();
284
        ScreenList.goBack(act);
285
        }
286
      });
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  private void markButton(RubikActivity act)
292
    {
293
    for(int b=0; b<mNumColors; b++)
294
      {
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
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public void savePreferences(SharedPreferences.Editor editor)
311
    {
312
    editor.putInt("stateSolver_color", mCurrentColor);
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public void restorePreferences(SharedPreferences preferences)
318
    {
319
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  public int getCurrentColor()
325
    {
326
    return mCurrentColor;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  public void setSolved(final String moves)
332
    {
333
    mSolving = false;
334
    final RubikActivity act = mWeakAct.get();
335

    
336
    if( act!=null )
337
      {
338
      act.runOnUiThread(new Runnable()
339
        {
340
        @Override
341
        public void run()
342
          {
343
          ScreenList.switchScreen(act, ScreenList.SOLU);
344
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
345
          solution.setupMoves(act, moves);
346
          }
347
        });
348
      }
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public void setSolved(final int[][] moves)
354
    {
355
    mSolving = false;
356
    final RubikActivity act = mWeakAct.get();
357

    
358
    if( act!=null )
359
      {
360
      act.runOnUiThread(new Runnable()
361
        {
362
        @Override
363
        public void run()
364
          {
365
          ScreenList.switchScreen(act, ScreenList.SOLU);
366
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
367
          solution.setupMoves(act, moves);
368
          }
369
        });
370
      }
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  public void displayErrorDialog(String message)
376
    {
377
    mSolving = false;
378
    RubikActivity act = mWeakAct.get();
379

    
380
    if( act!=null )
381
      {
382
      RubikDialogSolverError dialog = new RubikDialogSolverError();
383
      Bundle bundle = new Bundle();
384
      bundle.putString("argument", message );
385
      dialog.setArguments(bundle);
386
      dialog.show( act.getSupportFragmentManager(), null);
387
      }
388
    }
389
  }
(8-8/10)