Bladeren bron

Added basic buttons emulation

rsredsq 10 jaren geleden
bovenliggende
commit
fa4e3f6486

+ 6 - 1
Script/Packages/Atomic/Input.json

@@ -1,6 +1,11 @@
 {
 	"name" : "Input",
 	"sources" : ["Source/Atomic/Input"],
-	"classes" : ["Input"]
+	"classes" : ["Input"],
+	"overloads" : {
+		"Input" : {
+			"BindButton" : ["UIButton", "int"]
+		}
+	}
 
 }

+ 21 - 0
Source/Atomic/Input/Input.cpp

@@ -2018,4 +2018,25 @@ void Input::HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventDat
 
 }
 
+void Input::BindButton(UIButton* touchButton, int button)
+{
+    touchButton->SetEmulationButton(button);
+}
+
+void Input::FakeButtonDown(int button)
+{
+    SDL_Event evt;
+    evt.type = SDL_KEYDOWN;
+    evt.key.keysym.sym = button;
+    HandleSDLEvent(&evt);
+}
+
+void Input::FakeButtonUp(int button)
+{
+    SDL_Event evt;
+    evt.type = SDL_KEYUP;
+    evt.key.keysym.sym = button;
+    HandleSDLEvent(&evt);
+}
+
 }

+ 8 - 0
Source/Atomic/Input/Input.h

@@ -27,6 +27,7 @@
 #include "../Core/Object.h"
 #include "../Container/List.h"
 #include "../Input/InputEvents.h"
+#include "../UI/UIButton.h"
 
 namespace Atomic
 {
@@ -44,6 +45,7 @@ class Graphics;
 class Serializer;
 class UIElement;
 class XMLFile;
+class UIButton;
 
 const IntVector2 MOUSE_POSITION_OFFSCREEN = IntVector2(M_MIN_INT, M_MIN_INT);
 
@@ -290,6 +292,12 @@ public:
     /// Return whether application window is minimized.
     bool IsMinimized() const;
 
+    /// Binds UIButton element to the given button
+    void BindButton(UIButton* touchButton, int button);
+
+    void FakeButtonDown(int button);
+    void FakeButtonUp(int button);
+
 private:
     /// Initialize when screen mode initially set.
     void Initialize();

+ 4 - 0
Source/Atomic/UI/UI.cpp

@@ -185,6 +185,10 @@ void UI::Initialize(const String& languageFile)
     SubscribeToEvent(E_UPDATE, HANDLER(UI, HandleUpdate));
     SubscribeToEvent(SystemUI::E_CONSOLECLOSED, HANDLER(UI, HandleConsoleClosed));
 
+    SubscribeToEvent(E_TOUCHBEGIN, HANDLER(UI, HandleTouchBegin));
+    SubscribeToEvent(E_TOUCHEND, HANDLER(UI, HandleTouchEnd));
+    SubscribeToEvent(E_TOUCHMOVE, HANDLER(UI, HandleTouchMove));
+
     SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
 
     tb::TBWidgetListener::AddGlobalListener(this);

+ 4 - 1
Source/Atomic/UI/UI.h

@@ -150,7 +150,10 @@ private:
     void HandleTextInput(StringHash eventType, VariantMap& eventData);
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
     void HandleConsoleClosed(StringHash eventType, VariantMap& eventData);
-
+    //Touch Input
+    void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
+    void HandleTouchMove(StringHash eventType, VariantMap& eventData);
+    void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
 };
 
 }

+ 14 - 1
Source/Atomic/UI/UIButton.cpp

