www.pudn.com > Bluegammon蓝牙的应用编程.rar > CursorAnim.java


// Copyright (c) 2005 Sony Ericsson Mobile Communications AB 
// 
// This software is provided "AS IS," without a warranty of any kind.  
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  
// 
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se) 
 
package bluegammon.gui.animation; 
 
import javax.microedition.lcdui.Graphics; 
 
import bluegammon.gui.BoardCanvas; 
import bluegammon.logic.Board; 
import bluegammon.logic.BoardMediator; 
 
/** 
 * Animation of the cursors indicating the source and destination index of a 
 * possible movement. 
 *  
 * @author Peter Andersson 
 */ 
public class CursorAnim extends Animation 
{ 
  protected static final int SOURCE_COL = 0x22ffff; 
  protected static final int DEST_COL = 0xffff22; 
  protected static int m_cursorSize; 
  protected int m_sx, m_sy, 
  				m_dx, m_dy, 
  				m_csx, m_cdx, 
  				m_xAdjustS, m_xAdjustD, 
  				m_frame; 
 
  protected boolean m_sourceLeft, m_destLeft; 
  protected volatile boolean m_stopped; 
 
  /** 
   * Initializes this animation with the size of the cursor, depending on the 
   * canvas size of this device. 
   *  
   * @param cursorSize The size of the cursor. 
   */ 
  public static void init(int cursorSize) 
  { 
    m_cursorSize = cursorSize; 
  } 
 
  /** 
   * Creates a new cursor animation indicating where user moves a piece from and 
   * where the piece will end up. 
   *  
   * @param source   Source index of piece. 
   * @param dest     Destination index of piece. 
   * @param bxOnSrc  Number of pieces on source row. 
   * @param bxOnDst  Number of pieces on destination row. 
   * @param white    True for white player, false for black. 
   */ 
  public CursorAnim(int source, int dest, int bxOnSrc, int bxOnDst, 
      boolean white) 
  { 
    BoardCanvas canvas = BoardCanvas.getInstance(); 
 
    int srcPieceCount = Math.min(5, bxOnSrc); 
    int dstPieceCount = Math.min(5, bxOnDst); 
    srcPieceCount = Math.max(srcPieceCount, 1); 
 
    if (bxOnSrc > 4) 
      m_xAdjustS = -m_cursorSize; 
    if (bxOnDst > 4) 
      m_xAdjustD = m_cursorSize; 
 
    // Get source and destination coordinates for cursors 
    m_sx = canvas.getPieceX(source, srcPieceCount, white); 
    m_sy = canvas.getPieceY(source, srcPieceCount, white); 
    m_dx = canvas.getPieceX(dest, dstPieceCount, white); 
    m_dy = canvas.getPieceY(dest, dstPieceCount, white); 
 
    if (source <= Board.POS_BOARD) 
    { 
      m_sourceLeft = source > 11; 
    } 
    else if (source == Board.POS_GUARD) 
    { 
      m_sourceLeft = white; 
    } 
 
    if (dest <= Board.POS_BOARD) 
    { 
      m_destLeft = dest < 12; 
    } 
    else if (dest == Board.POS_OUT) 
    { 
      m_destLeft = !white; 
    } 
  } 
 
  public void paint(Graphics g) 
  { 
    if (!m_stopped) 
    { 
      g.setColor(SOURCE_COL); 
      m_csx = m_sx; 
      if (m_sourceLeft) 
        m_csx += m_xAdjustS + m_frame; 
      else 
        m_csx -= m_xAdjustS + m_frame; 
      drawArrow(g, m_csx, m_sy, m_sourceLeft); 
      g.setColor(DEST_COL); 
      m_cdx = m_dx; 
      if (m_destLeft) 
        m_cdx += m_xAdjustD + m_frame; 
      else 
        m_cdx -= m_xAdjustD + m_frame; 
      drawArrow(g, m_cdx, m_dy, m_destLeft); 
    } 
  } 
 
  /** 
   * Draws an arrow used for cursor 
   *  
   * @param g    Graphics context to draw to. 
   * @param x    X coordinate of arrow. 
   * @param y    Y coordinate of arrow. 
   * @param left True for left pointing arrow, false for right. 
   */ 
  protected void drawArrow(Graphics g, int x, int y, boolean left) 
  { 
    int w = 3; 
    int h = m_cursorSize / 2; 
    if (left) 
    { 
      for (int dy = 0; dy < h; dy++) 
        g.drawLine(x + (h - dy), y + dy, x + (h - dy) + w, y + dy); 
      for (int dy = h; dy <= (h << 1); dy++) 
        g.drawLine(x + (dy - h), y + dy, x + (dy - h) + w, y + dy); 
    } 
    else 
    { 
      for (int dy = 0; dy < h; dy++) 
        g.drawLine(x + dy, y + dy, x + dy + w, y + dy); 
      for (int dy = h; dy <= (h << 1); dy++) 
        g.drawLine(x + ((h << 1) - dy), y + dy, 
            	   x + ((h << 1) - dy) + w, y + dy); 
    } 
  } 
 
  public void next() 
  { 
    m_frame = (m_frame - 1) & 3; 
    BoardCanvas.getInstance().requestRepaint(); 
  } 
 
  public boolean isFinished() 
  { 
    return m_stopped || BoardMediator.isGameFinished() 
        || !BoardCanvas.getInstance().isUserMovable(); 
  } 
 
  public long getInterval() 
  { 
    return 200; 
  } 
 
  /** 
   * Stops this cursor animation. 
   */ 
  public void stop() 
  { 
    m_stopped = true; 
  } 
}