Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 9c2f0c91

1 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.states;
21 7289fd6c Leszek Koltunski
22
import android.content.SharedPreferences;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26 473611ee Leszek Koltunski
import android.graphics.PorterDuff;
27
import android.graphics.drawable.Drawable;
28 ca292407 Leszek Koltunski
import android.os.Bundle;
29 66e777b0 Leszek Koltunski
import androidx.core.content.ContextCompat;
30 7289fd6c Leszek Koltunski
import android.view.View;
31
import android.widget.ImageButton;
32
import android.widget.LinearLayout;
33
34 ca292407 Leszek Koltunski
import org.distorted.dialogs.RubikDialogSolverError;
35 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
36
import org.distorted.main.RubikActivity;
37 5a4d4fba Leszek Koltunski
import org.distorted.main.RubikPreRender;
38 9c2f0c91 Leszek Koltunski
import org.distorted.objects.TwistyObject;
39
import org.distorted.objects.ObjectList;
40 373fa45f Leszek Koltunski
import org.distorted.solvers.ImplementedSolversList;
41 a304ee64 Leszek Koltunski
import org.distorted.solvers.SolverMain;
42 7289fd6c Leszek Koltunski
43 373fa45f Leszek Koltunski
import java.lang.ref.WeakReference;
44
45 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
public class RubikStateSolver extends RubikStateAbstract
48
  {
49
  private static Bitmap[] mBitmap;
50
  private ImageButton[] mColorButton;
51 23ac2462 Leszek Koltunski
  private ImageButton mBackButton, mSolveButton;
52 5660465b Leszek Koltunski
  private boolean mSolving;
53 1f9772f3 Leszek Koltunski
  private int mCurrentColor;
54 41748f19 Leszek Koltunski
  private int[] mFaceColors;
55
  private int mNumFaces;
56 7ea57482 Leszek Koltunski
  private float mBitmapSize;
57 5660465b Leszek Koltunski
58 9c2f0c91 Leszek Koltunski
  private ObjectList mCurrentObject;
59 373fa45f Leszek Koltunski
  private int mCurrentObjectSize;
60
61
  private WeakReference<RubikActivity> mWeakAct;
62
63 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  void leaveState(RubikActivity act)
66
    {
67 7f84a768 Leszek Koltunski
68 7289fd6c Leszek Koltunski
    }
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72
  void enterState(final RubikActivity act)
73
    {
74 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
75 6d4d56cb Leszek Koltunski
    float heigh = act.getScreenHeightInPixels();
76 7ea57482 Leszek Koltunski
77
    int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
78
    int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
79
80
    mBitmapSize = Math.min(sizeV,sizeH);
81 e3c74c0f Leszek Koltunski
82 373fa45f Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
83
84 5660465b Leszek Koltunski
    mSolving = false;
85
86 373fa45f Leszek Koltunski
    mCurrentObject     = ImplementedSolversList.getObject(0);
87
    mCurrentObjectSize = ImplementedSolversList.getObjectSize(0);
88
89 2e8ec627 Leszek Koltunski
    act.setupObject(mCurrentObject, mCurrentObjectSize, null);
90 473611ee Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
91 7b7d65ce Leszek Koltunski
    play.setObjectAndSize(act, mCurrentObject, mCurrentObjectSize);
92 7289fd6c Leszek Koltunski
93 9c2f0c91 Leszek Koltunski
    mFaceColors = ObjectList.retFaceColors(mCurrentObject);
94 41748f19 Leszek Koltunski
    mNumFaces   = mFaceColors!=null ? mFaceColors.length : 0;
95 20931cf6 Leszek Koltunski
96 7289fd6c Leszek Koltunski
    // TOP ////////////////////////////
97
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
98
    layoutTop.removeAllViews();
99
100 af133d41 Leszek Koltunski
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
101
102
    LinearLayout layoutLeft = new LinearLayout(act);
103
    layoutLeft.setLayoutParams(paramsL);
104
    LinearLayout layoutMid = new LinearLayout(act);
105
    layoutMid.setLayoutParams(paramsL);
106
    LinearLayout layoutRight = new LinearLayout(act);
107
    layoutRight.setLayoutParams(paramsL);
108
109 41748f19 Leszek Koltunski
    if( mNumFaces>0 )
110
      {
111 7ebd72f7 Leszek Koltunski
      setupBitmaps();
112 ad0c8e0e Leszek Koltunski
      setupColorButtons(act,width);
113 41748f19 Leszek Koltunski
      markButton(act);
114
      }
115 473611ee Leszek Koltunski
116 7289fd6c Leszek Koltunski
    for(ImageButton button: mColorButton) layoutTop.addView(button);
117
118
    // BOT ////////////////////////////
119 92843d3b Leszek Koltunski
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
120
    layoutBot.removeAllViews();
121 264af0ad Leszek Koltunski
122 92843d3b Leszek Koltunski
    setupSolveButton(act,width);
123 ad0c8e0e Leszek Koltunski
    setupBackButton(act,width);
124 7289fd6c Leszek Koltunski
125 af133d41 Leszek Koltunski
    layoutLeft.addView(mSolveButton);
126
    layoutRight.addView(mBackButton);
127
128
    layoutBot.addView(layoutLeft);
129
    layoutBot.addView(layoutMid);
130
    layoutBot.addView(layoutRight);
131 7289fd6c Leszek Koltunski
    }
132
133 5660465b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134 7289fd6c Leszek Koltunski
135 7ebd72f7 Leszek Koltunski
  private void setupBitmaps()
136 7289fd6c Leszek Koltunski
    {
137 1bd07548 Leszek Koltunski
    final int SIZE = (int)mBitmapSize;
138
    final float R = SIZE*0.15f;
139
    final float M = SIZE*0.08f;
140 7289fd6c Leszek Koltunski
141 41748f19 Leszek Koltunski
    mBitmap = new Bitmap[mNumFaces];
142 7289fd6c Leszek Koltunski
143
    Paint paint = new Paint();
144
    paint.setColor(0xff008800);
145
    paint.setStyle(Paint.Style.FILL);
146
147
    paint.setAntiAlias(true);
148
    paint.setTextAlign(Paint.Align.CENTER);
149
    paint.setStyle(Paint.Style.FILL);
150
151 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
152 7289fd6c Leszek Koltunski
      {
153
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
154
      Canvas canvas = new Canvas(mBitmap[i]);
155
156
      paint.setColor(0xff000000);
157
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
158
159 41748f19 Leszek Koltunski
      paint.setColor(mFaceColors[i]);
160 7289fd6c Leszek Koltunski
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
161
      }
162
    }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166 ad0c8e0e Leszek Koltunski
  private void setupColorButtons(final RubikActivity act, final float width)
167 7289fd6c Leszek Koltunski
    {
168 41748f19 Leszek Koltunski
    mColorButton = new ImageButton[mNumFaces];
169 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
170
    int margin  = (int)(width*RubikActivity.MARGIN);
171 7289fd6c Leszek Koltunski
172 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
173 7289fd6c Leszek Koltunski
      {
174
      final int ii = i;
175 ad0c8e0e Leszek Koltunski
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
176
      params.topMargin    = margin;
177
      params.bottomMargin = margin;
178
      params.leftMargin   = margin;
179
      params.rightMargin  = margin;
180
181 7289fd6c Leszek Koltunski
      mColorButton[i] = new ImageButton(act);
182 ad0c8e0e Leszek Koltunski
      mColorButton[i].setLayoutParams(params);
183 7289fd6c Leszek Koltunski
      mColorButton[i].setPadding(padding,0,padding,0);
184
      mColorButton[i].setImageBitmap(mBitmap[i]);
185
186
      mColorButton[i].setOnClickListener( new View.OnClickListener()
187
        {
188
        @Override
189
        public void onClick(View view)
190
          {
191 473611ee Leszek Koltunski
          mCurrentColor = ii;
192
          markButton(act);
193 7289fd6c Leszek Koltunski
          }
194
        });
195
      }
196
    }
197
198 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200 ad0c8e0e Leszek Koltunski
  private void setupSolveButton(final RubikActivity act, final float width)
201 264af0ad Leszek Koltunski
    {
202 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
203
    int margin   = (int)(width*RubikActivity.MARGIN);
204 92843d3b Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
205 ad0c8e0e Leszek Koltunski
    params.topMargin    = margin;
206
    params.bottomMargin = margin;
207
    params.leftMargin   = margin;
208
    params.rightMargin  = margin;
209
210 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);
211
212
    mSolveButton = new ImageButton(act);
213 ad0c8e0e Leszek Koltunski
    mSolveButton.setLayoutParams(params);
214 264af0ad Leszek Koltunski
    mSolveButton.setPadding(padding,0,padding,0);
215 23ac2462 Leszek Koltunski
    mSolveButton.setImageResource(icon);
216 264af0ad Leszek Koltunski
217
    mSolveButton.setOnClickListener( new View.OnClickListener()
218
      {
219
      @Override
220
      public void onClick(View v)
221
        {
222 5660465b Leszek Koltunski
        if( !mSolving )
223
          {
224
          mSolving = true;
225 9c2f0c91 Leszek Koltunski
          TwistyObject object = act.getObject();
226 f0336037 Leszek Koltunski
          String objectString = object.retObjectString();
227 a304ee64 Leszek Koltunski
          SolverMain solver = new SolverMain( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
228 f0336037 Leszek Koltunski
          solver.start();
229 5660465b Leszek Koltunski
          }
230 264af0ad Leszek Koltunski
        }
231
      });
232
    }
233
234 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236 ad0c8e0e Leszek Koltunski
  private void setupBackButton(final RubikActivity act, final float width)
237 7289fd6c Leszek Koltunski
    {
238 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
239
    int margin  = (int)(width*RubikActivity.MARGIN);
240 92843d3b Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
241 ad0c8e0e Leszek Koltunski
    params.topMargin    = margin;
242
    params.bottomMargin = margin;
243
    params.leftMargin   = margin;
244
    params.rightMargin  = margin;
245
246 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);
247
248
    mBackButton = new ImageButton(act);
249 ad0c8e0e Leszek Koltunski
    mBackButton.setLayoutParams(params);
250 7289fd6c Leszek Koltunski
    mBackButton.setPadding(padding,0,padding,0);
251 4fb1fc0d Leszek Koltunski
    mBackButton.setImageResource(icon);
252 7289fd6c Leszek Koltunski
253
    mBackButton.setOnClickListener( new View.OnClickListener()
254
      {
255
      @Override
256
      public void onClick(View v)
257
        {
258 5a4d4fba Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
259
        pre.resetAllTextureMaps();
260 7289fd6c Leszek Koltunski
        RubikState.goBack(act);
261
        }
262
      });
263
    }
