aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/left_panel/board/BoardCanvas.hpp
blob: 34c49119afd52cc2a33464b502854025aa5ff9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include "Theme.hpp"
#include "ochess.hpp"
#include <map>
#include <tuple>
#include <utility>
#include <vector>
#include <wx/artprov.h>

// 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

typedef std::tuple<short, short, short> ClockTime;

class BoardCanvas : public wxPanel {
  Theme *t, *t_captures;
  std::string board;
  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;

public:
  BoardCanvas(wxFrame *parent);
  BoardCanvas(wxFrame *parent,std::uint32_t square_width, bool frozen);
  void ApplyPreferences();
  void DrawBoard(wxPaintDC &dc);
  void OnPaint(wxPaintEvent &event);
  void OnKeyEvent(wxKeyEvent &event);
  void MouseEvent(wxMouseEvent &event);
  void Zoom(std::int32_t zoom);
  void Swap();
  void SetupBoard(std::string board, bool is_black_turn,
                  std::map<char, std::uint8_t> captures);
  void SetClockTime(short hours, short min, short sec, bool IsBlack);
  DECLARE_EVENT_TABLE()
};