Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ ad0c8e0e

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.states;
21

    
22
import android.content.SharedPreferences;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.graphics.PorterDuff;
27
import android.graphics.drawable.Drawable;
28
import android.os.Bundle;
29
import androidx.core.content.ContextCompat;
30
import android.util.TypedValue;
31
import android.view.View;
32
import android.widget.Button;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35

    
36
import org.distorted.dialogs.RubikDialogSolverError;
37
import org.distorted.main.R;
38
import org.distorted.main.RubikActivity;
39
import org.distorted.main.RubikPreRender;
40
import org.distorted.objects.RubikObject;
41
import org.distorted.objects.RubikObjectList;
42
import org.distorted.solvers.ImplementedSolversList;
43
import org.distorted.solvers.SolverMain;
44

    
45
import java.lang.ref.WeakReference;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class RubikStateSolver extends RubikStateAbstract
50
  {
51
  private static Bitmap[] mBitmap;
52
  private ImageButton[] mColorButton;
53
  private Button mBackButton, mSolveButton;
54
  private boolean mSolving;
55
  private int mCurrentColor;
56
  private int[] mFaceColors;
57
  private int mNumFaces;
58
  private float mTitleSize, mButtonSize, mBitmapSize;
59

    
60
  private RubikObjectList mCurrentObject;
61
  private int mCurrentObjectSize;
62

    
63
  private WeakReference<RubikActivity> mWeakAct;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  void leaveState(RubikActivity act)
68
    {
69

    
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  void enterState(final RubikActivity act)
75
    {
76
    float width = act.getScreenWidthInPixels();
77
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
78
    mTitleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
79
    mBitmapSize = width*RubikActivity.BITMAP_TEXT_SIZE;
80

    
81
    mWeakAct = new WeakReference<>(act);
82

    
83
    mSolving = false;
84

    
85
    mCurrentObject     = ImplementedSolversList.getObject(0);
86
    mCurrentObjectSize = ImplementedSolversList.getObjectSize(0);
87

    
88
    act.setupObject(mCurrentObject, mCurrentObjectSize, null);
89
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
90
    play.setObjectAndSize(act, mCurrentObject, mCurrentObjectSize);
91

    
92
    mFaceColors = RubikObjectList.retFaceColors(mCurrentObject);
93
    mNumFaces   = mFaceColors!=null ? mFaceColors.length : 0;
94

    
95
    // TOP ////////////////////////////
96
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
97
    layoutTop.removeAllViews();
98

    
99
    if( mNumFaces>0 )
100
      {
101
      setupBitmaps();
102
      setupColorButtons(act,width);
103
      markButton(act);
104
      }
105

    
106
    for(ImageButton button: mColorButton) layoutTop.addView(button);
107

    
108
    // BOT ////////////////////////////
109
    setupSolveButton(act,width);
110

    
111
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
112
    layoutLeft.removeAllViews();
113
    layoutLeft.addView(mSolveButton);
114

    
115
    setupBackButton(act,width);
116

    
117
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
118
    layoutRight.removeAllViews();
119
    layoutRight.addView(mBackButton);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void setupBitmaps()
125
    {
126
    final int SIZE = (int)mBitmapSize;
127
    final float R = SIZE*0.15f;
128
    final float M = SIZE*0.08f;
129

    
130
    mBitmap = new Bitmap[mNumFaces];
131

    
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
    for(int i=0; i<mNumFaces; i++)
141
      {
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
      paint.setColor(mFaceColors[i]);
149
      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 width)
156
    {
157
    mColorButton = new ImageButton[mNumFaces];
158
    int padding = (int)(width*RubikActivity.PADDING);
159
    int margin  = (int)(width*RubikActivity.MARGIN);
160

    
161
    for(int i=0; i<mNumFaces; i++)
162
      {
163
      final int ii = i;
164
      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
      mColorButton[i] = new ImageButton(act);
171
      mColorButton[i].setLayoutParams(params);
172
      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
          mCurrentColor = ii;
181
          markButton(act);
182
          }
183
        });
184
      }
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void setupSolveButton(final RubikActivity act, final float width)
190
    {
191
    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
    mSolveButton = new Button(act);
200
    mSolveButton.setLayoutParams(params);
201
    mSolveButton.setPadding(padding,0,padding,0);
202
    mSolveButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
203
    mSolveButton.setText(R.string.solve);
204

    
205
    mSolveButton.setOnClickListener( new View.OnClickListener()
206
      {
207
      @Override
208
      public void onClick(View v)
209
        {
210
        if( !mSolving )
211
          {
212
          mSolving = true;
213
          RubikObject object = act.getObject();
214
          String objectString = object.retObjectString();
215
          SolverMain solver = new SolverMain( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
216
          solver.start();
217
          }
218
        }
219
      });
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void setupBackButton(final RubikActivity act, final float width)
225
    {
226
    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
    mBackButton = new Button(act);
235
    mBackButton.setLayoutParams(params);
236
    mBackButton.setPadding(padding,0,padding,0);
237
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
238
    mBackButton.setText(R.string.back);
239

    
240
    mBackButton.setOnClickListener( new View.OnClickListener()
241
      {
242
      @Override
243
      public void onClick(View v)
244
        {
245
        RubikPreRender pre = act.getPreRender();
246
        pre.resetAllTextureMaps();
247
        RubikState.goBack(act);
248
        }
249
      });
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private void markButton(RubikActivity act)
255
    {
256
    for(int b=0; b<mNumFaces; b++)
257
      {
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
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public void savePreferences(SharedPreferences.Editor editor)
274
    {
275
    editor.putInt("stateSolver_color", mCurrentColor);
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public void restorePreferences(SharedPreferences preferences)
281
    {
282
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public int getCurrentColor()
288
    {
289
    return mCurrentColor;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public void setSolved(final String moves)
295
    {
296
    mSolving = false;
297
    final RubikActivity act = mWeakAct.get();
298

    
299
    if( act!=null )
300
      {
301
      act.runOnUiThread(new Runnable()
302
        {
303
        @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
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public void displayErrorDialog( String message)
317
    {
318
    mSolving = false;
319
    RubikActivity act = mWeakAct.get();
320

    
321
    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
    }
330
  }
(8-8/9)