264
265 473611ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
  private void markButton(RubikActivity act)
268
    {
269 41748f19 Leszek Koltunski
    for(int b=0; b<mNumFaces; b++)
270 473611ee Leszek Koltunski
      {
271
      Drawable d = mColorButton[b].getBackground();
272
273
      if( b==mCurrentColor )
274
        {
275
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
276
        }
277
      else
278
        {
279
        d.clearColorFilter();
280
        }
281
      }
282
    }
283
284 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286
  public void savePreferences(SharedPreferences.Editor editor)
287
    {
288 1f9772f3 Leszek Koltunski
    editor.putInt("stateSolver_color", mCurrentColor);
289 7289fd6c Leszek Koltunski
    }
290
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293
  public void restorePreferences(SharedPreferences preferences)
294
    {
295 1f9772f3 Leszek Koltunski
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
296 7289fd6c Leszek Koltunski
    }
297 473611ee Leszek Koltunski
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300
  public int getCurrentColor()
301
    {
302
    return mCurrentColor;
303
    }
304 f0336037 Leszek Koltunski
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307 373fa45f Leszek Koltunski
  public void setSolved(final String moves)
308 f0336037 Leszek Koltunski
    {
309
    mSolving = false;
310 373fa45f Leszek Koltunski
    final RubikActivity act = mWeakAct.get();
311 f0336037 Leszek Koltunski
312 373fa45f Leszek Koltunski
    if( act!=null )
313 f0336037 Leszek Koltunski
      {
314 373fa45f Leszek Koltunski
      act.runOnUiThread(new Runnable()
315 f0336037 Leszek Koltunski
        {
316 373fa45f Leszek Koltunski
        @Override
317
        public void run()
318
          {
319
          RubikState.switchState(act,RubikState.SOLU);
320
          RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
321
          solution.setupMoves(act, moves);
322
          }
323
        });
324
      }
325 f0336037 Leszek Koltunski
    }
326
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329 373fa45f Leszek Koltunski
  public void displayErrorDialog( String message)
330 f0336037 Leszek Koltunski
    {
331
    mSolving = false;
332 373fa45f Leszek Koltunski
    RubikActivity act = mWeakAct.get();
333 f0336037 Leszek Koltunski
334 373fa45f Leszek Koltunski
    if( act!=null )
335
      {
336
      RubikDialogSolverError dialog = new RubikDialogSolverError();
337
      Bundle bundle = new Bundle();
338
      bundle.putString("error", message );
339
      dialog.setArguments(bundle);
340
      dialog.show( act.getSupportFragmentManager(), null);
341
      }
342 f0336037 Leszek Koltunski
    }
343 7289fd6c Leszek Koltunski
  }