Revision f3e12931
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikActivity.java | ||
---|---|---|
27 | 27 |
import android.preference.PreferenceManager; |
28 | 28 |
import android.support.v4.content.ContextCompat; |
29 | 29 |
import android.support.v7.app.AppCompatActivity; |
30 |
import android.util.DisplayMetrics; |
|
30 | 31 |
import android.view.View; |
32 |
import android.view.ViewGroup; |
|
33 |
import android.widget.ImageButton; |
|
34 |
import android.widget.LinearLayout; |
|
31 | 35 |
|
32 | 36 |
import org.distorted.component.HorizontalNumberPicker; |
33 | 37 |
import org.distorted.library.main.DistortedLibrary; |
... | ... | |
35 | 39 |
|
36 | 40 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
37 | 41 |
|
38 |
public class RubikActivity extends AppCompatActivity |
|
42 |
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
|
|
39 | 43 |
{ |
40 |
static final int DEFAULT_SIZE = 3; |
|
41 |
static final int SMALLEST_SIZE = 2; |
|
42 |
|
|
43 | 44 |
public static final int MIN_SCRAMBLE = 1; |
44 | 45 |
public static final int DEF_SCRAMBLE = 1; |
45 | 46 |
public static final int MAX_SCRAMBLE = 17; |
46 | 47 |
|
47 |
private static int mSize = DEFAULT_SIZE; |
|
48 |
private static int mSize = RubikSize.DEFAULT_SIZE;
|
|
48 | 49 |
private HorizontalNumberPicker mPicker; |
49 | 50 |
|
50 | 51 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
55 | 56 |
|
56 | 57 |
for(int b=0; b<RubikSize.LENGTH; b++) |
57 | 58 |
{ |
58 |
Drawable d = findViewById(RubikSize.getSize(b).getImageButton()).getBackground();
|
|
59 |
Drawable d = findViewById(b).getBackground();
|
|
59 | 60 |
|
60 |
if( size == b+SMALLEST_SIZE )
|
|
61 |
if( size == RubikSize.getSize(b).getCubeSize() )
|
|
61 | 62 |
{ |
62 | 63 |
d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY); |
63 | 64 |
} |
... | ... | |
101 | 102 |
mPicker.setValue(scramble); |
102 | 103 |
} |
103 | 104 |
|
105 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
106 |
|
|
107 |
private void addSizeButtons() |
|
108 |
{ |
|
109 |
LinearLayout layout = findViewById(R.id.sizeLayout); |
|
110 |
DisplayMetrics metrics = getResources().getDisplayMetrics(); |
|
111 |
float scale = metrics.density; |
|
112 |
int size = (int)(64*scale +0.5f); |
|
113 |
int padding = (int)(3*scale + 0.5f); |
|
114 |
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size); |
|
115 |
|
|
116 |
for(int i=0; i<RubikSize.LENGTH; i++) |
|
117 |
{ |
|
118 |
ImageButton button = new ImageButton(this); |
|
119 |
button.setLayoutParams(params); |
|
120 |
button.setId(i); |
|
121 |
button.setPadding(padding,0,padding,0); |
|
122 |
int iconID = RubikSize.getSize(i).getIconID(); |
|
123 |
button.setImageResource(iconID); |
|
124 |
button.setOnClickListener(this); |
|
125 |
layout.addView(button); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
104 | 129 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
105 | 130 |
|
106 | 131 |
static int getSize() |
... | ... | |
116 | 141 |
super.onCreate(savedState); |
117 | 142 |
setTheme(R.style.CustomActivityThemeNoActionBar); |
118 | 143 |
setContentView(R.layout.main); |
144 |
addSizeButtons(); |
|
119 | 145 |
markButton(mSize); |
120 | 146 |
|
121 | 147 |
mPicker = findViewById(R.id.rubikNumberPicker); |
... | ... | |
133 | 159 |
GLSurfaceView view = findViewById(R.id.rubikSurfaceView); |
134 | 160 |
view.onPause(); |
135 | 161 |
DistortedLibrary.onPause(); |
162 |
RubikScoresDownloader.onPause(); |
|
136 | 163 |
savePreferences(); |
137 | 164 |
super.onPause(); |
138 | 165 |
} |
... | ... | |
156 | 183 |
super.onDestroy(); |
157 | 184 |
} |
158 | 185 |
|
186 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
187 |
|
|
188 |
@Override |
|
189 |
public void onClick(View v) |
|
190 |
{ |
|
191 |
int id = v.getId(); |
|
192 |
|
|
193 |
if( id>=0 && id<RubikSize.LENGTH ) |
|
194 |
{ |
|
195 |
int size = RubikSize.getSize(id).getCubeSize(); |
|
196 |
|
|
197 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
198 |
boolean success = view.getRenderer().createCube(size); |
|
199 |
|
|
200 |
if( success ) |
|
201 |
{ |
|
202 |
markButton(size); |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
159 | 207 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
160 | 208 |
// PUBLIC API |
161 | 209 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
199 | 247 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
200 | 248 |
view.getRenderer().solveCube(); |
201 | 249 |
} |
202 |
|
|
203 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
204 |
|
|
205 |
public void setSize(View v) |
|
206 |
{ |
|
207 |
int size=0, id = v.getId(); |
|
208 |
|
|
209 |
for(int b=0; b<RubikSize.LENGTH; b++) |
|
210 |
if( RubikSize.getSize(b).getImageButton() == id ) |
|
211 |
{ |
|
212 |
size = b+SMALLEST_SIZE; |
|
213 |
break; |
|
214 |
} |
|
215 |
|
|
216 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
217 |
boolean success = view.getRenderer().createCube(size); |
|
218 |
|
|
219 |
if( success ) |
|
220 |
{ |
|
221 |
markButton(size); |
|
222 |
} |
|
223 |
} |
|
224 | 250 |
} |
Also available in: Unified diff
Improvements