mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-06 01:56:28 +02:00
Improve UI
This commit is contained in:
parent
73f7be6c03
commit
56252f2b6c
5 changed files with 69 additions and 54 deletions
|
@ -92,6 +92,14 @@ void Game::SetMoveAsMainline(HalfMove *m) {
|
|||
}
|
||||
}
|
||||
|
||||
bool Game::IsCheckmate(bool forBlack){
|
||||
arbiter.Setup(GetFen());
|
||||
if(forBlack){
|
||||
return arbiter.IsBlackTurn() && arbiter.IsCheckMate();
|
||||
}
|
||||
return !arbiter.IsBlackTurn() && arbiter.IsCheckMate();
|
||||
}
|
||||
|
||||
bool Game::Play(std::string move) {
|
||||
wxLogDebug("Playing move %s", move);
|
||||
std::string fen = GetFen();
|
||||
|
|
|
@ -31,6 +31,7 @@ public:
|
|||
std::string GetResult();
|
||||
bool Play(std::string move);
|
||||
bool IsBlackToPlay();
|
||||
bool IsCheckmate(bool forBlack);
|
||||
void Previous();
|
||||
void Next();
|
||||
void DeleteMove(HalfMove *m);
|
||||
|
|
|
@ -99,14 +99,19 @@ void GameTabLeftPanel::Notify(bool skip_animation) {
|
|||
}
|
||||
|
||||
// Update board canvas:
|
||||
GameState gs;
|
||||
gs.board=chessarbiter::FENParser::Parse(fen).board;
|
||||
gs.is_black_turn=game->IsBlackToPlay();
|
||||
gs.captures=captures;
|
||||
gs.white=game->GetTag("White");
|
||||
gs.black=game->GetTag("Black");
|
||||
gs.mat_black=game->IsCheckmate(true);
|
||||
gs.mat_white=game->IsCheckmate(false);
|
||||
if(skip_animation || !animate){
|
||||
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
|
||||
game->IsBlackToPlay(), captures,
|
||||
game->GetTag("White"),game->GetTag("Black"));
|
||||
board_canvas->SetupBoard(gs);
|
||||
}
|
||||
else{
|
||||
board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
|
||||
game->IsBlackToPlay(), captures,src,dst,repeat);
|
||||
board_canvas->Animate(gs, src,dst,repeat);
|
||||
}
|
||||
// Update last move
|
||||
last_move=m;
|
||||
|
|
|
@ -3,29 +3,36 @@
|
|||
wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
||||
|
||||
BoardCanvas::BoardCanvas(wxFrame *parent)
|
||||
: wxPanel(parent), black_side(false), is_black_turn(true), frozen(false),
|
||||
: wxPanel(parent), black_side(false), frozen(false),
|
||||
lock_square_size(false), t(new Theme()), t_captures(new Theme()) {
|
||||
board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR";
|
||||
is_dragging = false;
|
||||
valid_drag = false;
|
||||
adata.reuseBuffer=false;
|
||||
adata.buffer=nullptr;
|
||||
t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR);
|
||||
SetClockTime(-1, -1, -1, false);
|
||||
SetClockTime(-1, -1, -1, true);
|
||||
ApplyPreferences();
|
||||
// The following should be called when using an EVT_PAINT handler
|
||||
SetBackgroundStyle(wxBG_STYLE_PAINT);
|
||||
// Init animation data
|
||||
adata.duration=200;
|
||||
adata.duration_fast=80;
|
||||
adata.fps=30;
|
||||
adata.buffer=new wxBitmap(500,500,32);
|
||||
adata.reuseBuffer=false;
|
||||
adata.buffer=nullptr;
|
||||
// Init game state
|
||||
gs.is_black_turn=false;
|
||||
gs.board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR";
|
||||
gs.mat_black=false;
|
||||
gs.mat_white=false;
|
||||
// Init clocks
|
||||
SetClockTime(-1, -1, -1, false);
|
||||
SetClockTime(-1, -1, -1, true);
|
||||
// Init capture pieces
|
||||
t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR);
|
||||
// Load preferences
|
||||
ApplyPreferences();
|
||||
// The following should be called when using an EVT_PAINT handler
|
||||
SetBackgroundStyle(wxBG_STYLE_PAINT);
|
||||
// Let GameTableLeftPanel process keyboard events:
|
||||
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
|
||||
Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
|
||||
Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this);
|
||||
Bind(wxEVT_SIZE, &BoardCanvas::OnResize, this);
|
||||
|
||||
// Mouse events:
|
||||
Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this);
|
||||
Bind(wxEVT_LEFT_DOWN, &BoardCanvas::MouseEvent, this);
|
||||
|
@ -113,30 +120,26 @@ void BoardCanvas::ApplyPreferences() {
|
|||
Refresh();
|
||||
}
|
||||
|
||||
void BoardCanvas::SetupBoard(std::string board, bool is_black_turn,
|
||||
std::map<char, std::uint8_t> captures,
|
||||
std::string white_player, std::string black_player) {
|
||||
gs.board = board;
|
||||
gs.is_black_turn = is_black_turn;
|
||||
gs.captures = captures;
|
||||
gs.white=white_player;
|
||||
gs.black=black_player;
|
||||
|
||||
this->board = board;
|
||||
this->is_black_turn = is_black_turn;
|
||||
this->captures = captures;
|
||||
this->white_player=white_player;
|
||||
this->black_player=black_player;
|
||||
void BoardCanvas::SetupBoard(const GameState &new_gs) {
|
||||
gs.board = new_gs.board;
|
||||
gs.is_black_turn = new_gs.is_black_turn;
|
||||
gs.captures = new_gs.captures;
|
||||
gs.white=new_gs.white;
|
||||
gs.black=new_gs.black;
|
||||
gs.mat_white=new_gs.mat_white;
|
||||
gs.mat_black=new_gs.mat_black;
|
||||
gs.black_time=new_gs.black_time;
|
||||
gs.white_time=new_gs.white_time;
|
||||
Refresh();
|
||||
}
|
||||
|
||||
void BoardCanvas::Animate(const std::string &board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst,bool faster){
|
||||
void BoardCanvas::Animate(const GameState &new_gs, std::string src, std::string dst, bool faster){
|
||||
std::uint8_t pfile = src[0]-'a';
|
||||
std::uint8_t prank = src[1]-'1';
|
||||
adata.piece_moved = this->board[pfile + 8 * (7-prank)]; // Piece to move
|
||||
adata.piece_moved = gs.board[pfile + 8 * (7-prank)]; // Piece to move
|
||||
|
||||
// Now remove the piece that will be moved
|
||||
this->board[pfile + 8 * (7-prank)]=' ';
|
||||
gs.board[pfile + 8 * (7-prank)]=' ';
|
||||
wxMemoryDC memDC(*adata.buffer);
|
||||
DrawBoard(memDC);
|
||||
|
||||
|
@ -183,7 +186,7 @@ void BoardCanvas::Animate(const std::string &board, bool is_black_turn, std::map
|
|||
}
|
||||
adata.duration=faster ? duration_backup : duration_backup;
|
||||
adata.reuseBuffer=false;
|
||||
SetupBoard(board, is_black_turn, captures,white_player,black_player);
|
||||
SetupBoard(new_gs);
|
||||
}
|
||||
|
||||
void BoardCanvas::DrawBoard(wxDC &dc) {
|
||||
|
@ -236,7 +239,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
}
|
||||
std::uint32_t px = x + centrer_offset;
|
||||
std::uint32_t py = y + centrer_offset;
|
||||
char piece = board[(7 - pfile) + 8 * prank];
|
||||
char piece = gs.board[(7 - pfile) + 8 * prank];
|
||||
if (is_dragging && (7 - pfile) == active_square.x &&
|
||||
(7 - prank) == active_square.y) {
|
||||
dp = piece;
|
||||
|
@ -247,7 +250,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
}
|
||||
if (piece != ' ') {
|
||||
dc.DrawBitmap(*t->Get(piece), px, py, false);
|
||||
if(piece == 'k' || piece == 'K')
|
||||
if((piece == 'k' && gs.mat_black) || (piece == 'K' && gs.mat_white))
|
||||
dc.DrawBitmap(*t->Get('#'), x+square_width/2+centrer_offset, y+centrer_offset, false);
|
||||
}
|
||||
}
|
||||
|
@ -257,7 +260,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
dc.SetPen(wxPen(*wxBLACK, 3));
|
||||
std::uint32_t badgeY = boardY;
|
||||
std::uint32_t badgeWidth = square_width / 2;
|
||||
if (is_black_turn) {
|
||||
if (gs.is_black_turn) {
|
||||
dc.SetBrush(*wxBLACK_BRUSH);
|
||||
if (black_side) {
|
||||
badgeY = boardY + (8 * square_width) - badgeWidth;
|
||||
|
@ -286,15 +289,15 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
offsetYPlayerName = offsetY+captures_size;
|
||||
}
|
||||
for (char p : {'P', 'N', 'B', 'R', 'Q'}) {
|
||||
if (captures.find(p) != captures.end()) {
|
||||
for (std::uint8_t i = 0; i < captures[p]; i++) {
|
||||
if (gs.captures.find(p) != gs.captures.end()) {
|
||||
for (std::uint8_t i = 0; i < gs.captures[p]; i++) {
|
||||
dc.DrawBitmap(*t_captures->Get(p), boardX + offsetX, boardY + offsetY);
|
||||
offsetX += captures_size / 2;
|
||||
}
|
||||
offsetX += captures_size / 2;
|
||||
}
|
||||
}
|
||||
dc.DrawText(wxString(black_player),boardX,boardY + offsetYPlayerName);
|
||||
dc.DrawText(wxString(gs.black),boardX,boardY + offsetYPlayerName);
|
||||
// Black (white's captures):
|
||||
offsetX = 0;
|
||||
if (black_side) {
|
||||
|
@ -305,15 +308,15 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
offsetYPlayerName = offsetY+captures_size;
|
||||
}
|
||||
for (char p : {'p', 'n', 'b', 'r', 'q'}) {
|
||||
if (captures.find(p) != captures.end()) {
|
||||
for (std::uint8_t i = 0; i < captures[p]; i++) {
|
||||
if (gs.captures.find(p) != gs.captures.end()) {
|
||||
for (std::uint8_t i = 0; i < gs.captures[p]; i++) {
|
||||
dc.DrawBitmap(*t_captures->Get(p), boardX + offsetX, boardY + offsetY);
|
||||
offsetX += captures_size / 2;
|
||||
}
|
||||
offsetX += captures_size / 2;
|
||||
}
|
||||
}
|
||||
dc.DrawText(wxString(white_player),boardX,boardY + offsetYPlayerName);
|
||||
dc.DrawText(wxString(gs.white),boardX,boardY + offsetYPlayerName);
|
||||
|
||||
// Draw dragging piece
|
||||
if (DrawDraggingPiece) {
|
||||
|
@ -327,9 +330,9 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
}
|
||||
|
||||
// Draw Clocks
|
||||
if (std::get<0>(black_time) >= 0) {
|
||||
if (std::get<0>(gs.black_time) >= 0) {
|
||||
wxFont font = dc.GetFont();
|
||||
ClockTime clock = black_side ? white_time : black_time;
|
||||
ClockTime clock = black_side ? gs.white_time : gs.black_time;
|
||||
wxString time =
|
||||
wxString::Format("%ds", std::get<1>(clock), std::get<2>(clock));
|
||||
if (std::get<0>(clock) > 0) {
|
||||
|
@ -342,7 +345,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
|
|||
dc.GetTextExtent(time, &width, &height);
|
||||
dc.DrawText(time,
|
||||
wxPoint(boardX + square_width * 8 - width, boardY - height - numbers_size.y*2));
|
||||
clock = black_side ? black_time : white_time;
|
||||
clock = black_side ? gs.black_time : gs.white_time;
|
||||
time = wxString::Format("%ds", std::get<1>(clock), std::get<2>(clock));
|
||||
if (std::get<0>(clock) > 0) {
|
||||
time = wxString::Format("%d:%d", std::get<0>(clock), std::get<1>(clock));
|
||||
|
@ -394,7 +397,7 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) {
|
|||
if (IsCurrentSquareValid) {
|
||||
active_square.x = file;
|
||||
active_square.y = rank;
|
||||
if (board[(7 - rank) * 8 + file] != ' ') {
|
||||
if (gs.board[(7 - rank) * 8 + file] != ' ') {
|
||||
wxLogDebug("Drag start on square (%d,%d)", file, rank);
|
||||
valid_drag = true;
|
||||
}
|
||||
|
@ -423,8 +426,8 @@ void BoardCanvas::Swap() {
|
|||
void BoardCanvas::SetClockTime(short hours, short min, short sec,
|
||||
bool IsBlack) {
|
||||
if (IsBlack) {
|
||||
black_time = std::make_tuple(hours, min, sec);
|
||||
gs.black_time = std::make_tuple(hours, min, sec);
|
||||
} else {
|
||||
white_time = std::make_tuple(hours, min, sec);
|
||||
gs.white_time = std::make_tuple(hours, min, sec);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,6 +65,7 @@ typedef struct GameState {
|
|||
bool is_black_turn;
|
||||
bool mat_black;
|
||||
bool mat_white;
|
||||
ClockTime black_time, white_time;
|
||||
} GameState;
|
||||
|
||||
class BoardCanvas : public wxPanel {
|
||||
|
@ -82,7 +83,6 @@ class BoardCanvas : public wxPanel {
|
|||
wxSize canvas_size;
|
||||
wxPoint active_square;
|
||||
std::map<char, std::uint8_t> captures;
|
||||
ClockTime black_time, white_time;
|
||||
bool frozen,lock_square_size;
|
||||
|
||||
// Current animation state
|
||||
|
@ -100,9 +100,7 @@ public:
|
|||
void Zoom(std::int32_t zoom);
|
||||
void Swap();
|
||||
void OnResize(wxSizeEvent &e);
|
||||
void SetupBoard(std::string board, bool is_black_turn,
|
||||
std::map<char, std::uint8_t> captures,
|
||||
std::string white_player, std::string black_player);
|
||||
void Animate(const std::string &board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst,bool faster);
|
||||
void SetupBoard(const GameState &new_gs);
|
||||
void Animate(const GameState &new_gs, std::string src, std::string dst,bool faster);
|
||||
void SetClockTime(short hours, short min, short sec, bool IsBlack);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue