Selaa lähdekoodia

TouchState marshaling through a VariantMap.

rsredsq 10 vuotta sitten
vanhempi
sitoutus
0fde58004e

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

@@ -4,7 +4,8 @@
 	"classes" : ["Input"],
 	"overloads" : {
 		"Input" : {
-			"BindButton" : ["UIButton", "int"]
+			"BindButton" : ["UIButton", "int"],
+			"GetTouch" : ["unsigned"]
 		}
 	}
 

+ 1 - 5
Source/Atomic/Input/Input.cpp

@@ -71,11 +71,6 @@ int ConvertSDLKeyCode(int keySym, int scanCode)
         return SDL_toupper(keySym);
 }
 
-UIElement* TouchState::GetTouchedElement()
-{
-    return touchedElement_.Get();
-}
-
 #ifdef EMSCRIPTEN
 #define EM_TRUE 1
 
@@ -1614,6 +1609,7 @@ void Input::HandleSDLEvent(void* sdlEvent)
 #endif
             state.delta_ = IntVector2::ZERO;
             state.pressure_ = evt.tfinger.pressure;
+            state.touchedWidget_ = GetSubsystem<UI>()->GetWidgetAt(state.position_.x_, state.position_.y_, true);
 
             using namespace TouchBegin;
 

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

@@ -52,9 +52,6 @@ const IntVector2 MOUSE_POSITION_OFFSCREEN = IntVector2(M_MIN_INT, M_MIN_INT);
 /// %Input state for a finger touch.
 struct TouchState
 {
-    /// Return last touched UI element, used by scripting integration.
-    UIElement* GetTouchedElement();
-
     /// Touch (finger) ID.
     int touchID_;
     /// Position in screen coordinates.
@@ -66,7 +63,7 @@ struct TouchState
     /// Finger pressure.
     float pressure_;
     /// Last touched UI element from screen joystick.
-    WeakPtr<UIElement> touchedElement_;
+    UIWidget* touchedWidget_;
 };
 
 /// %Input state for a joystick.

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

@@ -802,4 +802,10 @@ SystemUI::MessageBox* UI::ShowSystemMessageBox(const String& title, const String
 
 }
 
+
+UIWidget* UI::GetWidgetAt(int x, int y, bool include_children)
+{
+    return WrapWidget(rootWidget_->GetWidgetAt(x, y, include_children));
+}
+
 }

+ 2 - 0
Source/Atomic/UI/UI.h

@@ -103,6 +103,8 @@ public:
 
     UIRenderer* GetRenderer() { return renderer_; }
 
+    UIWidget* GetWidgetAt(int x, int y, bool include_children);
+
 private:
 
     static WeakPtr<Context> uiContext_;

+ 9 - 0
Source/AtomicJS/Javascript/JSAPI.cpp

@@ -408,6 +408,7 @@ void js_push_variant(duk_context *ctx, const Variant& v)
     VariantType type = v.GetType();
     RefCounted* ref;
     Vector2 vector2 = Vector2::ZERO;
+    IntVector2 intVector2 = IntVector2::ZERO;
     Vector3 vector3 = Vector3::ZERO;
     Vector4 vector4 = Vector4::ZERO;
     Color color = Color::BLACK;
@@ -480,6 +481,14 @@ void js_push_variant(duk_context *ctx, const Variant& v)
         duk_push_number(ctx, vector2.y_);
         duk_put_prop_index(ctx, -2, 1);
         break;
+    case VAR_INTVECTOR2:
+        intVector2 = v.GetIntVector2();
+        duk_push_array(ctx);
+        duk_push_number(ctx, intVector2.x_);
+        duk_put_prop_index(ctx, -2, 0);
+        duk_push_number(ctx, intVector2.y_);
+        duk_put_prop_index(ctx, -2, 1);
+        break;
     case VAR_VECTOR3:
         vector3 = v.GetVector3();
         duk_push_array(ctx);

+ 2 - 0
Source/AtomicJS/Javascript/JSAtomic.cpp

@@ -45,6 +45,7 @@
 #include "JSAtomic3D.h"
 #endif
 #include "JSIO.h"
+#include "JSInput.h"
 #include "JSUIAPI.h"
 #include "JSScene.h"
 
@@ -293,6 +294,7 @@ void jsapi_init_atomic(JSVM* vm)
 #ifdef ATOMIC_3D
     jsapi_init_atomic3d(vm);
 #endif
+    jsapi_init_input(vm);
     jsapi_init_ui(vm);
     jsapi_init_scene(vm);
 

+ 64 - 0
Source/AtomicJS/Javascript/JSInput.cpp

@@ -0,0 +1,64 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include <Atomic/Input/Input.h>
+
+#include "JSInput.h"
+
+namespace Atomic
+{
+
+    static int Input_GetTouch(duk_context* ctx)
+    {
+        duk_push_this(ctx);
+
+        Input* input = js_to_class_instance<Input>(ctx, -1, 0);
+
+        unsigned index = (unsigned)duk_to_number(ctx, 0);
+
+        TouchState* touchState = input->GetTouch(index);
+
+        VariantMap vmap;
+        vmap["touchID"] = touchState->touchID_;
+        vmap["position"] = touchState->position_;
+        vmap["lastPosition"] = touchState->lastPosition_;
+        vmap["delta"] = touchState->delta_;
+        vmap["pressure"] = touchState->pressure_;
+        vmap["touchedWidget"] = touchState->touchedWidget_;
+
+        js_push_variantmap(ctx, vmap);
+        
+        return 1;
+    }
+
+    void jsapi_init_input(JSVM* vm)
+    {
+        duk_context* ctx = vm->GetJSContext();
+
+        js_class_get_prototype(ctx, "Atomic", "Input");
+        duk_push_c_function(ctx, Input_GetTouch, 1);
+        duk_put_prop_string(ctx, -2, "getTouch");
+        duk_pop(ctx); // pop AObject prototype
+
+    }
+
+}

+ 34 - 0
Source/AtomicJS/Javascript/JSInput.h

@@ -0,0 +1,34 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "JSAPI.h"
+
+namespace Atomic
+{
+
+    class JSVM;
+
+    void jsapi_init_input(JSVM* vm);
+
+}