commit 6112c32a831773fc7dfabed88c336a9c6de2bc21 Author: Relintai Date: Sat Apr 20 14:54:22 2019 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5230b5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.import +*.d +*.o +*.meta + diff --git a/SCsub b/SCsub new file mode 100644 index 0000000..5fdab32 --- /dev/null +++ b/SCsub @@ -0,0 +1,5 @@ +Import('env') + +env.add_source_files(env.modules_sources,"register_types.cpp") + +env.add_source_files(env.modules_sources,"touch_button.cpp") diff --git a/config.py b/config.py new file mode 100644 index 0000000..1c8cd12 --- /dev/null +++ b/config.py @@ -0,0 +1,5 @@ +def can_build(env, platform): + return True + +def configure(env): + pass diff --git a/config.pyc b/config.pyc new file mode 100644 index 0000000..8e757aa Binary files /dev/null and b/config.pyc differ diff --git a/register_types.cpp b/register_types.cpp new file mode 100644 index 0000000..f87c8ce --- /dev/null +++ b/register_types.cpp @@ -0,0 +1,12 @@ +#include "register_types.h" + +#include "touch_button.h" + +void register_ui_extensions_types() { + ClassDB::register_class(); +} + +void unregister_ui_extensions_types() { + +} + diff --git a/register_types.h b/register_types.h new file mode 100644 index 0000000..fa1cd41 --- /dev/null +++ b/register_types.h @@ -0,0 +1,3 @@ + +void register_ui_extensions_types(); +void unregister_ui_extensions_types(); diff --git a/touch_button.cpp b/touch_button.cpp new file mode 100644 index 0000000..4334ddc --- /dev/null +++ b/touch_button.cpp @@ -0,0 +1,103 @@ +#include "touch_button.h" + +void TouchButton::_notification(int p_what) { + + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + + if (!Engine::get_singleton()->is_editor_hint()) { + set_process_input(is_visible_in_tree()); + } + + } break; + case NOTIFICATION_VISIBILITY_CHANGED: { + if (Engine::get_singleton()->is_editor_hint()) + break; + if (is_visible_in_tree()) { + set_process_input(true); + } else { + set_process_input(false); + } + } break; + } + + Button::_notification(p_what); +} + +void TouchButton::_input(Ref p_event) { + + if (!get_tree()) + return; + + if (p_event->get_device() == -1) + return; + + ERR_FAIL_COND(!is_visible_in_tree()); + + if (is_disabled()) // no interaction with disabled button + return; + + Ref b = p_event; + + if (b.is_valid() && _is_point_inside(b->get_position())) { + get_viewport()->set_input_as_handled(); + + Ref ev; + ev.instance(); + + ev->set_button_index(BUTTON_LEFT); + ev->set_pressed(b->is_pressed()); + ev->set_position(b->get_position()); + ev->set_global_position(b->get_position()); + + BaseButton::_gui_input(ev); + update(); + } + /* + Ref mm = p_event; + + if (mm.is_valid() && _is_point_inside(mm->get_position())) { + Ref ev; + ev.instance(); + + ev->set_relative(mm->get_relative()); + ev->set_speed(mm->get_speed()); + ev->set_position(b->get_position()); + ev->set_global_position(b->get_position()); + + BaseButton::_gui_input(ev); + update(); + }*/ +} + + +void TouchButton::_gui_input(Ref p_event) { + if (p_event->get_device() == -1) { + accept_event(); + return; + } + + BaseButton::_gui_input(p_event); +} + +void TouchButton::_unhandled_input(Ref p_event) { + + if (p_event->get_device() == -1) { + //accept_event(); + return; + } + + Button::_unhandled_input(p_event); +} + +void TouchButton::_bind_methods() { + ClassDB::bind_method(D_METHOD("_input"), &TouchButton::_input); +} + +bool TouchButton::_is_point_inside(const Vector2 &vec) { + Point2 point; + point.x = vec.x; + point.y = vec.y; + + return get_global_rect().has_point(point); +} diff --git a/touch_button.h b/touch_button.h new file mode 100644 index 0000000..0933464 --- /dev/null +++ b/touch_button.h @@ -0,0 +1,25 @@ +#ifndef TOUCH_BUTTON_H +#define TOUCH_BUTTON_H + +#include "core/engine.h" +#include "core/os/os.h" +#include "scene/gui/button.h" +#include "scene/main/viewport.h" + +class TouchButton : public Button { + GDCLASS(TouchButton, Button); + +public: + TouchButton() {} + + bool _is_point_inside(const Point2 &p_point); + +protected: + void _notification(int p_what); + virtual void _gui_input(Ref p_event); + virtual void _unhandled_input(Ref p_event); + void _input(Ref p_event); + static void _bind_methods(); +}; + +#endif