From a01a3c1927c787a2032d2441d81426dde130e44f Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sat, 14 May 2022 15:44:05 +0300 Subject: [PATCH] add an property to widgets to make them omit layout... and add on_event function pointer that users can set on any widgets to act upon various events manually --- .gitignore | 2 ++ src/DocumentLayout.cpp | 3 +++ src/Widget.cpp | 2 ++ src/Widget.hpp | 7 +++++++ src/Window.hpp | 1 + src/main.cpp | 31 +++++++++++++++---------------- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index dceeaad..5fc81ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ builddir/ +buildclang/ +releaseclang/ .cache/ compile_commands.json diff --git a/src/DocumentLayout.cpp b/src/DocumentLayout.cpp index a50220f..b28422b 100644 --- a/src/DocumentLayout.cpp +++ b/src/DocumentLayout.cpp @@ -14,6 +14,9 @@ void DocumentLayout::run() { auto& children = m_target->children(); for (auto& child : children) { + if (child->absolute()) + continue; + if (child->current_geometry().height() > largest_height_so_far) { largest_height_so_far = child->current_geometry().height(); } diff --git a/src/Widget.cpp b/src/Widget.cpp index 60b43ad..ff66fe1 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -221,6 +221,8 @@ void Widget::dispatch_event(Event &event) { if (!m_accepts_events) return; + on_event(event); + switch (event.type()) { case EventType::MouseMove: { handle_mouse_move_event(reinterpret_cast(event)); diff --git a/src/Widget.hpp b/src/Widget.hpp index 98fd363..e0d7c83 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "Box.hpp" #include "Events.hpp" #include "Forward.hpp" @@ -44,6 +45,7 @@ private: bool m_is_active { false }; bool m_consumes_hits { false }; bool m_accepts_events { true }; + bool m_absolute { false }; ControlWidgetType m_control_type { ControlWidgetType::Widget }; public: Widget() {} @@ -58,6 +60,8 @@ public: m_accepts_events = false; } + std::function on_event { [](Event&){} }; + Box ¤t_geometry() { return m_current_geometry; } void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_full_relayout(); } void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } } @@ -94,6 +98,9 @@ public: bool accepts_events() { return m_accepts_events; } void set_accepts_events(bool accepts_events) { m_accepts_events = accepts_events; } + bool absolute() { return m_absolute; } + void set_absolute(bool absolute) { m_absolute = absolute; } + void set_layout(std::shared_ptr layout); std::shared_ptr layout() { return m_layout; } diff --git a/src/Window.hpp b/src/Window.hpp index b2ec63d..1e1ae00 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -6,6 +6,7 @@ #include "Painter.hpp" #include #include +#include namespace Raven { diff --git a/src/main.cpp b/src/main.cpp index 82e7e40..7b5ebc2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "Label.hpp" #include "Layout.hpp" #include "src/DocumentLayout.hpp" +#include "src/Events.hpp" #include #include #include @@ -17,25 +18,23 @@ int main() { auto main_widget = window.set_main_widget(); main_widget->set_layout(6.0); - int number = 0; + // int number = 0; + bool is_dragging = false; - for (int i = 0; i < 500; i++) { - auto button = main_widget->add("text "); - /* - button->on_click = [&]() { - number++; + auto button = main_widget->add("hello"); + button->set_absolute(true); + button->on_click = [&]() { + is_dragging = !is_dragging; + }; - window.start_batch(); - for (auto& c : main_widget->children()) { - if (c->type() == Raven::WidgetType::Button) { - auto button_child = std::static_pointer_cast(c); - button_child->set_text(std::to_string(number)); - } + main_widget->on_event = [&](Raven::Event &event) { + if (event.type() == Raven::EventType::MouseMove) { + auto mouse_move = reinterpret_cast(event); + if (is_dragging) { + button->move_to(mouse_move.point().x(), mouse_move.point().y()); } - window.end_batch(); - }; - */ - } + } + }; window.run(true); return 0;