Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 5de2c572

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 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
31 7289fd6c Leszek Koltunski
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 5a4d4fba Leszek Koltunski
import org.distorted.main.RubikPreRender;
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 Bitmap[] mBitmap;
52
  private ImageButton[] mColorButton;
53 264af0ad Leszek Koltunski
  private Button 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 1bd07548 Leszek Koltunski
  private float mTitleSize, mButtonSize, mBitmapSize;
59 5660465b Leszek Koltunski
60 373fa45f Leszek Koltunski
  private RubikObjectList mCurrentObject;
61
  private int mCurrentObjectSize;
62
63
  private WeakReference<RubikActivity> mWeakAct;
64
65 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
  void leaveState(RubikActivity act)
68
    {
69 7f84a768 Leszek Koltunski
70 7289fd6c Leszek Koltunski
    }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
  void enterState(final RubikActivity act)
75
    {
76 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
77
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
78
    mTitleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
79 1bd07548 Leszek Koltunski
    mBitmapSize = width*RubikActivity.BITMAP_TEXT_SIZE;
80 e3c74c0f Leszek Koltunski
81 373fa45f Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
82
83 5660465b Leszek Koltunski
    mSolving = false;
84
85 373fa45f Leszek Koltunski
    mCurrentObject     = ImplementedSolversList.getObject(0);
86
    mCurrentObjectSize = ImplementedSolversList.getObjectSize(0);
87
88 2e8ec627 Leszek Koltunski
    act.setupObject(mCurrentObject, mCurrentObjectSize, null);
89 473611ee Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
90 7b7d65ce Leszek Koltunski
    play.setObjectAndSize(act, mCurrentObject, mCurrentObjectSize);
91 7289fd6c Leszek Koltunski
92 373fa45f Leszek Koltunski
    mFaceColors = RubikObjectList.retFaceColors(mCurrentObject);
93 41748f19 Leszek Koltunski
    mNumFaces   = mFaceColors!=null ? mFaceColors.length : 0;
94 20931cf6 Leszek Koltunski
95 7289fd6c Leszek Koltunski
    // TOP ////////////////////////////
96
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
97
    layoutTop.removeAllViews();
98
99 41748f19 Leszek Koltunski
    if( mNumFaces>0 )
100
      {
101 7ebd72f7 Leszek Koltunski
      setupBitmaps();
102 ad0c8e0e Leszek Koltunski
      setupColorButtons(act,width);
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 ad0c8e0e Leszek Koltunski
    setupSolveButton(act,width);
110 264af0ad Leszek Koltunski
111
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
112
    layoutLeft.removeAllViews();
113
    layoutLeft.addView(mSolveButton);
114
115 ad0c8e0e Leszek Koltunski
    setupBackButton(act,width);
116 7289fd6c Leszek Koltunski
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 7ebd72f7 Leszek Koltunski
  private void setupBitmaps()
125 7289fd6c Leszek Koltunski
    {
126 1bd07548 Leszek Koltunski
    final int SIZE = (int)mBitmapSize;
127
    final float R = SIZE*0.15f;
128
    final float M = SIZE*0.08f;
129 7289fd6c Leszek Koltunski
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 ad0c8e0e Leszek Koltunski
  private void setupColorButtons(final RubikActivity act, final float width)
156 7289fd6c Leszek Koltunski
    {
157 41748f19 Leszek Koltunski
    mColorButton = new ImageButton[mNumFaces];
158 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
159
    int margin  = (int)(width*RubikActivity.MARGIN);
160 7289fd6c Leszek Koltunski
161 41748f19 Leszek Koltunski
    for(int i=0; i<mNumFaces; i++)
162 7289fd6c Leszek Koltunski
      {
163
      final int ii = i;
164 ad0c8e0e Leszek Koltunski
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
165
      params.topMargin    = margin;
166
      params.bottomMargin = margin;
167
      params.leftMargin   = margin;
168
      params.rightMargin  = margin;
169
170 7289fd6c Leszek Koltunski
      mColorButton[i] = new ImageButton(act);
171 ad0c8e0e Leszek Koltunski
      mColorButton[i].setLayoutParams(params);
172 7289fd6c Leszek Koltunski
      mColorButton[i].setPadding(padding,0,padding,0);
173
      mColorButton[i].setImageBitmap(mBitmap[i]);
174
175
      mColorButton[i].setOnClickListener( new View.OnClickListener()
176
        {
177
        @Override
178
        public void onClick(View view)
179
          {
180 473611ee Leszek Koltunski
          mCurrentColor = ii;
181
          markButton(act);
182 7289fd6c Leszek Koltunski
          }
183
        });
184
      }
185
    }
186
187 264af0ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 ad0c8e0e Leszek Koltunski
  private void setupSolveButton(final RubikActivity act, final float width)
190 264af0ad Leszek Koltunski
    {
191 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
192
    int margin   = (int)(width*RubikActivity.MARGIN);
193
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
194
    params.topMargin    = margin;
195
    params.bottomMargin = margin;
196
    params.leftMargin   = margin;
197
    params.rightMargin  = margin;
198
199 264af0ad Leszek Koltunski
    mSolveButton = new Button(act);
200 ad0c8e0e Leszek Koltunski
    mSolveButton.setLayoutParams(params);
201 264af0ad Leszek Koltunski
    mSolveButton.setPadding(padding,0,padding,0);
202 e3c74c0f Leszek Koltunski
    mSolveButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
203 264af0ad Leszek Koltunski
    mSolveButton.setText(R.string.solve);
204
205
    mSolveButton.setOnClickListener( new View.OnClickListener()
206
      {
207
      @Override
208
      public void onClick(View v)
209
        {
210 5660465b Leszek Koltunski
        if( !mSolving )
211
          {
212
          mSolving = true;
213 f0336037 Leszek Koltunski
          RubikObject object = act.getObject();
214
          String objectString = object.retObjectString();
215 a304ee64 Leszek Koltunski
          SolverMain solver = new SolverMain( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
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 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
227
    int margin  = (int)(width*RubikActivity.MARGIN);
228
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
229
    params.topMargin    = margin;
230
    params.bottomMargin = margin;
231
    params.leftMargin   = margin;
232
    params.rightMargin  = margin;
233
234 7289fd6c Leszek Koltunski
    mBackButton = new Button(act);
235 ad0c8e0e Leszek Koltunski
    mBackButton.setLayoutParams(params);
236 7289fd6c Leszek Koltunski
    mBackButton.setPadding(padding,0,padding,0);
237 e3c74c0f Leszek Koltunski
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
238 7289fd6c Leszek Koltunski
    mBackButton.setText(R.string.back);
239
240
    mBackButton.setOnClickListener( new View.OnClickListener()
241
      {
242
      @Override
243
      public void onClick(View v)
244
        {
245 5a4d4fba Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
246
        pre.resetAllTextureMaps();
247 7289fd6c Leszek Koltunski
        RubikState.goBack(act);
248
        }
249
      });
250
    }
251
252 473611ee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254
  private void markButton(RubikActivity act)
255
    {
256 41748f19 Leszek Koltunski
    for(int b=0; b<mNumFaces; b++)
257 473611ee Leszek Koltunski
      {
258
      Drawable d = mColorButton[b].getBackground();
259
260
      if( b==mCurrentColor )
261
        {
262
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
263
        }
264
      else
265
        {
266
        d.clearColorFilter();
267
        }
268
      }
269
    }
270
271 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273
  public void savePreferences(SharedPreferences.Editor editor)
274
    {
275 1f9772f3 Leszek Koltunski
    editor.putInt("stateSolver_color", mCurrentColor);
276 7289fd6c Leszek Koltunski
    }
277
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
280
  public void restorePreferences(SharedPreferences preferences)
281
    {
282 1f9772f3 Leszek Koltunski
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
283 7289fd6c Leszek Koltunski
    }
284 473611ee Leszek Koltunski
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
287
  public int getCurrentColor()
288
    {
289
    return mCurrentColor;
290
    }
291 f0336037 Leszek Koltunski
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293
294 373fa45f Leszek Koltunski
  public void setSolved(final String moves)
295 f0336037 Leszek Koltunski
    {
296
    mSolving = false;
297 373fa45f Leszek Koltunski
    final RubikActivity act = mWeakAct.get();
298 f0336037 Leszek Koltunski
299 373fa45f Leszek Koltunski
    if( act!=null )
300 f0336037 Leszek Koltunski
      {
301 373fa45f Leszek Koltunski
      act.runOnUiThread(new Runnable()
302 f0336037 Leszek Koltunski
        {
303 373fa45f Leszek Koltunski
        @Override
304
        public void run()
305
          {
306
          RubikState.switchState(act,RubikState.SOLU);
307
          RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
308
          solution.setupMoves(act, moves);
309
          }
310
        });
311
      }
312 f0336037 Leszek Koltunski
    }
313
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316 373fa45f Leszek Koltunski
  public void displayErrorDialog( String message)
317 f0336037 Leszek Koltunski
    {
318
    mSolving = false;
319 373fa45f Leszek Koltunski
    RubikActivity act = mWeakAct.get();
320 f0336037 Leszek Koltunski
321 373fa45f Leszek Koltunski
    if( act!=null )
322
      {
323
      RubikDialogSolverError dialog = new RubikDialogSolverError();
324
      Bundle bundle = new Bundle();
325
      bundle.putString("error", message );
326
      dialog.setArguments(bundle);
327
      dialog.show( act.getSupportFragmentManager(), null);
328
      }
329 f0336037 Leszek Koltunski
    }
330 7289fd6c Leszek Koltunski
  }