Improve arrow drawing

This commit is contained in:
Loic Guegan 2023-01-09 09:55:20 +01:00
parent 335547e081
commit f1a54fd165
2 changed files with 22 additions and 3 deletions

View file

@ -13,9 +13,11 @@ wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
BoardCanvas::BoardCanvas(wxFrame *parent)
: wxPanel(parent), black_side(false), frozen(false),
lock_square_size(false), t(new Theme()), t_captures(new Theme()) {
color_arrows=wxColour(145, 233, 255);
is_dragging = false;
valid_drag = false;
valid_arrow = false;
arrows_offset = t->GetSquaresSizes()/2.5;
// Init animation data
adata.duration=100;
adata.duration_fast=100;
@ -409,6 +411,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) {
std::uint32_t dx = boardX + (7 - dfile) * square_width + square_width/2;
std::uint32_t dy = boardY + drank * square_width + square_width/2;
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){
@ -538,6 +541,14 @@ void BoardCanvas::DrawArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst){
double angle=acos(vect.x/length);
angle= (vect.y>0) ? angle=angle : -angle;
// Translate starting point (xsrc,ysrc) about arrows_offset px to not start on the center of the square (less confusing visually)
double k=arrows_offset/length;
xsrc=xsrc+(xdst-xsrc)*k;
ysrc=ysrc+(ydst-ysrc)*k;
vect.x=(xdst-xsrc);
vect.y=(ydst-ysrc);
length=ceil(sqrt(pow(vect.x,2)+pow(vect.y,2)));
// Compute metrics
std::uint8_t thickness=50;
std::uint8_t tip_height=sqrt(3)/2*thickness;
@ -557,7 +568,6 @@ void BoardCanvas::DrawArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst){
// Draw
dc.SetPen(wxNullPen);
dc.SetBrush(wxColour(255,0,0));
dc.DrawPolygon(3,tip);
dc.DrawPolygon(4,tail);
}
@ -572,6 +582,15 @@ void BoardCanvas::DrawLArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst, b
return;
}
// Translate starting point (xsrc,ysrc) about arrows_offset px to not start on the center of the square (less confusing visually)
if(!flip)
xsrc+= (xdst-xsrc) > 0 ? arrows_offset : -arrows_offset;
else
ysrc+= (ydst-ysrc) > 0 ? arrows_offset : -arrows_offset;
X=xdst-xsrc;
Y=ydst-ysrc;
// Draw L shaped arrows
if(flip){
// Compute metrics
std::uint8_t thickness=50;
@ -589,7 +608,6 @@ void BoardCanvas::DrawLArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst, b
// Draw
dc.SetPen(wxNullPen);
dc.SetBrush(wxColour(255,0,0));
dc.DrawPolygon(4,tail1);
dc.DrawPolygon(4,tail2);
dc.DrawPolygon(3,tip);
@ -610,7 +628,6 @@ void BoardCanvas::DrawLArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst, b
// Draw
dc.SetPen(wxNullPen);
dc.SetBrush(wxColour(255,0,0));
dc.DrawPolygon(4,tail1);
dc.DrawPolygon(4,tail2);
dc.DrawPolygon(3,tip);

View file

@ -76,6 +76,8 @@ class BoardCanvas : public wxPanel {
// *t is theme for board+pieces and
// *t_captures is theme for captured pieces (scale down version of t)
Theme *t, *t_captures;
wxColour color_arrows;
int arrows_offset;
// Board to draw (char version)
std::string board;
std::string white_player,black_player;