Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ 3f7a4363

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