Cleaned up code and made GameView prettier. :)

This commit is contained in:
Christopher Beckmann 2015-11-12 11:24:16 +01:00
parent f4ea6bf66b
commit a8486fe5a2
4 changed files with 65 additions and 33 deletions

View file

@ -34,6 +34,14 @@ public class GameController {
public GameController(GameType type) {
setGameType(type);
gameField = new GameField(size, sectionHeight, sectionWidth);
setValue(0, 1, 8); setValue(0, 4, 2);
setValue(1, 1, 6); setValue(1, 2, 7);
setValue(2, 4, 8); setValue(2, 5, 5);
setValue(3, 4, 4); setValue(3, 6, 6);
setValue(4, 7, 9); setValue(4, 8, 3);
setValue(6, 0, 1); setValue(6, 1, 5);
setValue(7, 2, 8); setValue(7, 3, 5);
setValue(8, 2, 9); setValue(8, 3, 4);
}
/*public boolean loadLevel(GameField level) {

View file

@ -25,7 +25,10 @@ public class SudokuCellView extends View {
int mWidth;
int mSectionHeight;
int mSectionWidth;
int mRow;
int mCol;
boolean touched;
boolean selected;
public SudokuCellView(Context context) {
@ -34,25 +37,26 @@ public class SudokuCellView extends View {
public SudokuCellView(Context context, AttributeSet attrs){
super(context, attrs);
}
public void setSelected(boolean b) {
this.selected = b;
}
public void setValues (int width, int sectionHeight, int sectionWidth, GameCell gameCell) {
mSectionHeight = sectionHeight;
mSectionWidth = sectionWidth;
mGameCell = gameCell;
mWidth=width;
setWillNotDraw(false);
mWidth = width;
mRow = gameCell.getRow();
mCol = gameCell.getCol();
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
if(mGameCell == null) return false;
//this.setBackgroundColor(Color.CYAN);
touched = true;
Toast t = Toast.makeText(getContext(), mGameCell.toString(), Toast.LENGTH_SHORT);
t.show();
return true;
}
@ -60,32 +64,30 @@ public class SudokuCellView extends View {
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mWidth, mWidth);
params.topMargin = mRow*mWidth;
params.leftMargin = mCol*mWidth;
this.setLayoutParams(params);
if(mGameCell == null) {
return;
}
int row = mGameCell.getRow();
int col = mGameCell.getCol();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mWidth, mWidth);
params.topMargin = row*mWidth;
params.leftMargin = col*mWidth;
this.setLayoutParams(params);
drawBackground(canvas);
drawValue(canvas);
}
public void drawBackground(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.GRAY);
RectF rect = new RectF(0, 0, mWidth, mWidth);
canvas.drawRect(rect, p);
p.setColor(Color.WHITE);
RectF rectInner = new RectF(2, 2, mWidth-2, mWidth-2);
canvas.drawRect(rectInner, p);
RectF rect = new RectF(3, 3, mWidth-3, mWidth-3);
canvas.drawRect(rect, p);
if(touched) {
p.setColor(Color.GREEN);
RectF rectTouched = new RectF(2, 2, mWidth-2, mWidth-2);
RectF rectTouched = new RectF(3, 3, mWidth-3, mWidth-3);
canvas.drawRect(rectTouched, p);
}
drawValue(canvas);
}
public void drawValue(Canvas canvas) {
@ -102,11 +104,11 @@ public class SudokuCellView extends View {
}
public int getColumn() {
return mGameCell.getCol();
return mCol;
}
public int getRow() {
return mGameCell.getRow();
return mRow;
}

View file

@ -1,14 +1,12 @@
package tu_darmstadt.sudoku.view;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import tu_darmstadt.sudoku.controller.GameController;
@ -18,6 +16,9 @@ import tu_darmstadt.sudoku.controller.GameController;
public class SudokuFieldLayout extends RelativeLayout {
private GameController gameController;
private int sectionHeight;
private int sectionWidth;
private int gameCellWidth;
public SudokuCellView [][] gamecells;
AttributeSet attrs;
@ -25,6 +26,7 @@ public class SudokuFieldLayout extends RelativeLayout {
public SudokuFieldLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.attrs=attrs;
setBackgroundColor(Color.argb(255, 200, 200, 200));
}
public void setGame(GameController gc) {
@ -37,12 +39,13 @@ public class SudokuFieldLayout extends RelativeLayout {
public void onClick(View v) {
if (v instanceof SudokuCellView) {
SudokuCellView view = (SudokuCellView) v;
Toast t = Toast.makeText(getContext(), "(" + view.getRow() + " " + view.getColumn() + ")", Toast.LENGTH_SHORT);
t.show();
}
}
};
sectionHeight = gameController.getSectionHeight();
sectionWidth = gameController.getSectionWidth();
for (int i = 0; i < gameController.getSize(); i++) {
for (int j = 0; j < gameController.getSize(); j++) {
gamecells[i][j] = new SudokuCellView(getContext(), attrs);
@ -51,16 +54,36 @@ public class SudokuFieldLayout extends RelativeLayout {
}
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.BLACK);
p.setStrokeWidth(2);
// TODO: Draw Borders
//canvas.drawLine(0, 0, 0, getHeight(), p);
for(int i = 0; i <= (gameController.getSize()/sectionWidth); i++) {
for(int j = -2; j < 2; j++) {
canvas.drawLine((i * getWidth() / sectionWidth) + j, 0, (i * getWidth() / sectionWidth) + j, getHeight(), p);
}
}
for(int i = 0; i <= (gameController.getSize()/sectionHeight); i++) {
canvas.drawLine(0, i*getHeight() / sectionHeight, getHeight(), i*getHeight() / sectionHeight, p);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed,l,t,r,b);
if(changed && gameController != null) {
int width = (Math.min(r-l, b-t)) / gameController.getSize();
gameCellWidth = (Math.min(r-l, b-t)) / gameController.getSize();
for (int i = 0; i < gameController.getSize(); i++) {
for (int j = 0; j < gameController.getSize(); j++) {
gamecells[i][j].setValues(width, gameController.getSectionHeight(), gameController.getSectionWidth(), gameController.getGameCell(i, j));
gamecells[i][j].setValues(gameCellWidth, sectionHeight, sectionWidth, gameController.getGameCell(i, j));
}
}
}

View file

@ -13,6 +13,5 @@
android:id="@+id/sudokuLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@color/colorPrimary" />
android:gravity="center"/>
</RelativeLayout>