1 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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.uistate;
|
21 |
|
|
|
22 |
|
|
import android.content.SharedPreferences;
|
23 |
|
|
import android.graphics.Bitmap;
|
24 |
|
|
import android.graphics.Canvas;
|
25 |
|
|
import android.graphics.Paint;
|
26 |
|
|
import android.util.DisplayMetrics;
|
27 |
|
|
import android.view.View;
|
28 |
|
|
import android.widget.Button;
|
29 |
|
|
import android.widget.ImageButton;
|
30 |
|
|
import android.widget.LinearLayout;
|
31 |
|
|
|
32 |
|
|
import org.distorted.magic.R;
|
33 |
|
|
import org.distorted.magic.RubikActivity;
|
34 |
|
|
import org.distorted.object.RubikObjectList;
|
35 |
|
|
|
36 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
|
38 |
|
|
public class RubikStateSolver extends RubikStateAbstract
|
39 |
|
|
{
|
40 |
|
|
private static final int NUM_FACES = 6;
|
41 |
|
|
private static final int BITMAP_SIZE = 35;
|
42 |
|
|
|
43 |
|
|
private static final int[] FACE_COLORS = new int[]
|
44 |
|
|
{
|
45 |
|
|
0xffffff00, 0xffffffff,
|
46 |
|
|
0xff0000ff, 0xff00ff00,
|
47 |
|
|
0xffff0000, 0xffb5651d
|
48 |
|
|
};
|
49 |
|
|
|
50 |
|
|
private static Bitmap[] mBitmap;
|
51 |
|
|
private ImageButton[] mColorButton;
|
52 |
264af0ad
|
Leszek Koltunski
|
private Button mBackButton, mSolveButton;
|
53 |
7289fd6c
|
Leszek Koltunski
|
|
54 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55 |
|
|
|
56 |
|
|
void leaveState(RubikActivity act)
|
57 |
|
|
{
|
58 |
|
|
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
|
|
|
63 |
|
|
void enterState(final RubikActivity act)
|
64 |
|
|
{
|
65 |
|
|
act.changeObject(RubikObjectList.CUBE,3,null);
|
66 |
|
|
|
67 |
|
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
68 |
|
|
final float scale = metrics.density;
|
69 |
|
|
|
70 |
|
|
// TOP ////////////////////////////
|
71 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
72 |
|
|
layoutTop.removeAllViews();
|
73 |
|
|
|
74 |
|
|
if( mBitmap ==null ) setupBitmaps(scale);
|
75 |
|
|
if( mColorButton==null ) setupColorButtons(act,scale);
|
76 |
|
|
|
77 |
|
|
for(ImageButton button: mColorButton) layoutTop.addView(button);
|
78 |
|
|
|
79 |
|
|
// BOT ////////////////////////////
|
80 |
264af0ad
|
Leszek Koltunski
|
if( mSolveButton==null ) setupSolveButton(act,scale);
|
81 |
|
|
|
82 |
|
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
83 |
|
|
layoutLeft.removeAllViews();
|
84 |
|
|
layoutLeft.addView(mSolveButton);
|
85 |
|
|
|
86 |
7289fd6c
|
Leszek Koltunski
|
if( mBackButton==null ) setupBackButton(act,scale);
|
87 |
|
|
|
88 |
|
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
89 |
|
|
layoutRight.removeAllViews();
|
90 |
|
|
layoutRight.addView(mBackButton);
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
//createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
|
95 |
|
|
|
96 |
|
|
private void setupBitmaps(float scale)
|
97 |
|
|
{
|
98 |
|
|
final int SIZE = (int)(scale*BITMAP_SIZE);
|
99 |
|
|
final float R = SIZE*0.10f;
|
100 |
|
|
final float M = SIZE*0.05f;
|
101 |
|
|
|
102 |
|
|
mBitmap = new Bitmap[NUM_FACES];
|
103 |
|
|
|
104 |
|
|
Paint paint = new Paint();
|
105 |
|
|
paint.setColor(0xff008800);
|
106 |
|
|
paint.setStyle(Paint.Style.FILL);
|
107 |
|
|
|
108 |
|
|
paint.setAntiAlias(true);
|
109 |
|
|
paint.setTextAlign(Paint.Align.CENTER);
|
110 |
|
|
paint.setStyle(Paint.Style.FILL);
|
111 |
|
|
|
112 |
|
|
for(int i=0; i<NUM_FACES; i++)
|
113 |
|
|
{
|
114 |
|
|
mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
|
115 |
|
|
Canvas canvas = new Canvas(mBitmap[i]);
|
116 |
|
|
|
117 |
|
|
paint.setColor(0xff000000);
|
118 |
|
|
canvas.drawRect(0, 0, SIZE, SIZE, paint);
|
119 |
|
|
|
120 |
|
|
paint.setColor(FACE_COLORS[i]);
|
121 |
|
|
canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126 |
|
|
|
127 |
|
|
private void setupColorButtons(final RubikActivity act, final float scale)
|
128 |
|
|
{
|
129 |
|
|
mColorButton = new ImageButton[NUM_FACES];
|
130 |
|
|
|
131 |
|
|
for(int i=0; i<NUM_FACES; i++)
|
132 |
|
|
{
|
133 |
|
|
final int ii = i;
|
134 |
|
|
int padding = (int)(3*scale + 0.5f);
|
135 |
|
|
LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
136 |
|
|
mColorButton[i] = new ImageButton(act);
|
137 |
|
|
mColorButton[i].setLayoutParams(objectParams);
|
138 |
|
|
mColorButton[i].setPadding(padding,0,padding,0);
|
139 |
|
|
mColorButton[i].setImageBitmap(mBitmap[i]);
|
140 |
|
|
|
141 |
|
|
mColorButton[i].setOnClickListener( new View.OnClickListener()
|
142 |
|
|
{
|
143 |
|
|
@Override
|
144 |
|
|
public void onClick(View view)
|
145 |
|
|
{
|
146 |
|
|
android.util.Log.e("solver", "button "+FACE_COLORS[ii]+" clicked");
|
147 |
|
|
}
|
148 |
|
|
});
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
|
152 |
264af0ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153 |
|
|
|
154 |
|
|
private void setupSolveButton(final RubikActivity act, final float scale)
|
155 |
|
|
{
|
156 |
|
|
int padding = (int)(3*scale + 0.5f);
|
157 |
|
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
158 |
|
|
mSolveButton = new Button(act);
|
159 |
|
|
mSolveButton.setLayoutParams(backParams);
|
160 |
|
|
mSolveButton.setPadding(padding,0,padding,0);
|
161 |
|
|
mSolveButton.setText(R.string.solve);
|
162 |
|
|
|
163 |
|
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
164 |
|
|
{
|
165 |
|
|
@Override
|
166 |
|
|
public void onClick(View v)
|
167 |
|
|
{
|
168 |
|
|
RubikState.switchState(act,RubikState.SOLU);
|
169 |
|
|
}
|
170 |
|
|
});
|
171 |
|
|
}
|
172 |
|
|
|
173 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174 |
|
|
|
175 |
|
|
private void setupBackButton(final RubikActivity act, final float scale)
|
176 |
|
|
{
|
177 |
|
|
int padding = (int)(3*scale + 0.5f);
|
178 |
|
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
179 |
|
|
mBackButton = new Button(act);
|
180 |
|
|
mBackButton.setLayoutParams(backParams);
|
181 |
|
|
mBackButton.setPadding(padding,0,padding,0);
|
182 |
|
|
mBackButton.setText(R.string.back);
|
183 |
|
|
|
184 |
|
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
185 |
|
|
{
|
186 |
|
|
@Override
|
187 |
|
|
public void onClick(View v)
|
188 |
|
|
{
|
189 |
|
|
RubikState.goBack(act);
|
190 |
|
|
}
|
191 |
|
|
});
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195 |
|
|
|
196 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
197 |
|
|
{
|
198 |
|
|
mColorButton = null;
|
199 |
|
|
mBackButton = null;
|
200 |
811b0446
|
Leszek Koltunski
|
mSolveButton = null;
|
201 |
7289fd6c
|
Leszek Koltunski
|
}
|
202 |
|
|
|
203 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204 |
|
|
|
205 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
206 |
|
|
{
|
207 |
|
|
|
208 |
|
|
}
|
209 |
|
|
}
|