Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ 7a60305a

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 final int RESET_DURATION = 1000;
48

    
49
  private static Bitmap[] mBitmap;
50
  private ImageButton[] mColorButton;
51
  private TransparentImageButton mResetButton,mBackButton, mSolveButton;
52
  private boolean mSolving;
53
  private int mCurrentColor;
54
  private int[] mFaceColors;
55
  private int mNumColors;
56
  private float mBitmapSize;
57
  private WeakReference<RubikActivity> mWeakAct;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
74
    float width = act.getScreenWidthInPixels();
75
    float heigh = act.getScreenHeightInPixels();
76

    
77
    mWeakAct = new WeakReference<>(act);
78
    mSolving = false;
79

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

    
87
    generateFaceColors(currentObject);
88

    
89
    final float BUTTON_RATIO = 0.75f;
90
    int sizeV = (int)(heigh*RubikActivity.RATIO_BAR*BUTTON_RATIO);
91
    int sizeH = (int)((width/mNumColors)*BUTTON_RATIO);
92
    mBitmapSize = Math.min(sizeV,sizeH);
93

    
94
    // TOP ////////////////////////////
95
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
96
    layoutTop.removeAllViews();
97

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

    
105
    for(ImageButton button: mColorButton) layoutTop.addView(button);
106

    
107
    // BOT ////////////////////////////
108
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
109

    
110
    LinearLayout layoutL = new LinearLayout(act);
111
    layoutL.setLayoutParams(params);
112
    LinearLayout layoutM = new LinearLayout(act);
113
    layoutM.setLayoutParams(params);
114
    LinearLayout layoutR = new LinearLayout(act);
115
    layoutR.setLayoutParams(params);
116

    
117
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
118
    layoutBot.removeAllViews();
119

    
120
    setupResetButton(act);
121
    setupSolveButton(act);
122
    setupBackButton(act);
123

    
124
    layoutL.addView(mResetButton);
125
    layoutM.addView(mSolveButton);
126
    layoutR.addView(mBackButton);
127

    
128
    layoutBot.addView(layoutL);
129
    layoutBot.addView(layoutM);
130
    layoutBot.addView(layoutR);
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// This doesn't quite work in many cases, but in case of the solvers that will pop up in foreseeable
135
// future it should be ok.
136

    
137
  public void generateFaceColors(int object)
