Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 6d4d56cb

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