ochess/src/game_tab/left_panel/board/BoardCanvas.hpp

109 lines
4 KiB
C++
Raw Normal View History

2022-02-23 18:11:55 +01:00
#pragma once
#include "Theme.hpp"
#include "ochess.hpp"
#include <map>
#include <tuple>
#include <utility>
#include <vector>
#include <wx/artprov.h>
2022-12-28 08:38:21 +01:00
#include <wx/dcbuffer.h>
2022-02-23 18:11:55 +01:00
// Local events
wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
// Foreign events
wxDECLARE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent);
#define REFRESH_MOUSE_LOCATION() \
{ \
const wxPoint pt = wxGetMousePosition(); \
mouseX = pt.x - this->GetScreenPosition().x; \
mouseY = pt.y - this->GetScreenPosition().y; \
}
#define INIT_CURRENT_SQUARE() \
std::uint32_t file = 7 - (mouseX - boardX) / square_width; \
std::uint32_t rank = (mouseY - boardY) / square_width; \
if (!black_side) { \
file = 7 - file; \
rank = 7 - rank; \
} \
bool IsCurrentSquareValid = file >= 0 && file <= 7 && rank >= 0 && rank <= 7;
#define MOUSE_ON(x, y, width, height) \
(mouseX >= (x) && mouseX <= ((x) + (width)) && mouseY >= (y) && \
mouseY <= ((y) + (height)))
#define CAPTURE_FACTOR 0.5
2022-12-29 18:32:35 +01:00
#define SQUARE_NUM_PADDING 5
2022-02-23 18:11:55 +01:00
typedef std::tuple<short, short, short> ClockTime;
2022-12-29 13:00:23 +01:00
// Drawing buffer (ANIMATIONS)
typedef struct AnimState {
2022-12-29 13:24:23 +01:00
/// @brief Temporary buffer to reduce latency
2022-12-29 13:00:23 +01:00
wxBitmap *buffer;
2022-12-29 13:24:23 +01:00
/// @brief Should *buffer be used?
2022-12-29 13:00:23 +01:00
bool reuseBuffer;
2022-12-29 13:24:23 +01:00
/// @brief Current animated frame
int frame;
/// @brief Total number of frames for the animation
int frames;
/// @brief Animation durations (in ms)
int duration,duration_fast;
/// @brief Animation FPS
std::uint8_t fps;
/// @brief Board to draw at the end of the animation
2022-12-29 13:00:23 +01:00
std::string final_board;
2022-12-29 13:24:23 +01:00
/// @brief Which player is to move at the end of the animation
2022-12-29 13:00:23 +01:00
bool final_is_black_turn;
2022-12-29 13:24:23 +01:00
/// @brief Final state of captured pieces at the end of the animation
2022-12-29 13:00:23 +01:00
std::map<char, std::uint8_t> final_captures;
2022-12-29 13:24:23 +01:00
/// @brief Current animated piece
2022-12-29 13:00:23 +01:00
char piece_moved;
2022-12-29 13:24:23 +01:00
/// @brief Starting point of the animated piece
2022-12-29 13:00:23 +01:00
wxPoint src;
2022-12-29 13:24:23 +01:00
/// @brief Translation vector of the animated piece
2022-12-29 13:00:23 +01:00
wxPoint transVect;
} AnimState;
2022-02-23 18:11:55 +01:00
class BoardCanvas : public wxPanel {
2022-12-28 10:37:57 +01:00
// *t is theme for board+pieces and
// *t_captures is theme for captured pieces (scale down version of t)
2022-02-23 18:11:55 +01:00
Theme *t, *t_captures;
2022-12-28 10:37:57 +01:00
// Board to draw (char version)
2022-02-23 18:11:55 +01:00
std::string board;
2022-12-30 13:17:06 +01:00
std::string white_player,black_player;
2022-12-28 10:37:57 +01:00
// Various canvas state variables
2022-02-23 18:11:55 +01:00
bool black_side, is_dragging, valid_drag, is_black_turn;
std::uint32_t boardX, boardY, square_width, mouseX, mouseY, lastClickX,
lastClickY;
wxSize canvas_size;
wxPoint active_square;
std::map<char, std::uint8_t> captures;
ClockTime black_time, white_time;
bool frozen,lock_square_size;
2022-12-29 13:24:23 +01:00
// Current animation state
2022-12-29 13:00:23 +01:00
AnimState adata;
2022-12-28 10:01:32 +01:00
2022-02-23 18:11:55 +01:00
public:
BoardCanvas(wxFrame *parent);
BoardCanvas(wxFrame *parent,std::uint32_t square_width, bool frozen);
~BoardCanvas();
2022-02-23 18:11:55 +01:00
void ApplyPreferences();
2022-12-28 10:37:57 +01:00
void DrawBoard(wxDC &dc);
2022-02-23 18:11:55 +01:00
void OnPaint(wxPaintEvent &event);
void MouseEvent(wxMouseEvent &event);
void Zoom(std::int32_t zoom);
void Swap();
void SetupBoard(std::string board, bool is_black_turn,
2022-12-30 13:17:06 +01:00
std::map<char, std::uint8_t> captures,
std::string white_player, std::string black_player);
2022-12-29 15:02:17 +01:00
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);
2022-02-23 18:11:55 +01:00
void SetClockTime(short hours, short min, short sec, bool IsBlack);
DECLARE_EVENT_TABLE()
};