Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 1bd07548

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(scale);
106
      setupColorButtons(act,scale);
107

    
108
      markButton(act);
109
      }
110

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

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

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

    
120
    setupBackButton(act,scale);
121

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

    
135
    mBitmap = new Bitmap[mNumFaces];
136

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

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

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

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

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

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

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

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

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

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

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

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

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

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

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