📄 boardcanvas.java
字号:
tmpCol = col1;
col1 = col2;
col2 = tmpCol;
int x = 0;
int k = (2 * m_triangK) / 2;
for (int yy = y; yy < y + m_diagH/2; yy++)
{
drawAntialiasedLine(0,x,yy, col1, COL_BOARD, ig, k);
drawAntialiasedLine(m_width, m_width - x, yy, col2, COL_BOARD, ig, k);
x += k;
}
for (int yy = y + m_diagH / 2; yy < y + m_diagH; yy++)
{
drawAntialiasedLine(m_width, m_width - x, yy, col2, COL_BOARD, ig, k);
drawAntialiasedLine(0,x,yy, col1, COL_BOARD, ig, k);
x -= k;
}
}
}
}
m_boardRegions[Board.POS_OUT][0] = 0;
m_boardRegions[Board.POS_OUT][1] = 0;
m_boardRegions[Board.POS_OUT][2] = m_width;
m_boardRegions[Board.POS_OUT][3] = m_boardRegions[0][1] - 1;
m_boardRegions[Board.POS_GUARD][0] = 0;
m_boardRegions[Board.POS_GUARD][1] = m_boardRegions[5][1] + m_boardRegions[5][3];
m_boardRegions[Board.POS_GUARD][2] = m_width;
m_boardRegions[Board.POS_GUARD][3] = m_boardRegions[6][1] - m_boardRegions[5][1] -
m_boardRegions[5][3] - 1;
}
return m_cleanBoardImg;
}
/**
* Draws pieces for a specified row-index.
* @param g Graphics context to draw to.
* @param index The row-index.
*/
protected void drawPieces(Graphics g, int index)
{
Context3D c3d = Context3D.getInstance();
Graphics3D g3d = c3d.bindScene(g);
int x,y;
// on board
if (index <= Board.POS_BOARD)
{
for (int j = 0; j < getPieces(index); j++)
{
drawPiece3D(getPieceX(index, j, isWhite(index)),
getPieceY(index, j, isWhite(index)),
0, false, 0, isWhite(index), c3d, g3d);
}
}
// on guard
else if (index == Board.POS_GUARD)
{
for (int i = 0; i < getPieces(true, Board.POS_GUARD); i++)
{
drawPiece3D(getPieceX(Board.POS_GUARD, i, true),
getPieceY(Board.POS_GUARD, i, true),
0, false, 0, true, c3d, g3d);
}
for (int i = 0; i < getPieces(false, Board.POS_GUARD); i++)
{
drawPiece3D(getPieceX(Board.POS_GUARD, i, false),
getPieceY(Board.POS_GUARD, i, false),
0, false, 0, false, c3d, g3d);
}
}
// out
else if (index == Board.POS_OUT)
{
for (int i = 0; i < getPieces(true, Board.POS_OUT); i++)
{
drawPiece3D(getPieceX(Board.POS_OUT, i, true),
getPieceY(Board.POS_OUT, i, true),
-60, false, 0, true, c3d, g3d);
}
for (int i = 0; i < getPieces(false, Board.POS_OUT); i++)
{
drawPiece3D(getPieceX(Board.POS_OUT, i, false),
getPieceY(Board.POS_OUT, i, false),
60, false, 0, false, c3d, g3d);
}
}
m_dirtyRows[index] = false;
g3d.releaseTarget();
}
/**
* Returns x coordinate for a piece on specified row-index.
* @param index The row-index.
* @param pieceNo The piece number on this index.
* @param white true if white, false if black.
* @return the x-coordinate.
*/
public int getPieceX(int index, int pieceNo, boolean white)
{
int x = 0;
if (index == Board.POS_GUARD)
{
if (white)
{
x = m_width / 2 + (1 * m_pieceDiameter) / 2 + 1;
}
else
{
x = m_width / 2 - (3 * m_pieceDiameter) / 2 - 1;
}
x -= 2 * pieceNo;
}
else if (index == Board.POS_OUT)
{
if (white)
{
x = m_width - m_pieceDiameter - 1 - pieceNo * 2;
}
else
{
x = 1 + pieceNo * 2;
}
}
else
{
if (index < 12)
{
x = 0;
}
else
{
x = m_width - m_pieceDiameter - 1;
}
int dx = Math.min(pieceNo, 4) * (m_diagH + 1);
dx += (pieceNo - Math.min(pieceNo,4)) * 2;
if (index <= BoardState.POS_BOARD / 2)
{
x += dx;
}
else
{
x -= dx;
}
}
return x;
}
/**
* Returns y coordinate for a piece on specified row-index.
* @param index The row-index.
* @param pieceNo The piece number on this index.
* @param white true if white, false if black.
* @return the y-coordinate.
*/
public int getPieceY(int index, int pieceNo, boolean white)
{
int y = 0;
if (index == Board.POS_GUARD)
{
y = 2 + 7 * m_diagH;
}
else if (index == Board.POS_OUT)
{
y = 1;
}
else
{
if (index < 6) // 0 - 5 upper left
y = 2 + (index+1) * m_diagH;
else if (index < 12) // 6 - 11 bottom left
y = 4 + (index+2) * m_diagH;
else if (index < 18) // 12 - 17 bottom right
y = m_boardH - 2 - (index - 10) * m_diagH;
else // 18 - 23 upper right
y = m_boardH - 4 - (index - 9) * m_diagH;
}
return y;
}
/**
* Draws a piece on specified coordinates.
* @param x x-coordinate.
* @param y y-coordinate.
* @param angle Piece angle.
* @param horizTilt True for horizontal tilt, false for vertical.
* @param dz Piece delta z.
* @param white True for white piece, false for black.
* @param g Graphics context to draw to.
*/
public void drawPiece(int x, int y,
float angle, boolean horizTilt,
float dz, boolean white, Graphics g)
{
Context3D c3d = Context3D.getInstance();
Graphics3D g3d = c3d.bindScene(g);
drawPiece3D(x,y,angle,horizTilt,dz,white,c3d,g3d);
g3d.releaseTarget();
}
/**
* Draws a piece on specified coordinates.
* @param x x-coordinate.
* @param y y-coordinate.
* @param angle Piece angle.
* @param horizTilt True for horizontal tilt, false for vertical.
* @param dz Piece delta z.
* @param white True for white piece, false for black.
* @param g3d 3D Graphics context to draw to.
*/
public void drawPiece3D(int x, int y,
float angle, boolean horizTilt,
float dz, boolean white, Context3D c3d, Graphics3D g3d)
{
float z = PIECE_Z + dz;
int size = (int)(c3d.toScreenCoordinateX(2f,z)+0.5f);
x += size/2;
y += size/2;
m_transform.setIdentity();
m_transform.postTranslate(
c3d.to3DCoordinateX(x,z-dz), c3d.to3DCoordinateY(y+1,z-dz), z);
if (horizTilt)
{
m_transform.postRotate(angle,1f,0,0);
}
else
{
m_transform.postRotate(angle,0,1f,0);
}
g3d.render(c3d.getPieceVertexBuffer(), c3d.getPieceIndexBuffer(),
c3d.getPieceAppearance(white), m_transform);
}
/**
* Draws a horizontal antialiased line.
* @param x1 Start x - coordinate.
* @param x2 End x - coordinate.
* @param y Y - coordinate.
* @param col1 Starting color.
* @param col2 End color.
* @param g Graphics context to draw to.
*/
protected void drawAntialiasedLine(int x1, int x2, int y, int col1, int col2, Graphics g, int k)
{
if (x1 == x2) return;
g.setColor(col1);
int col;
if (x1 > x2) k = -k;
int x = (x2 - k) << 8;
int dx = (k << 8) >> 3;
g.drawLine(x1,y,x>>8,y);
for (int i = 0; i < 8; i++)
{
col = blendColors(col1, col2, i);
g.setColor(col);
g.drawLine(x>>8,y,(x+dx)>>8,y);
x += dx;
}
}
/**
* Blends two colors.
* @param col1 Color 1
* @param col2 Color 2
* @param i A number between 0 and 7, 0 means color 1, 7 means color 2.
* @return The blended color.
*/
protected int blendColors(int col1, int col2, int i)
{
if (i < 0)
i = 0;
else if (i > 8)
i = 8;
int ii = 8 - i;
return (((i * (col2 & 0xff0000)) >>> 3) & 0xff0000)
+ (((ii * (col1 & 0xff0000)) >>> 3) & 0xff0000)
| (((i * (col2 & 0x00ff00)) >>> 3) & 0x00ff00)
+ (((ii * (col1 & 0x00ff00)) >>> 3) & 0x00ff00)
| (((i * (col2 & 0x0000ff)) >>> 3) & 0x0000ff)
+ (((ii * (col1 & 0x0000ff)) >>> 3) & 0x0000ff);
}
// RequestRepaintable impl
/**
* Requests a repaint of whole canvas.
*/
public void requestRepaint()
{
m_repaint = true;
}
/**
* Committs all repaint request in an actual repaint.
* Called from animation logic.
*/
public void commitRepaint()
{
if (m_repaint)
{
repaint();
m_repaint = false;
}
}
// I/O
/**
* Saves the state of the graphical representation of the board.
* @param dos The output stream.
* @return Number of bytes written.
* @throws IOException if save failed.
*/
public int saveCanvas(DataOutputStream dos) throws IOException
{
int len = 0;
dos.writeBoolean(m_queryCommit);
len += 1;
dos.writeBoolean(m_undoOn);
len += 1;
dos.writeInt(m_diceValues[0]);
len += 4;
dos.writeInt(m_diceValues[1]);
len += 4;
dos.writeInt(m_consumeStatuses[0]);
len += 4;
dos.writeInt(m_consumeStatuses[1]);
len += 4;
return len;
}
/**
* Loads the state of a graphical representation of the board.
* @param dis The input stream.
* @return Number of bytes read.
* @throws IOException if load failed.
*/
public int loadCanvas(DataInputStream dis) throws IOException
{
int len = 0;
m_queryCommit = dis.readBoolean();
len += 1;
m_undoOn = dis.readBoolean();
len += 1;
m_diceValues[0] = dis.readInt();
len += 4;
m_diceValues[1] = dis.readInt();
len += 4;
m_consumeStatuses[0] = dis.readInt();
len += 4;
m_consumeStatuses[1] = dis.readInt();
len += 4;
m_animationEngine.removeAll();
allowInteraction(true);
setDrawDiceValues(true);
setQueryCommit(m_queryCommit);
updateUndoCommand();
return len;
}
/**
* C'tor and initialization
*/
protected BoardCanvas()
{
setFullScreenMode(true);
m_width = getWidth();
m_height = getHeight();
// calculate common graphic properties on given width and height
m_diagW = (3 * m_width) / 8;
m_diagH = (m_height-6) / 15;
m_triangK = (2 * m_diagW) / m_diagH;
m_boardH = m_diagH * 15 + 6;
m_pieceDiameter = m_diagH - 2;
m_diceValueUnit = 1 + m_pieceDiameter / 7;
for (int i = 0; i < 6; i++)
{
m_diceValPts[i] = new int[i + 1][2];
switch (i)
{
case 0:
m_diceValPts[0][0][0] = 3 * m_diceValueUnit;
m_diceValPts[0][0][1] = 3 * m_diceValueUnit;
break;
case 1:
m_diceValPts[1][0][0] = 1 * m_diceValueUnit;
m_diceValPts[1][0][1] = 1 * m_diceValueUnit;
m_diceValPts[1][1][0] = 5 * m_diceValueUnit;
m_diceValPts[1][1][1] = 5 * m_diceValueUnit;
break;
case 2:
m_diceValPts[2][0][0] = 5 * m_diceValueUnit;
m_diceValPts[2][0][1] = 1 * m_diceValueUnit;
m_diceValPts[2][1][0] = 3 * m_diceValueUnit;
m_diceValPts[2][1][1] = 3 * m_diceValueUnit;
m_diceValPts[2][2][0] = 1 * m_diceValueUnit;
m_diceValPts[2][2][1] = 5 * m_diceValueUnit;
break;
case 3:
m_diceValPts[3][0][0] = 1 * m_diceValueUnit;
m_diceValPts[3][0][1] = 1 * m_diceValueUnit;
m_diceValPts[3][1][0] = 5 * m_diceValueUnit;
m_diceValPts[3][1][1] = 1 * m_diceValueUnit;
m_diceValPts[3][2][0] = 1 * m_diceValueUnit;
m_diceValPts[3][2][1] = 5 * m_diceValueUnit;
m_diceValPts[3][3][0] = 5 * m_diceValueUnit;
m_diceValPts[3][3][1] = 5 * m_diceValueUnit;
break;
case 4:
m_diceValPts[4][0][0] = 1 * m_diceValueUnit;
m_diceValPts[4][0][1] = 1 * m_diceValueUnit;
m_diceValPts[4][1][0] = 5 * m_diceValueUnit;
m_diceValPts[4][1][1] = 1 * m_diceValueUnit;
m_diceValPts[4][2][0] = 1 * m_diceValueUnit;
m_diceValPts[4][2][1] = 5 * m_diceValueUnit;
m_diceValPts[4][3][0] = 5 * m_diceValueUnit;
m_diceValPts[4][3][1] = 5 * m_diceValueUnit;
m_diceValPts[4][4][0] = 3 * m_diceValueUnit;
m_diceValPts[4][4][1] = 3 * m_diceValueUnit;
break;
case 5:
m_diceValPts[5][0][0] = 1 * m_diceValueUnit;
m_diceValPts[5][0][1] = 1 * m_diceValueUnit;
m_diceValPts[5][1][0] = 1 * m_diceValueUnit;
m_diceValPts[5][1][1] = 3 * m_diceValueUnit;
m_diceValPts[5][2][0] = 1 * m_diceValueUnit;
m_diceValPts[5][2][1] = 5 * m_diceValueUnit;
m_diceValPts[5][3][0] = 5 * m_diceValueUnit;
m_diceValPts[5][3][1] = 1 * m_diceValueUnit;
m_diceValPts[5][4][0] = 5 * m_diceValueUnit;
m_diceValPts[5][4][1] = 3 * m_diceValueUnit;
m_diceValPts[5][5][0] = 5 * m_diceValueUnit;
m_diceValPts[5][5][1] = 5 * m_diceValueUnit;
break;
}
}
// Initialize 3d context and animations
Context3D.getInstance().init(getWidth(), getHeight());
CursorAnim.init(m_pieceDiameter);
PieceMoveAnim.init(m_pieceDiameter);
DicesNewTurnAnim.init(m_diceValueUnit);
// Start animation engine
m_animationEngine = new AnimationEngine(this);
new Thread(m_animationEngine, "AnimationThread").start();
// Create image buffer
m_pieceBoardImg = Image.createImage(m_width, m_boardH);
// Initialize softbuttons
m_softbuttons.init(this,
Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL), CMD_UNDO, CMD_EXIT);
m_softbuttons.setCommandListener(this);
}
/**
* Returns singleton instance.
* @return The singleton instance.
*/
public static BoardCanvas getInstance()
{
if (m_inst == null)
{
m_inst = new BoardCanvas();
m_inst.setFullScreenMode(true);
}
return m_inst;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -