bluegammon.gui
Class BoardCanvas

java.lang.Object
  extended byjavax.microedition.lcdui.Displayable
      extended byjavax.microedition.lcdui.Canvas
          extended bybluegammon.gui.PopupCanvas
              extended bybluegammon.gui.BoardCanvas
All Implemented Interfaces:
javax.microedition.lcdui.CommandListener, RepaintRequestable

public class BoardCanvas
extends PopupCanvas
implements javax.microedition.lcdui.CommandListener, RepaintRequestable

Singleton class handling all graphical details of a backgammon view. There can only be one BoardCanvas per device, thus the singleton pattern.

BoardCanvas handles the BackGammon board gui view. Contains functionality for signalling gui changes on user interaction. Uses a Board instance for representation of gui state of the backgammon board.

The BoardCanvas also contains drawing primitives for the backgammon board.

Author:
Peter Andersson
See Also:
BoardMediator, BoardState

Field Summary
static javax.microedition.lcdui.Command CMD_EXIT
          Exit command
static javax.microedition.lcdui.Command CMD_UNDO
          Undo command
static float PIECE_Z
          The 3d z depth of a piece lying on backgammon board
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Method Summary
 void addPiece(boolean white, int index)
          Adds a new piece to the canvas
 void addWinningPieceAnimation(boolean white, WinnerAnim winAnim)
          Adds a piece animation when there is a winner.
 void allowInteraction(boolean allow)
          Sets whether the canvas accepts any interaction or not.
 void commandAction(javax.microedition.lcdui.Command c, javax.microedition.lcdui.Displayable d)
          Handles command invokations from SoftButtonControl.
 void commitRepaint()
          Committs all repaint request in an actual repaint.
 void consumeDiceValue(int diceVal)
          Called from logic when user has made a move using specified dice value.
 void cursorBackIndex()
          Steps backward amongst cursor indices.
 void cursorNearestIndex(int oldIndex, int oldTarget)
          Moves to nearest cursor index after a change in possible moves.
 void cursorNextIndex()
          Steps forward amongst cursor indices.
 void cursorSwapSide()
          Moves cursor to nearest matching cursor index on opposite side of board, given current cursor position.
 void drawPiece(int x, int y, float angle, boolean horizTilt, float dz, boolean white, javax.microedition.lcdui.Graphics g)
          Draws a piece on specified coordinates.
 void drawPiece3D(int x, int y, float angle, boolean horizTilt, float dz, boolean white, Context3D c3d, javax.microedition.m3g.Graphics3D g3d)
          Draws a piece on specified coordinates.
 void finishGame(boolean white, int pieces, int pts)
          Setups gui when there is a winner.
 int getBoardHeight()
          Returns the actual height of the board in pixels
 javax.microedition.lcdui.Image getBoardImage()
          Returns an image of a clean backgammon board (without pieces).
 int getCurrentCursorIndex()
          Returns current cursor index of possible moves.
static BoardCanvas getInstance()
          Returns singleton instance.
 int getPieces(boolean white, int index)
          Returns number of pieces on specified index of specified color
 int getPieces(int index)
          Returns number pieces on specified index
 int getPieceX(int index, int pieceNo, boolean white)
          Returns x coordinate for a piece on specified row-index.
 int getPieceY(int index, int pieceNo, boolean white)
          Returns y coordinate for a piece on specified row-index.
 void invalidate()
          Invalidates this canvas, forcing a full repaint on next call to paint()
 boolean isCursorValid()
          Returns whether there is a valid cursor or not.
 boolean isUserMovable()
          Returns whether the player can move any pieces or not.
 boolean isWhite(int index)
          Returns true if there are white pieces on specified index, returns false if there are black or no pieces.
 int loadCanvas(java.io.DataInputStream dis)
          Loads the state of a graphical representation of the board.
 void movePiece(boolean white, int from, int to, int piecesOnSrc, int piecesOnDst)
          Called from logic when a piece is moved.
 void removePiece(boolean white, int index)
          Removes a piece from the canvas
 void requestRepaint()
          Requests a repaint of whole canvas.
 int saveCanvas(java.io.DataOutputStream dos)
          Saves the state of the graphical representation of the board.
 void selectTurns(int wDice, int bDice)
          Called from logic on a new game, when players toss one dice each and the one with highest value starts.
 void setCurrentLocalPlayer(LocalPlayer player)
          Set the local player that will receive interaction calls made on this canvas.
 void setDiceValues(int dice1, int dice2)
          Sets the dice values to be drawn and resets consume status
 void setDrawDiceValues(boolean enable)
          Enables/Disables drawing of dice values.
 void setPieces(boolean white, int index, int pieces)
          Sets number of pieces on specified index
 void setQueryCommit(boolean commit)
          Called from logic when the user should commit his/her moves or when the user has commited.
 void setStartPositions()
          Sets up start positions for white and black player
 void shutdown()
          Shuts down the canvas logic.
 void throwDices(boolean white)
          Called from logic when dices are thrown on a new turn, starts the dice animation.
 void undoConsumedDiceValue(int diceVal)
          Called from logic when user has undoed a move based on specified dice value.
 void updateCursor()
          Updates the cursor.
 void updateUndoCommand()
          Enables/disables the possibility for user to perform a undo, depending on game state.
 boolean waitingForCommit()
          Returns whether the canvas is waiting for the user to commit his/her moves.
 