@@ -35,7 +35,7 @@ using namespace tb;
 namespace Atomic
 {
 
-UIButton::UIButton(Context* context, bool createWidget) : UIWidget(context, false)
+UIButton::UIButton(Context* context, bool createWidget) : UIWidget(context, false), emulationButton_(-1)
 {
     if (createWidget)
     {
@@ -58,6 +58,11 @@ void UIButton::SetSqueezable(bool value)
     ((TBButton*)widget_)->SetSqueezable(value);
 }
 
+void UIButton::SetEmulationButton(int emulationButton)
+{
+    emulationButton_ = emulationButton;
+}
+
 bool UIButton::OnEvent(const tb::TBWidgetEvent &ev)
 {
     if (ev.type == EVENT_TYPE_CLICK)
@@ -69,6 +74,14 @@ bool UIButton::OnEvent(const tb::TBWidgetEvent &ev)
             fileSystem->SystemOpen(text);
         }
     }
+    if (ev.type == EVENT_TYPE_POINTER_DOWN && emulationButton_ >= 0)
+    {
+        GetSubsystem<Input>()->FakeButtonDown(emulationButton_);
+    }
+    if (ev.type == EVENT_TYPE_POINTER_UP && emulationButton_ >= 0)
+    {
+        GetSubsystem<Input>()->FakeButtonUp(emulationButton_);
+    }
     return UIWidget::OnEvent(ev);
 }
 

+ 5 - 1
Source/Atomic/UI/UIButton.h

@@ -24,6 +24,8 @@
 
 #include "UIWidget.h"
 
+#include "../Input/Input.h"
+
 namespace Atomic
 {
 
@@ -39,12 +41,14 @@ public:
 
     void SetSqueezable(bool value);
 
+    void SetEmulationButton(int button);
+
 protected:
 
     virtual bool OnEvent(const tb::TBWidgetEvent &ev);
 
 private:
-
+    int emulationButton_;
 };
 
 }

+ 58 - 0
Source/Atomic/UI/UIInput.cpp

@@ -157,6 +157,64 @@ void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
 
 }
 
+//Touch Input
+void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
+{
+    if (inputDisabled_ || consoleVisible_)
+        return;
+
+    Input* input = GetSubsystem<Input>();
+    using namespace TouchBegin;
+    int x = eventData[P_X].GetInt();
+    int y = eventData[P_Y].GetInt();
+
+    IntVector2 pos = IntVector2(x, y);
+    
+    static double last_time = 0;
+    static int counter = 1;
+
+    Time* t = GetSubsystem<Time>();
+
+    double time = t->GetElapsedTime() * 1000;
+    if (time < last_time + 600)
+        counter++;
+    else
+        counter = 1;
+
+    last_time = time;
+    rootWidget_->InvokePointerDown(pos.x_, pos.y_, counter, TB_MODIFIER_NONE, true);
+}
+
+void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
+{
+    if (inputDisabled_ || consoleVisible_)
+        return;
+
+    Input* input = GetSubsystem<Input>();
+    using namespace TouchMove;
+    int x = eventData[P_X].GetInt();
+    int y = eventData[P_Y].GetInt();
+
+    IntVector2 pos = IntVector2(x, y);
+
+    rootWidget_->InvokePointerMove(pos.x_, pos.y_, TB_MODIFIER_NONE, true);
+}
+
+void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
+{
+    if (inputDisabled_ || consoleVisible_)
+        return;
+
+    Input* input = GetSubsystem<Input>();
+    using namespace TouchEnd;
+    int x = eventData[P_X].GetInt();
+    int y = eventData[P_Y].GetInt();
+
+    IntVector2 pos = IntVector2(x, y);
+
+    rootWidget_->InvokePointerUp(pos.x_, pos.y_, TB_MODIFIER_NONE, true);
+}
+
 static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
 {
 #ifdef __APPLE__

+ 13 - 0
Source/Atomic/UI/UIWidget.h

@@ -237,6 +237,16 @@ class UIWidget : public Object, public tb::TBWidgetDelegate
 
     tb::TBWidget* GetInternalWidget() { return widget_; }
 
+    void SetDelegate(UIWidget* widget) { widget_->SetDelegate(widget); }
+
+    void SetMultiTouch(bool multiTouch) { multiTouch_ = multiTouch; }
+
+    bool IsMultiTouch() { return multiTouch_; }
+
+    void SetCapturing(bool capturing) { widget_->SetCapturing(capturing); }
+
+    bool GetCapturing() { return widget_->GetCapturing(); }
+
 protected:
 
     void ConvertEvent(UIWidget* handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data);
@@ -253,6 +263,9 @@ protected:
 
     SharedPtr<UIDragObject> dragObject_;
 
+
+    bool multiTouch_;
+
 };
 
 }

