Commit 533da878 authored by Loic Guegan's avatar Loic Guegan
Browse files

Improve editor

parent c823df22
Pipeline #219445261 passed with stage
in 8 minutes and 36 seconds
......@@ -15,6 +15,7 @@ public:
std::string *move;
Move* next;
std::vector<Move*> variations;
Move():move(nullptr),next(nullptr){}
bool fold;
Move():move(nullptr),next(nullptr),fold(false){}
};
}
......@@ -22,26 +22,71 @@ void PGNEditor::Render(State *state) {
this->MaxY = 1;
this->MinX = 0;
this->MinY = 0;
if(state->MouseClick){
this->LastMouseClickX=state->mouseX;
this->LastMouseClickY=state->mouseY;
if (state->MouseClick) {
this->LastMouseClickX = state->mouseX;
this->LastMouseClickY = state->mouseY;
}
// Start Drawing
this->DrawHintBar();
if(state->moveline != nullptr)
if (state->moveline != nullptr)
this->DrawLine(state->moveline);
this->DrawScrollBars();
state->MouseClick=false;
state->MouseClick = false;
}
void PGNEditor::DrawHintBar() {
// Never issue a call to DrawCall here (otherwise MaxX and MaxY will be corrupted)
Shape hintBar(state->offsetX, 0, state->hintBarWidth, canvasH);
hintBar.target = Shape::HINTBAR;
Draw(hintBar); // Do not issues a DrawCall here (MaxY will change);
}
void PGNEditor::ScrollV(int percent) {
double VSSize = (double) canvasH
* ((double) canvasH / std::max(canvasH, MaxY - MinY));
int maxScroll=(canvasH - VSSize);
int amount=maxScroll*percent/100;
CurrentVSY += amount;
if (CurrentVSY <= 0)
CurrentVSY = 0;
else if (CurrentVSY >= maxScroll)
CurrentVSY = maxScroll;
double percentPage = 0;
if ((canvasH - VSSize) > 0)
percentPage = CurrentVSY / (canvasH - VSSize);
int maxOffset = std::max((MaxY - MinY) - canvasH, 0);
state->offsetY = -(double) maxOffset * percentPage;
}
void PGNEditor::ScrollH(int percent) {
double HSSize = (double) canvasW
* ((double) canvasW / std::max(canvasW, MaxX - MinX));
int maxScroll=(canvasW - HSSize);
int amount=maxScroll*percent/100;
CurrentHSX += amount;
if (CurrentHSX <= 0)
CurrentHSX = 0;
else if (CurrentHSX >= maxScroll)
CurrentHSX = maxScroll;
double percentPage = 0;
if ((canvasW - HSSize) > 0)
percentPage = CurrentHSX / (canvasW - HSSize);
int maxOffset = std::max((MaxX - MinX) - canvasW, 0);
state->offsetX = -(double) maxOffset * percentPage;
}
bool PGNEditor::MouseHover(Shape shape){
return(shape.x <= state->mouseX && shape.y <= state->mouseY
&& (shape.x+shape.GetWidth(state)) >= state->mouseX && (shape.GetHeight(state)+shape.y) >= state->mouseY);
}
void PGNEditor::DrawScrollBars() {
// Never issue a call to DrawCall here (otherwise MaxX and MaxY will be corrupted)
......@@ -61,93 +106,85 @@ void PGNEditor::DrawScrollBars() {
double VSSize = (double) canvasH
* ((double) canvasH / std::max(canvasH, MaxY - MinY));
if(state->IsDragging){
if(LastMouseClickOnHS){
CurrentHSX+=(state->mouseX-LastMouseClickX);
LastMouseClickX=state->mouseX;
if(CurrentHSX<=0)
CurrentHSX=0;
else if(CurrentHSX>=(canvasW-HSSize))
CurrentHSX=(canvasW-HSSize);
double percent=0;
if((canvasW-HSSize)>0)
percent=CurrentHSX/(canvasW-HSSize);
int maxOffset=(MaxX-MinX)-canvasW;
state->offsetX=-(double)maxOffset*percent;
if (state->IsDragging) {
if (LastMouseClickOnHS) {
CurrentHSX += (state->mouseX - LastMouseClickX);
LastMouseClickX = state->mouseX;
if (CurrentHSX <= 0)
CurrentHSX = 0;
else if (CurrentHSX >= (canvasW - HSSize))
CurrentHSX = (canvasW - HSSize);
double percent = 0;
if ((canvasW - HSSize) > 0)
percent = CurrentHSX / (canvasW - HSSize);
int maxOffset = (MaxX - MinX) - canvasW;
state->offsetX = -(double) maxOffset * percent;
}
if(LastMouseClickOnVS){
CurrentVSY+=(state->mouseY-LastMouseClickY);
LastMouseClickY=state->mouseY;
if(CurrentVSY<=0)
CurrentVSY=0;
else if(CurrentVSY>=(canvasH-VSSize))
CurrentVSY=(canvasH-VSSize);
double percent=0;
if((canvasH-VSSize)>0)
percent=CurrentVSY/(canvasH-VSSize);
int maxOffset=std::max((MaxY-MinY)-canvasH,0);
state->offsetY=-(double)maxOffset*percent;
if (LastMouseClickOnVS) {
CurrentVSY += (state->mouseY - LastMouseClickY);
LastMouseClickY = state->mouseY;
if (CurrentVSY <= 0)
CurrentVSY = 0;
else if (CurrentVSY >= (canvasH - VSSize))
CurrentVSY = (canvasH - VSSize);
double percent = 0;
if ((canvasH - VSSize) > 0)
percent = CurrentVSY / (canvasH - VSSize);
int maxOffset = std::max((MaxY - MinY) - canvasH, 0);
state->offsetY = -(double) maxOffset * percent;
}
}
//std::cout << "MaxY:" << MaxY << " MinY " << MinY << " Diff:"<< MaxY-MinY << std::endl << std::flush;
Shape HS(CurrentHSX, canvasH, HSSize, state->scrollbarThickness);
HS.target = Shape::SCROLLBAR_H;
Shape VS(canvasW,CurrentVSY, state->scrollbarThickness, VSSize);
Shape VS(canvasW, CurrentVSY, state->scrollbarThickness, VSSize);
VS.target = Shape::SCROLLBAR_V;
// Check mouse over
if (HS.x <= state->mouseX && HS.y <= state->mouseY
&& (HS.x + HSSize) >= state->mouseX
&& (HS.y + state->scrollbarThickness) >= state->mouseY){
&& (HS.y + state->scrollbarThickness) >= state->mouseY) {
HS.mouseHover = true;
HS.mouseClick=state->MouseClick;
HS.mouseClick = state->MouseClick;
}
if (VS.x <= state->mouseX && VS.y <= state->mouseY
&& (VS.x + state->scrollbarThickness) >= state->mouseX
&& (VS.y + VSSize) >= state->mouseY){
&& (VS.y + VSSize) >= state->mouseY) {
VS.mouseHover = true;
VS.mouseClick=state->MouseClick;
VS.mouseClick = state->MouseClick;
}
if(state->MouseClick){
LastMouseClickOnHS=(state->MouseClick && HS.mouseHover);
LastMouseClickOnVS=(state->MouseClick && VS.mouseHover);
if (state->MouseClick) {
LastMouseClickOnHS = (state->MouseClick && HS.mouseHover);
LastMouseClickOnVS = (state->MouseClick && VS.mouseHover);
}
Draw(HS);
Draw(VS);
}
void PGNEditor::DrawLine(Move *line) {
DrawCount();
int CurrentXBack=CurrentX;
int CurrentXBack = CurrentX;
CurrentX += state->counterW;
if (WhiteToPlay) {
DrawMove(*line->move);
if(line->next!=nullptr){
if (line->next != nullptr) {
CurrentX += state->colWidth;
DrawMove(*line->next->move);
}
// Restore state
CurrentX = CurrentXBack;
CurrentY+=state->rowWidth;
CurrentY += state->rowWidth;
DrawVariations(line); // Draw White Variations
if(line->next!=nullptr){
WhiteToPlay=false;
if (line->next != nullptr) {
WhiteToPlay = false;
DrawVariations(line->next); // Draw black Variations
WhiteToPlay=true;
line=line->next; // Update line pointer
WhiteToPlay = true;
line = line->next; // Update line pointer
}
} else {
......@@ -155,38 +192,51 @@ void PGNEditor::DrawLine(Move *line) {
DrawMove(*line->move);
// Restore state
CurrentX = CurrentXBack;
CurrentY+=state->rowWidth;
CurrentY += state->rowWidth;
DrawVariations(line);
WhiteToPlay=true;
WhiteToPlay = true;
}
MoveCount++;
if(line->next!=nullptr)
if (line->next != nullptr)
DrawLine(line->next);
}
void PGNEditor::DrawVariations(Move* line){
void PGNEditor::DrawVariations(Move *line) {
if (line->variations.size() > 0) {
bool WhiteToPlayBak = WhiteToPlay;
int CurrentXBack = CurrentX;
int MoveCountBak = MoveCount;
CurrentX+=state->variationOffset;
CurrentX += state->variationOffset;
// Draw separator
Shape sep(CurrentX,CurrentY,state->counterW+state->colWidth*2,state->varationSepW);
sep.target=Shape::VAR_SEP;
Shape sep(CurrentX, CurrentY, state->counterW + state->colWidth * 2,
state->varationSepW);
if(line->fold)
sep.target=Shape::VAR_SEP_FOLD;
else
sep.target = Shape::VAR_SEP;
DrawCall(sep);
CurrentY+=state->varationSepW;
CurrentY += state->varationSepW;
if(state->MouseClick){
if(MouseHover(sep)){
line->fold=!line->fold;
}
}
if(line->fold){
WhiteToPlay = WhiteToPlayBak;
CurrentX = CurrentXBack;
MoveCount = MoveCountBak;
return;
}
for (auto move : line->variations) {
DrawLine(move);
}
WhiteToPlay=WhiteToPlayBak;
CurrentX=CurrentXBack;
WhiteToPlay = WhiteToPlayBak;
CurrentX = CurrentXBack;
MoveCount = MoveCountBak;
}
}
......@@ -203,9 +253,9 @@ void PGNEditor::DrawCount() {
Shape bg(CurrentX, CurrentY, state->counterW, state->rowWidth);
bg.target = Shape::MOVE_COUNTER;
DrawCall(bg);
std::string dots=".";
if(!WhiteToPlay)
dots+="..";
std::string dots = ".";
if (!WhiteToPlay)
dots += "..";
Shape count(std::to_string(MoveCount) + dots, CurrentX, CurrentY);
count.target = Shape::MOVE_COUNTER;
DrawCall(count);
......@@ -221,19 +271,19 @@ void PGNEditor::DrawCall(Shape shape) {
int maxX = shape.x + width;
int maxY = shape.y + height;
if ((maxX+ state->extraCanvasMargin) > MaxX)
MaxX = (maxX+ state->extraCanvasMargin);
if ((maxY+ state->extraCanvasMargin) > MaxY)
MaxY = (maxY+ state->extraCanvasMargin);
if ((maxX + state->extraCanvasMargin) > MaxX)
MaxX = (maxX + state->extraCanvasMargin);
if ((maxY + state->extraCanvasMargin) > MaxY)
MaxY = (maxY + state->extraCanvasMargin);
if (shape.x < MinX)
MinX = shape.x;
if (shape.y < MinY)
MinY = shape.y;
if (shape.x <= state->mouseX && shape.y <= state->mouseY
&& maxX >= state->mouseX && maxY >= state->mouseY){
&& maxX >= state->mouseX && maxY >= state->mouseY) {
shape.mouseHover = true;
shape.mouseClick=state->MouseClick;
shape.mouseClick = state->MouseClick;
}
// Issue a draw only if necessary
......
......@@ -46,6 +46,9 @@ private:
protected:
void Render(State *state);
virtual void Draw(Shape shape) = 0;
void ScrollV(int percent);
void ScrollH(int percent);
bool MouseHover(Shape shape);
public:
PGNEditor():LastMouseClickOnHS(false),LastMouseClickOnVS(false),CurrentVSY(0),CurrentHSX(0){
......
......@@ -28,7 +28,8 @@ public:
SCROLLBAR_V,
SCROLLBAR_CORNER,
HINTBAR,
VAR_SEP
VAR_SEP,
VAR_SEP_FOLD,
} TARGET;
double x,y;
......
......@@ -12,11 +12,13 @@ namespace gui {
BEGIN_EVENT_TABLE(Editor, wxPanel)
EVT_MOUSE_EVENTS(Editor::MouseEvent)
EVT_KEY_DOWN(Editor::OnKeyDown)
EVT_PAINT(Editor::OnPaint)
END_EVENT_TABLE()
Editor::Editor(wxFrame *parent) :
wxPanel(parent), DC(nullptr) {
this->Bind(wxEVT_PAINT, &Editor::OnPaint, this);
//this->Bind(wxEVT_PAINT, &Editor::OnPaint, this);
this->SetBackgroundColour(*wxWHITE);
state = new State();
......@@ -26,17 +28,28 @@ Editor::Editor(wxFrame *parent) :
state->moveline = &(pgn.game->GetFirstMove()->editorMove);
}
this->Bind(wxEVT_CHAR_HOOK, &Editor::OnKeyDown, this);
}
void Editor::OnKeyDown(wxKeyEvent &event) {
if(event.GetKeyCode()==WXK_NUMPAD0)
ScrollV(-10);
else
ScrollV(10);
Refresh();
}
void Editor::MouseEvent(wxMouseEvent &event) {
SetFocus();
if(event.LeftDown()){
state->MouseClick=true;
}
else if(event.GetWheelRotation()!=0){
if(event.GetWheelRotation()>0)
ScrollV(-10);
else
ScrollV(10);
}
state->IsDragging=event.Dragging();
Refresh();
}
......@@ -69,6 +82,8 @@ void Editor::Draw(Shape shape) {
else if (shape.target == Shape::SCROLLBAR_H
|| shape.target == Shape::SCROLLBAR_V || shape.target == Shape::VAR_SEP)
DC->SetBrush(wxColour(100, 100, 100));
if(shape.target == Shape::VAR_SEP_FOLD)
DC->SetBrush(wxColour(255, 0, 0));
DC->DrawRectangle(shape.x, shape.y, shape.w, shape.h);
} else {
if(shape.mouseHover){
......
......@@ -30,6 +30,9 @@ public:
void Draw(Shape shape) override;
void OnKeyDown(wxKeyEvent& event);
void MouseEvent(wxMouseEvent &event);
bool AcceptsFocus() const {return true;}
bool AcceptsFocusFromKeyboard() const {return true;}
DECLARE_EVENT_TABLE()
};
......
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