aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/wxWidgets/main.cpp53
-rw-r--r--src/CGEditor.cpp8
-rw-r--r--src/CGEditor.hpp2
-rw-r--r--src/components/MoveTable.cpp8
-rw-r--r--src/components/MoveTable.hpp3
-rw-r--r--src/components/Scrollbar.cpp36
-rw-r--r--src/components/Scrollbar.hpp2
7 files changed, 100 insertions, 12 deletions
diff --git a/examples/wxWidgets/main.cpp b/examples/wxWidgets/main.cpp
index 9f35981..78fa38c 100644
--- a/examples/wxWidgets/main.cpp
+++ b/examples/wxWidgets/main.cpp
@@ -10,19 +10,22 @@
* @brief CGEditor Window
*
*/
-class MyFrame : public wxFrame, public cgeditor::CGEditor {
+class EditorPanel : public wxPanel, public cgeditor::CGEditor {
wxPaintDC *dc;
-
+ /// @brief Used to test focus:
+ CMI::HalfMove *toFocus;
public:
- MyFrame()
- : wxFrame(NULL, wxID_ANY, "Hello World CGEditor"), CGEditor(), dc(NULL) {
- CreateStatusBar();
- SetStatusText("CGEditor");
+ EditorPanel(wxWindow *parent)
+ : wxPanel(parent), CGEditor(), dc(NULL) {
// Create a game
CGEditor::status.Moves = BuildExampleGame();
+ // Set move to focus to the last move (downest move in the window!):
+ toFocus= CGEditor::status.Moves;
+ while(toFocus->GetMainline()!=nullptr){
+ toFocus=toFocus->GetMainline();
+ }
}
-private:
void OnExit(wxCommandEvent &event) {
(void)event; // Disable unused variable warning
Close(true); }
@@ -81,6 +84,19 @@ private:
}
}
+ void KeyboardEvent(wxKeyEvent& event){
+ switch ( event.GetKeyCode() )
+ {
+ case WXK_DOWN:
+ CGEditor::FocusOnMove(toFocus);
+ Refresh();
+ if(toFocus->GetParent()!=nullptr){
+ toFocus=toFocus->GetParent(); // Get previous move (not that it can be on the same line!)
+ }
+ break;
+ }
+ }
+
/**
* @brief Convenient fonction to center text
*
@@ -191,15 +207,30 @@ private:
DECLARE_EVENT_TABLE()
};
-wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
-EVT_PAINT(MyFrame::OnPaint)
-EVT_MOUSE_EVENTS(MyFrame::MouseEvent)
+wxBEGIN_EVENT_TABLE(EditorPanel, wxPanel)
+EVT_PAINT(EditorPanel::OnPaint)
+EVT_MOUSE_EVENTS(EditorPanel::MouseEvent)
+EVT_KEY_DOWN(EditorPanel::KeyboardEvent)
wxEND_EVENT_TABLE()
+
+class MainWindow: public wxFrame
+{
+ wxPanel* editorPane;
+public:
+
+ MainWindow(const wxString& title):wxFrame(NULL, wxID_ANY, title){
+ CreateStatusBar();
+ SetStatusText("CGEditor");
+ editorPane = new EditorPanel(this);
+ }
+
+};
+
class MyApp : public wxApp {
public:
virtual bool OnInit() {
- MyFrame *frame = new MyFrame();
+ MainWindow *frame = new MainWindow("Hello World CGEditor");
frame->Show(true);
return true;
}
diff --git a/src/CGEditor.cpp b/src/CGEditor.cpp
index fadbdc8..cf405db 100644
--- a/src/CGEditor.cpp
+++ b/src/CGEditor.cpp
@@ -1,5 +1,5 @@
#include "CGEditor.hpp"
-#include <iostream>
+
namespace cgeditor {
CGEditor::CGEditor() {
@@ -180,4 +180,10 @@ std::uint8_t CGEditor::GetNAGId(const std::string& symbol) const{
return 0;
}
+void CGEditor::FocusOnMove(CMI::HalfMove* move){
+ double X,Y;
+ MT->GetMoveXY(move,X,Y);
+ SBH->Focus(X+status.MoveWidth);
+ SBV->Focus(Y+status.MoveHeight);
+}
} // namespace cgeditor
diff --git a/src/CGEditor.hpp b/src/CGEditor.hpp
index e8b5286..72c2785 100644
--- a/src/CGEditor.hpp
+++ b/src/CGEditor.hpp
@@ -40,5 +40,7 @@ public:
std::string GetNAGSymbol(const std::uint8_t) const;
/// @brief Convert NAG symbol to id using the NagTable
std::uint8_t GetNAGId(const std::string&) const;
+ /// @brief Try to scroll to focus on a given move
+ void FocusOnMove(CMI::HalfMove*);
};
} // namespace cgeditor \ No newline at end of file
diff --git a/src/components/MoveTable.cpp b/src/components/MoveTable.cpp
index 4878275..88767b1 100644
--- a/src/components/MoveTable.cpp
+++ b/src/components/MoveTable.cpp
@@ -69,6 +69,9 @@ std::uint32_t MoveTable::UpdateMoves(CMI::HalfMove *m, std::uint32_t line,
move_bound.text = m->GetSAN();
move_bound.ShouldApplyScroll = true;
bool isMouseOver = IsMouseOver(move_bound);
+ // Save in cache:
+ MovesStates[m].Bound=move_bound;
+ MovesStates[m].Line=line;
//---------- Update current focus move ----------
if (isMouseOver) {
@@ -346,4 +349,9 @@ std::uint32_t MoveTable::DrawVariations(CMI::HalfMove *m, std::uint32_t line,
}
return (line);
}
+
+void MoveTable::GetMoveXY(CMI::HalfMove* m, double &X, double &Y){
+ X=MovesStates.at(m).Bound.x;
+ Y=MovesStates.at(m).Bound.y;
+}
} // namespace cgeditor \ No newline at end of file
diff --git a/src/components/MoveTable.hpp b/src/components/MoveTable.hpp
index 1b6323e..3a50a8d 100644
--- a/src/components/MoveTable.hpp
+++ b/src/components/MoveTable.hpp
@@ -50,6 +50,8 @@ class MoveTable : public Component {
typedef struct MoveState {
bool IsFolded=false;
bool IsHidden=false;
+ int Line=0;
+ Element Bound;
} MoveState;
std::uint32_t UpdateMoves(CMI::HalfMove *, std::uint32_t, std::uint32_t,bool only_black);
std::int32_t CurrentMove;
@@ -66,5 +68,6 @@ public:
void Refresh();
std::vector<Element> GetVariationsMarging() { return (VariationMargins); }
void SyncCache();
+ void GetMoveXY(CMI::HalfMove* m, double &X, double &Y);
};
} // namespace cgeditor \ No newline at end of file
diff --git a/src/components/Scrollbar.cpp b/src/components/Scrollbar.cpp
index 76c5c1a..ac713e9 100644
--- a/src/components/Scrollbar.cpp
+++ b/src/components/Scrollbar.cpp
@@ -109,4 +109,40 @@ void Scrollbar::Refresh() {
elements.push_back(bar);
}
+void Scrollbar::Focus(double XorY){
+ double MTCanvasHeight = status->CanvasHeight - status->ScrollbarWidth;
+ double MTCanvasWidth = status->CanvasWidth - status->ScrollbarWidth;
+ // Check horizontal or vertical and if should scroll
+ if(IsHorizontal && status->MoveTableMaxX > MTCanvasWidth){
+ // First normalize:
+ XorY-=MTCanvasWidth; // Do not scroll on first page
+ XorY=std::max(0.0,XorY); // Ensure focus is greater than 0
+ XorY=std::min(XorY,(status->MoveTableMaxX-MTCanvasWidth)); // Ensure focus is than scroll max
+ // Then compute percent and apply scroll
+ double percent = XorY / (status->MoveTableMaxX-MTCanvasWidth);
+ status->ScrollX =
+ -(status->MoveTableMaxX - MTCanvasWidth) * percent;
+ // Do not forget to update scrollbar:
+ double maxScroll = bg.width - bar.width;
+ bar.x = maxScroll * percent;
+ bar.x = std::max(bg.x, bar.x);
+ bar.x = std::min(bg.x + maxScroll, bar.x);
+ }
+ else if(status->MoveTableMaxY > MTCanvasHeight){
+ // First normalize:
+ XorY-=MTCanvasHeight; // Do not scroll on first page
+ XorY=std::max(0.0,XorY); // Ensure focus is greater than 0
+ XorY=std::min(XorY,(status->MoveTableMaxY-MTCanvasHeight)); // Ensure focus is than scroll max
+ // Then compute percent and apply scroll
+ double percent = XorY / (status->MoveTableMaxY-MTCanvasHeight);
+ status->ScrollY =
+ -(status->MoveTableMaxY - MTCanvasHeight) * percent;
+ // Do not forget to update scrollbar:
+ double maxScroll = bg.height - bar.height;
+ bar.y = maxScroll * percent;
+ bar.y = std::max(bg.y, bar.y);
+ bar.y = std::min(bg.y + maxScroll, bar.y);
+ }
+}
+
} // namespace cgeditor \ No newline at end of file
diff --git a/src/components/Scrollbar.hpp b/src/components/Scrollbar.hpp
index 3ed97ae..fa49eca 100644
--- a/src/components/Scrollbar.hpp
+++ b/src/components/Scrollbar.hpp
@@ -12,5 +12,7 @@ class Scrollbar : public Component {
public:
Scrollbar(Status* s,bool IsHorizontal);
void Refresh();
+ /// @brief Goto a given graphical coordinate (if possible using current scroll range)
+ void Focus(double XorY);
};
} // namespace cgeditor \ No newline at end of file