Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenSolver.java @ fcd5b990

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.screens;
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.view.View;
31
import android.widget.ImageButton;
32
import android.widget.LinearLayout;
33

    
34
import org.distorted.dialogs.RubikDialogSolverError;
35
import org.distorted.main.R;
36
import org.distorted.main.RubikActivity;
37
import org.distorted.main.RubikPreRender;
38
import org.distorted.objects.TwistyObject;
39
import org.distorted.objects.ObjectList;
40
import org.distorted.solvers.ImplementedSolversList;
41
import org.distorted.solvers.SolverMain;
42

    
43
import java.lang.ref.WeakReference;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class RubikScreenSolver extends RubikScreenAbstract
48
  {
49
  private static Bitmap[] mBitmap;
50
  private ImageButton[] mColorButton;
51
  private ImageButton mBackButton, mSolveButton;
52
  private boolean mSolving;
53
  private int mCurrentColor;
54
  private int[] mFaceColors;
55
  private int mNumFaces;
56
  private float mBitmapSize;
57

    
58
  private ObjectList mCurrentObject;
59
  private int mCurrentObjectSize;
60

    
61
  private WeakReference<RubikActivity> mWeakAct;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  void leaveState(RubikActivity act)
66
    {
67

    
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  void enterState(final RubikActivity act)
73
    {
74
    float width = act.getScreenWidthInPixels();
75
    float heigh = act.getScreenHeightInPixels();
76

    
77
    int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
78
    int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
79

    
80
    mBitmapSize = Math.min(sizeV,sizeH);
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
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getStateClass();
91
    play.setObjectAndSize(act, mCurrentObject, mCurrentObjectSize);
92

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

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

    
100
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
101

    
102
    LinearLayout layoutLeft = new LinearLayout(act);
103
    layoutLeft.setLayoutParams(paramsL);
104
    LinearLayout layoutMid = new LinearLayout(act);
105
    layoutMid.setLayoutParams(paramsL);
106
    LinearLayout layoutRight = new LinearLayout(act);
107
    layoutRight.setLayoutParams(paramsL);
108

    
109
    if( mNumFaces>0 )
110
      {
111
      setupBitmaps();
112
      setupColorButtons(act,width);
113
      markButton(act);
114
      }
115

    
116
    for(ImageButton button: mColorButton) layoutTop.addView(button);
117

    
118
    // BOT ////////////////////////////
119
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
120
    layoutBot.removeAllViews();
121

    
122
    setupSolveButton(act,width);
123
    setupBackButton(act,width);
124

    
125
    layoutLeft.addView(mSolveButton);
126
    layoutRight.addView(mBackButton);
127

    
128
    layoutBot.addView(layoutLeft);
129
    layoutBot.addView(layoutMid);
130
    layoutBot.addView(layoutRight);
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private void setupBitmaps()
136
    {
137
    final int SIZE = (int)mBitmapSize;
138
    final float R = SIZE*0.15f;
139
    final float M = SIZE*0.08f;
140

    
141
    mBitmap = new Bitmap[mNumFaces];
142

    
143
    Paint paint = new Paint();
144
    paint.setColor(0xff008800);
145
    paint.setStyle(Paint.Style.FILL);
146

    
147
    paint.setAntiAlias(true);
148
    paint.setTextAlign(Paint.Align.CENTER);
149
    paint.setStyle(Paint.Style.FILL);
150

    
151
    for(int i=0; i<mNumFaces; i++)
152
      {
153
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
154
      Canvas canvas = new Canvas(mBitmap[i]);
155

    
156
      paint.setColor(0xff000000);
157
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
158

    
159
      paint.setColor(mFaceColors[i]);
160
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
161
      }
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void setupColorButtons(final RubikActivity act, final float width)
167
    {
168
    mColorButton = new ImageButton[mNumFaces];
169
    int padding = (int)(width*RubikActivity.PADDING);
170
    int margin  = (int)(width*RubikActivity.MARGIN);
171

    
172
    for(int i=0; i<mNumFaces; i++)
173
      {
174
      final int ii = i;
175
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
176
      params.topMargin    = margin;
177
      params.bottomMargin = margin;
178
      params.leftMargin   = margin;
179
      params.rightMargin  = margin;
180

    
181
      mColorButton[i] = new ImageButton(act);
182
      mColorButton[i].setLayoutParams(params);
183
      mColorButton[i].setPadding(padding,0,padding,0);
184
      mColorButton[i].setImageBitmap(mBitmap[i]);
185

    
186
      mColorButton[i].setOnClickListener( new View.OnClickListener()
187
        {
188
        @Override
189
        public void onClick(View view)
190
          {
191
          mCurrentColor = ii;
192
          markButton(act);
193
          }
194
        });
195
      }
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void setupSolveButton(final RubikActivity act, final float width)
201
    {
202
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_solve,R.drawable.ui_medium_solve, R.drawable.ui_big_solve, R.drawable.ui_huge_solve);
203
    mSolveButton = new TransparentImageButton(act,icon,width, LinearLayout.LayoutParams.MATCH_PARENT);
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
          TwistyObject 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
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
227
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
228

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public int getCurrentColor()
277
    {
278
    return mCurrentColor;
279
    }
280

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

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

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

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

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

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