Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 7ebd72f7

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.DisplayMetrics;
31
import android.util.TypedValue;
32
import android.view.View;
33
import android.widget.Button;
34
import android.widget.ImageButton;
35
import android.widget.LinearLayout;
36

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

    
46
import java.lang.ref.WeakReference;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
61
  private RubikObjectList mCurrentObject;
62
  private int mCurrentObjectSize;
63

    
64
  private WeakReference<RubikActivity> mWeakAct;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  void leaveState(RubikActivity act)
69
    {
70

    
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

    
84
    mSolving = false;
85

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

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

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

    
96
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
97
    final float scale = metrics.density;
98

    
99
    // TOP ////////////////////////////
100
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
101
    layoutTop.removeAllViews();
102

    
103
    if( mNumFaces>0 )
104
      {
105
      setupBitmaps();
106
      setupColorButtons(act,scale);
107
      markButton(act);
108
      }
109

    
110
    for(ImageButton button: mColorButton) layoutTop.addView(button);
111

    
112
    // BOT ////////////////////////////
113
    setupSolveButton(act,scale);
114

    
115
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
116
    layoutLeft.removeAllViews();
117
    layoutLeft.addView(mSolveButton);
118

    
119
    setupBackButton(act,scale);
120

    
121
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
122
    layoutRight.removeAllViews();
123
    layoutRight.addView(mBackButton);
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void setupBitmaps()
129
    {
130
    final int SIZE = (int)mBitmapSize;
131
    final float R = SIZE*0.15f;
132
    final float M = SIZE*0.08f;
133

    
134
    mBitmap = new Bitmap[mNumFaces];
135

    
136
    Paint paint = new Paint();
137
    paint.setColor(0xff008800);
138
    paint.setStyle(Paint.Style.FILL);
139

    
140
    paint.setAntiAlias(true);
141
    paint.setTextAlign(Paint.Align.CENTER);
142
    paint.setStyle(Paint.Style.FILL);
143

    
144
    for(int i=0; i<mNumFaces; i++)
145
      {
146
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
147
      Canvas canvas = new Canvas(mBitmap[i]);
148

    
149
      paint.setColor(0xff000000);
150
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
151

    
152
      paint.setColor(mFaceColors[i]);
153
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
154
      }
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void setupColorButtons(final RubikActivity act, final float scale)
160
    {
161
    mColorButton = new ImageButton[mNumFaces];
162

    
163
    for(int i=0; i<mNumFaces; i++)
164
      {
165
      final int ii = i;
166
      int padding = (int)(3*scale + 0.5f);
167
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
168
      mColorButton[i] = new ImageButton(act);
169
      mColorButton[i].setLayoutParams(objectParams);
170
      mColorButton[i].setPadding(padding,0,padding,0);
171
      mColorButton[i].setImageBitmap(mBitmap[i]);
172

    
173
      mColorButton[i].setOnClickListener( new View.OnClickListener()
174
        {
175
        @Override
176
        public void onClick(View view)
177
          {
178
          mCurrentColor = ii;
179
          markButton(act);
180
          }
181
        });
182
      }
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  private void setupSolveButton(final RubikActivity act, final float scale)
188
    {
189
    int padding = (int)(3*scale + 0.5f);
190
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
191
    mSolveButton = new Button(act);
192
    mSolveButton.setLayoutParams(backParams);
193
    mSolveButton.setPadding(padding,0,padding,0);
194
    mSolveButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
195
    mSolveButton.setText(R.string.solve);
196

    
197
    mSolveButton.setOnClickListener( new View.OnClickListener()
198
      {
199
      @Override
200
      public void onClick(View v)
201
        {
202
        if( !mSolving )
203
          {
204
          mSolving = true;
205
          RubikObject object = act.getObject();
206
          String objectString = object.retObjectString();
207
          SolverMain solver = new SolverMain( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
208
          solver.start();
209
          }
210
        }
211
      });
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private void setupBackButton(final RubikActivity act, final float scale)
217
    {
218
    int padding = (int)(3*scale + 0.5f);
219
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
220
    mBackButton = new Button(act);
221
    mBackButton.setLayoutParams(backParams);
222
    mBackButton.setPadding(padding,0,padding,0);
223
    mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
224
    mBackButton.setText(R.string.back);
225

    
226
    mBackButton.setOnClickListener( new View.OnClickListener()
227
      {
228
      @Override
229
      public void onClick(View v)
230
        {
231
        RubikPreRender pre = act.getPreRender();
232
        pre.resetAllTextureMaps();
233
        RubikState.goBack(act);
234
        }
235
      });
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  private void markButton(RubikActivity act)
241
    {
242
    for(int b=0; b<mNumFaces; b++)
243
      {
244
      Drawable d = mColorButton[b].getBackground();
245

    
246
      if( b==mCurrentColor )
247
        {
248
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
249
        }
250
      else
251
        {
252
        d.clearColorFilter();
253
        }
254
      }
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  public void savePreferences(SharedPreferences.Editor editor)
260
    {
261
    editor.putInt("stateSolver_color", mCurrentColor);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public void restorePreferences(SharedPreferences preferences)
267
    {
268
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public int getCurrentColor()
274
    {
275
    return mCurrentColor;
276
    }
277

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

    
280
  public void setSolved(final String moves)
281
    {
282
    mSolving = false;
283
    final RubikActivity act = mWeakAct.get();
284

    
285
    if( act!=null )
286
      {
287
      act.runOnUiThread(new Runnable()
288
        {
289
        @Override
290
        public void run()
291
          {
292
          RubikState.switchState(act,RubikState.SOLU);
293
          RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
294
          solution.setupMoves(act, moves);
295
          }
296
        });
297
      }
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  public void displayErrorDialog( String message)
303
    {
304
    mSolving = false;
305
    RubikActivity act = mWeakAct.get();
306

    
307
    if( act!=null )
308
      {
309
      RubikDialogSolverError dialog = new RubikDialogSolverError();
310
      Bundle bundle = new Bundle();
311
      bundle.putString("error", message );
312
      dialog.setArguments(bundle);
313
      dialog.show( act.getSupportFragmentManager(), null);
314
      }
315
    }
316
  }
(9-9/10)