GameView can now show the GameBoard correctly. Interactions are not yet possible.
This commit is contained in:
parent
add419fa01
commit
f4ea6bf66b
6 changed files with 77 additions and 22 deletions
|
@ -5,7 +5,7 @@
|
|||
<GradleProjectSettings>
|
||||
<option name="distributionType" value="LOCAL" />
|
||||
<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="modules">
|
||||
<set>
|
||||
|
|
|
@ -181,6 +181,14 @@ public class GameController {
|
|||
}
|
||||
}
|
||||
|
||||
public int getSectionHeight() {
|
||||
return sectionHeight;
|
||||
}
|
||||
|
||||
public int getSectionWidth() {
|
||||
return sectionWidth;
|
||||
}
|
||||
|
||||
public void notifyListeners() {
|
||||
for(IModelChangeListener l : listeners) {
|
||||
l.onModelChanged();
|
||||
|
|
|
@ -2,12 +2,15 @@ package tu_darmstadt.sudoku.view;
|
|||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.provider.ContactsContract;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.Typeface;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.jar.Attributes;
|
||||
|
||||
|
@ -20,6 +23,9 @@ public class SudokuCellView extends View {
|
|||
|
||||
GameCell mGameCell;
|
||||
int mWidth;
|
||||
int mSectionHeight;
|
||||
int mSectionWidth;
|
||||
boolean touched;
|
||||
|
||||
|
||||
public SudokuCellView(Context context) {
|
||||
|
@ -31,21 +37,68 @@ public class SudokuCellView extends View {
|
|||
|
||||
}
|
||||
|
||||
public void setValues (int width, GameCell gameCell) {
|
||||
public void setValues (int width, int sectionHeight, int sectionWidth, GameCell gameCell) {
|
||||
mSectionHeight = sectionHeight;
|
||||
mSectionWidth = sectionWidth;
|
||||
mGameCell = gameCell;
|
||||
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
|
||||
public void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
if(mGameCell == null) {
|
||||
return;
|
||||
}
|
||||
int row = mGameCell.getRow();
|
||||
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() {
|
||||
|
|
|
@ -6,6 +6,7 @@ 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;
|
||||
|
||||
|
@ -16,14 +17,11 @@ import tu_darmstadt.sudoku.controller.GameController;
|
|||
*/
|
||||
public class SudokuFieldLayout extends RelativeLayout {
|
||||
|
||||
|
||||
private GameController gameController;
|
||||
|
||||
public SudokuCellView [][] gamecells;
|
||||
AttributeSet attrs;
|
||||
|
||||
|
||||
|
||||
public SudokuFieldLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.attrs=attrs;
|
||||
|
@ -48,9 +46,6 @@ public class SudokuFieldLayout extends RelativeLayout {
|
|||
for (int i = 0; i < gameController.getSize(); i++) {
|
||||
for (int j = 0; j < gameController.getSize(); j++) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
@ -60,13 +55,12 @@ public class SudokuFieldLayout extends RelativeLayout {
|
|||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
super.onLayout(changed,l,t,r,b);
|
||||
|
||||
if(changed) {
|
||||
int width = (Math.min(getWidth(), getHeight())) / gameController.getSize();
|
||||
//width -= 20;
|
||||
if(changed && gameController != null) {
|
||||
int width = (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.getGameCell(i, j));
|
||||
gamecells[i][j].setValues(width, gameController.getSectionHeight(), gameController.getSectionWidth(), gameController.getGameCell(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
tools:context="tu_darmstadt.sudoku.view.MainActivity">
|
||||
|
||||
<TextView android:id="@+id/testString"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:editable="true"
|
||||
android:clickable="true"
|
||||
android:onClick="onClicktext"
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<tu_darmstadt.sudoku.view.SudokuFieldLayout
|
||||
android:id="@+id/sudokuLayout"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
</tu_darmstadt.sudoku.view.SudokuFieldLayout>
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:background="@color/colorPrimary" />
|
||||
</RelativeLayout>
|
||||
|
|
Loading…
Reference in a new issue