1 |
211b48f2
|
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.PorterDuff;
|
24 |
|
|
import android.graphics.drawable.Drawable;
|
25 |
|
|
import android.support.v4.content.ContextCompat;
|
26 |
|
|
import android.util.DisplayMetrics;
|
27 |
|
|
import android.view.LayoutInflater;
|
28 |
|
|
import android.view.View;
|
29 |
|
|
import android.view.ViewGroup;
|
30 |
|
|
import android.widget.Button;
|
31 |
|
|
import android.widget.ImageButton;
|
32 |
|
|
import android.widget.LinearLayout;
|
33 |
|
|
|
34 |
|
|
import org.distorted.component.HorizontalNumberPicker;
|
35 |
|
|
import org.distorted.magic.R;
|
36 |
|
|
import org.distorted.magic.RubikActivity;
|
37 |
27a70eae
|
Leszek Koltunski
|
import org.distorted.object.RubikObjectList;
|
38 |
211b48f2
|
Leszek Koltunski
|
|
39 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40 |
|
|
|
41 |
|
|
public class RubikStatePlay extends RubikStateAbstract
|
42 |
|
|
{
|
43 |
|
|
private static final int MIN_SCRAMBLE = 1;
|
44 |
|
|
private static final int DEF_SCRAMBLE = 1;
|
45 |
|
|
public static final int MAX_SCRAMBLE = 18;
|
46 |
4888e97c
|
Leszek Koltunski
|
private static final int DEF_OBJECT = RubikObjectList.CUBE.ordinal();
|
47 |
|
|
private static final int DEF_SIZE = 1; // i.e. the second from the list of CUBE's sizes
|
48 |
211b48f2
|
Leszek Koltunski
|
|
49 |
|
|
private HorizontalNumberPicker mPicker;
|
50 |
|
|
private int mPickerValue;
|
51 |
4888e97c
|
Leszek Koltunski
|
private int mObject = DEF_OBJECT;
|
52 |
|
|
private int mSize = DEF_SIZE;
|
53 |
211b48f2
|
Leszek Koltunski
|
|
54 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55 |
|
|
|
56 |
329c0aeb
|
Leszek Koltunski
|
void leaveState(RubikActivity act)
|
57 |
211b48f2
|
Leszek Koltunski
|
{
|
58 |
|
|
mPickerValue = mPicker.getValue();
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
|
|
|
63 |
329c0aeb
|
Leszek Koltunski
|
void enterState(RubikActivity act)
|
64 |
211b48f2
|
Leszek Koltunski
|
{
|
65 |
|
|
LayoutInflater inflater = act.getLayoutInflater();
|
66 |
|
|
|
67 |
|
|
// TOP ////////////////////////////
|
68 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
|
69 |
|
|
layoutTop.removeAllViews();
|
70 |
|
|
|
71 |
|
|
final View viewTop = inflater.inflate(R.layout.play_title, null);
|
72 |
|
|
layoutTop.addView(viewTop);
|
73 |
|
|
|
74 |
|
|
// BOT ////////////////////////////
|
75 |
|
|
LinearLayout layoutBot = act.findViewById(R.id.mainBar);
|
76 |
|
|
layoutBot.removeAllViews();
|
77 |
|
|
|
78 |
|
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
79 |
|
|
float scale = metrics.density;
|
80 |
|
|
int size = (int)(60*scale +0.5f);
|
81 |
|
|
int padding = (int)(3*scale + 0.5f);
|
82 |
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
|
83 |
|
|
|
84 |
4888e97c
|
Leszek Koltunski
|
for(int i=0; i< RubikObjectList.NUM_OBJECTS; i++)
|
85 |
211b48f2
|
Leszek Koltunski
|
{
|
86 |
4888e97c
|
Leszek Koltunski
|
int[] iconIDs = RubikObjectList.getObject(i).getIconIDs();
|
87 |
|
|
int len = iconIDs.length;
|
88 |
|
|
|
89 |
|
|
for(int s=0; s<len; s++)
|
90 |
|
|
{
|
91 |
|
|
ImageButton button = new ImageButton(act);
|
92 |
|
|
button.setLayoutParams(params);
|
93 |
|
|
button.setId(RubikObjectList.pack(i,s));
|
94 |
|
|
button.setPadding(padding,0,padding,0);
|
95 |
|
|
button.setImageResource(iconIDs[s]);
|
96 |
|
|
button.setOnClickListener(act);
|
97 |
|
|
layoutBot.addView(button);
|
98 |
|
|
}
|
99 |
211b48f2
|
Leszek Koltunski
|
}
|
100 |
|
|
|
101 |
|
|
ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
|
102 |
|
|
|
103 |
|
|
Button button = new Button(act);
|
104 |
|
|
button.setLayoutParams(params2);
|
105 |
|
|
button.setId(BUTTON_ID_BACK);
|
106 |
|
|
button.setPadding(padding,0,padding,0);
|
107 |
|
|
button.setText(R.string.back);
|
108 |
|
|
button.setOnClickListener(act);
|
109 |
|
|
layoutBot.addView(button);
|
110 |
|
|
|
111 |
4888e97c
|
Leszek Koltunski
|
markButton(act,mObject,mSize);
|
112 |
211b48f2
|
Leszek Koltunski
|
|
113 |
|
|
mPicker = act.findViewById(R.id.rubikNumberPicker);
|
114 |
|
|
|
115 |
|
|
if( mPicker!=null )
|
116 |
|
|
{
|
117 |
|
|
mPicker.setMin(MIN_SCRAMBLE);
|
118 |
|
|
mPicker.setMax(MAX_SCRAMBLE);
|
119 |
|
|
mPicker.setValue(mPickerValue);
|
120 |
|
|
}
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124 |
|
|
|
125 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
126 |
|
|
{
|
127 |
|
|
if( mPicker!=null )
|
128 |
|
|
{
|
129 |
4888e97c
|
Leszek Koltunski
|
editor.putInt("statePlay_scramble", mPicker.getValue() );
|
130 |
211b48f2
|
Leszek Koltunski
|
}
|
131 |
8e1231ae
|
Leszek Koltunski
|
|
132 |
4888e97c
|
Leszek Koltunski
|
editor.putInt("statePlay_object", mObject);
|
133 |
|
|
editor.putInt("statePlay_size" , mSize);
|
134 |
211b48f2
|
Leszek Koltunski
|
}
|
135 |
|
|
|
136 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137 |
|
|
|
138 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
139 |
|
|
{
|
140 |
4888e97c
|
Leszek Koltunski
|
mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
|
141 |
|
|
mObject = preferences.getInt("statePlay_object" , DEF_OBJECT );
|
142 |
|
|
mSize = preferences.getInt("statePlay_size" , DEF_SIZE );
|
143 |
211b48f2
|
Leszek Koltunski
|
}
|
144 |
|
|
|
145 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146 |
|
|
|
147 |
|
|
public int getPicker()
|
148 |
|
|
{
|
149 |
|
|
return mPicker!=null ? mPicker.getValue() : 1;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153 |
|
|
|
154 |
4888e97c
|
Leszek Koltunski
|
public int getObject()
|
155 |
211b48f2
|
Leszek Koltunski
|
{
|
156 |
4888e97c
|
Leszek Koltunski
|
return mObject;
|
157 |
211b48f2
|
Leszek Koltunski
|
}
|
158 |
|
|
|
159 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160 |
|
|
|
161 |
4888e97c
|
Leszek Koltunski
|
public int getSize()
|
162 |
211b48f2
|
Leszek Koltunski
|
{
|
163 |
4888e97c
|
Leszek Koltunski
|
return mSize;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167 |
|
|
|
168 |
|
|
public void markButton(RubikActivity act, int object, int size)
|
169 |
|
|
{
|
170 |
|
|
mObject = object;
|
171 |
|
|
mSize = size;
|
172 |
|
|
|
173 |
|
|
int lookingFor = RubikObjectList.pack(object,size);
|
174 |
|
|
int len = RubikObjectList.getTotal();
|
175 |
211b48f2
|
Leszek Koltunski
|
|
176 |
4888e97c
|
Leszek Koltunski
|
for(int button=0; button<len; button++)
|
177 |
211b48f2
|
Leszek Koltunski
|
{
|
178 |
4888e97c
|
Leszek Koltunski
|
Drawable d = act.findViewById(button).getBackground();
|
179 |
211b48f2
|
Leszek Koltunski
|
|
180 |
4888e97c
|
Leszek Koltunski
|
if( button==lookingFor )
|
181 |
211b48f2
|
Leszek Koltunski
|
{
|
182 |
|
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
183 |
|
|
}
|
184 |
|
|
else
|
185 |
|
|
{
|
186 |
|
|
d.clearColorFilter();
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
}
|