Project

General

Profile

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

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

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 373fa45f Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
59
60 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
63 7289fd6c Leszek Koltunski
    {
64 7f84a768 Leszek Koltunski
65 7289fd6c Leszek Koltunski
    }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
70 7289fd6c Leszek Koltunski
    {
71 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
72 6d4d56cb Leszek Koltunski
    float heigh = act.getScreenHeightInPixels();
73 7ea57482 Leszek Koltunski
74
    int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
75
    int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
76
77
    mBitmapSize = Math.min(sizeV,sizeH);
78 373fa45f Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
79 5660465b Leszek Koltunski
    mSolving = false;
80
81 abf36986 Leszek Koltunski
    ObjectList currentObject= ImplementedSolversList.getObject(0);
82
    int currentObjectSize   = ImplementedSolversList.getObjectSize(0);
83 373fa45f Leszek Koltunski
84 abf36986 Leszek Koltunski
    act.setupObject(currentObject, currentObjectSize, null);
85 f5da732a Leszek Koltunski
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
86 abf36986 Leszek Koltunski
    play.setObjectAndSize(act, currentObject, currentObjectSize);
87 7289fd6c Leszek Koltunski
88 abf36986 Leszek Koltunski
    mFaceColors = ObjectList.retFaceColors(currentObject);
89 41748f19 Leszek Koltunski
    mNumFaces   = mFaceColors!=null ? mFaceColors.length : 0;
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 5660465b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129 7289fd6c Leszek Koltunski
130 7ebd72f7 Leszek Koltunski
  private void setupBitmaps()
131 7289fd6c Leszek Koltunski
    {
132 1bd07548 Leszek Koltunski
    final int SIZE = (int)mBitmapSize;
133
    final float R = SIZE*0.15f;
134
    final float M = SIZE*0.08f;
135 7289fd6c Leszek Koltunski
136 41748f19 Leszek Koltunski
    mBitmap = new Bitmap[mNumFaces];
137 7289fd6c Leszek Koltunski
138
    Paint paint = new Paint();
139
    paint.setColor(0xff008800);
140
    paint.setStyle(Paint.Style.FILL);
141
142
    paint.setAntiAlias(true);
143
    paint.setTextAlign(Paint.Align.CENTER);
144
    paint.setStyle(Paint.Style.FILL);
145
146 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
147 7289fd6c Leszek Koltunski
      {
148
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
149
      Canvas canvas = new Canvas(mBitmap[i]);
150
151
      paint.setColor(0xff000000);
152
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
153
154 41748f19 Leszek Koltunski
      paint.setColor(mFaceColors[i]);
155 7289fd6c Leszek Koltunski
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
156
      }
157
    }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 ad0c8e0e Leszek Koltunski
  private void setupColorButtons(final RubikActivity act, final float width)
162 7289fd6c Leszek Koltunski
    {
163 41748f19 Leszek Koltunski
    mColorButton = new ImageButton[mNumFaces];
164 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
165
    int margin  = (int)(width*RubikActivity.MARGIN);
166 7289fd6c Leszek Koltunski
167 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
168 7289fd6c Leszek Koltunski
      {
169
      final int ii = i;
170 ad0c8e0e Leszek Koltunski
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
171
      params.topMargin    = margin;
172
      params.bottomMargin = margin;
173
      params.leftMargin   = margin;
174
      params.rightMargin  = margin;
175
176 7289fd6c Leszek Koltunski
      mColorButton[i] = new ImageButton(act);
177 ad0c8e0e Leszek Koltunski
      mColorButton[i].setLayoutParams(params);
178 7289fd6c Leszek Koltunski
      mColorButton[i].setPadding(padding,0,padding,0);
179
      mColorButton[i].setImageBitmap(mBitmap[i]);
180
181
      mColorButton[i].setOnClickListener( new View.OnClickListener()
182
        {
183
        @Override
184
        public void onClick(View view)
185
          {
186 473611ee Leszek Koltunski
          mCurrentColor = ii;
187
          markButton(act);
188 7289fd6c Leszek Koltunski
          }
189
        });
190
      }
191
    }
192
193 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195 ad0c8e0e Leszek Koltunski
  private void setupSolveButton(final RubikActivity act, final float width)
196 264af0ad Leszek Koltunski
    {
197 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);
198 da768c35 Leszek Koltunski
    mSolveButton = new TransparentImageButton(act,icon,width, LinearLayout.LayoutParams.MATCH_PARENT);
199 264af0ad Leszek Koltunski
200
    mSolveButton.setOnClickListener( new View.OnClickListener()
201
      {
202
      @Override
203
      public void onClick(View v)
204
        {
205 5660465b Leszek Koltunski
        if( !mSolving )
206
          {
207
          mSolving = true;
208 9c2f0c91 Leszek Koltunski
          TwistyObject object = act.getObject();
209 ecf3f149 Leszek Koltunski
          SolverMain solver = new SolverMain( act.getResources(), object );
210 f0336037 Leszek Koltunski
          solver.start();
211 5660465b Leszek Koltunski
          }
212 264af0ad Leszek Koltunski
        }
213
      });
214
    }
