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 |
27a70eae
|
Leszek Koltunski
|
private static final int DEF_BUTTON = RubikObjectList.CUBE3.ordinal();
|
47 |
211b48f2
|
Leszek Koltunski
|
|
48 |
|
|
private HorizontalNumberPicker mPicker;
|
49 |
|
|
private int mPickerValue;
|
50 |
8e1231ae
|
Leszek Koltunski
|
private int mButton = DEF_BUTTON;
|
51 |
211b48f2
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
|
54 |
329c0aeb
|
Leszek Koltunski
|
void leaveState(RubikActivity act)
|
55 |
211b48f2
|
Leszek Koltunski
|
{
|
56 |
|
|
mPickerValue = mPicker.getValue();
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60 |
|
|
|
61 |
329c0aeb
|
Leszek Koltunski
|
void enterState(RubikActivity act)
|
62 |
211b48f2
|
Leszek Koltunski
|
{
|
63 |
|
|
LayoutInflater inflater = act.getLayoutInflater();
|
64 |
|
|
|
65 |
|
|
// TOP ////////////////////////////
|
66 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
|
67 |
|
|
layoutTop.removeAllViews();
|
68 |
|
|
|
69 |
|
|
final View viewTop = inflater.inflate(R.layout.play_title, null);
|
70 |
|
|
layoutTop.addView(viewTop);
|
71 |
|
|
|
72 |
|
|
// BOT ////////////////////////////
|
73 |
|
|
LinearLayout layoutBot = act.findViewById(R.id.mainBar);
|
74 |
|
|
layoutBot.removeAllViews();
|
75 |
|
|
|
76 |
|
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
77 |
|
|
float scale = metrics.density;
|
78 |
|
|
int size = (int)(60*scale +0.5f);
|
79 |
|
|
int padding = (int)(3*scale + 0.5f);
|
80 |
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
|
81 |
|
|
|
82 |
27a70eae
|
Leszek Koltunski
|
for(int i = 0; i< RubikObjectList.LENGTH; i++)
|
83 |
211b48f2
|
Leszek Koltunski
|
{
|
84 |
|
|
ImageButton button = new ImageButton(act);
|
85 |
|
|
button.setLayoutParams(params);
|
86 |
|
|
button.setId(i);
|
87 |
|
|
button.setPadding(padding,0,padding,0);
|
88 |
27a70eae
|
Leszek Koltunski
|
int iconID = RubikObjectList.getObject(i).getIconID();
|
89 |
211b48f2
|
Leszek Koltunski
|
button.setImageResource(iconID);
|
90 |
|
|
button.setOnClickListener(act);
|
91 |
|
|
layoutBot.addView(button);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,size);
|
95 |
|
|
|
96 |
|
|
Button button = new Button(act);
|
97 |
|
|
button.setLayoutParams(params2);
|
98 |
|
|
button.setId(BUTTON_ID_BACK);
|
99 |
|
|
button.setPadding(padding,0,padding,0);
|
100 |
|
|
button.setText(R.string.back);
|
101 |
|
|
button.setOnClickListener(act);
|
102 |
|
|
layoutBot.addView(button);
|
103 |
|
|
|
104 |
|
|
markButton(act,mButton);
|
105 |
|
|
|
106 |
|
|
mPicker = act.findViewById(R.id.rubikNumberPicker);
|
107 |
|
|
|
108 |
|
|
if( mPicker!=null )
|
109 |
|
|
{
|
110 |
|
|
mPicker.setMin(MIN_SCRAMBLE);
|
111 |
|
|
mPicker.setMax(MAX_SCRAMBLE);
|
112 |
|
|
mPicker.setValue(mPickerValue);
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117 |
|
|
|
118 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
119 |
|
|
{
|
120 |
|
|
if( mPicker!=null )
|
121 |
|
|
{
|
122 |
|
|
editor.putInt("scramble", mPicker.getValue() );
|
123 |
|
|
}
|
124 |
8e1231ae
|
Leszek Koltunski
|
|
125 |
|
|
editor.putInt("button", mButton);
|
126 |
211b48f2
|
Leszek Koltunski
|
}
|
127 |
|
|
|
128 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129 |
|
|
|
130 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
131 |
|
|
{
|
132 |
|
|
mPickerValue= preferences.getInt("scramble", DEF_SCRAMBLE);
|
133 |
8e1231ae
|
Leszek Koltunski
|
mButton = preferences.getInt("button" , DEF_BUTTON );
|
134 |
211b48f2
|
Leszek Koltunski
|
}
|
135 |
|
|
|
136 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137 |
|
|
|
138 |
|
|
public int getPicker()
|
139 |
|
|
{
|
140 |
|
|
return mPicker!=null ? mPicker.getValue() : 1;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144 |
|
|
|
145 |
|
|
public int getButton()
|
146 |
|
|
{
|
147 |
|
|
return mButton;
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151 |
|
|
|
152 |
|
|
public void markButton(RubikActivity act, int button)
|
153 |
|
|
{
|
154 |
|
|
mButton = button;
|
155 |
|
|
|
156 |
27a70eae
|
Leszek Koltunski
|
for(int b = 0; b< RubikObjectList.LENGTH; b++)
|
157 |
211b48f2
|
Leszek Koltunski
|
{
|
158 |
|
|
Drawable d = act.findViewById(b).getBackground();
|
159 |
|
|
|
160 |
|
|
if( b==button )
|
161 |
|
|
{
|
162 |
|
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
163 |
|
|
}
|
164 |
|
|
else
|
165 |
|
|
{
|
166 |
|
|
d.clearColorFilter();
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
}
|