Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 66e777b0

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 1f9772f3 Leszek Koltunski
package org.distorted.states;
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.util.DisplayMetrics;
31
import android.view.View;
32
import android.widget.Button;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35
36 ca292407 Leszek Koltunski
import org.distorted.dialogs.RubikDialogSolverError;
37 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
38
import org.distorted.main.RubikActivity;
39 473611ee Leszek Koltunski
import org.distorted.main.RubikPostRender;
40 f0336037 Leszek Koltunski
import org.distorted.objects.RubikObject;
41 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
42 373fa45f Leszek Koltunski
import org.distorted.solvers.ImplementedSolversList;
43 a304ee64 Leszek Koltunski
import org.distorted.solvers.SolverMain;
44 7289fd6c Leszek Koltunski
45 373fa45f Leszek Koltunski
import java.lang.ref.WeakReference;
46
47 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
public class RubikStateSolver extends RubikStateAbstract
50
  {
51
  private static final int BITMAP_SIZE = 35;
52
53
  private static Bitmap[] mBitmap;
54
  private ImageButton[] mColorButton;
55 264af0ad Leszek Koltunski
  private Button mBackButton, mSolveButton;
56 5660465b Leszek Koltunski
  private boolean mSolving;
57 1f9772f3 Leszek Koltunski
  private int mCurrentColor;
58 41748f19 Leszek Koltunski
  private int[] mFaceColors;
59
  private int mNumFaces;
60 5660465b Leszek Koltunski
61 373fa45f Leszek Koltunski
  private RubikObjectList mCurrentObject;
62
  private int mCurrentObjectSize;
63
64
  private WeakReference<RubikActivity> mWeakAct;
65
66 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
  void leaveState(RubikActivity act)
69
    {
70 7f84a768 Leszek Koltunski
71 7289fd6c Leszek Koltunski
    }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
  void enterState(final RubikActivity act)
76
    {
77 373fa45f Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
78
79 5660465b Leszek Koltunski
    mSolving = false;
80
81 373fa45f Leszek Koltunski
    mCurrentObject     = ImplementedSolversList.getObject(0);
82
    mCurrentObjectSize = ImplementedSolversList.getObjectSize(0);
83
84
    act.changeObject(mCurrentObject, mCurrentObjectSize, null);
85 473611ee Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
86 373fa45f Leszek Koltunski
    play.setObjectAndSize(mCurrentObject, mCurrentObjectSize);
87 7289fd6c Leszek Koltunski
88 373fa45f Leszek Koltunski
    mFaceColors = RubikObjectList.retFaceColors(mCurrentObject);
89 41748f19 Leszek Koltunski
    mNumFaces   = mFaceColors!=null ? mFaceColors.length : 0;
90 20931cf6 Leszek Koltunski
91 7289fd6c Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
92
    final float scale = metrics.density;
93
94
    // TOP ////////////////////////////
95
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
96
    layoutTop.removeAllViews();
97
98 41748f19 Leszek Koltunski
    if( mNumFaces>0 )
99
      {
100
      if( mBitmap     ==null ) setupBitmaps(scale);
101
      if( mColorButton==null ) setupColorButtons(act,scale);
102 7289fd6c Leszek Koltunski
103 41748f19 Leszek Koltunski
      markButton(act);
104
      }
105 473611ee Leszek Koltunski
106 7289fd6c Leszek Koltunski
    for(ImageButton button: mColorButton) layoutTop.addView(button);
107
108
    // BOT ////////////////////////////
109 264af0ad Leszek Koltunski
    if( mSolveButton==null ) setupSolveButton(act,scale);
110
111
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
112
    layoutLeft.removeAllViews();
113
    layoutLeft.addView(mSolveButton);
114
115 7289fd6c Leszek Koltunski
    if( mBackButton==null ) setupBackButton(act,scale);
116
117
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
118
    layoutRight.removeAllViews();
119
    layoutRight.addView(mBackButton);
120
    }
121
122 5660465b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123 7289fd6c Leszek Koltunski
124
  private void setupBitmaps(float scale)
125
    {
126
    final int SIZE = (int)(scale*BITMAP_SIZE);
127
    final float R = SIZE*0.10f;
128
    final float M = SIZE*0.05f;
129
130 41748f19 Leszek Koltunski
    mBitmap = new Bitmap[mNumFaces];
131 7289fd6c Leszek Koltunski
132
    Paint paint = new Paint();
133
    paint.setColor(0xff008800);
134
    paint.setStyle(Paint.Style.FILL);
135
136
    paint.setAntiAlias(true);
137
    paint.setTextAlign(Paint.Align.CENTER);
138
    paint.setStyle(Paint.Style.FILL);
139
140 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
141 7289fd6c Leszek Koltunski
      {
142
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
143
      Canvas canvas = new Canvas(mBitmap[i]);
144
145
      paint.setColor(0xff000000);
146
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
147
148 41748f19 Leszek Koltunski
      paint.setColor(mFaceColors[i]);
149 7289fd6c Leszek Koltunski
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
150
      }
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
  private void setupColorButtons(final RubikActivity act, final float scale)
156
    {
157 41748f19 Leszek Koltunski
    mColorButton = new ImageButton[mNumFaces];
158 7289fd6c Leszek Koltunski
159 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
160 7289fd6c Leszek Koltunski
      {
161
      final int ii = i;
162
      int padding = (int)(3*scale + 0.5f);
163
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
164
      mColorButton[i] = new ImageButton(act);
165
      mColorButton[i].setLayoutParams(objectParams);
166
      mColorButton[i].setPadding(padding,0,padding,0);
167
      mColorButton[i].setImageBitmap(mBitmap[i]);
168
169
      mColorButton[i].setOnClickListener( new View.OnClickListener()
170
        {
171
        @Override
172
        public void onClick(View view)
173
          {
174 473611ee Leszek Koltunski
          mCurrentColor = ii;
175
          markButton(act);
176 7289fd6c Leszek Koltunski
          }
177
        });
178
      }
179
    }
180
181 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183
  private void setupSolveButton(final RubikActivity act, final float scale)
184
    {
185
    int padding = (int)(3*scale + 0.5f);
186
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
187
    mSolveButton = new Button(act);
188
    mSolveButton.setLayoutParams(backParams);
189
    mSolveButton.setPadding(padding,0,padding,0);
190
    mSolveButton.setText(R.string.solve);
191
192
    mSolveButton.setOnClickListener( new View.OnClickListener()
193
      {
194
      @Override
195
      public void onClick(View v)
196
        {
197 5660465b Leszek Koltunski
        if( !mSolving )
198
          {
199
          mSolving = true;
200 f0336037 Leszek Koltunski
          RubikObject object = act.getObject();
201
          String objectString = object.retObjectString();
202 a304ee64 Leszek Koltunski
          SolverMain solver = new SolverMain( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
203 f0336037 Leszek Koltunski
          solver.start();
204 5660465b Leszek Koltunski
          }
205 264af0ad Leszek Koltunski
        }
206
      });
207
    }
208
209 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211
  private void setupBackButton(final RubikActivity act, final float scale)
212
    {
213
    int padding = (int)(3*scale + 0.5f);
214
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
215
    mBackButton = new Button(act);
216
    mBackButton.setLayoutParams(backParams);
217
    mBackButton.setPadding(padding,0,padding,0);
218
    mBackButton.setText(R.string.back);
219
220
    mBackButton.setOnClickListener( new View.OnClickListener()
221
      {
222
      @Override
223
      public void onClick(View v)
224
        {
225 7f84a768 Leszek Koltunski
        RubikPostRender post = act.getPostRender();
226
        post.resetAllTextureMaps();
227 7289fd6c Leszek Koltunski
        RubikState.goBack(act);
228
        }
229
      });
230
    }
231
232 473611ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234
  private void markButton(RubikActivity act)
235
    {
236 41748f19 Leszek Koltunski
    for(int b=0; b<mNumFaces; b++)
237 473611ee Leszek Koltunski
      {
238
      Drawable d = mColorButton[b].getBackground();
239
240
      if( b==mCurrentColor )
241
        {
242
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
243
        }
244
      else
245
        {
246
        d.clearColorFilter();
247
        }
248
      }
249
    }
250
251 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
  public void savePreferences(SharedPreferences.Editor editor)
254
    {
255
    mColorButton = null;
256
    mBackButton  = null;
257 811b0446 Leszek Koltunski
    mSolveButton = null;
258 1f9772f3 Leszek Koltunski
259
    editor.putInt("stateSolver_color", mCurrentColor);
260 7289fd6c Leszek Koltunski
    }
261
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264
  public void restorePreferences(SharedPreferences preferences)
265
    {
266 1f9772f3 Leszek Koltunski
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
267 7289fd6c Leszek Koltunski
    }
268 473611ee Leszek Koltunski
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
271
  public int getCurrentColor()
272
    {
273
    return mCurrentColor;
274
    }
275 f0336037 Leszek Koltunski
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278 373fa45f Leszek Koltunski
  public void setSolved(final String moves)
279 f0336037 Leszek Koltunski
    {
280
    mSolving = false;
281 373fa45f Leszek Koltunski
    final RubikActivity act = mWeakAct.get();
282 f0336037 Leszek Koltunski
283 373fa45f Leszek Koltunski
    if( act!=null )
284 f0336037 Leszek Koltunski
      {
285 373fa45f Leszek Koltunski
      act.runOnUiThread(new Runnable()
286 f0336037 Leszek Koltunski
        {
287 373fa45f Leszek Koltunski
        @Override
288
        public void run()
289
          {
290
          RubikState.switchState(act,RubikState.SOLU);
291
          RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
292
          solution.setupMoves(act, moves);
293
          }
294
        });
295
      }
296 f0336037 Leszek Koltunski
    }
297
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300 373fa45f Leszek Koltunski
  public void displayErrorDialog( String message)
301 f0336037 Leszek Koltunski
    {
302
    mSolving = false;
303 373fa45f Leszek Koltunski
    RubikActivity act = mWeakAct.get();
304 f0336037 Leszek Koltunski
305 373fa45f Leszek Koltunski
    if( act!=null )
306
      {
307
      RubikDialogSolverError dialog = new RubikDialogSolverError();
308
      Bundle bundle = new Bundle();
309
      bundle.putString("error", message );
310
      dialog.setArguments(bundle);
311
      dialog.show( act.getSupportFragmentManager(), null);
312
      }
313 f0336037 Leszek Koltunski
    }
314 7289fd6c Leszek Koltunski
  }