Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ b600ccd9

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 fcd5b990 Leszek Koltunski
package org.distorted.screens;
21 7289fd6c Leszek Koltunski
22 3f7a4363 Leszek Koltunski
import java.lang.ref.WeakReference;
23
24 7289fd6c Leszek Koltunski
import android.content.SharedPreferences;
25
import android.graphics.Bitmap;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28 473611ee Leszek Koltunski
import android.graphics.PorterDuff;
29
import android.graphics.drawable.Drawable;
30 ca292407 Leszek Koltunski
import android.os.Bundle;
31 66e777b0 Leszek Koltunski
import androidx.core.content.ContextCompat;
32 7289fd6c Leszek Koltunski
import android.view.View;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35
36 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
37 3f7a4363 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
38 318c0a7d Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
39 3f7a4363 Leszek Koltunski
40 ca292407 Leszek Koltunski
import org.distorted.dialogs.RubikDialogSolverError;
41 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
42 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
43
import org.distorted.main.RubikActivity;
44 373fa45f Leszek Koltunski
import org.distorted.solvers.ImplementedSolversList;
45 a304ee64 Leszek Koltunski
import org.distorted.solvers.SolverMain;
46 7289fd6c Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 fcd5b990 Leszek Koltunski
public class RubikScreenSolver extends RubikScreenAbstract
50 7289fd6c Leszek Koltunski
  {
51
  private static Bitmap[] mBitmap;
52
  private ImageButton[] mColorButton;
53 dd874ae8 Leszek Koltunski
  private TransparentImageButton mBackButton, mSolveButton;
54 5660465b Leszek Koltunski
  private boolean mSolving;
55 1f9772f3 Leszek Koltunski
  private int mCurrentColor;
56 41748f19 Leszek Koltunski
  private int[] mFaceColors;
57
  private int mNumFaces;
58 7ea57482 Leszek Koltunski
  private float mBitmapSize;
59 373fa45f Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
60
61 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
64 7289fd6c Leszek Koltunski
    {
65 9523ae28 Leszek Koltunski
    ObjectControl control = act.getControl();
66
    control.unsetLock();
67 7289fd6c Leszek Koltunski
    }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
72 7289fd6c Leszek Koltunski
    {
73 9523ae28 Leszek Koltunski
    ObjectControl control = act.getControl();
74
    control.setLock(false);
75
76 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
77 6d4d56cb Leszek Koltunski
    float heigh = act.getScreenHeightInPixels();
78 7ea57482 Leszek Koltunski
79
    int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
80
    int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
81
82
    mBitmapSize = Math.min(sizeV,sizeH);
83 373fa45f Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
84 5660465b Leszek Koltunski
    mSolving = false;
85
86 318c0a7d Leszek Koltunski
    ObjectType currentObject= ImplementedSolversList.getObject(0);
87 b20e89d2 Leszek Koltunski
    act.changeIfDifferent(currentObject,control);
88 33f6abfd Leszek Koltunski
    control.solveOnly();
89
90 f5da732a Leszek Koltunski
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
91 7ac0ee88 Leszek Koltunski
    play.setObject(act, currentObject);
92 7289fd6c Leszek Koltunski
93 4ab3d91b Leszek Koltunski
    generateFaceColors();
94 20931cf6 Leszek Koltunski
95 7289fd6c Leszek Koltunski
    // TOP ////////////////////////////
96
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
97
    layoutTop.removeAllViews();
98
99 af133d41 Leszek Koltunski
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
100
101
    LinearLayout layoutLeft = new LinearLayout(act);
102
    layoutLeft.setLayoutParams(paramsL);
103
    LinearLayout layoutMid = new LinearLayout(act);
104
    layoutMid.setLayoutParams(paramsL);
105
    LinearLayout layoutRight = new LinearLayout(act);
106
    layoutRight.setLayoutParams(paramsL);
107
108 41748f19 Leszek Koltunski
    if( mNumFaces>0 )
109
      {
110 7ebd72f7 Leszek Koltunski
      setupBitmaps();
111 ad0c8e0e Leszek Koltunski
      setupColorButtons(act,width);
112 41748f19 Leszek Koltunski
      markButton(act);
113
      }
114 473611ee Leszek Koltunski
115 7289fd6c Leszek Koltunski
    for(ImageButton button: mColorButton) layoutTop.addView(button);
116
117
    // BOT ////////////////////////////
118 92843d3b Leszek Koltunski
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
119
    layoutBot.removeAllViews();
120 264af0ad Leszek Koltunski
121 dd874ae8 Leszek Koltunski
    setupSolveButton(act);
122
    setupBackButton(act);
123 7289fd6c Leszek Koltunski
124 af133d41 Leszek Koltunski
    layoutLeft.addView(mSolveButton);
125
    layoutRight.addView(mBackButton);
126
127
    layoutBot.addView(layoutLeft);
128
    layoutBot.addView(layoutMid);
129
    layoutBot.addView(layoutRight);
130 7289fd6c Leszek Koltunski
    }
131
132 4ab3d91b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134
  public void generateFaceColors()
135
    {
136
    mFaceColors= new int[] {  TwistyObject.COLOR_YELLOW,
137
                              TwistyObject.COLOR_WHITE,
138
                              TwistyObject.COLOR_BLUE ,
139
                              TwistyObject.COLOR_GREEN,
140
                              TwistyObject.COLOR_RED   ,
141
                              TwistyObject.COLOR_ORANGE };
142
    mNumFaces = 6;
143
    }
144
145 5660465b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146 7289fd6c Leszek Koltunski
147 7ebd72f7 Leszek Koltunski
  private void setupBitmaps()
148 7289fd6c Leszek Koltunski
    {
149 1bd07548 Leszek Koltunski
    final int SIZE = (int)mBitmapSize;
150
    final float R = SIZE*0.15f;
151
    final float M = SIZE*0.08f;
152 7289fd6c Leszek Koltunski
153 41748f19 Leszek Koltunski
    mBitmap = new Bitmap[mNumFaces];
154 7289fd6c Leszek Koltunski
155
    Paint paint = new Paint();
156
    paint.setColor(0xff008800);
157
    paint.setStyle(Paint.Style.FILL);
158
159
    paint.setAntiAlias(true);
160
    paint.setTextAlign(Paint.Align.CENTER);
161
    paint.setStyle(Paint.Style.FILL);
162
163 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
164 7289fd6c Leszek Koltunski
      {
165
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
166
      Canvas canvas = new Canvas(mBitmap[i]);
167
168
      paint.setColor(0xff000000);
169
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
170
171 41748f19 Leszek Koltunski
      paint.setColor(mFaceColors[i]);
172 7289fd6c Leszek Koltunski
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
173
      }
174
    }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178 ad0c8e0e Leszek Koltunski
  private void setupColorButtons(final RubikActivity act, final float width)
179 7289fd6c Leszek Koltunski
    {
180 41748f19 Leszek Koltunski
    mColorButton = new ImageButton[mNumFaces];
181 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
182
    int margin  = (int)(width*RubikActivity.MARGIN);
183 7289fd6c Leszek Koltunski
184 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
185 7289fd6c Leszek Koltunski
      {
186
      final int ii = i;
187 ad0c8e0e Leszek Koltunski
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
188
      params.topMargin    = margin;
189
      params.bottomMargin = margin;
190
      params.leftMargin   = margin;
191
      params.rightMargin  = margin;
192
193 7289fd6c Leszek Koltunski
      mColorButton[i] = new ImageButton(act);
194 ad0c8e0e Leszek Koltunski
      mColorButton[i].setLayoutParams(params);
195 7289fd6c Leszek Koltunski
      mColorButton[i].setPadding(padding,0,padding,0);
196
      mColorButton[i].setImageBitmap(mBitmap[i]);
197
198
      mColorButton[i].setOnClickListener( new View.OnClickListener()
199
        {
200
        @Override
201
        public void onClick(View view)
202
          {
203 473611ee Leszek Koltunski
          mCurrentColor = ii;
204
          markButton(act);
205 7289fd6c Leszek Koltunski
          }
206
        });
207
      }
208
    }
209
210 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 dd874ae8 Leszek Koltunski
  private void setupSolveButton(final RubikActivity act)
213 264af0ad Leszek Koltunski
    {
214 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);
215 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
216
    mSolveButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
217 264af0ad Leszek Koltunski
218
    mSolveButton.setOnClickListener( new View.OnClickListener()
219
      {
220
      @Override
221
      public void onClick(View v)
222
        {
223 5660465b Leszek Koltunski
        if( !mSolving )
224
          {
225
          mSolving = true;
226 9c2f0c91 Leszek Koltunski
          TwistyObject object = act.getObject();
227 ecf3f149 Leszek Koltunski
          SolverMain solver = new SolverMain( act.getResources(), object );
228 f0336037 Leszek Koltunski
          solver.start();
229 5660465b Leszek Koltunski
          }
230 264af0ad Leszek Koltunski
        }
231
      });
232
    }
