Project

General

Profile

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

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

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 java.lang.ref.WeakReference;
23

    
24
import android.content.SharedPreferences;
25
import android.graphics.Bitmap;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28
import android.graphics.PorterDuff;
29
import android.graphics.drawable.Drawable;
30
import android.os.Bundle;
31
import androidx.core.content.ContextCompat;
32
import android.view.View;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35

    
36
import org.distorted.objectlib.main.ObjectControl;
37
import org.distorted.objectlib.main.TwistyObject;
38

    
39
import org.distorted.dialogs.RubikDialogSolverError;
40
import org.distorted.helpers.TransparentImageButton;
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.objects.RubikObjectList;
44
import org.distorted.solvers.ImplementedSolversList;
45
import org.distorted.solvers.SolverMain;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class RubikScreenSolver extends RubikScreenAbstract
50
  {
51
  private static Bitmap[] mBitmap;
52
  private ImageButton[] mColorButton;
53
  private TransparentImageButton mBackButton, mSolveButton;
54
  private boolean mSolving;
55
  private int mCurrentColor;
56
  private int[] mFaceColors;
57
  private int mNumFaces;
58
  private float mBitmapSize;
59
  private WeakReference<RubikActivity> mWeakAct;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  void leaveScreen(RubikActivity act)
64
    {
65
    ObjectControl control = act.getControl();
66
    control.unsetLock();
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  void enterScreen(final RubikActivity act)
72
    {
73
    ObjectControl control = act.getControl();
74
    control.setLock(false);
75

    
76
    float width = act.getScreenWidthInPixels();
77
    float heigh = act.getScreenHeightInPixels();
78

    
79
    int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
80
    int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
81

    
82
    mBitmapSize = Math.min(sizeV,sizeH);
83
    mWeakAct = new WeakReference<>(act);
84
    mSolving = false;
85

    
86
    int currentObject= ImplementedSolversList.getObject(0);
87
    act.changeIfDifferent(currentObject,control);
88
    control.solveOnly();
89

    
90
    RubikObjectList.setCurrObject(currentObject);
91

    
92
    generateFaceColors();
93

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

    
98
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
99

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

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

    
114
    for(ImageButton button: mColorButton) layoutTop.addView(button);
115

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

    
120
    setupSolveButton(act);
121
    setupBackButton(act);
122

    
123
    layoutLeft.addView(mSolveButton);
124
    layoutRight.addView(mBackButton);
125

    
126
    layoutBot.addView(layoutLeft);
127
    layoutBot.addView(layoutMid);
128
    layoutBot.addView(layoutRight);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public void generateFaceColors()
134
    {
135
    mFaceColors= new int[] {  TwistyObject.COLOR_YELLOW,
136
                              TwistyObject.COLOR_WHITE,
137
                              TwistyObject.COLOR_BLUE ,
138
                              TwistyObject.COLOR_GREEN,
139
                              TwistyObject.COLOR_RED   ,
140
                              TwistyObject.COLOR_ORANGE };
141
    mNumFaces = 6;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  private void setupBitmaps()
147
    {
148
    final int SIZE = (int)mBitmapSize;
149
    final float R = SIZE*0.15f;
150
    final float M = SIZE*0.08f;
151

    
152
    mBitmap = new Bitmap[mNumFaces];
153

    
154
    Paint paint = new Paint();
155
    paint.setColor(0xff008800);
156
    paint.setStyle(Paint.Style.FILL);
157

    
158
    paint.setAntiAlias(true);
159
    paint.setTextAlign(Paint.Align.CENTER);
160
    paint.setStyle(Paint.Style.FILL);
161

    
162
    for(int i=0; i<mNumFaces; i++)
163
      {
164
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
165
      Canvas canvas = new Canvas(mBitmap[i]);
166

    
167
      paint.setColor(0xff000000);
168
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
169

    
170
      paint.setColor(mFaceColors[i]);
171
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  private void setupColorButtons(final RubikActivity act, final float width)
178
    {
179
    mColorButton = new ImageButton[mNumFaces];
180
    int padding = (int)(width*RubikActivity.PADDING);
181
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
182

    
183
    for(int i=0; i<mNumFaces; i++)
184
      {
185
      final int ii = i;
186
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
187
      params.topMargin    = margin;
188
      params.bottomMargin = margin;
189
      params.leftMargin   = margin;
190
      params.rightMargin  = margin;
191

    
192
      mColorButton[i] = new ImageButton(act);
193
      mColorButton[i].setLayoutParams(params);
194
      mColorButton[i].setPadding(padding,0,padding,0);
195
      mColorButton[i].setImageBitmap(mBitmap[i]);
196

    
197
      mColorButton[i].setOnClickListener( new View.OnClickListener()
198
        {
199
        @Override
200
        public void onClick(View view)
201
          {
202
          mCurrentColor = ii;
203
          markButton(act);
204
          }
205
        });
206
      }
207
    }
208

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

    
211
  private void setupSolveButton(final RubikActivity act)
212
    {
213
    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);
214
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
215
    mSolveButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
216

    
217
    mSolveButton.setOnClickListener( new View.OnClickListener()
218
      {
219
      @Override
220
      public void onClick(View v)
221
        {
222
        if( !mSolving )
223
          {
224
          mSolving = true;
225
          TwistyObject object = act.getObject();
226
          SolverMain solver = new SolverMain( act.getResources(), object );
227
          solver.start();
228
          }
229
        }
230
      });
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  private void setupBackButton(final RubikActivity act)
236
    {
237
    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);
238
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
239
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
240

    
241
    mBackButton.setOnClickListener( new View.OnClickListener()
242
      {
243
      @Override
244
      public void onClick(View v)
245
        {
246
        ObjectControl control = act.getControl();
247
        control.resetAllTextureMaps();
248
        ScreenList.goBack(act);
249
        }
250
      });
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  private void markButton(RubikActivity act)
256
    {
257
    for(int b=0; b<mNumFaces; b++)
258
      {
259
      Drawable d = mColorButton[b].getBackground();
260

    
261
      if( b==mCurrentColor )
262
        {
263
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
264
        }
265
      else
266
        {
267
        d.clearColorFilter();
268
        }
269
      }
270
    }
271

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

    
274
  public void savePreferences(SharedPreferences.Editor editor)
275
    {
276
    editor.putInt("stateSolver_color", mCurrentColor);
277
    }
278

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

    
281
  public void restorePreferences(SharedPreferences preferences)
282
    {
283
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  public int getCurrentColor()
289
    {
290
    return mCurrentColor;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  public void setSolved(final String moves)
296
    {
297
    mSolving = false;
298
    final RubikActivity act = mWeakAct.get();
299

    
300
    if( act!=null )
301
      {
302
      act.runOnUiThread(new Runnable()
303
        {
304
        @Override
305
        public void run()
306
          {
307
          ScreenList.switchScreen(act, ScreenList.SOLU);
308
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
309
          solution.setupMoves(act, moves);
310
          }
311
        });
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public void displayErrorDialog( String message)
318
    {
319
    mSolving = false;
320
    RubikActivity act = mWeakAct.get();
321

    
322
    if( act!=null )
323
      {
324
      RubikDialogSolverError dialog = new RubikDialogSolverError();
325
      Bundle bundle = new Bundle();
326
      bundle.putString("error", message );
327
      dialog.setArguments(bundle);
328
      dialog.show( act.getSupportFragmentManager(), null);
329
      }
330
    }
331
  }
(9-9/11)