Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ 255492a0

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