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

Initial options screen working well!

parent 80be38ab
...@@ -10,14 +10,14 @@ ...@@ -10,14 +10,14 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
</activity>
<activity android:name=".GridActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".GridActivity">
</activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -2,10 +2,10 @@ package edu.liu.floodgame; ...@@ -2,10 +2,10 @@ package edu.liu.floodgame;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.LinearLayout;
public class ColorButton extends View implements View.OnClickListener { public class ColorButton extends View implements View.OnClickListener {
...@@ -27,10 +27,16 @@ 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) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
GridActivity activity = (GridActivity) getContext(); GridActivity activity = (GridActivity) getContext();
int half = canvas.getHeight() / 2; int half = canvas.getHeight()/2;
Paint paint = new Paint(); Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.FILL);
paint.setColor(activity.colors[color]); paint.setColor(activity.colorFromPalette(color));
canvas.drawCircle(half, half, half, paint); 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; package edu.liu.floodgame;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
...@@ -12,19 +14,9 @@ import java.util.Random; ...@@ -12,19 +14,9 @@ import java.util.Random;
public class GridActivity extends AppCompatActivity implements FloodGrid { public class GridActivity extends AppCompatActivity implements FloodGrid {
final int[] colors = { TypedArray colors;
0xFFfbb4ae,
0xFFb3cde3,
0xFFccebc5,
0xFFdecbe4
// 0xFFfed9a6,
// 0xFFffffcc,
// 0xFFe5d8bd,
// 0xFFfddaec
};
final int MAX_COLORS = 8; // The number of color buttons supported by UI 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 COLOR_ARRAY_KEY = "COLOR_ARRAY";
final String NUM_MOVES_KEY = "NUM_MOVES"; final String NUM_MOVES_KEY = "NUM_MOVES";
...@@ -32,6 +24,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -32,6 +24,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
private GridLayout gridLayout; private GridLayout gridLayout;
private LinearLayout buttonBar; private LinearLayout buttonBar;
private TextView numMovesText; private TextView numMovesText;
private int numColors;
private int numMoves = 0; private int numMoves = 0;
private boolean gameOver = false; private boolean gameOver = false;
...@@ -41,11 +34,15 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -41,11 +34,15 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
setContentView(R.layout.activity_flood_grid); setContentView(R.layout.activity_flood_grid);
numMovesText = findViewById(R.id.num_moves); 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. // buttons are at odd offsets.
buttonBar = findViewById(R.id.buttonBar); buttonBar = findViewById(R.id.buttonBar);
int buttonIdx = 0; int buttonIdx = 0;
for( ; buttonIdx < NUM_COLORS; buttonIdx++) { for( ; buttonIdx < numColors; buttonIdx++) {
ColorButton button = (ColorButton) buttonBar.getChildAt(2*buttonIdx+1); ColorButton button = (ColorButton) buttonBar.getChildAt(2*buttonIdx+1);
button.color = buttonIdx; button.color = buttonIdx;
} }
...@@ -58,13 +55,13 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -58,13 +55,13 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
// Set up grid // Set up grid
gridLayout = findViewById(R.id.grid); gridLayout = findViewById(R.id.grid);
gridLayout.removeAllViews(); gridLayout.removeAllViews();
final int EDGE_SIZE = 5; final int BOARD_SIZE = intent.getIntExtra(MainActivity.BOARD_SIZE_KEY, 5);
gridLayout.setColumnCount(EDGE_SIZE); gridLayout.setColumnCount(BOARD_SIZE);
for(int i = 0; i < EDGE_SIZE * EDGE_SIZE; i++) { for(int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
gridLayout.addView(new GridCellView(this)); gridLayout.addView(new GridCellView(this));
} }
if(savedInstanceState == null) { if(savedInstanceState == null) {
FloodGridOps.randomize(this, new Random(), NUM_COLORS); FloodGridOps.randomize(this, new Random(), numColors);
gameOver = false; gameOver = false;
setNumMoves(0); setNumMoves(0);
} else { } else {
...@@ -113,6 +110,10 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -113,6 +110,10 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
cell.setColor(color); cell.setColor(color);
} }
int colorFromPalette(int c) {
return colors.getColor(c, 0);
}
public void setNumMoves(int m) { public void setNumMoves(int m) {
numMoves = m; numMoves = m;
if(gameOver) { if(gameOver) {
...@@ -126,7 +127,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid { ...@@ -126,7 +127,7 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
public void onClickColor(int color) { public void onClickColor(int color) {
if(gameOver) { if(gameOver) {
// Clicking when game already over restarts // Clicking when game already over restarts
FloodGridOps.randomize(this, new Random(), NUM_COLORS); FloodGridOps.randomize(this, new Random(), numColors);
gameOver = false; gameOver = false;
setNumMoves(0); setNumMoves(0);
} }
......
package edu.liu.floodgame; package edu.liu.floodgame;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.view.View; import android.view.View;
...@@ -20,6 +18,6 @@ public class GridCellView extends View { ...@@ -20,6 +18,6 @@ public class GridCellView extends View {
void setColor(int color) { void setColor(int color) {
this.color = color; this.color = color;
GridActivity activity = (GridActivity) getContext(); GridActivity activity = (GridActivity) getContext();
setBackgroundColor(activity.colors[color]); setBackgroundColor(activity.colorFromPalette(color));
} }
} }
package edu.liu.floodgame; package edu.liu.floodgame;
import android.support.v7.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View; 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 android.widget.TextView;
import java.util.Random; public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, CompoundButton.OnCheckedChangeListener {
public class MainActivity extends AppCompatActivity {
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 SeekBar boardSizeSlider, numColorsSlider;
private TextView textView; private TextView boardSizeValue, numColorsValue;
private Button decrButton, resetButton; private RadioButton[] paletteRadios;
private int counter = 0; private PaletteSwatchesView[] paletteSwatches;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
this.textView = findViewById(R.id.textView);
this.decrButton = findViewById(R.id.decrement); boardSizeValue = findViewById(R.id.boardSizeValue);
this.resetButton = findViewById(R.id.reset); 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) { if(savedInstanceState == null) {
System.out.println("No existing bundle"); System.out.println("No existing bundle");
setCounter(0);
} }
else { else {
System.out.println("Using existing bundle"); System.out.println("Using existing bundle");
setCounter(savedInstanceState.getInt(COUNTER));
} }
} }
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(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;
}
} }
public void clickIncrement(View v) { private void setBoardSizeFromSlider(int position) {
setCounter(counter+1); boardSizeValue.setText(Integer.toString(sliderPositionToBoardSize(position)));
} }
public void clickDecrement(View v) { private static int sliderPositionToNumColors(int position) {
setCounter(counter-1); return position + 3;
} }
public void clickReset(View v) { private void setNumColorsFromSlider(int position) {
setCounter(0); int nc = sliderPositionToNumColors(position);
numColorsValue.setText(Integer.toString(nc));
for(PaletteSwatchesView psv : paletteSwatches) {
psv.setNumColors(nc);
}
} }
public void clickRandom(View v) { @Override
setCounter(rng.nextInt(1000)-500); public void onStartTrackingTouch(SeekBar seekBar) {
} }
private void setCounter(int newValue) { @Override
counter = newValue; public void onStopTrackingTouch(SeekBar seekBar) {
textView.setText(Integer.toString(counter));
//decrButton.setEnabled(counter > 0);
resetButton.setEnabled(counter != 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;
}
}
@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;
}
}
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 @@ ...@@ -56,7 +56,7 @@
android:id="@+id/buttonBar" android:id="@+id/buttonBar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/default_gap" android:layout_marginBottom="24dp"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="@dimen/default_gap" android:padding="@dimen/default_gap"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
......
<?xml version="1.0" encoding="utf-8"?> <?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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/default_gap"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView <TextView
android:id="@+id/textView" android:id="@+id/title"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="24dp"
android:text="@string/greeting" android:gravity="center"
android:text="@string/title"
android:textSize="54sp" android:textSize="54sp"
app:fontFamily="sans-serif-black" android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent" app:fontFamily="casual"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" /> app:layout_constraintTop_toTopOf="parent" />
<Space
android:layout_width="match_parent"
android:layout_height="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">
<LinearLayout <TextView
android:id="@+id/linearLayout" android:id="@+id/boardSizeLabel"
style="?android:attr/buttonBarStyle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:paddingRight="@dimen/default_gap"
android:orientation="horizontal" android:text="Board Size"
app:layout_constraintBottom_toTopOf="@+id/textView" android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent" app:fontFamily="sans-serif-medium" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> <TextView
android:id="@+id/boardSizeValue"
<Button android:layout_width="40dp"
android:id="@+id/decrement" 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"