233
234 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236 dd874ae8 Leszek Koltunski
  private void setupBackButton(final RubikActivity act)
237 7289fd6c Leszek Koltunski
    {
238 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);
239 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
240
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
241 7289fd6c Leszek Koltunski
242
    mBackButton.setOnClickListener( new View.OnClickListener()
243
      {
244
      @Override
245
      public void onClick(View v)
246
        {
247 2afc6754 Leszek Koltunski
        ObjectControl control = act.getControl();
248
        control.resetAllTextureMaps();
249 fcd5b990 Leszek Koltunski
        ScreenList.goBack(act);
250 7289fd6c Leszek Koltunski
        }
251
      });
252
    }
253
254 473611ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  private void markButton(RubikActivity act)
257
    {
258 41748f19 Leszek Koltunski
    for(int b=0; b<mNumFaces; b++)
259 473611ee Leszek Koltunski
      {
260
      Drawable d = mColorButton[b].getBackground();
261
262
      if( b==mCurrentColor )
263
        {
264
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
265
        }
266
      else
267
        {
268
        d.clearColorFilter();
269
        }
270
      }
271
    }
272
273 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
  public void savePreferences(SharedPreferences.Editor editor)
276
    {
277 1f9772f3 Leszek Koltunski
    editor.putInt("stateSolver_color", mCurrentColor);
278 7289fd6c Leszek Koltunski
    }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
  public void restorePreferences(SharedPreferences preferences)