Methods inherited from class bluegammon.gui.PopupCanvas
getPopup, setPopup
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, isDoubleBuffered, repaint, repaint, serviceRepaints, setFullScreenMode
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setCommandListener, setTicker, setTitle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface bluegammon.gui.animation.RepaintRequestable
isShown, repaint
 

Field Detail

PIECE_Z

public static final float PIECE_Z
The 3d z depth of a piece lying on backgammon board

See Also:
Constant Field Values

CMD_UNDO

public static final javax.microedition.lcdui.Command CMD_UNDO
Undo command


CMD_EXIT

public static final javax.microedition.lcdui.Command CMD_EXIT
Exit command

Method Detail

setStartPositions

public void setStartPositions()
Sets up start positions for white and black player


selectTurns

public void selectTurns(int wDice,
                        int bDice)
Called from logic on a new game, when players toss one dice each and the one with highest value starts.

Parameters:
wDice - value of white dice
bDice - value of black dice

throwDices

public void throwDices(boolean white)
Called from logic when dices are thrown on a new turn, starts the dice animation.

Parameters:
white - the color of dices

movePiece

public void movePiece(boolean white,
                      int from,
                      int to,
                      int piecesOnSrc,
                      int piecesOnDst)
Called from logic when a piece is moved.

Parameters:
white - Color of piece
from - From index.
to - To index.
piecesOnSrc - Number of pieces on source index
piecesOnDst - Number of pieces on dest index

consumeDiceValue

public void consumeDiceValue(int diceVal)
Called from logic when user has made a move using specified dice value. Updates consume status.

Parameters:
diceVal - The dice value used

undoConsumedDiceValue

public void undoConsumedDiceValue(int diceVal)
Called from logic when user has undoed a move based on specified dice value. Updates specified dicevalue consume status.

Parameters:
diceVal - The dice value being undoed

setQueryCommit

public void setQueryCommit(boolean commit)
Called from logic when the user should commit his/her moves or when the user has commited.

Parameters:
commit - True if user should commit, false if user has commited.

finishGame

public void finishGame(boolean white,
                       int pieces,
                       int pts)
Setups gui when there is a winner.

Parameters:
white - The color of the winner.
pieces - Pieces left for loser.
pts - Points per piece,

invalidate

public void invalidate()
Invalidates this canvas, forcing a full repaint on next call to paint()


shutdown

public void shutdown()
Shuts down the canvas logic.


setCurrentLocalPlayer

public void setCurrentLocalPlayer(LocalPlayer player)
Set the local player that will receive interaction calls made on this canvas.

Parameters:
player - The local player, or null if current player is not local.

addPiece

public void addPiece(boolean white,
                     int index)
Adds a new piece to the canvas

Parameters:
white - true if white piece, false if black
index - where to place the piece

removePiece

public void removePiece(boolean white,
                        int index)
Removes a piece from the canvas

Parameters:
white - true if white piece, false if black
index - where from to remove the piece

setPieces

public void setPieces(boolean white,
                      int index,
                      int pieces)
Sets number of pieces on specified index

Parameters:
white - true if white piece, false if black
index - The board index
pieces - Number of pieces

getPieces

public int getPieces(int index)
Returns number pieces on specified index

Parameters:
index - The board index
Returns:
Number of pieces

getPieces

public int getPieces(boolean white,
                     int index)
Returns number of pieces on specified index of specified color

Parameters:
white - true if white piece, false if black
index - The board index
Returns:
Number of pieces

isWhite

public boolean isWhite(int index)
Returns true if there are white pieces on specified index, returns false if there are black or no pieces.

Parameters:
index - The board index
Returns:
true if white, false if black.

isUserMovable

public boolean isUserMovable()
Returns whether the player can move any pieces or not.

Returns:
true if user can move pieces, false if not.

getCurrentCursorIndex

public int getCurrentCursorIndex()
Returns current cursor index of possible moves.

Returns:
the cursor index.

waitingForCommit

public boolean waitingForCommit()
Returns whether the canvas is waiting for the user to commit his/her moves.

Returns:
true if waiting for commit, false if not.

updateCursor

public void updateCursor()
Updates the cursor. Called when possible moves has been altered. Stops any current cursor animation and creates a new one if there are possible moves.


