Commit 448399d1 authored by Loic Guegan's avatar Loic Guegan
Browse files

Cleaning Editor code

parent b70571c1
Pipeline #219605347 passed with stage
in 8 minutes and 37 seconds
......@@ -16,7 +16,7 @@ public:
bool WhiteToPlay;
/// @brief How much units variations should be shifted to the right
int variationOffset;
int VarOffset;
/// @brief Width of a column/row
int colWidth,rowWidth;
int scrollbarThickness;
......@@ -26,11 +26,11 @@ public:
int hintBarWidth;
/// @brief Size of the move counter column
int counterW;
int varationSepW;
int extraCanvasMargin;
int VarSepH;
int ExtraCanvasSize;
EditorConfig(){
variationOffset=20;
VarOffset=20;
colWidth=50;
rowWidth=20;
scrollbarThickness=20;
......@@ -40,8 +40,8 @@ public:
leftMargin=10;
hintBarWidth=30;
counterW=20;
varationSepW=5;
extraCanvasMargin=50;
VarSepH=10;
ExtraCanvasSize=50;
}
};
......
......@@ -21,15 +21,15 @@ void PGNEditor::Render() {
this->LineCount = 1;
this->CurrentX = Config.leftMargin + Config.hintBarWidth;
this->CurrentY = Config.topMargin;
InCanvasSize.w = CanvasW - Config.scrollbarThickness;
InCanvasSize.h = CanvasH - Config.scrollbarThickness;
InCanvasSize.w = CanvasSize.w - Config.scrollbarThickness;
InCanvasSize.h = CanvasSize.h - Config.scrollbarThickness;
this->MaxX = CurrentX;
this->MaxY = CurrentY;
this->MinX = CurrentX;
this->MinY = CurrentY;
if (IsMouseClicked) {
this->LastMouseClickX = MouseX;
this->LastMouseClickY = MouseY;
this->LastMouseClick.x = Mouse.x;
this->LastMouseClick.y = Mouse.y;
}
// Start Drawing
......@@ -46,7 +46,7 @@ void PGNEditor::Render() {
void PGNEditor::DrawHintBar() {
// Never issue a call to DrawCall here (otherwise MaxX and MaxY will be corrupted)
Shape hintBar(OffsetX, 0, Config.hintBarWidth, InCanvasSize.h);
Shape hintBar(Offset.x, 0, Config.hintBarWidth, InCanvasSize.h);
hintBar.target = Shape::HINTBAR;
Draw(hintBar); // Do not issues a DrawCall here (MaxY will change);
}
......@@ -62,8 +62,8 @@ void PGNEditor::ScrollV(int percent) {
float percent2=CurrentVSY / (InCanvasSize.h - VSSize);
percent2=clamp(percent2,0.0,1.0);
int maxOffset = std::max((MaxY - MinY) - InCanvasSize.h, 0);
OffsetY = -(maxOffset * percent2);
OffsetY = clamp(OffsetY,-maxOffset,0);
Offset.y = -(maxOffset * percent2);
Offset.y = clamp(Offset.y,-maxOffset,0);
}
void PGNEditor::ScrollH(int percent) {
......@@ -77,16 +77,16 @@ void PGNEditor::ScrollH(int percent) {
float percent2=CurrentHSX / (InCanvasSize.w - HSSize);
percent2=clamp(percent2,0.0,1.0);
int maxOffset = std::max((MaxX - MinX) - InCanvasSize.w, 0);
OffsetX = -(maxOffset * percent2);
OffsetX = clamp(OffsetX,-maxOffset,0);
Offset.x = -(maxOffset * percent2);
Offset.x = clamp(Offset.x,-maxOffset,0);
}
bool PGNEditor::MouseHover(Shape shape){
int x=shape.x+OffsetX;
int y=shape.y+OffsetY;
int x=shape.x+Offset.x;
int y=shape.y+Offset.y;
int maxX=x+shape.GetWidth(&Config);
int maxY=y+shape.GetHeight(&Config);
return((x <= MouseX) && (y <= MouseY) && (maxX >= MouseX) && (maxY >= MouseY));
return((x <= Mouse.x) && (y <= Mouse.y) && (maxX >= Mouse.x) && (maxY >= Mouse.y));
}
void PGNEditor::SetConfiguration(EditorConfig config){
......@@ -100,9 +100,9 @@ void PGNEditor::DrawScrollBars() {
Shape corner(InCanvasSize.w, InCanvasSize.h, Config.scrollbarThickness,
Config.scrollbarThickness);
corner.target = Shape::SCROLLBAR_CORNER;
if (corner.x <= MouseX && corner.y <= MouseY
&& CanvasW >= MouseX
&& CanvasH >= MouseY)
if (corner.x <= Mouse.x && corner.y <= Mouse.y
&& CanvasSize.w >= Mouse.x
&& CanvasSize.h >= Mouse.y)
corner.mouseHover = true;
Draw(corner);
......@@ -114,27 +114,27 @@ void PGNEditor::DrawScrollBars() {
if (IsMouseDragging) {
if (LastMouseClickOnHS) {
CurrentHSX += (MouseX - LastMouseClickX);
LastMouseClickX = MouseX;
CurrentHSX += (Mouse.x - LastMouseClick.x);
LastMouseClick.x = Mouse.x;
CurrentHSX=clamp(CurrentHSX,0,InCanvasSize.w - HSSize);
float percent=CurrentHSX / (InCanvasSize.w - HSSize);
percent=clamp(percent,0.0,1.0);
int maxOffset = std::max((MaxX - MinX) - InCanvasSize.w, 0);
OffsetX = -(maxOffset * percent);
OffsetX = clamp(OffsetX,-maxOffset,0);
Offset.x = -(maxOffset * percent);
Offset.x = clamp(Offset.x,-maxOffset,0);
}
else if (LastMouseClickOnVS) {
CurrentVSY += (MouseY - LastMouseClickY);
LastMouseClickY = MouseY;
CurrentVSY += (Mouse.y - LastMouseClick.y);
LastMouseClick.y = Mouse.y;
CurrentVSY=clamp(CurrentVSY,0,InCanvasSize.h - VSSize);
float percent=CurrentVSY / (InCanvasSize.h - VSSize);
percent=clamp(percent,0.0,1.0);
int maxOffset = std::max((MaxY - MinY) - InCanvasSize.h, 0);
OffsetY = -(maxOffset * percent);
OffsetY = clamp(OffsetY,-maxOffset,0);
Offset.y = -(maxOffset * percent);
Offset.y = clamp(Offset.y,-maxOffset,0);
}
}
//std::cout << "MaxX:" << MaxX << " MinX " << MinX << " Diff:"<< MaxX-MinX << " OffsetX " << OffsetX <<std::endl << std::flush;
//std::cout << "MaxX:" << MaxX << " MinX " << MinX << " Diff:"<< MaxX-MinX << " Offset.x " << Offset.x <<std::endl << std::flush;
CurrentHSX=clamp(CurrentHSX,0,InCanvasSize.w - HSSize);
CurrentVSY=clamp(CurrentVSY,0,InCanvasSize.h - VSSize);
......@@ -144,15 +144,15 @@ void PGNEditor::DrawScrollBars() {
VS.target = Shape::SCROLLBAR_V;
// Check mouse over
if (HS.x <= MouseX && HS.y <= MouseY
&& (HS.x + HSSize) >= MouseX
&& (HS.y + Config.scrollbarThickness) >= MouseY) {
if (HS.x <= Mouse.x && HS.y <= Mouse.y
&& (HS.x + HSSize) >= Mouse.x
&& (HS.y + Config.scrollbarThickness) >= Mouse.y) {
HS.mouseHover = true;
HS.mouseClick = IsMouseClicked;
}
if (VS.x <= MouseX && VS.y <= MouseY
&& (VS.x + Config.scrollbarThickness) >= MouseX
&& (VS.y + VSSize) >= MouseY) {
if (VS.x <= Mouse.x && VS.y <= Mouse.y
&& (VS.x + Config.scrollbarThickness) >= Mouse.x
&& (VS.y + VSSize) >= Mouse.y) {
VS.mouseHover = true;
VS.mouseClick = IsMouseClicked;
}
......@@ -209,17 +209,17 @@ void PGNEditor::DrawVariations(Move *line) {
bool WhiteToPlayBak = WhiteToPlay;
int CurrentXBack = CurrentX;
int MoveCountBak = MoveCount;
CurrentX += Config.variationOffset;
CurrentX += Config.VarOffset;
// Draw separator
Shape sep(CurrentX, CurrentY, Config.counterW + Config.colWidth * 2,
Config.varationSepW);
Config.VarSepH);
if(line->fold)
sep.target=Shape::VAR_SEP_FOLD;
else
sep.target = Shape::VAR_SEP;
DrawCall(sep);
CurrentY += Config.varationSepW;
CurrentY += Config.VarSepH;
if(IsMouseClicked){
if(MouseHover(sep)){
......@@ -262,8 +262,8 @@ void PGNEditor::DrawCount() {
}
void PGNEditor::DrawCall(Shape shape) {
shape.x += OffsetX;
shape.y += OffsetY;
shape.x += Offset.x;
shape.y += Offset.y;
int height = shape.GetHeight(&Config);
int width = shape.GetWidth(&Config);
......@@ -271,17 +271,17 @@ void PGNEditor::DrawCall(Shape shape) {
int maxX = shape.x + width;
int maxY = shape.y + height;
if ((maxX + Config.extraCanvasMargin) > MaxX)
MaxX = (maxX + Config.extraCanvasMargin);
if ((maxY + Config.extraCanvasMargin) > MaxY)
MaxY = (maxY + Config.extraCanvasMargin);
if ((maxX + Config.ExtraCanvasSize) > MaxX)
MaxX = (maxX + Config.ExtraCanvasSize);
if ((maxY + Config.ExtraCanvasSize) > MaxY)
MaxY = (maxY + Config.ExtraCanvasSize);
if (shape.x < MinX)
MinX = shape.x;
if (shape.y < MinY)
MinY = shape.y;
if (shape.x <= MouseX && shape.y <= MouseY
&& maxX >= MouseX && maxY >= MouseY) {
if (shape.x <= Mouse.x && shape.y <= Mouse.y
&& maxX >= Mouse.x && maxY >= Mouse.y) {
shape.mouseHover = true;
shape.mouseClick = IsMouseClicked;
}
......
......@@ -25,15 +25,15 @@ private:
int MoveCount;
int LineCount;
Point Current;
int CurrentX;
int CurrentY;
int LastMouseClickX;
int LastMouseClickY;
bool LastMouseClickOnHS;
bool LastMouseClickOnVS;
Point LastMouseClick;
int CurrentVSY;
int CurrentHSX;
int OffsetY,OffsetX;
Point Offset;
bool LastMouseClickOnHS,LastMouseClickOnVS;
/// @brief Used by scrollbars
int MaxX,MinX;
......@@ -50,8 +50,8 @@ private:
protected:
bool IsMouseClicked;
bool IsMouseDragging;
int CanvasW,CanvasH;
int MouseX,MouseY;
Size CanvasSize;
Point Mouse;
Move *MoveLine;
bool WhiteToPlay;
......@@ -65,12 +65,6 @@ public:
PGNEditor():PGNEditor(EditorConfig()){}
PGNEditor(EditorConfig config):LastMouseClickOnHS(false),LastMouseClickOnVS(false),CurrentVSY(0),CurrentHSX(0){
this->Config=config;
OffsetY=0;
OffsetX=0;
CanvasW=0;
CanvasH=0;
MouseX=0;
MouseY=0;
WhiteToPlay=true;
}
};
......
......@@ -14,7 +14,9 @@ class Point {
public:
int x,y;
Point():x(0),y(0){
}
bool operator==(const Point p) const{
return(x==p.x && y==p.y);
}
};
......@@ -24,6 +26,18 @@ public:
Size():w(0),h(0){
}
bool operator==(const Size &s) const{
return(w==s.w && h==s.h);
}
bool operator!=(const Size &s) const {
return(!(*this==s));
}
bool operator>(const Size &s) const {
return(w*h > s.h*s.w);
}
bool operator<(const Size &s) const {
return(!(*this>s) && *this!=s);
}
};
}
......
......@@ -49,12 +49,12 @@ void Editor::MouseEvent(wxMouseEvent &event) {
void Editor::OnPaint(wxPaintEvent &event) {
wxPaintDC dc(this);
DC = &dc;
DC->GetSize(&CanvasW, &CanvasH);
DC->GetSize(&CanvasSize.w, &CanvasSize.h);
// Update mouse position
const wxPoint pt = wxGetMousePosition();
MouseX = pt.x - this->GetScreenPosition().x;
MouseY = pt.y - this->GetScreenPosition().y;
Mouse.x = pt.x - this->GetScreenPosition().x;
Mouse.y = pt.y - this->GetScreenPosition().y;
// Render editor
Render();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment