X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/FreeShisen/blobdiff_plain/5fe784394f21fdf2feae1851281bd1f458b73fd2..aa2f597b741eacca2929c0e31ee6e9fe77151d2c:/src/de/cwde/freeshisen/ShisenShoView.java diff --git a/src/de/cwde/freeshisen/ShisenShoView.java b/src/de/cwde/freeshisen/ShisenShoView.java index 617e5f7..6a38ffc 100644 --- a/src/de/cwde/freeshisen/ShisenShoView.java +++ b/src/de/cwde/freeshisen/ShisenShoView.java @@ -1,17 +1,19 @@ package de.cwde.freeshisen; +import java.lang.ref.WeakReference; import java.util.List; import java.util.Locale; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; +import android.app.AlertDialog; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; -import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Paint.Cap; @@ -21,6 +23,7 @@ import android.graphics.Rect; import android.graphics.Typeface; import android.os.Handler; import android.os.Message; +import android.preference.PreferenceManager; import android.view.MenuItem; import android.view.MotionEvent; import android.view.SurfaceHolder; @@ -28,6 +31,7 @@ import android.view.SurfaceView; class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { + private static final String INVALID_TIME = "9:99:99"; private static final String COLOR_TEXT = "#FFFFFF"; private static final String COLOR_TEXT_SHADOW = "#000000"; private static final String COLOR_HINT = "#F0C000"; @@ -38,40 +42,57 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { private int screenWidth; private int screenHeight; - private int tilesetRows; - private int tilesetCols; - private int tileHeight; - private int tileWidth; private Bitmap bg; - private Bitmap tile[]; - private Point selection1 = new Point(0,0); - private Point selection2 = new Point(0,0); - private List path=null; - private List pairs=null; + private Point selection1 = new Point(0, 0); + private Point selection2 = new Point(0, 0); + private List path = null; + private List pairs = null; private long startTime; private long playTime; private long baseTime; private Timer timer; - private static Handler timerHandler; + private Tileset tileset; - private boolean timerRegistered=false; + static class hHandler extends Handler { + private final WeakReference mTarget; + + hHandler(ShisenShoView target) { + mTarget = new WeakReference(target); + } + + @Override + public void handleMessage(Message msg) { + ShisenShoView target = mTarget.get(); + if (target != null) + target.onUpdateTime(); + } + } + + private Handler timerHandler = new hHandler(this); + + private boolean timerRegistered = false; private ShisenSho app; private StatePlay cstate; private StatePaint pstate; private Canvas canvas = null; private SurfaceHolder surfaceHolder = null; + private String time = INVALID_TIME; - public ShisenShoView(ShisenSho shishenSho) { - super((Context) shishenSho); - this.app = shishenSho; + public ShisenShoView(ShisenSho shisenSho) { + super((Context) shisenSho); + this.app = shisenSho; cstate = StatePlay.UNINITIALIZED; surfaceHolder = getHolder(); surfaceHolder.addCallback(this); + tileset = new Tileset(shisenSho); } public ShisenShoView(Context ctx) { - super((Context) ctx); - // silence lint? + super(ctx); + this.app = (ShisenSho) ctx; + cstate = StatePlay.UNINITIALIZED; + surfaceHolder = getHolder(); + surfaceHolder.addCallback(this); } private void paint(StatePaint pstate) { @@ -83,45 +104,6 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { this.cstate=cstate; } - private void loadTileset() { - BitmapFactory.Options ops = new BitmapFactory.Options(); - ops.inScaled = false; - Bitmap tileset = BitmapFactory.decodeResource(getResources(), R.drawable.tileset, ops); - tileset.setDensity(Bitmap.DENSITY_NONE); - - // The tile set has 4 rows x 9 columns - tilesetRows = 4; - tilesetCols = 9; - int loadedtileWidth = tileset.getWidth()/tilesetCols; - int loadedtileHeight = tileset.getHeight()/tilesetRows; - tile = new Bitmap[tilesetRows*tilesetCols]; - - // align to screen: - // "large" is 16x6, and we want to have a nice border, so we use 17x7 and - // choose the lowest scale so everything fits - float scalex = ((float) screenWidth/17) / loadedtileWidth; - float scaley = ((float) screenHeight/7) / loadedtileHeight; - if (scaley < scalex) { - scalex = scaley; - } else { - scaley = scalex; - } - Matrix matrix = new Matrix(); - matrix.setScale(scalex, scaley); - - int k=0; - for (int i=0; i 0) { @@ -392,26 +364,21 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { Point a = pair.a; Point b = pair.b; path = app.board.getPath(a, b); - paint.setColor(hintcolor); - paint.setStyle(Style.STROKE); - paint.setStrokeCap(Cap.ROUND); - paint.setStrokeJoin(Join.ROUND); - paint.setStrokeWidth(3); - highlightTile(cbuffer, x0, y0, a, hintcolor); + highlightTile(canvas, x0, y0, a, hintcolor); if (path != null) { Point p0 = null; for (Point p1 : path) { if (p0 != null) { - drawLine(cbuffer, x0, y0, p0, p1, paint); + drawLine(canvas, x0, y0, p0, p1, hintcolor); } p0 = p1; } path = null; } - highlightTile(cbuffer, x0, y0, b, hintcolor); + highlightTile(canvas, x0, y0, b, hintcolor); } break; } @@ -419,16 +386,16 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { // Win & loose notifications switch (pstate) { case WIN: - drawMessage(cbuffer, screenWidth / 2, screenHeight / 2, true, + drawMessage(canvas, screenWidth / 2, screenHeight / 2, true, "You Win!", 100); break; case LOSE: - drawMessage(cbuffer, screenWidth / 2, screenHeight / 2, true, + drawMessage(canvas, screenWidth / 2, screenHeight / 2, true, "Game Over", 100); break; } - if (app.timeCounter) switch (pstate) { + switch (pstate) { case BOARD: case SELECTED1: case SELECTED2: @@ -441,31 +408,21 @@ class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { int hours = (int) (playTime / (60 * 60)); int minutes = (int) ((playTime / 60) % 60); int seconds = (int) (playTime % 60); - String time = String.format(Locale.US, "%01d:%02d:%02d", - hours, minutes, seconds); + if (hours < 10) { + time = String.format(Locale.US, "%01d:%02d:%02d", + hours, minutes, seconds); + } else { + time = INVALID_TIME; + } int timePosX=screenWidth-120; int timePosY=screenHeight-10; - - drawMessage(cbuffer, timePosX, timePosY, false, time, 30); - break; - } - - // Debug messages - /* - debugMessage="StatePlay: "+cstate+"\n"+"StatePaint: "+pstate; - if (debugMessage!=null && debugMessage.length()>0) { - int l = 20; - String lines[] = debugMessage.split("\n"); - for (int i=0; i