283
    {
284 1f9772f3 Leszek Koltunski
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
285 7289fd6c Leszek Koltunski
    }
286 473611ee Leszek Koltunski
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289
  public int getCurrentColor()
290
    {
291
    return mCurrentColor;
292
    }
293 f0336037 Leszek Koltunski
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296 373fa45f Leszek Koltunski
  public void setSolved(final String moves)
297 f0336037 Leszek Koltunski
    {
298
    mSolving = false;
299 373fa45f Leszek Koltunski
    final RubikActivity act = mWeakAct.get();
300 f0336037 Leszek Koltunski
301 373fa45f Leszek Koltunski
    if( act!=null )
302 f0336037 Leszek Koltunski
      {
303 373fa45f Leszek Koltunski
      act.runOnUiThread(new Runnable()
304 f0336037 Leszek Koltunski
        {
305 373fa45f Leszek Koltunski
        @Override
306
        public void run()
307
          {
308 f5da732a Leszek Koltunski
          ScreenList.switchScreen(act, ScreenList.SOLU);
309
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
310 373fa45f Leszek Koltunski
          solution.setupMoves(act, moves);
311
          }
312
        });
313
      }
314 f0336037 Leszek Koltunski
    }
315
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318 373fa45f Leszek Koltunski
  public void displayErrorDialog( String message)
319 f0336037 Leszek Koltunski
    {
320
    mSolving = false;
321 373fa45f Leszek Koltunski
    RubikActivity act = mWeakAct.get();
322 f0336037 Leszek Koltunski
323 373fa45f Leszek Koltunski
    if( act!=null )
324
      {
325
      RubikDialogSolverError dialog = new RubikDialogSolverError();
326
      Bundle bundle = new Bundle();
327
      bundle.putString("error", message );
328
      dialog.setArguments(bundle);
329
      dialog.show( act.getSupportFragmentManager(), null);
330
      }
331 f0336037 Leszek Koltunski
    }
332 7289fd6c Leszek Koltunski
  }