Project

General

Profile

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

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

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