diff --git a/char_layout_node.h b/char_layout_node.h index 763b89ecd..4149ec696 100644 --- a/char_layout_node.h +++ b/char_layout_node.h @@ -3,6 +3,7 @@ #include "layout_reference.h" #include "layout_node.h" +#include "layout_cursor.h" //#define TREE_LOGGING 1 @@ -12,6 +13,29 @@ public: size_t size() const override { return sizeof(CharLayoutNode); } + + void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) override { + if (cursor->position() == LayoutCursor::Position::Right) { + cursor->setPosition(LayoutCursor::Position::Left); + return; + } + LayoutNode * parentNode = parent(); + if (parentNode != nullptr) { + parentNode->moveCursorLeft(cursor, shouldRecomputeLayout); + } + } + + void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) override { + if (cursor->position() == LayoutCursor::Position::Left) { + cursor->setPosition(LayoutCursor::Position::Right); + return; + } + LayoutNode * parentNode = parent(); + if (parentNode != nullptr) { + parentNode->moveCursorRight(cursor, shouldRecomputeLayout); + } + } + #if TREE_LOGGING const char * description() const override { static char Description[] = {'C', 'h', 'a', 'r', ' ', m_char, 0}; diff --git a/layout_cursor_reference.h b/layout_cursor_reference.h index 8bc23eed1..42b4dec5e 100644 --- a/layout_cursor_reference.h +++ b/layout_cursor_reference.h @@ -9,7 +9,6 @@ class LayoutCursor; template class LayoutCursorReference : public LayoutReference { public: - //using LayoutReference::LayoutReference; LayoutCursorReference(LayoutReference * r) : LayoutReference(r->node()) { @@ -24,8 +23,8 @@ public: /* We cannot have LayoutCursor cursorLeftOf(LayoutCursor cursor) because of * circular header dependency */ - virtual void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) {} - virtual void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) {} + virtual void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) { return this->node()->moveCursorLeft(cursor, shouldRecomputeLayout); } + virtual void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) { return this->node()->moveCursorRight(cursor, shouldRecomputeLayout); } virtual void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) {} virtual void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) {} }; diff --git a/layout_node.h b/layout_node.h index e3137ce77..4fbafe641 100644 --- a/layout_node.h +++ b/layout_node.h @@ -3,6 +3,8 @@ #include "tree_node.h" +class LayoutCursor; + class LayoutNode : public TreeNode { public: /* Hierarchy */ @@ -13,6 +15,13 @@ public: int origin(); int absoluteOrigin(); + /* Tree navigation */ + virtual void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) {} + virtual void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) {} + virtual void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) {} + virtual void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) {} + + LayoutNode * childAtIndex(int i) { return static_cast(childTreeAtIndex(i)); } protected: diff --git a/test.cpp b/test.cpp index 255883f76..6586020bd 100644 --- a/test.cpp +++ b/test.cpp @@ -30,5 +30,7 @@ int main() { bool recompute = false; cursor.moveLeft(&recompute); cursor.log(); + cursor.moveRight(&recompute); + cursor.log(); return 0; }