+ 14 - 10
Source/ThirdParty/TurboBadger/tb_widgets.cpp

@@ -73,6 +73,7 @@ TBWidget::TBWidget()
     , m_long_click_timer(nullptr)
     , m_delegate(nullptr)
 	, m_packed_init(0)
+    , capturing_(true)
 {
 #ifdef TB_RUNTIME_DEBUG_INFO
 	last_measure_time = 0;
@@ -1274,9 +1275,10 @@ void TBWidget::StopLongClickTimer()
 
 void TBWidget::InvokePointerDown(int x, int y, int click_count, MODIFIER_KEYS modifierkeys, bool touch)
 {
-	if (!captured_widget)
+    TBWidget* down_widget = GetWidgetAt(x, y, true);
+	if (!captured_widget && down_widget->capturing_)
 	{
-		SetCapturedWidget(GetWidgetAt(x, y, true));
+		SetCapturedWidget(down_widget);
 		SetHoveredWidget(captured_widget, touch);
 		//captured_button = button;
 
@@ -1320,27 +1322,29 @@ void TBWidget::InvokePointerDown(int x, int y, int click_count, MODIFIER_KEYS mo
 			focus_target = focus_target->m_parent;
 		}
 	}
-	if (captured_widget)
+	if (down_widget)
 	{
-		captured_widget->ConvertFromRoot(x, y);
+		down_widget->ConvertFromRoot(x, y);
 		pointer_move_widget_x = pointer_down_widget_x = x;
 		pointer_move_widget_y = pointer_down_widget_y = y;
 		TBWidgetEvent ev(EVENT_TYPE_POINTER_DOWN, x, y, touch, modifierkeys);
 		ev.count = click_count;
-		captured_widget->InvokeEvent(ev);
+		down_widget->InvokeEvent(ev);
 	}
 }
 
 void TBWidget::InvokePointerUp(int x, int y, MODIFIER_KEYS modifierkeys, bool touch)
 {
-	if (captured_widget)
+    TBWidget* down_widget = GetWidgetAt(x, y, true);
+	if (down_widget)
 	{
-		captured_widget->ConvertFromRoot(x, y);
+        down_widget->OnCaptureChanged(false);
+		down_widget->ConvertFromRoot(x, y);
 		TBWidgetEvent ev_up(EVENT_TYPE_POINTER_UP, x, y, touch, modifierkeys);
 		TBWidgetEvent ev_click(EVENT_TYPE_CLICK, x, y, touch, modifierkeys);
-		captured_widget->InvokeEvent(ev_up);
-		if (!cancel_click && captured_widget && captured_widget->GetHitStatus(x, y))
-			captured_widget->InvokeEvent(ev_click);
+		down_widget->InvokeEvent(ev_up);
+		if (!cancel_click && down_widget->GetHitStatus(x, y))
+			down_widget->InvokeEvent(ev_click);
 		if (captured_widget) // && button == captured_button
 			captured_widget->ReleaseCapture();
 	}

+ 6 - 0
Source/ThirdParty/TurboBadger/tb_widgets.h

@@ -985,6 +985,12 @@ public:
 		by GetCalculatedFontDescription) */
 	TBFontFace *GetFont() const;
 
+    void SetCapturing(bool capturing) { capturing_ = capturing; }
+
+    bool GetCapturing() { return capturing_; }
+
+    bool capturing_;
+
 private:
 	friend class TBWidgetListener;	///< It does iteration of m_listeners for us.
 	TBWidget *m_parent;				///< The parent of this widget