215
216 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218 ad0c8e0e Leszek Koltunski
  private void setupBackButton(final RubikActivity act, final float width)
219 7289fd6c Leszek Koltunski
    {
220 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);
221 da768c35 Leszek Koltunski
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
222 7289fd6c Leszek Koltunski
223
    mBackButton.setOnClickListener( new View.OnClickListener()
224
      {
225
      @Override
226
      public void onClick(View v)
227
        {
228 5a4d4fba Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
229
        pre.resetAllTextureMaps();
230 fcd5b990 Leszek Koltunski
        ScreenList.goBack(act);
231 7289fd6c Leszek Koltunski
        }
232
      });
233
    }
234
235 473611ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237
  private void markButton(RubikActivity act)
238
    {
239 41748f19 Leszek Koltunski
    for(int b=0; b<mNumFaces; b++)
240 473611ee Leszek Koltunski
      {
241
      Drawable d = mColorButton[b].getBackground();
242
243
      if( b==mCurrentColor )
244
        {
245
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
246
        }
247
      else
248
        {
249
        d.clearColorFilter();
250
        }
251
      }
252
    }
253
254 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
  public void savePreferences(SharedPreferences.Editor editor)
257
    {
258 1f9772f3 Leszek Koltunski
    editor.putInt("stateSolver_color", mCurrentColor);
259 7289fd6c Leszek Koltunski
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
  public void restorePreferences(SharedPreferences preferences)
264
    {
265 1f9772f3 Leszek Koltunski
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
266 7289fd6c Leszek Koltunski
    }
267 473611ee Leszek Koltunski
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
  public int getCurrentColor()
271
    {
272
    return mCurrentColor;
273
    }
274 f0336037 Leszek Koltunski
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277 373fa45f Leszek Koltunski
  public void setSolved(final String moves)
278 f0336037 Leszek Koltunski
    {
279
    mSolving = false;
280 373fa45f Leszek Koltunski
    final RubikActivity act = mWeakAct.get();
281 f0336037 Leszek Koltunski
282 373fa45f Leszek Koltunski
    if( act!=null )
283 f0336037 Leszek Koltunski
      {
284 373fa45f Leszek Koltunski
      act.runOnUiThread(new Runnable()
285 f0336037 Leszek Koltunski
        {
286 373fa45f Leszek Koltunski
        @Override
287
        public void run()
288
          {
289 f5da732a Leszek Koltunski
          ScreenList.switchScreen(act, ScreenList.SOLU);
290
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
291 373fa45f Leszek Koltunski
          solution.setupMoves(act, moves);
292
          }
293
        });
294
      }
295 f0336037 Leszek Koltunski
    }
296
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
299 373fa45f Leszek Koltunski
  public void displayErrorDialog( String message)
300 f0336037 Leszek Koltunski
    {
301
    mSolving = false;
302 373fa45f Leszek Koltunski
    RubikActivity act = mWeakAct.get();
303 f0336037 Leszek Koltunski
304 373fa45f Leszek Koltunski
    if( act!=null )
305
      {
306
      RubikDialogSolverError dialog = new RubikDialogSolverError();
307
      Bundle bundle = new Bundle();
308
      bundle.putString("error", message );
309
      dialog.setArguments(bundle);
310
      dialog.show( act.getSupportFragmentManager(), null);
311
      }
312 f0336037 Leszek Koltunski
    }
313 7289fd6c Leszek Koltunski
  }