Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 66e777b0

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.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.RubikPostRender;
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 final int BITMAP_SIZE = 35;
52

    
53
  private static Bitmap[] mBitmap;
54
  private ImageButton[] mColorButton;
55
  private Button mBackButton, mSolveButton;
56
  private boolean mSolving;
57
  private int mCurrentColor;
58
  private int[] mFaceColors;
59
  private int mNumFaces;
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
    mWeakAct = new WeakReference<>(act);
78

    
79
    mSolving = false;
80

    
81
    mCurrentObject     = ImplementedSolversList.getObject(0);
82
    mCurrentObjectSize = ImplementedSolversList.getObjectSize(0);
83

    
84
    act.changeObject(mCurrentObject, mCurrentObjectSize, null);
85
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
86
    play.setObjectAndSize(mCurrentObject, mCurrentObjectSize);
87

    
88
    mFaceColors = RubikObjectList.retFaceColors(mCurrentObject);
89
    mNumFaces   = mFaceColors!=null ? mFaceColors.length : 0;
90

    
91
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
92
    final float scale = metrics.density;
93

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

    
98
    if( mNumFaces>0 )
99
      {
100
      if( mBitmap     ==null ) setupBitmaps(scale);
101
      if( mColorButton==null ) setupColorButtons(act,scale);
102

    
103
      markButton(act);
104
      }
105

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

    
108
    // BOT ////////////////////////////
109
    if( mSolveButton==null ) setupSolveButton(act,scale);
110

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

    
115
    if( mBackButton==null ) setupBackButton(act,scale);
116

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

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

    
124
  private void setupBitmaps(float scale)
125
    {
126
    final int SIZE = (int)(scale*BITMAP_SIZE);
127
    final float R = SIZE*0.10f;
128
    final float M = SIZE*0.05f;
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 scale)
156
    {
157
    mColorButton = new ImageButton[mNumFaces];
158

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

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void setupSolveButton(final RubikActivity act, final float scale)
184
    {
185
    int padding = (int)(3*scale + 0.5f);
186
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
187
    mSolveButton = new Button(act);
188
    mSolveButton.setLayoutParams(backParams);
189
    mSolveButton.setPadding(padding,0,padding,0);
190
    mSolveButton.setText(R.string.solve);
191

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  private void setupBackButton(final RubikActivity act, final float scale)
212
    {
213
    int padding = (int)(3*scale + 0.5f);
214
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
215
    mBackButton = new Button(act);
216
    mBackButton.setLayoutParams(backParams);
217
    mBackButton.setPadding(padding,0,padding,0);
218
    mBackButton.setText(R.string.back);
219

    
220
    mBackButton.setOnClickListener( new View.OnClickListener()
221
      {
222
      @Override
223
      public void onClick(View v)
224
        {
225
        RubikPostRender post = act.getPostRender();
226
        post.resetAllTextureMaps();
227
        RubikState.goBack(act);
228
        }
229
      });
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  private void markButton(RubikActivity act)
235
    {
236
    for(int b=0; b<mNumFaces; b++)
237
      {
238
      Drawable d = mColorButton[b].getBackground();
239

    
240
      if( b==mCurrentColor )
241
        {
242
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
243
        }
244
      else
245
        {
246
        d.clearColorFilter();
247
        }
248
      }
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  public void savePreferences(SharedPreferences.Editor editor)
254
    {
255
    mColorButton = null;
256
    mBackButton  = null;
257
    mSolveButton = null;
258

    
259
    editor.putInt("stateSolver_color", mCurrentColor);
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  public int getCurrentColor()
272
    {
273
    return mCurrentColor;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

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

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

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

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