Commit 95bc8475 authored by Christopher League's avatar Christopher League
Browse files

Handles variations in number of colors better

parent 516e5bf0
......@@ -10,6 +10,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary true
}
buildTypes {
release {
......
package edu.liu.floodgame;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
......@@ -7,18 +11,26 @@ public class ColorButton extends View implements View.OnClickListener {
int color;
ColorButton(GridActivity activity, int size, int color) {
super(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(size, size);
setLayoutParams(lp);
this.color = color;
setBackgroundColor(activity.colors[color]);
ColorButton(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
@Override
public void onClick(View view) {
System.out.println("You clicked color " + color);
((GridActivity) getContext()).onClickColor(color);
GridActivity activity = (GridActivity) getContext();
activity.onClickColor(color);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
GridActivity activity = (GridActivity) getContext();
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);
}
}
package edu.liu.floodgame;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Space;
import java.util.Random;
......@@ -14,14 +18,15 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
0xFFfbb4ae,
0xFFb3cde3,
0xFFccebc5,
0xFFdecbe4,
0xFFfed9a6,
0xFFffffcc,
0xFFe5d8bd,
0xFFfddaec
0xFFdecbe4
// 0xFFfed9a6,
// 0xFFffffcc,
// 0xFFe5d8bd,
// 0xFFfddaec
};
final int NUM_COLORS = colors.length;
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 = "COLOR_ARRAY";
private GridLayout gridLayout;
......@@ -31,21 +36,27 @@ public class GridActivity extends AppCompatActivity implements FloodGrid {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flood_grid);
// Set up button bar
// Set up button bar. It contains MAX_COLORS buttons and MAX_COLORS+1 gaps, so the
// buttons are at odd offsets.
buttonBar = findViewById(R.id.buttonBar);
buttonBar.removeAllViews();
for(int i = 0; i < NUM_COLORS; i++) {
buttonBar.addView(new ColorButton(this, 100, i));
int buttonIdx = 0;
for( ; buttonIdx < NUM_COLORS; buttonIdx++) {
ColorButton button = (ColorButton) buttonBar.getChildAt(2*buttonIdx+1);
button.color = buttonIdx;
}
// Remove any subsequent buttons and gaps
for( ; buttonIdx < MAX_COLORS; buttonIdx++) {
buttonBar.getChildAt(2*buttonIdx).setVisibility(View.GONE);
buttonBar.getChildAt(2*buttonIdx+1).setVisibility(View.GONE);
}
// Set up grid
gridLayout = findViewById(R.id.grid);
gridLayout.removeAllViews();
final int NUM_ROWS = 10;
final int NUM_COLUMNS = 8;
final int NUM_CELLS = NUM_ROWS * NUM_COLUMNS;
gridLayout.setColumnCount(NUM_COLUMNS);
for(int i = 0; i < NUM_CELLS; i++) {
final int EDGE_SIZE = 5;
gridLayout.setColumnCount(EDGE_SIZE);
for(int i = 0; i < EDGE_SIZE * EDGE_SIZE; i++) {
gridLayout.addView(new GridCellView(this));
}
if(savedInstanceState == null) {
......
package edu.liu.floodgame;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.GridLayout;
import android.view.View;
......
<vector android:height="24dp" android:viewportHeight="11.358956"
android:viewportWidth="11.358956" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="1" android:fillColor="#7071ff"
android:pathData="M2.4054,0L8.9535,0A2.4054,2.4054 0,0 1,11.359 2.4054L11.359,8.9535A2.4054,2.4054 0,0 1,8.9535 11.359L2.4054,11.359A2.4054,2.4054 0,0 1,0 8.9535L0,2.4054A2.4054,2.4054 0,0 1,2.4054 0z"
android:strokeAlpha="1" android:strokeColor="#0001e1"
android:strokeLineCap="round" android:strokeLineJoin="miter" android:strokeWidth="0"/>
<path android:fillAlpha="1" android:fillColor="#ffc470"
android:pathData="M5.8799,5.3454m-2.2718,0a2.2718,2.2718 0,1 1,4.5436 0a2.2718,2.2718 0,1 1,-4.5436 0"
android:strokeAlpha="1" android:strokeColor="#0001e1"
android:strokeLineCap="round" android:strokeLineJoin="miter" android:strokeWidth="0"/>
</vector>
......@@ -54,37 +54,125 @@
<LinearLayout
android:id="@+id/buttonBar"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#ccc"
android:layout_marginBottom="@dimen/default_gap"
android:orientation="horizontal"
android:padding="10dp"
android:paddingRight="0dp"
android:padding="@dimen/default_gap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/grid">
app:layout_constraintStart_toStartOf="parent">
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Button
android:id="@+id/button"
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eea1c7"
tools:layout_conversion_absoluteHeight="48dp"
tools:layout_conversion_absoluteWidth="162dp"
tools:layout_editor_absoluteX="10dp"
tools:layout_editor_absoluteY="546dp" />
<Button
android:id="@+id/button2"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_conversion_absoluteHeight="48dp"
tools:layout_conversion_absoluteWidth="230dp"
tools:layout_editor_absoluteX="172dp"
tools:layout_editor_absoluteY="546dp" />
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<edu.liu.floodgame.ColorButton
android:layout_width="@dimen/color_button"
android:layout_height="@dimen/color_button"
android:layout_marginStart="@dimen/default_gap"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginEnd="@dimen/default_gap"
android:layout_marginRight="@dimen/default_gap" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="default_gap">8dp</dimen>
<dimen name="color_button">30dp</dimen>
</resources>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment