Commit e5325a70 authored by Christopher League's avatar Christopher League
Browse files

Initial options screen working well!

parent 80be38ab
......@@ -10,14 +10,14 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".GridActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GridActivity">
</activity>
</application>
</manifest>
\ No newline at end of file
......@@ -2,10 +2,10 @@ package edu.liu.floodgame;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class ColorButton extends View implements View.OnClickListener {
......@@ -27,10 +27,16 @@ public class ColorButton extends View implements View.OnClickListener {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
GridActivity activity = (GridActivity) getContext();
int half = canvas.getHeight() / 2;
int half = canvas.getHeight()/2;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(activity.colors[color]);
canvas.drawCircle(half, half, half, paint);
paint.setColor(activity.colorFromPalette(color));
canvas.drawCircle(half, half, half-1, paint);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(4f);
paint.setAntiAlias(true);
paint.setAlpha(127);
canvas.drawCircle(half, half, half-3, paint);
}
}
package edu.liu.floodgame;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
......@@ -12,19 +14,9 @@ import java.util.Random;
public class GridActivity extends AppCompatActivity implements FloodGrid {
final int[] colors = {
0xFFfbb4ae,
0xFFb3cde3,
0xFFccebc5,
0xFFdecbe4
// 0xFFfed9a6,
// 0xFFffffcc,
// 0xFFe5d8bd,
// 0xFFfddaec
};
TypedArray colors;
final int MAX_COLORS = 8; // The number of color buttons supported by UI
final int NUM_COLORS = colors.length < MAX_COLORS? colors.length : MAX_COLORS;
final String COLOR_ARRAY_KEY = "COLOR_ARRAY";
final String NUM_MOVES_KEY = "NUM_MOVES";
......@@ -32,6 +24,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
private GridLayout gridLayout;
private LinearLayout buttonBar;
private TextView numMovesText;
private int numColors;
private int numMoves = 0;
private boolean gameOver = false;
......@@ -41,11 +34,15 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
setContentView(R.layout.activity_flood_grid);
numMovesText = findViewById(R.id.num_moves);
// Set up button bar. It contains MAX_COLORS buttons and MAX_COLORS+1 gaps, so the
// Grab palette
Intent intent = getIntent();
colors = getResources().obtainTypedArray(intent.getIntExtra(MainActivity.PALETTE_KEY, R.array.accentPalette));
numColors = intent.getIntExtra(MainActivity.NUM_COLORS_KEY, colors.length());
// buttons are at odd offsets.
buttonBar = findViewById(R.id.buttonBar);
int buttonIdx = 0;
for( ; buttonIdx < NUM_COLORS; buttonIdx++) {
for( ; buttonIdx < numColors; buttonIdx++) {
ColorButton button = (ColorButton) buttonBar.getChildAt(2*buttonIdx+1);
button.color = buttonIdx;
}
......@@ -58,13 +55,13 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
// Set up grid
gridLayout = findViewById(R.id.grid);
gridLayout.removeAllViews();
final int EDGE_SIZE = 5;
gridLayout.setColumnCount(EDGE_SIZE);
for(int i = 0; i < EDGE_SIZE * EDGE_SIZE; i++) {
final int BOARD_SIZE = intent.getIntExtra(MainActivity.BOARD_SIZE_KEY, 5);
gridLayout.setColumnCount(BOARD_SIZE);
for(int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
gridLayout.addView(new GridCellView(this));
}
if(savedInstanceState == null) {
FloodGridOps.randomize(this, new Random(), NUM_COLORS);
FloodGridOps.randomize(this, new Random(), numColors);
gameOver = false;
setNumMoves(0);
} else {
......@@ -113,6 +110,10 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
cell.setColor(color);
}
int colorFromPalette(int c) {
return colors.getColor(c, 0);
}
public void setNumMoves(int m) {
numMoves = m;
if(gameOver) {
......@@ -126,7 +127,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
public void onClickColor(int color) {
if(gameOver) {
// Clicking when game already over restarts
FloodGridOps.randomize(this, new Random(), NUM_COLORS);
FloodGridOps.randomize(this, new Random(), numColors);
gameOver = false;
setNumMoves(0);
}
......
package edu.liu.floodgame;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.GridLayout;
import android.view.View;
......@@ -20,6 +18,6 @@ public class GridCellView extends View {
void setColor(int color) {
this.color = color;
GridActivity activity = (GridActivity) getContext();
setBackgroundColor(activity.colors[color]);
setBackgroundColor(activity.colorFromPalette(color));
}
}
package edu.liu.floodgame;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, CompoundButton.OnCheckedChangeListener {
final private String COUNTER = "counter";
public static final String BOARD_SIZE_KEY = "BOARD_SIZE";
public static final String NUM_COLORS_KEY = "NUM_COLORS";
public static final String PALETTE_KEY = "PALETTE";
private Random rng = new Random();
private TextView textView;
private Button decrButton, resetButton;
private int counter = 0;
private SeekBar boardSizeSlider, numColorsSlider;
private TextView boardSizeValue, numColorsValue;
private RadioButton[] paletteRadios;
private PaletteSwatchesView[] paletteSwatches;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textView = findViewById(R.id.textView);
this.decrButton = findViewById(R.id.decrement);
this.resetButton = findViewById(R.id.reset);
boardSizeValue = findViewById(R.id.boardSizeValue);
boardSizeSlider = findViewById(R.id.boardSizeSlider);
boardSizeSlider.setOnSeekBarChangeListener(this);
setBoardSizeFromSlider(boardSizeSlider.getProgress());
numColorsValue = findViewById(R.id.numColorsValue);
numColorsSlider = findViewById(R.id.numColorsSlider);
numColorsSlider.setOnSeekBarChangeListener(this);
paletteRadios = new RadioButton[] {
findViewById(R.id.radioPalette1),
findViewById(R.id.radioPalette2),
findViewById(R.id.radioPalette3)
};
for(RadioButton r : paletteRadios) {
r.setOnCheckedChangeListener(this);
}
paletteSwatches = new PaletteSwatchesView[] {
findViewById(R.id.swatchesPalette1),
findViewById(R.id.swatchesPalette2),
findViewById(R.id.swatchesPalette3)
};
for(int i = 0; i < paletteSwatches.length; i++) {
paletteSwatches[i].setPaletteId(paletteRadioIdArrayId(paletteRadios[i].getId()));
}
setNumColorsFromSlider(numColorsSlider.getProgress());
if(savedInstanceState == null) {
System.out.println("No existing bundle");
setCounter(0);
}
else {
System.out.println("Using existing bundle");
setCounter(savedInstanceState.getInt(COUNTER));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(COUNTER, counter);
}
private static int sliderPositionToBoardSize(int position) {
switch(position) {
case 0: return 5;
case 1: return 10;
case 2: return 15;
case 3: return 20;
default: return 7;
}
}
private void setBoardSizeFromSlider(int position) {
boardSizeValue.setText(Integer.toString(sliderPositionToBoardSize(position)));
}
private static int sliderPositionToNumColors(int position) {
return position + 3;
}
private void setNumColorsFromSlider(int position) {
int nc = sliderPositionToNumColors(position);
numColorsValue.setText(Integer.toString(nc));
for(PaletteSwatchesView psv : paletteSwatches) {
psv.setNumColors(nc);
}
}
public void clickIncrement(View v) {
setCounter(counter+1);
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void clickDecrement(View v) {
setCounter(counter-1);
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void clickReset(View v) {
setCounter(0);
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch(seekBar.getId()) {
case R.id.boardSizeSlider:
setBoardSizeFromSlider(i);
break;
case R.id.numColorsSlider:
setNumColorsFromSlider(i);
break;
}
}
public void clickRandom(View v) {
setCounter(rng.nextInt(1000)-500);
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean on) {
if(on) {
// Turn everyone else off
for (RadioButton r : paletteRadios) {
if (r.getId() != compoundButton.getId()) {
r.setChecked(false);
}
}
}
}
int paletteRadioIdArrayId(int id) {
switch(id) {
case R.id.radioPalette3:
return R.array.pastels1Palette;
case R.id.radioPalette2:
return R.array.darkPalette;
case R.id.radioPalette1:
default:
return R.array.accentPalette;
}
}
private void setCounter(int newValue) {
counter = newValue;
textView.setText(Integer.toString(counter));
//decrButton.setEnabled(counter > 0);
resetButton.setEnabled(counter != 0);
int getSelectedPaletteId() {
int id = 0;
for(RadioButton r : paletteRadios) {
if(r.isChecked()) {
id = r.getId();
break;
}
}
return paletteRadioIdArrayId(id);
}
public void clickStartGame(View v) {
Intent intent = new Intent(this, GridActivity.class);
intent.putExtra(BOARD_SIZE_KEY, sliderPositionToBoardSize(boardSizeSlider.getProgress()));
intent.putExtra(NUM_COLORS_KEY, sliderPositionToNumColors(numColorsSlider.getProgress()));
intent.putExtra(PALETTE_KEY, getSelectedPaletteId());
startActivity(intent);
}
}
package edu.liu.floodgame;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.camera2.params.BlackLevelPattern;
import android.util.AttributeSet;
import android.view.View;
public class PaletteSwatchesView extends View {
int paletteArrayId = R.array.accentPalette;
int numColors = 6;
public PaletteSwatchesView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setPaletteId(int paletteArrayId) {
this.paletteArrayId = paletteArrayId;
}
public void setNumColors(int numColors) {
this.numColors = numColors;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
TypedArray ta = getResources().obtainTypedArray(paletteArrayId);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
int h = canvas.getHeight();
int w = canvas.getWidth() / numColors;
for(int i = 0; i < numColors; i++) {
paint.setColor(ta.getColor(i, Color.BLACK));
canvas.drawRect(i*w, 0, i*w+w, h, paint);
}
ta.recycle();
}
}
......@@ -56,7 +56,7 @@
android:id="@+id/buttonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/default_gap"
android:layout_marginBottom="24dp"
android:orientation="horizontal"
android:padding="@dimen/default_gap"
app:layout_constraintBottom_toBottomOf="parent"
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/default_gap"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/greeting"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="@string/title"
android:textSize="54sp"
app:fontFamily="sans-serif-black"
app:layout_constraintBottom_toBottomOf="parent"
android:textStyle="bold"
app:fontFamily="casual"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linearLayout"
style="?android:attr/buttonBarStyle"
android:layout_width="wrap_content"
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickDecrement"
android:text="@string/decrement"
app:layout_constraintEnd_toStartOf="@+id/increment"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="154dp" />
<Button
android:id="@+id/increment"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:padding="@dimen/default_gap">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="fill_horizontal|center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/boardSizeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="@dimen/default_gap"
android:text="Board Size"
android:textSize="18sp"
app:fontFamily="sans-serif-medium" />
<TextView
android:id="@+id/boardSizeValue"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="99"
android:textSize="18sp"
app:fontFamily="sans-serif-medium" />
<SeekBar
android:id="@+id/boardSizeSlider"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="3"
android:progress="0" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:id="@+id/numColorsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:paddingRight="@dimen/default_gap"
android:text="Colors"
android:textSize="18sp"
app:fontFamily="sans-serif-medium" />
<TextView
android:id="@+id/numColorsValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="9"
android:textSize="18sp"
app:fontFamily="sans-serif-medium" />
<SeekBar
android:id="@+id/numColorsSlider"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="5"
android:progress="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickIncrement"
android:text="@string/increment"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/decrement"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:id="@+id/paletteLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:paddingRight="@dimen/default_gap"
android:text="Palette"
android:textSize="18sp"
app:fontFamily="sans-serif-medium" />
<RadioButton
android:id="@+id/radioPalette1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:gravity="right"
android:textSize="8sp" />
<edu.liu.floodgame.PaletteSwatchesView
android:id="@+id/swatchesPalette1"
android:layout_width="match_parent"
android:layout_height="@dimen/color_button"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickReset"
android:text="@string/reset" />
android:gravity="center_vertical">
<View
android:layout_width="0dp"
android:layout_height="0dp" />
<RadioButton
android:id="@+id/radioPalette2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:gravity="right"