138
    {
139
    if( object== ObjectSignatures.PYRA_3 ||
140
        object== ObjectSignatures.PYRA_4 ||
141
        object== ObjectSignatures.PYRA_5 ||
142
        object== ObjectSignatures.PDUO_2 ||
143
        object== ObjectSignatures.JING_2 ||
144
        object== ObjectSignatures.MORP_2 ||
145
        object== ObjectSignatures.MORP_3 ||
146
        object== ObjectSignatures.MORP_4  )
147
      {
148
      mNumColors  = ShapeTetrahedron.NUM_FACES;
149
      mFaceColors = ShapeTetrahedron.FACE_COLORS;
150
      }
151
    else if( object== ObjectSignatures.DIAM_2 ||
152
        object== ObjectSignatures.DIAM_3 ||
153
        object== ObjectSignatures.DIAM_4 ||
154
        object== ObjectSignatures.TRAJ_3 ||
155
        object== ObjectSignatures.TRAJ_4 ||
156
        object== ObjectSignatures.PDIA_3  )
157
      {
158
      mNumColors  = ShapeOctahedron.NUM_FACES;
159
      mFaceColors = ShapeOctahedron.FACE_COLORS;
160
      }
161
    else if( object== ObjectSignatures.CRYS_3 ||
162
        object== ObjectSignatures.STAR_3 ||
163
        object== ObjectSignatures.PENT_2 ||
164
        object== ObjectSignatures.KILO_3 ||
165
        object== ObjectSignatures.KILO_5 ||
166
        object== ObjectSignatures.MEGA_3 ||
167
        object== ObjectSignatures.MEGA_5  )
168
      {
169
      mNumColors  = ShapeDodecahedron.NUM_FACES;
170
      mFaceColors = ShapeDodecahedron.FACE_COLORS;
171
      }
172
    else if( object== ObjectSignatures.BALL_4 )
173
      {
174
      mNumColors  = ShapeDiamond.NUM_FACES;
175
      mFaceColors = ShapeDiamond.FACE_COLORS;
176
      }
177
    else
178
      {
179
      mNumColors  = ShapeHexahedron.NUM_FACES;
180
      mFaceColors = ShapeHexahedron.FACE_COLORS;
181
      }
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private void setupBitmaps()
187
    {
188
    final int SIZE = (int)mBitmapSize;
189
    final float R = SIZE*0.15f;
190
    final float M = SIZE*0.08f;
191

    
192
    mBitmap = new Bitmap[mNumColors];
193

    
194
    Paint paint = new Paint();
195
    paint.setColor(0xff008800);
196
    paint.setStyle(Paint.Style.FILL);
197

    
198
    paint.setAntiAlias(true);
199
    paint.setTextAlign(Paint.Align.CENTER);
200
    paint.setStyle(Paint.Style.FILL);
201

    
202
    for(int i=0; i<mNumColors; i++)
203
      {
204
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
205
      Canvas canvas = new Canvas(mBitmap[i]);
206

    
207
      paint.setColor(0xff000000);
208
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
209

    
210
      paint.setColor(mFaceColors[i]);
211
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  private void setupColorButtons(final RubikActivity act, final float width)
218
    {
219
    mColorButton = new ImageButton[mNumColors];
220
    int padding = (int)(width*RubikActivity.PADDING);
221
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
222

    
223
    for(int i=0; i<mNumColors; i++)
224
      {
225
      final int ii = i;
226
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
227
      params.topMargin    = margin;
228
      params.bottomMargin = margin;
229
      params.leftMargin   = margin;
230
      params.rightMargin  = margin;
231

    
232
      mColorButton[i] = new ImageButton(act);
233
      mColorButton[i].setLayoutParams(params);
234
      mColorButton[i].setPadding(padding,0,padding,0);
235
      mColorButton[i].setImageBitmap(mBitmap[i]);
236

    
237
      mColorButton[i].setOnClickListener( new View.OnClickListener()
238
        {
239
        @Override
240
        public void onClick(View view)
241
          {
242
          mCurrentColor = ii;
243
          markButton(act);
244
          }
245
        });
246
      }
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  private void setupResetButton(final RubikActivity act)
252
    {
253
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_reset,R.drawable.ui_medium_reset, R.drawable.ui_big_reset, R.drawable.ui_huge_reset);
254
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
255
    mResetButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
256

    
257
    mResetButton.setOnClickListener( new View.OnClickListener()
258
      {
259
      @Override
260
      public void onClick(View v)
261
        {
262
        ObjectControl control = act.getControl();
263
        control.resetTextureMapsEffect(RESET_DURATION);
264
        }
265
      });
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private void setupSolveButton(final RubikActivity act)
271
    {
272
    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);
273
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
274
    mSolveButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
275

    
276
    mSolveButton.setOnClickListener( new View.OnClickListener()
277
      {
278
      @Override
279
      public void onClick(View v)
280
        {
281
        if( !mSolving )
282
          {
283
          mSolving = true;
284
          TwistyObject object = act.getObject();
285
          SolverMain solver = new SolverMain( act.getResources(), object );
286
          solver.start();
287
          }
288
        }
289
      });
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  private void setupBackButton(final RubikActivity act)
295
    {
296
    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);
297
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
298
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
299

    
300
    mBackButton.setOnClickListener( new View.OnClickListener()
301
      {
302
      @Override
303
      public void onClick(View v)
304
        {
305
        ObjectControl control = act.getControl();
306
        control.resetAllTextureMaps();
307
        ScreenList.goBack(act);
308
        }
309
      });
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  private void markButton(RubikActivity act)
315
    {
316
    for(int b=0; b<mNumColors; b++)
317
      {
318
      Drawable d = mColorButton[b].getBackground();
319

    
320
      if( b==mCurrentColor )
321
        {
322
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
323
        }
324
      else
325
        {
326
        d.clearColorFilter();
327
        }
328
      }
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  public void savePreferences(SharedPreferences.Editor editor)
334
    {
335
    editor.putInt("stateSolver_color", mCurrentColor);
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  public void restorePreferences(SharedPreferences preferences)
341
    {
342
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  public int getCurrentColor()
348
    {
349
    return mCurrentColor;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  public void setSolved(final String moves)
355
    {
356
    mSolving = false;
357
    final RubikActivity act = mWeakAct.get();
358

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

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public void setSolved(final int[][] moves)
377
    {
378
    mSolving = false;
379
    final RubikActivity act = mWeakAct.get();
380

    
381
    if( act!=null )
382
      {
383
      act.runOnUiThread(new Runnable()
384
        {
385
        @Override
386
        public void run()
387
          {
388
          ScreenList.switchScreen(act, ScreenList.SOLU);
389
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
390
          solution.setupMoves(act, moves);
391
          }
392
        });
393
      }
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public void displayErrorDialog(String message)
399
    {
400
    mSolving = false;
401
    RubikActivity act = mWeakAct.get();
402

    
403
    if( act!=null )
404
      {
405
      RubikDialogSolverError dialog = new RubikDialogSolverError();
406
      Bundle bundle = new Bundle();
407
      bundle.putString("argument", message );
408
      dialog.setArguments(bundle);
409
      dialog.show( act.getSupportFragmentManager(), null);
410
      }
411
    }
412
  }
(8-8/10)