Project

General

Profile

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

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

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.helpers.TransparentImageButton;
36
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
38
import org.distorted.main.RubikPreRender;
39
import org.distorted.objects.TwistyObject;
40
import org.distorted.objects.ObjectList;
41
import org.distorted.solvers.ImplementedSolversList;
42
import org.distorted.solvers.SolverMain;
43

    
44
import java.lang.ref.WeakReference;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  void leaveScreen(RubikActivity act)
63
    {
64

    
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  void enterScreen(final RubikActivity act)
70
    {
71
    float width = act.getScreenWidthInPixels();
72
    float heigh = act.getScreenHeightInPixels();
73

    
74
    int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
75
    int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
76

    
77
    mBitmapSize = Math.min(sizeV,sizeH);
78
    mWeakAct = new WeakReference<>(act);
79
    mSolving = false;
80

    
81
    ObjectList currentObject= ImplementedSolversList.getObject(0);
82
    int currentObjectSize   = ImplementedSolversList.getObjectSize(0);
83

    
84
    act.setupObject(currentObject, currentObjectSize, null);
85
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
86
    play.setObjectAndSize(act, currentObject, currentObjectSize);
87

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

    
91
    // TOP ////////////////////////////
92
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
93
    layoutTop.removeAllViews();
94

    
95
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
96

    
97
    LinearLayout layoutLeft = new LinearLayout(act);
98
    layoutLeft.setLayoutParams(paramsL);
99
    LinearLayout layoutMid = new LinearLayout(act);
100
    layoutMid.setLayoutParams(paramsL);
101
    LinearLayout layoutRight = new LinearLayout(act);
102
    layoutRight.setLayoutParams(paramsL);
103

    
104
    if( mNumFaces>0 )
105
      {
106
      setupBitmaps();
107
      setupColorButtons(act,width);
108
      markButton(act);
109
      }
110

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

    
113
    // BOT ////////////////////////////
114
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
115
    layoutBot.removeAllViews();
116

    
117
    setupSolveButton(act,width);
118
    setupBackButton(act,width);
119

    
120
    layoutLeft.addView(mSolveButton);
121
    layoutRight.addView(mBackButton);
122

    
123
    layoutBot.addView(layoutLeft);
124
    layoutBot.addView(layoutMid);
125
    layoutBot.addView(layoutRight);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

    
136
    mBitmap = new Bitmap[mNumFaces];
137

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

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

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

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  private void setupColorButtons(final RubikActivity act, final float width)
162
    {
163
    mColorButton = new ImageButton[mNumFaces];
164
    int padding = (int)(width*RubikActivity.PADDING);
165
    int margin  = (int)(width*RubikActivity.MARGIN);
166

    
167
    for(int i=0; i<mNumFaces; i++)
168
      {
169
      final int ii = i;
170
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
171
      params.topMargin    = margin;
172
      params.bottomMargin = margin;
173
      params.leftMargin   = margin;
174
      params.rightMargin  = margin;
175

    
176
      mColorButton[i] = new ImageButton(act);
177
      mColorButton[i].setLayoutParams(params);
178
      mColorButton[i].setPadding(padding,0,padding,0);
179
      mColorButton[i].setImageBitmap(mBitmap[i]);
180

    
181
      mColorButton[i].setOnClickListener( new View.OnClickListener()
182
        {
183
        @Override
184
        public void onClick(View view)
185
          {
186
          mCurrentColor = ii;
187
          markButton(act);
188
          }
189
        });
190
      }
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  private void setupSolveButton(final RubikActivity act, final float width)
196
    {
197
    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);
198
    mSolveButton = new TransparentImageButton(act,icon,width, LinearLayout.LayoutParams.MATCH_PARENT);
199

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private void setupBackButton(final RubikActivity act, final float width)
219
    {
220
    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);
221
    mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
222

    
223
    mBackButton.setOnClickListener( new View.OnClickListener()
224
      {
225
      @Override
226
      public void onClick(View v)
227
        {
228
        RubikPreRender pre = act.getPreRender();
229
        pre.resetAllTextureMaps();
230
        ScreenList.goBack(act);
231
        }
232
      });
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public void savePreferences(SharedPreferences.Editor editor)
257
    {
258
    editor.putInt("stateSolver_color", mCurrentColor);
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

    
282
    if( act!=null )
283
      {
284
      act.runOnUiThread(new Runnable()
285
        {
286
        @Override
287
        public void run()
288
          {
289
          ScreenList.switchScreen(act, ScreenList.SOLU);
290
          RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
291
          solution.setupMoves(act, moves);
292
          }
293
        });
294
      }
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

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

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