GameView can now show the GameBoard correctly. Interactions are not yet possible.

This commit is contained in:
Christopher Beckmann 2015-11-12 01:50:38 +01:00
parent add419fa01
commit f4ea6bf66b
6 changed files with 77 additions and 22 deletions

View file

@ -5,7 +5,7 @@
<GradleProjectSettings> <GradleProjectSettings>
<option name="distributionType" value="LOCAL" /> <option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="D:\Program Files\Android\Android Studio\gradle\gradle-2.4" /> <option name="gradleHome" value="C:\Program Files\Android\Android Studio\gradle\gradle-2.4" />
<option name="gradleJvm" value="1.8" /> <option name="gradleJvm" value="1.8" />
<option name="modules"> <option name="modules">
<set> <set>

View file

@ -181,6 +181,14 @@ public class GameController {
} }
} }
public int getSectionHeight() {
return sectionHeight;
}
public int getSectionWidth() {
return sectionWidth;
}
public void notifyListeners() { public void notifyListeners() {
for(IModelChangeListener l : listeners) { for(IModelChangeListener l : listeners) {
l.onModelChanged(); l.onModelChanged();

View file

@ -2,12 +2,15 @@ package tu_darmstadt.sudoku.view;
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.provider.ContactsContract; import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.jar.Attributes; import java.util.jar.Attributes;
@ -20,6 +23,9 @@ public class SudokuCellView extends View {
GameCell mGameCell; GameCell mGameCell;
int mWidth; int mWidth;
int mSectionHeight;
int mSectionWidth;
boolean touched;
public SudokuCellView(Context context) { public SudokuCellView(Context context) {
@ -27,25 +33,72 @@ public class SudokuCellView extends View {
} }
public SudokuCellView(Context context, AttributeSet attrs){ public SudokuCellView(Context context, AttributeSet attrs){
super(context,attrs); super(context, attrs);
} }
public void setValues (int width, GameCell gameCell) { public void setValues (int width, int sectionHeight, int sectionWidth, GameCell gameCell) {
mSectionHeight = sectionHeight;
mSectionWidth = sectionWidth;
mGameCell = gameCell; mGameCell = gameCell;
mWidth=width; mWidth=width;
setWillNotDraw(false);
} }
@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;
}
@Override @Override
public void onDraw(Canvas canvas) { public void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
if(mGameCell == null) {
return;
}
int row = mGameCell.getRow(); int row = mGameCell.getRow();
int col = mGameCell.getCol(); int col = mGameCell.getCol();
this.setLayoutParams(new RelativeLayout.LayoutParams(mWidth, mWidth)); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mWidth, mWidth);
params.topMargin = row*mWidth;
params.leftMargin = col*mWidth;
this.setLayoutParams(params);
canvas.drawRect(col*mWidth, row*mWidth, (col+1)*mWidth+1, (row+1)*mWidth+1,new Paint()); 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);
if(touched) {
p.setColor(Color.GREEN);
RectF rectTouched = new RectF(2, 2, mWidth-2, mWidth-2);
canvas.drawRect(rectTouched, p);
}
drawValue(canvas);
}
public void drawValue(Canvas canvas) {
if(mGameCell.getValue() == 0) return;
Paint p = new Paint();
if (mGameCell.isFixed()) {
p.setTypeface(Typeface.DEFAULT_BOLD);
}
p.setAntiAlias(true);
p.setTextSize(Math.min(mWidth * 3 / 4, mWidth * 3 / 4));
p.setTextAlign(Paint.Align.CENTER);
canvas.drawText(String.valueOf(mGameCell.getValue()), mWidth/2, mWidth/2 + mWidth/4, p);
} }
public int getColumn() { public int getColumn() {

View file

@ -6,6 +6,7 @@ import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
@ -16,14 +17,11 @@ import tu_darmstadt.sudoku.controller.GameController;
*/ */
public class SudokuFieldLayout extends RelativeLayout { public class SudokuFieldLayout extends RelativeLayout {
private GameController gameController; private GameController gameController;
public SudokuCellView [][] gamecells; public SudokuCellView [][] gamecells;
AttributeSet attrs; AttributeSet attrs;
public SudokuFieldLayout(Context context, AttributeSet attrs) { public SudokuFieldLayout(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.attrs=attrs; this.attrs=attrs;
@ -48,9 +46,6 @@ public class SudokuFieldLayout extends RelativeLayout {
for (int i = 0; i < gameController.getSize(); i++) { for (int i = 0; i < gameController.getSize(); i++) {
for (int j = 0; j < gameController.getSize(); j++) { for (int j = 0; j < gameController.getSize(); j++) {
gamecells[i][j] = new SudokuCellView(getContext(), attrs); gamecells[i][j] = new SudokuCellView(getContext(), attrs);
gamecells[i][j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
gamecells[i][j].setOnClickListener(listener);
if(i != 8 || j != 7) continue;
addView(gamecells[i][j]); addView(gamecells[i][j]);
} }
} }
@ -60,13 +55,12 @@ public class SudokuFieldLayout extends RelativeLayout {
protected void onLayout(boolean changed, int l, int t, int r, int b) { protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed,l,t,r,b); super.onLayout(changed,l,t,r,b);
if(changed) { if(changed && gameController != null) {
int width = (Math.min(getWidth(), getHeight())) / gameController.getSize(); int width = (Math.min(r-l, b-t)) / gameController.getSize();
//width -= 20;
for (int i = 0; i < gameController.getSize(); i++) { for (int i = 0; i < gameController.getSize(); i++) {
for (int j = 0; j < gameController.getSize(); j++) { for (int j = 0; j < gameController.getSize(); j++) {
gamecells[i][j].setValues(width, gameController.getGameCell(i, j)); gamecells[i][j].setValues(width, gameController.getSectionHeight(), gameController.getSectionWidth(), gameController.getGameCell(i, j));
} }
} }
} }

View file

@ -8,8 +8,8 @@
tools:context="tu_darmstadt.sudoku.view.MainActivity"> tools:context="tu_darmstadt.sudoku.view.MainActivity">
<TextView android:id="@+id/testString" <TextView android:id="@+id/testString"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:editable="true" android:editable="true"
android:clickable="true" android:clickable="true"
android:onClick="onClicktext" android:onClick="onClicktext"

View file

@ -12,7 +12,7 @@
<tu_darmstadt.sudoku.view.SudokuFieldLayout <tu_darmstadt.sudoku.view.SudokuFieldLayout
android:id="@+id/sudokuLayout" android:id="@+id/sudokuLayout"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:gravity="center"
</tu_darmstadt.sudoku.view.SudokuFieldLayout> android:background="@color/colorPrimary" />
</RelativeLayout> </RelativeLayout>