updateUndoCommand

public void updateUndoCommand()
Enables/disables the possibility for user to perform a undo, depending on game state.


allowInteraction

public void allowInteraction(boolean allow)
Sets whether the canvas accepts any interaction or not.

Parameters:
allow - enable/disable interaction.

setDiceValues

public void setDiceValues(int dice1,
                          int dice2)
Sets the dice values to be drawn and resets consume status

Parameters:
dice1 - value of dice 1 (0-5)
dice2 - value of dice 2 (0-5)

setDrawDiceValues

public void setDrawDiceValues(boolean enable)
Enables/Disables drawing of dice values.

Parameters:
enable - True when dice values should be drawn, falsw when dice values should not be drawn.

commandAction

public void commandAction(javax.microedition.lcdui.Command c,
                          javax.microedition.lcdui.Displayable d)
Handles command invokations from SoftButtonControl.

Specified by:
commandAction in interface javax.microedition.lcdui.CommandListener
Parameters:
c - the command.
d - the displayable.

cursorNearestIndex

public void cursorNearestIndex(int oldIndex,
                               int oldTarget)
Moves to nearest cursor index after a change in possible moves.

Parameters:
oldIndex - old index before change
oldTarget - old target index before change

cursorSwapSide

public void cursorSwapSide()
Moves cursor to nearest matching cursor index on opposite side of board, given current cursor position.


cursorNextIndex

public void cursorNextIndex()
Steps forward amongst cursor indices.


cursorBackIndex

public void cursorBackIndex()
Steps backward amongst cursor indices.


isCursorValid

public boolean isCursorValid()
Returns whether there is a valid cursor or not.

Returns:
True if there is a valid cursor, false otherwise.

getBoardHeight

public int getBoardHeight()
Returns the actual height of the board in pixels

Returns:
the board height

addWinningPieceAnimation

public void addWinningPieceAnimation(boolean white,
                                     WinnerAnim winAnim)
Adds a piece animation when there is a winner. Selects among existing pieces on board.

Parameters:
white - True to animate white pieces, false for black.
winAnim - The comprising winning animation.

getBoardImage

public javax.microedition.lcdui.Image getBoardImage()
Returns an image of a clean backgammon board (without pieces).

Returns:
A backgammon board image.

getPieceX

public int getPieceX(int index,
                     int pieceNo,
                     boolean white)
Returns x coordinate for a piece on specified row-index.

Parameters:
index - The row-index.
pieceNo - The piece number on this index.
white - true if white, false if black.
Returns:
the x-coordinate.

getPieceY

public int getPieceY(int index,
                     int pieceNo,
                     boolean white)
Returns y coordinate for a piece on specified row-index.

Parameters:
index - The row-index.
pieceNo - The piece number on this index.
white - true if white, false if black.
Returns:
the y-coordinate.

drawPiece

public void drawPiece(int x,
                      int y,
                      float angle,
                      boolean horizTilt,
                      float dz,
                      boolean white,
                      javax.microedition.lcdui.Graphics g)
Draws a piece on specified coordinates.

Parameters:
x - x-coordinate.
y - y-coordinate.
angle - Piece angle.
horizTilt - True for horizontal tilt, false for vertical.
dz - Piece delta z.
white - True for white piece, false for black.
g - Graphics context to draw to.

drawPiece3D

public void drawPiece3D(int x,
                        int y,
                        float angle,
                        boolean horizTilt,
                        float dz,
                        boolean white,
                        Context3D c3d,
                        javax.microedition.m3g.Graphics3D g3d)
Draws a piece on specified coordinates.

Parameters:
x - x-coordinate.
y - y-coordinate.
angle - Piece angle.
horizTilt - True for horizontal tilt, false for vertical.
dz - Piece delta z.
white - True for white piece, false for black.
g3d - 3D Graphics context to draw to.

requestRepaint

public void requestRepaint()
Requests a repaint of whole canvas.

Specified by:
requestRepaint in interface RepaintRequestable

commitRepaint

public void commitRepaint()
Committs all repaint request in an actual repaint. Called from animation logic.

Specified by:
commitRepaint in interface RepaintRequestable

saveCanvas

public int saveCanvas(java.io.DataOutputStream dos)
               throws java.io.IOException
Saves the state of the graphical representation of the board.

Parameters:
dos - The output stream.
Returns:
Number of bytes written.
Throws:
java.io.IOException - if save failed.

loadCanvas

public int loadCanvas(java.io.DataInputStream dis)
               throws java.io.IOException
Loads the state of a graphical representation of the board.

Parameters:
dis - The input stream.
Returns:
Number of bytes read.
Throws:
java.io.IOException - if load failed.

getInstance

public static BoardCanvas getInstance()
Returns singleton instance.

Returns:
The singleton instance.