Project

General

Profile

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

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

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 android.support.v4.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.Solver;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class RubikStateSolver extends RubikStateAbstract
47
  {
48
  private static final int NUM_FACES   =  6;
49
  private static final int BITMAP_SIZE = 35;
50

    
51
  private static final int[] FACE_COLORS = new int[]
52
         {
53
           0xffffff00, 0xffffffff,
54
           0xff0000ff, 0xff00ff00,
55
           0xffff0000, 0xffb5651d
56
         };
57

    
58
  private static Bitmap[] mBitmap;
59
  private ImageButton[] mColorButton;
60
  private Button mBackButton, mSolveButton;
61
  private boolean mSolving;
62
  private int mCurrentColor;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  void leaveState(RubikActivity act)
67
    {
68

    
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  void enterState(final RubikActivity act)
74
    {
75
    mSolving = false;
76

    
77
    act.changeObject(RubikObjectList.CUBE,3,null);
78
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
79
    play.setObjectAndSize(RubikObjectList.CUBE,3);
80

    
81
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
82
    final float scale = metrics.density;
83

    
84
    // TOP ////////////////////////////
85
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
86
    layoutTop.removeAllViews();
87

    
88
    if( mBitmap     ==null ) setupBitmaps(scale);
89
    if( mColorButton==null ) setupColorButtons(act,scale);
90

    
91
    markButton(act);
92

    
93
    for(ImageButton button: mColorButton) layoutTop.addView(button);
94

    
95
    // BOT ////////////////////////////
96
    if( mSolveButton==null ) setupSolveButton(act,scale);
97

    
98
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
99
    layoutLeft.removeAllViews();
100
    layoutLeft.addView(mSolveButton);
101

    
102
    if( mBackButton==null ) setupBackButton(act,scale);
103

    
104
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
105
    layoutRight.removeAllViews();
106
    layoutRight.addView(mBackButton);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  private void setupBitmaps(float scale)
112
    {
113
    final int SIZE = (int)(scale*BITMAP_SIZE);
114
    final float R = SIZE*0.10f;
115
    final float M = SIZE*0.05f;
116

    
117
    mBitmap = new Bitmap[NUM_FACES];
118

    
119
    Paint paint = new Paint();
120
    paint.setColor(0xff008800);
121
    paint.setStyle(Paint.Style.FILL);
122

    
123
    paint.setAntiAlias(true);
124
    paint.setTextAlign(Paint.Align.CENTER);
125
    paint.setStyle(Paint.Style.FILL);
126

    
127
    for(int i=0; i<NUM_FACES; i++)
128
      {
129
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
130
      Canvas canvas = new Canvas(mBitmap[i]);
131

    
132
      paint.setColor(0xff000000);
133
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
134

    
135
      paint.setColor(FACE_COLORS[i]);
136
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
137
      }
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  private void setupColorButtons(final RubikActivity act, final float scale)
143
    {
144
    mColorButton = new ImageButton[NUM_FACES];
145

    
146
    for(int i=0; i<NUM_FACES; i++)
147
      {
148
      final int ii = i;
149
      int padding = (int)(3*scale + 0.5f);
150
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
151
      mColorButton[i] = new ImageButton(act);
152
      mColorButton[i].setLayoutParams(objectParams);
153
      mColorButton[i].setPadding(padding,0,padding,0);
154
      mColorButton[i].setImageBitmap(mBitmap[i]);
155

    
156
      mColorButton[i].setOnClickListener( new View.OnClickListener()
157
        {
158
        @Override
159
        public void onClick(View view)
160
          {
161
          mCurrentColor = ii;
162
          markButton(act);
163
          }
164
        });
165
      }
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void setupSolveButton(final RubikActivity act, final float scale)
171
    {
172
    int padding = (int)(3*scale + 0.5f);
173
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
174
    mSolveButton = new Button(act);
175
    mSolveButton.setLayoutParams(backParams);
176
    mSolveButton.setPadding(padding,0,padding,0);
177
    mSolveButton.setText(R.string.solve);
178

    
179
    mSolveButton.setOnClickListener( new View.OnClickListener()
180
      {
181
      @Override
182
      public void onClick(View v)
183
        {
184
        if( !mSolving )
185
          {
186
          mSolving = true;
187
          RubikObject object = act.getObject();
188
          String objectString = object.retObjectString();
189
          Solver solver = new Solver( act, objectString );
190
          solver.start();
191
          }
192
        }
193
      });
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  private void setupBackButton(final RubikActivity act, final float scale)
199
    {
200
    int padding = (int)(3*scale + 0.5f);
201
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
202
    mBackButton = new Button(act);
203
    mBackButton.setLayoutParams(backParams);
204
    mBackButton.setPadding(padding,0,padding,0);
205
    mBackButton.setText(R.string.back);
206

    
207
    mBackButton.setOnClickListener( new View.OnClickListener()
208
      {
209
      @Override
210
      public void onClick(View v)
211
        {
212
        RubikPostRender post = act.getPostRender();
213
        post.resetAllTextureMaps();
214
        RubikState.goBack(act);
215
        }
216
      });
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private void markButton(RubikActivity act)
222
    {
223
    for(int b=0; b<NUM_FACES; b++)
224
      {
225
      Drawable d = mColorButton[b].getBackground();
226

    
227
      if( b==mCurrentColor )
228
        {
229
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
230
        }
231
      else
232
        {
233
        d.clearColorFilter();
234
        }
235
      }
236
    }
237

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

    
240
  public void savePreferences(SharedPreferences.Editor editor)
241
    {
242
    mColorButton = null;
243
    mBackButton  = null;
244
    mSolveButton = null;
245

    
246
    editor.putInt("stateSolver_color", mCurrentColor);
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  public void restorePreferences(SharedPreferences preferences)
252
    {
253
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public int getCurrentColor()
259
    {
260
    return mCurrentColor;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  public void setSolved( final RubikActivity act, final int numMoves, final String moves)
266
    {
267
    mSolving = false;
268

    
269
    act.runOnUiThread(new Runnable()
270
      {
271
      @Override
272
      public void run()
273
        {
274
        RubikState.switchState(act,RubikState.SOLU);
275
        RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
276
        solution.setupMoves(act, numMoves, moves);
277
        }
278
      });
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public void displayErrorDialog( final RubikActivity act, String message)
284
    {
285
    mSolving = false;
286

    
287
    RubikDialogSolverError dialog = new RubikDialogSolverError();
288
    Bundle bundle = new Bundle();
289
    bundle.putString("error", message );
290
    dialog.setArguments(bundle);
291
    dialog.show( act.getSupportFragmentManager(), null);
292
    }
293
  }
(7-7/8)