diff options
Diffstat (limited to 'src/game_tab/left_panel/board')
| -rw-r--r-- | src/game_tab/left_panel/board/BoardCanvas.cpp | 88 | ||||
| -rw-r--r-- | src/game_tab/left_panel/board/BoardCanvas.hpp | 20 |
2 files changed, 51 insertions, 57 deletions
diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 9c80196..9294006 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -18,9 +18,10 @@ BoardCanvas::BoardCanvas(wxFrame *parent) valid_drag = false; arrow_drag = false; arrows_offset = t->GetSquaresSizes()/2.5; + arrow_thickness = t->GetSquaresSizes()/1.8; // Init animation data adata.duration=100; - adata.duration_fast=100; + adata.duration_fast=50; adata.fps=60; adata.buffer=new wxBitmap(500,500,32); adata.animate=false; @@ -230,30 +231,19 @@ void BoardCanvas::DrawBoard(wxDC &dc) { // Draw highlighted squares for(int i=0;i<(gs.squares_hl.size()+squares_hl.size());i++){ - const std::string &s=i<gs.squares_hl.size() ? gs.squares_hl[i] : squares_hl[i-gs.squares_hl.size()]; - std::uint8_t sfile = s[0]-'a'; - std::uint8_t srank = s[1]-'1'; + const GameState::Square &s=i<gs.squares_hl.size() ? gs.squares_hl[i] : squares_hl[i-gs.squares_hl.size()]; + std::uint8_t sfile = s.square[0]-'a'; + std::uint8_t srank = s.square[1]-'1'; if (!black_side) { srank = 7 - srank; sfile = 7 - sfile; } if(srank == rank && sfile==file){ - char type='a'; - if(s.size()>2) - type=s[2]; - // Default highlight (type='a' or something else not supported) dc.SetPen(wxNullPen); - dc.SetBrush(wxColour(255, 102, 122)); - if(type=='b') - dc.SetBrush(wxColour(120, 255, 102)); - else if(type=='c') - dc.SetBrush(wxColour(102, 196, 255)); - else if(type=='d') - dc.SetBrush(wxColour(255, 204, 213)); - else if(type=='e') - dc.SetBrush(wxColour(220, 255, 204)); - else if(type=='f') - dc.SetBrush(wxColour(204, 231, 255)); + if(s.color!=wxNullColour) + dc.SetBrush(s.color); + else + dc.SetBrush(wxColour(255, 102, 122)); dc.DrawRectangle(wxRect(x,y,square_width,square_width)); } } @@ -396,12 +386,12 @@ void BoardCanvas::DrawBoard(wxDC &dc) { boardY + square_width * 8 + numbers_size.y*2)); } // Draw arrows - for(int i=0;i<(gs.arrows.size()+arrows.size());i++){ - const std::string &arrow= i<gs.arrows.size() ? gs.arrows[i] : arrows[i-gs.arrows.size()]; - std::uint8_t sfile = arrow[0]-'a'; - std::uint8_t srank = arrow[1]-'1'; - std::uint8_t dfile = arrow[2]-'a'; - std::uint8_t drank = arrow[3]-'1'; + for(int i=0;i<(gs.arrows.size()+arrows.size());i++){ + const GameState::Arrow &arrow= i<gs.arrows.size() ? gs.arrows[i] : arrows[i-gs.arrows.size()]; + std::uint8_t sfile = arrow.src[0]-'a'; + std::uint8_t srank = arrow.src[1]-'1'; + std::uint8_t dfile = arrow.dst[0]-'a'; + std::uint8_t drank = arrow.dst[1]-'1'; if (!black_side) { srank = 7 - srank; sfile = 7 - sfile; @@ -414,37 +404,20 @@ void BoardCanvas::DrawBoard(wxDC &dc) { std::uint32_t dy = boardY + drank * square_width + square_width/2; // Parse arrow for metadata (maybe having a datatype is better) - std::uint8_t thickness=50; - if(arrow.size()>4){ - std::string color="#"; - std::string new_thickness=""; - char key='?'; - for(const char &c:arrow){ - if(c=='#'||c=='%'){ - key=c; - continue; - }else if(key=='#'){ - color+=c; - } else if(key=='%'){ - new_thickness+=c; - } - } - if(color.size()>1) - dc.SetBrush(wxColour(color)); - if(new_thickness.size()>0) - thickness=(std::uint8_t)std::stoi(new_thickness); - } else + if(arrow.color!=wxNullColour) + dc.SetBrush(arrow.color); + else dc.SetBrush(color_arrows); if(((abs(drank-srank) == 2) && (abs(dfile-sfile) == 1))|| ((abs(drank-srank) == 1) && (abs(dfile-sfile) == 2))){ if(abs(drank-srank) == 1){ - DrawLArrow(dc,sx,sy,dx,dy,false,thickness); + DrawLArrow(dc,sx,sy,dx,dy,false,arrow_thickness*arrow.scale); } else { - DrawLArrow(dc,sx,sy,dx,dy,true,thickness); + DrawLArrow(dc,sx,sy,dx,dy,true,arrow_thickness*arrow.scale); } } else { - DrawArrow(dc,sx,sy,dx,dy,thickness); + DrawArrow(dc,sx,sy,dx,dy,arrow_thickness*arrow.scale); } } } @@ -519,14 +492,22 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { std::to_string(+active_square.y + 1); std::string dst=((char)('a' + file)) + std::to_string(rank + 1); if(src!=dst){ - arrows.push_back(src+dst); + arrows.push_back(DEFAULT_ARROW(src,dst)); wxLogDebug("Draw arrow %s",src+dst); } else { - if(std::count(squares_hl.begin(), squares_hl.end(), src)){ - squares_hl.erase(std::remove(squares_hl.begin(), squares_hl.end(), src), squares_hl.end()); - }else{ - squares_hl.push_back(src+"f"); + int id=0; + bool removed=false; + for(GameState::Square &s:squares_hl){ + if(s.square==src){ + squares_hl.erase(squares_hl.begin()+id); + removed=true; + break; + } + id++; + } + if(!removed){ + squares_hl.push_back(DEFAULT_SQUARE(src)); wxLogDebug("Highlight square %s",src); } } @@ -548,6 +529,7 @@ void BoardCanvas::Zoom(std::int32_t zoom) { if(!t->Zoom(zoom)) return; t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR); + arrow_thickness = t->GetSquaresSizes()/1.8; Refresh(); } diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 553994c..bc850b7 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -35,6 +35,8 @@ wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent); #define CAPTURE_FACTOR 0.35 #define SQUARE_NUM_PADDING 5 +#define DEFAULT_ARROW(SRC,DST) {(SRC),(DST),wxNullColour,1} +#define DEFAULT_SQUARE(SQUARE) {(SQUARE),wxNullColour} typedef std::tuple<short, short, short> ClockTime; @@ -61,11 +63,20 @@ typedef struct AnimState { } AnimState; typedef struct GameState { + typedef struct Arrow { + std::string src,dst; + wxColour color=wxNullColour; + float scale=1; + } Arrow; + typedef struct Square { + std::string square; + wxColour color=wxNullColour; + } Square; std::string white, black; std::string board; std::map<char, std::uint8_t> captures; - std::vector<std::string> squares_hl; - std::vector<std::string> arrows; + std::vector<Square> squares_hl; + std::vector<Arrow> arrows; bool is_black_turn; bool mat_black; bool mat_white; @@ -78,10 +89,11 @@ class BoardCanvas : public wxPanel { Theme *t, *t_captures; wxColour color_arrows; int arrows_offset; + std::uint8_t arrow_thickness; std::string white_player,black_player; // Current highlighted squares and arrows: - std::vector<std::string> squares_hl; - std::vector<std::string> arrows; + std::vector<GameState::Square> squares_hl; + std::vector<GameState::Arrow> arrows; // Various canvas state variables bool black_side, is_dragging, valid_drag, arrow_drag, is_black_turn; |
