Browse Source

Expose touchedElement of TouchState to Lua, and to AngelScript as an ordinary handle to simplify code. Closes #342.

Lasse Öörni 11 years ago
parent
commit
76a7a60918

+ 1 - 1
Bin/Data/Scripts/NinjaSnowWar.as

@@ -893,7 +893,7 @@ void UpdateControls()
             for (uint i = 0; i < input.numTouches; ++i)
             {
                 TouchState@ touch = input.touches[i];
-                if (touch.touchedElement.Get() is null)
+                if (touch.touchedElement is null)
                 {
                     // Touch on empty space
                     playerControls.yaw += touchSensitivity * gameCamera.fov / graphics.height * touch.delta.x;

+ 5 - 0
Source/Engine/Input/Input.cpp

@@ -70,6 +70,11 @@ int ConvertSDLKeyCode(int keySym, int scanCode)
         return SDL_toupper(keySym);
 }
 
+UIElement* TouchState::GetTouchedElement()
+{
+    return touchedElement_.Get();
+}
+
 void JoystickState::Initialize(unsigned numButtons, unsigned numAxes, unsigned numHats)
 {
     buttons_.Resize(numButtons);

+ 3 - 0
Source/Engine/Input/Input.h

@@ -39,6 +39,9 @@ class XMLFile;
 /// %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.

+ 4 - 1
Source/Engine/LuaScript/pkgs/Input/Input.pkg

@@ -3,12 +3,15 @@ $#include "Input.h"
 
 struct TouchState
 {
+    UIElement* GetTouchedElement();
+
     int touchID_ @ touchID;
     IntVector2 position_ @ position;
     IntVector2 lastPosition_ @ lastPosition;
     IntVector2 delta_ @ delta;
     float pressure_ @ pressure;
-    WeakPtr<UIElement> touchedElement_ @ touchedElement;
+
+    tolua_readonly tolua_property__get_set UIElement* touchedElement;
 };
 
 struct JoystickState

+ 0 - 1
Source/Engine/Script/InputAPI.cpp

@@ -453,7 +453,6 @@ static void RegisterInput(asIScriptEngine* engine)
     engine->RegisterObjectProperty("TouchState", "const IntVector2 lastPosition", offsetof(TouchState, lastPosition_));
     engine->RegisterObjectProperty("TouchState", "const IntVector2 delta", offsetof(TouchState, delta_));
     engine->RegisterObjectProperty("TouchState", "const float pressure", offsetof(TouchState, pressure_));
-    engine->RegisterObjectProperty("TouchState", "const WeakHandle touchedElement", offsetof(TouchState, touchedElement_));
 
     engine->RegisterObjectType("JoystickState", 0, asOBJ_REF);
     engine->RegisterObjectBehaviour("JoystickState", asBEHAVE_ADDREF, "void f()", asFUNCTION(FakeAddRef), asCALL_CDECL_OBJLAST);

+ 4 - 0
Source/Engine/Script/UIAPI.cpp

@@ -27,6 +27,7 @@
 #include "DropDownList.h"
 #include "FileSelector.h"
 #include "Font.h"
+#include "Input.h"
 #include "LineEdit.h"
 #include "ListView.h"
 #include "MessageBox.h"
@@ -104,6 +105,9 @@ static void RegisterUIElement(asIScriptEngine* engine)
     engine->RegisterGlobalProperty("const uint DD_SOURCE_AND_TARGET", (void*)&DD_SOURCE_AND_TARGET);
 
     RegisterUIElement<UIElement>(engine, "UIElement");
+
+    // Register TouchState touchedElement property now
+    engine->RegisterObjectMethod("TouchState", "UIElement@+ get_touchedElement()", asMETHOD(TouchState, GetTouchedElement), asCALL_THISCALL);
 }
 
 static void RegisterBorderImage(asIScriptEngine* engine)