X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/FreeShisen/blobdiff_plain/b023eec723db5ee6d0a6a6aca7795c391d98377e..d0e04237b00df7c11616f359200255a8b47e79d7:/src/de/cwde/shisensho/ShisenShoView.java diff --git a/src/de/cwde/shisensho/ShisenShoView.java b/src/de/cwde/shisensho/ShisenShoView.java new file mode 100644 index 0000000..cb1a57c --- /dev/null +++ b/src/de/cwde/shisensho/ShisenShoView.java @@ -0,0 +1,662 @@ +package de.cwde.shisensho; + +import java.util.List; +import java.util.Locale; +import java.util.Timer; +import java.util.TimerTask; + +import de.cwde.shisensho.R; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Paint.Cap; +import android.graphics.Paint.Join; +import android.graphics.Paint.Style; +import android.graphics.Rect; +import android.graphics.Typeface; +import android.os.Handler; +import android.os.Message; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { + + private enum StatePlay { UNINITIALIZED, IDLE, SELECTED1, SELECTED2, GAMEOVER }; + private enum StatePaint { BOARD, SELECTED1, SELECTED2, MATCHED, WIN, LOSE, HINT, TIME }; + + 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 int[] selection1=new int[2]; + private int[] selection2=new int[2]; + 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 boolean timerRegistered=false; + private ShisenSho app; + private StatePlay cstate; + private StatePaint pstate; + private Canvas canvas = null; + private SurfaceHolder surfaceHolder = null; + public ShisenShoView(ShisenSho shishenSho) { + super((Context)shishenSho); + this.app = shishenSho; + cstate = StatePlay.UNINITIALIZED; + surfaceHolder = getHolder(); + surfaceHolder.addCallback(this); + } + + private void paint(StatePaint pstate) { + this.pstate=pstate; + repaint(); + } + + private void control(StatePlay cstate) { + 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; + tileWidth = tileset.getWidth()/tilesetCols; + tileHeight = tileset.getHeight()/tilesetRows; + tile = new Bitmap[tilesetRows*tilesetCols]; + int k=0; + for (int i=0; i0) { + Line pair=pairs.get(0); + Point a=pair.a; + Point b=pair.b; + path=app.board.getPath(a,b); + paint.setColor(orange); + paint.setStyle(Style.STROKE); + paint.setStrokeCap(Cap.ROUND); + paint.setStrokeJoin(Join.ROUND); + paint.setStrokeWidth(3); + + cbuffer.drawRect(new Rect( + x0+a.j*tileWidth-2, + y0+a.i*tileHeight-2, + x0+a.j*tileWidth-2+tileWidth+2*2, + y0+a.i*tileHeight-2+tileHeight+2*2), + paint); + + if (path!=null) { + Point p0=null; + for (Point p1 : path) { + if (p0!=null) { + cbuffer.drawLine( + x0+p0.j*tileWidth-2+(tileWidth/2), + y0+p0.i*tileHeight-2+(tileHeight/2), + x0+p1.j*tileWidth-2+(tileWidth/2), + y0+p1.i*tileHeight-2+(tileHeight/2), + paint); + } + p0=p1; + } + path=null; + } + + cbuffer.drawRect(new Rect( + x0+b.j*tileWidth-2, + y0+b.i*tileHeight-2, + x0+b.j*tileWidth-2+tileWidth+2*2, + y0+b.i*tileHeight-2+tileHeight+2*2), + paint); + } + break; + } + + // Win & loose notifications + switch (pstate) { + case WIN: + drawMessage(cbuffer, screenWidth/2,screenHeight/2,true,"You Win!", "#FFFFFF", 100); + break; + case LOSE: + drawMessage(cbuffer, screenWidth/2,screenHeight/2,true,"Game Over", "#FFFFFF", 100); + break; + } + + if (app.timeCounter) switch (pstate) { + case BOARD: + case SELECTED1: + case SELECTED2: + case MATCHED: + case WIN: + case LOSE: + case HINT: + case TIME: + updateTime(); + 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); + + int timePosX=screenWidth-120; + int timePosY=screenHeight-10; + + drawMessage(cbuffer, timePosX+1,timePosY+1,false,time,"#000000",30); + drawMessage(cbuffer, timePosX,timePosY,false,time,"#FFFFFF",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=0 && + i=0 && j=0 && i=0 && j0) { + app.board.play(a,b); + } + path=null; + paint(StatePaint.BOARD); + + pairs=app.board.getPairs(1); + if (pairs.size()==0) { + if (app.board.getNumPieces()==0) { + paint(StatePaint.WIN); + } else { + paint(StatePaint.LOSE); + } + control(StatePlay.GAMEOVER); + } else { + control(StatePlay.IDLE); + } + //undo.sensitive=app.board.getCanUndo(); + } + } + break; + case GAMEOVER: + reset(); + paint(StatePaint.BOARD); + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + surfaceHolder = holder; + if (cstate!=StatePlay.GAMEOVER && app.timeCounter && !timerRegistered) { + registerTimer(); + } + repaint(); + } + + public void surfaceCreated(SurfaceHolder holder) { + surfaceHolder = holder; + repaint(); + } + + public void surfaceDestroyed(SurfaceHolder holder) { + surfaceHolder = null; + if (timerRegistered) { + unregisterTimer(); + } + } + + /* + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (!initialized) initialize(); + + long currTime = System.currentTimeMillis(); + + a = (float)(currTime - startTime) / (float)duration; + if (a > (float)1.0) a = (float)1.0; + + x = Math.round(nextx*a + prevx*(1-a)); + y = Math.round(nexty*a + prevy*(1-a)); + + if (a == (float)1.0) computeNextTarget(); + + int bgWidth = bg.getWidth(); + int bgHeight = bg.getHeight(); + for (int i=0; i