From cd7c2c6260c9aa41ea4604772629ed85dc35a5ed Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 12 May 2020 17:24:19 +0700 Subject: [PATCH] dummy drag event --- .../enduro2d/high/components/touchable.hpp | 58 ++++++++++++++++--- .../scripts/emmy/components/touchable.lua | 22 ++++++- .../library/scripts/sample_10/sample_10.lua | 9 +++ .../high_binds/components/touchable_binds.cpp | 43 ++++++++++++++ .../enduro2d/high/components/touchable.cpp | 14 ++++- sources/enduro2d/high/starter.cpp | 2 + .../enduro2d/high/systems/script_system.cpp | 4 ++ .../enduro2d/high/systems/touch_system.cpp | 2 +- .../touch_system_collector.cpp | 17 +++++- .../touch_system_collector.hpp | 39 +++++++++---- 10 files changed, 183 insertions(+), 27 deletions(-) diff --git a/headers/enduro2d/high/components/touchable.hpp b/headers/enduro2d/high/components/touchable.hpp index 1930679d..a8aea924 100644 --- a/headers/enduro2d/high/components/touchable.hpp +++ b/headers/enduro2d/high/components/touchable.hpp @@ -10,12 +10,14 @@ namespace e2d::touchable_events { + class mouse_drag_evt; class mouse_move_evt; class mouse_hover_evt; class mouse_scroll_evt; class mouse_button_evt; using event = std::variant final : public empty_factory_loader {}; + template <> + class factory_loader final + : public empty_factory_loader {}; + template <> class factory_loader final : public empty_factory_loader {}; @@ -127,19 +134,18 @@ namespace e2d namespace e2d::touchable_events { - // - // base_evt - // - namespace impl { + // + // base_evt + // + template < typename Event > class base_evt { public: - base_evt(bool bubbling) - : bubbling_(bubbling) {} - - base_evt(gobject target, bool bubbling) + base_evt( + gobject target, + bool bubbling) : target_(target) , bubbling_(bubbling) {} @@ -156,6 +162,38 @@ namespace e2d::touchable_events }; } + // + // mouse_drag_evt + // + + class mouse_drag_evt final : public impl::base_evt { + public: + ENUM_HPP_CLASS_DECL(types, u8, + (start) + (move) + (end)) + public: + mouse_drag_evt( + gobject target, + types type, + const v2f& local_point, + const v2f& world_point) + : base_evt(target, true) + , type_(type) + , local_point_(local_point) + , world_point_(world_point) {} + + [[nodiscard]] types type() const noexcept { return type_; } + [[nodiscard]] const v2f& local_point() const noexcept { return local_point_; } + [[nodiscard]] const v2f& world_point() const noexcept { return world_point_; } + private: + types type_ = types::start; + v2f local_point_ = v2f::zero(); + v2f world_point_ = v2f::zero(); + }; + + ENUM_HPP_REGISTER_TRAITS(mouse_drag_evt::types) + // // mouse_move_evt // @@ -189,7 +227,9 @@ namespace e2d::touchable_events (enter) (leave)) public: - mouse_hover_evt(gobject target, types type) + mouse_hover_evt( + gobject target, + types type) : base_evt(target, type == types::over || type == types::out) , type_(type) {} diff --git a/samples/bin/library/scripts/emmy/components/touchable.lua b/samples/bin/library/scripts/emmy/components/touchable.lua index c05e8e7a..e890a369 100644 --- a/samples/bin/library/scripts/emmy/components/touchable.lua +++ b/samples/bin/library/scripts/emmy/components/touchable.lua @@ -9,6 +9,9 @@ local touchable = { ---@type boolean pushing = false, + ---@type boolean + dragging = false, + ---@type boolean hovering = false, @@ -68,6 +71,23 @@ local touchable_base_evt = { } touchable.touchable_base_evt = touchable_base_evt +-- +-- touchable_mouse_drag_evt +-- + +---@class touchable_mouse_drag_evt : touchable_base_evt +local touchable_mouse_drag_evt = { + ---@type string + type = "start", + + ---@type v2f + local_point = v2f.zero(), + + --@type v2f + world_point = v2f.zero() +} +touchable.touchable_mouse_drag_evt = touchable_mouse_drag_evt + -- -- touchable_mouse_move_evt -- @@ -130,7 +150,7 @@ local touchable_mouse_button_evt = { } touchable.touchable_mouse_button_evt = touchable_mouse_button_evt ----@alias touchable_event touchable_mouse_move_evt | touchable_mouse_hover_evt | touchable_mouse_scroll_evt | touchable_mouse_button_evt +---@alias touchable_event touchable_mouse_drag_evt | touchable_mouse_move_evt | touchable_mouse_hover_evt | touchable_mouse_scroll_evt | touchable_mouse_button_evt -- ----------------------------------------------------------------------------- -- diff --git a/samples/bin/library/scripts/sample_10/sample_10.lua b/samples/bin/library/scripts/sample_10/sample_10.lua index 05b37258..b8d55566 100644 --- a/samples/bin/library/scripts/sample_10/sample_10.lua +++ b/samples/bin/library/scripts/sample_10/sample_10.lua @@ -13,6 +13,15 @@ end ---@param type string ---@param event touchable_event function M:on_event(go, type, event) + if type == "touchable.mouse_drag_evt" then + the_debug:trace(string.format("event: %q\n-->type: %q\n-->target: %q\n-->local_point: %s\n-->world_point: %s", + type, + event.type, + event.target.named and event.target.named.name or "[unnamed]", + event.local_point, + event.world_point)) + end + if type == "touchable.mouse_move_evt" then the_debug:trace(string.format("event: %q\n-->target: %q\n-->local_point: %s\n-->world_point: %s", type, diff --git a/sources/enduro2d/high/bindings/high_binds/components/touchable_binds.cpp b/sources/enduro2d/high/bindings/high_binds/components/touchable_binds.cpp index 25c8c1a8..c5e61aca 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/touchable_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/touchable_binds.cpp @@ -63,6 +63,19 @@ namespace e2d::bindings::high } ), + "dragging", sol::property( + [](const gcomponent& c) -> bool { + return c.component().exists(); + }, + [](gcomponent& c, bool yesno){ + if ( yesno ) { + c.component().ensure(); + } else { + c.component().remove(); + } + } + ), + "hovering", sol::property( [](const gcomponent& c) -> bool { return c.component().exists(); @@ -188,6 +201,36 @@ namespace e2d::bindings::high // events // + l["touchable"].get_or_create() + .new_usertype("mouse_drag_evt", + sol::no_constructor, + + "target", sol::property( + [](const touchable_events::mouse_drag_evt& c) -> gobject { + return c.target(); + }), + + "bubbling", sol::property( + [](const touchable_events::mouse_drag_evt& c) -> bool { + return c.bubbling(); + }), + + "type", sol::property( + [](const touchable_events::mouse_drag_evt& c) -> str { + return str(enum_hpp::to_string_or_throw(c.type())); + }), + + "local_point", sol::property( + [](const touchable_events::mouse_drag_evt& c) -> v2f { + return c.local_point(); + }), + + "world_point", sol::property( + [](const touchable_events::mouse_drag_evt& c) -> v2f { + return c.world_point(); + }) + ); + l["touchable"].get_or_create() .new_usertype("mouse_move_evt", sol::no_constructor, diff --git a/sources/enduro2d/high/components/touchable.cpp b/sources/enduro2d/high/components/touchable.cpp index 032a01ca..0c764727 100644 --- a/sources/enduro2d/high/components/touchable.cpp +++ b/sources/enduro2d/high/components/touchable.cpp @@ -59,7 +59,7 @@ namespace e2d void component_inspector::operator()(gcomponent& c) const { // - // pushing/hovering + // pushing/dragging/hovering // if ( bool pushing = c.component().exists(); @@ -74,6 +74,18 @@ namespace e2d ImGui::SameLine(); + if ( bool dragging = c.component().exists(); + ImGui::Checkbox("dragging", &dragging) ) + { + if ( dragging ) { + c.component().ensure(); + } else { + c.component().remove(); + } + } + + ImGui::SameLine(); + if ( bool hovering = c.component().exists(); ImGui::Checkbox("hovering", &hovering) ) { diff --git a/sources/enduro2d/high/starter.cpp b/sources/enduro2d/high/starter.cpp index 023f98de..d7a1a21e 100644 --- a/sources/enduro2d/high/starter.cpp +++ b/sources/enduro2d/high/starter.cpp @@ -234,6 +234,7 @@ namespace e2d .register_component("toggle.pressed") .register_component("touchable") .register_component("touchable.pushing") + .register_component("touchable.dragging") .register_component("touchable.hovering") .register_component("touchable.clicked") .register_component("touchable.pressed") @@ -279,6 +280,7 @@ namespace e2d //.register_component("toggle.pressed") .register_component("touchable") //.register_component("touchable.pushing") + //.register_component("touchable.dragging") //.register_component("touchable.hovering") //.register_component("touchable.clicked") //.register_component("touchable.pressed") diff --git a/sources/enduro2d/high/systems/script_system.cpp b/sources/enduro2d/high/systems/script_system.cpp index 709632db..34c7df05 100644 --- a/sources/enduro2d/high/systems/script_system.cpp +++ b/sources/enduro2d/high/systems/script_system.cpp @@ -87,6 +87,10 @@ namespace behaviours::call_result r = behaviours::call_result::success; std::visit(utils::overloaded { [](std::monostate){}, + [&b,&a,&r](const touchable_events::mouse_drag_evt& e){ + r = behaviours::call_meta_method( + b, "on_event", a.node()->owner(), "touchable.mouse_drag_evt", e); + }, [&b,&a,&r](const touchable_events::mouse_move_evt& e){ r = behaviours::call_meta_method( b, "on_event", a.node()->owner(), "touchable.mouse_move_evt", e); diff --git a/sources/enduro2d/high/systems/touch_system.cpp b/sources/enduro2d/high/systems/touch_system.cpp index 703c55e1..7673b928 100644 --- a/sources/enduro2d/high/systems/touch_system.cpp +++ b/sources/enduro2d/high/systems/touch_system.cpp @@ -28,7 +28,7 @@ namespace e2d internal_state(input& i, window& w) : input_(i) , window_(w) - , collector_(window_.register_event_listener()) {} + , collector_(window_.register_event_listener(i)) {} ~internal_state() noexcept { window_.unregister_event_listener(collector_); diff --git a/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.cpp b/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.cpp index e16ca245..3fbfaf4d 100644 --- a/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.cpp +++ b/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.cpp @@ -8,6 +8,9 @@ namespace e2d::touch_system_impl { + collector::collector(input& i) + : input_(i) {} + void collector::clear() noexcept { events_.clear(); } @@ -37,11 +40,16 @@ namespace e2d::touch_system_impl } void collector::on_move_cursor(const v2f& pos) noexcept { - events_.push_back(mouse_move_event(pos)); + E2D_UNUSED(pos); + events_.push_back(mouse_move_event( + input_.mouse().cursor_pos())); } void collector::on_mouse_scroll(const v2f& delta) noexcept { - events_.push_back(mouse_scroll_event(delta)); + E2D_UNUSED(delta); + events_.push_back(mouse_scroll_event( + input_.mouse().cursor_pos(), + input_.mouse().scroll_delta())); } void collector::on_mouse_button(mouse_button button, mouse_button_action action) noexcept { @@ -53,6 +61,9 @@ namespace e2d::touch_system_impl return; } - events_.push_back(mouse_button_event(button, action)); + events_.push_back(mouse_button_event( + input_.mouse().cursor_pos(), + button, + action)); } } diff --git a/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.hpp b/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.hpp index 210cc4b4..a7700af8 100644 --- a/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.hpp +++ b/sources/enduro2d/high/systems/touch_system_impl/touch_system_collector.hpp @@ -12,24 +12,38 @@ namespace e2d::touch_system_impl { class collector final : public window::event_listener { public: - struct mouse_move_event { - v2f pos{v2f::zero()}; - mouse_move_event(const v2f& npos) - : pos(npos) {} + struct mouse_event { + v2f screen_point{v2f::zero()}; + mouse_event( + const v2f& screen_point) + : screen_point(screen_point) {} }; - struct mouse_scroll_event { + struct mouse_move_event : mouse_event { + mouse_move_event( + const v2f& screen_point) + : mouse_event(screen_point) {} + }; + + struct mouse_scroll_event : mouse_event { v2f delta{v2f::zero()}; - mouse_scroll_event(const v2f& ndelta) - : delta(ndelta) {} + mouse_scroll_event( + const v2f& screen_point, + const v2f& delta) + : mouse_event(screen_point) + , delta(delta) {} }; - struct mouse_button_event { + struct mouse_button_event : mouse_event { mouse_button button{mouse_button::unknown}; mouse_button_action action{mouse_button_action::unknown}; - mouse_button_event(mouse_button nbutton, mouse_button_action naction) - : button(nbutton) - , action(naction) {} + mouse_button_event( + const v2f& screen_point, + mouse_button button, + mouse_button_action action) + : mouse_event(screen_point) + , button(button) + , action(action) {} }; public: using event = std::variant; using events = vector; public: - collector() = default; + collector(input& i); void clear() noexcept; @@ -60,6 +74,7 @@ namespace e2d::touch_system_impl mouse_button button, mouse_button_action action) noexcept final; private: + input& input_; vector events_; }; }