2
0
Эх сурвалжийг харах

Add more vector type support in Lua, rename ToluaUrho3DEx file to tolua++urho3d.

aster2013 12 жил өмнө
parent
commit
27f7a2b31f

+ 1 - 1
Source/Extras/LuaScript/LuaFunction.cpp

@@ -26,7 +26,7 @@
 #include "LuaScript.h"
 #include "LuaScriptInstance.h"
 #include "VectorBuffer.h"
-#include "ToluaUrho3DEx.h"
+#include "tolua++urho3d.h"
 
 extern "C"
 {

+ 1 - 1
Source/Extras/LuaScript/LuaScript.cpp

@@ -32,7 +32,6 @@
 #include "Profiler.h"
 #include "ResourceCache.h"
 #include "Scene.h"
-#include "ToluaUrho3DEx.h"
 
 extern "C"
 {
@@ -41,6 +40,7 @@ extern "C"
 #include <lauxlib.h>
 }
 #include "tolua++.h"
+#include "tolua++urho3d.h"
 
 #include "DebugNew.h"
 

+ 1 - 1
Source/Extras/LuaScript/LuaScriptInstance.cpp

@@ -34,7 +34,6 @@
 #include "ResourceCache.h"
 #include "Scene.h"
 #include "SceneEvents.h"
-#include "ToluaUrho3DEx.h"
 #include "ProcessUtils.h"
 #include "VectorBuffer.h"
 
@@ -45,6 +44,7 @@ extern "C"
 #include <lauxlib.h>
 }
 #include "tolua++.h"
+#include "tolua++urho3d.h"
 
 #include "DebugNew.h"
 

+ 0 - 112
Source/Extras/LuaScript/ToluaUrho3DEx.cpp

@@ -1,112 +0,0 @@
-//
-// Copyright (c) 2008-2013 the Urho3D project.
-//
-// 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 "Precompiled.h"
-#include "ToluaUrho3DEx.h"
-
-const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def)
-{
-    const char* s = tolua_tostring(L, narg, def);
-    return s?s:"";
-}
-
-const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def)
-{
-    return tolua_tourho3dstring(L, narg, def.CString());
-}
-
-int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
-{
-    if (lua_istable(L, lo))
-    {
-        int length = lua_objlen(L, lo);
-        for (int i = 1; i <= length; ++i)
-        {
-            lua_pushinteger(L, i);
-            lua_gettable(L, lo);
-            if (!lua_isstring(L, -1))
-            {
-                lua_pop(L, 1);
-
-                err->index = lo;
-                err->array = 0;
-                err->type = type;
-
-                return 0;
-            }
-
-            lua_pop(L, 1);
-        }
-
-        return 1;
-    }
-
-    err->index = lo;
-    err->array = 0;
-    err->type = type;
-    return 0;
-}
-
-void* tolua_tourho3dconstvectorstring(lua_State* L, int narg, void* def)
-{
-    if (!lua_istable(L, narg))
-        return 0;
-
-    static Vector<String> result;
-    result.Clear();
-
-    int length = lua_objlen(L, narg);
-    for (int i = 1; i <= length; ++i)
-    {
-        lua_pushinteger(L, i);
-        lua_gettable(L, narg);
-
-        if (!lua_isstring(L, -1))
-        {
-            lua_pop(L, 1);
-            return 0;
-        }
-
-        String string = tolua_tourho3dstring(L, -1, "");
-        result.Push(string);
-
-        lua_pop(L, 1);
-    }
-
-    return &result;
-}
-
-int tolua_pushurho3dconstvectorstring(lua_State*L, void* data, const char* type)
-{
-#ifndef TOLUA_RELEASE
-    assert(strcmp(type, "const Vector<String>") == 0);
-#endif
-
-    const Vector<String>& vectorstring = *((const Vector<String>*)data);
-    lua_newtable(L);
-    for (unsigned i = 0; i < vectorstring.Size(); ++i)
-    {
-        tolua_pushurho3dstring(L, vectorstring[i]);
-        lua_rawseti(L, -2, i + 1);
-    }
-    return 1;
-}

+ 0 - 13
Source/Extras/LuaScript/pkgs/Network/HttpRequest.pkg

@@ -38,16 +38,3 @@ class SharedPtr : public T
 };
 
 $renaming SharedPtr<HttpRequest> @ HttpRequestSPtr
-
-${
-static void* tolua_tourho3dhttprequest(lua_State* L, int reg, void* def)
-{
-    void* userdata = tolua_tousertype(L, reg, def);
-    
-    const char* type = tolua_typename(L, reg);
-    if (strcmp(type, "HttpRequest") == 0)
-        return userdata;
-        
-    return *static_cast<SharedPtr<HttpRequest>*>(userdata);
-}
-$}

+ 12 - 1
Source/Extras/LuaScript/pkgs/UI/DropDownList.pkg

@@ -18,6 +18,8 @@ class DropDownList : public Menu
    
     unsigned GetNumItems() const;
     UIElement* GetItem(unsigned index) const;
+    // PODVector<UIElement*> GetItems() const;
+    tolua_outside const PODVector<UIElement*>& DropDownListGetItems @ GetItems() const;
     unsigned GetSelection() const;
     UIElement* GetSelectedItem() const;
     ListView* GetListView() const;
@@ -34,4 +36,13 @@ class DropDownList : public Menu
     tolua_readonly tolua_property__get_set UIElement* placeholder;
     tolua_property__get_set String& placeholderText;
     tolua_property__get_set bool resizePopup;
-};
+};
+
+${
+static const PODVector<UIElement*>& DropDownListGetItems(const DropDownList* list)
+{
+    static PODVector<UIElement*> items;
+    items = list->GetItems();
+    return items;
+}
+$}

+ 22 - 0
Source/Extras/LuaScript/pkgs/UI/ListView.pkg

@@ -20,6 +20,7 @@ class ListView : public ScrollView
     void RemoveItem(unsigned index);
     void RemoveAllItems();
     void SetSelection(unsigned index);
+    void SetSelections(const PODVector<unsigned>& indices);
     void AddSelection(unsigned index);
     void RemoveSelection(unsigned index);
     void ToggleSelection(unsigned index);
@@ -37,9 +38,14 @@ class ListView : public ScrollView
     
     unsigned GetNumItems() const;
     UIElement* GetItem(unsigned index) const;
+    // PODVector<UIElement*> GetItems() const;
+    tolua_outside const PODVector<UIElement*>& ListViewGetItems @ GetItems() const;
     unsigned FindItem(UIElement* item) const;
     unsigned GetSelection() const;
+    const PODVector<unsigned>& GetSelections() const;
     UIElement* GetSelectedItem() const;
+    // PODVector<UIElement*> GetSelectedItems() const;
+    tolua_outside const PODVector<UIElement*>& ListViewGetSelectedItems @ GetSelectedItems() const;
     bool IsSelected(unsigned index) const;
     bool IsExpanded(unsigned index) const;
     HighlightMode GetHighlightMode() const;
@@ -57,3 +63,19 @@ class ListView : public ScrollView
     tolua_property__get_set bool hierarchyMode;
     tolua_property__get_set int baseIndent;
 };
+
+${
+static const PODVector<UIElement*>& ListViewGetItems(const ListView* listView)
+{
+    static PODVector<UIElement*> items;
+    items = listView->GetItems();
+    return items;
+}
+
+static const PODVector<UIElement*>& ListViewGetSelectedItems(const ListView* listView)
+{
+    static PODVector<UIElement*> items;
+    items = listView->GetSelectedItems();
+    return items;
+}
+$}

+ 5 - 0
Source/Extras/LuaScript/pkgs/UI/Text.pkg

@@ -41,7 +41,12 @@ class Text : public UIElement
     const Color& GetEffectColor() const;
     int GetRowHeight() const;
     unsigned GetNumRows() const;
+    const PODVector<int>& GetRowWidths() const;
+    const PODVector<IntVector2>& GetCharPositions() const;
+    const PODVector<IntVector2>& GetCharSizes() const;
     
+    void SetEffectDepthBias(float bias);
+    float GetEffectDepthBias() const;
     void SetFontAttr(ResourceRef value);
     ResourceRef GetFontAttr() const;
     

+ 1 - 0
Source/Extras/LuaScript/pkgs/UI/Text3D.pkg

@@ -43,6 +43,7 @@ class Text3D : public Drawable
     int GetWidth() const;
     int GetRowHeight() const;
     unsigned GetNumRows() const;
+    const PODVector<int>& GetRowWidths() const;
     const Color& GetColor(Corner corner) const;
     float GetOpacity() const;
     bool GetFaceCamera() const;

+ 3 - 3
Source/Extras/LuaScript/pkgs/UI/UI.pkg

@@ -24,13 +24,12 @@ class UI : public Object
     UIElement* GetRoot() const;
     UIElement* GetRootModalElement() const;
     Cursor* GetCursor() const;
-
+    IntVector2 GetCursorPosition() const;
     UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true);
     UIElement* GetElementAt(int x, int y, bool enabledOnly = true);
 
     UIElement* GetFocusElement() const;
     UIElement* GetFrontElement() const;
-    IntVector2 GetCursorPosition() const;
     const String GetClipBoardText() const;
     float GetDoubleClickInterval() const;
     int GetMaxFontTextureSize() const;
@@ -43,9 +42,10 @@ class UI : public Object
     tolua_readonly tolua_property__get_set UIElement* root;
     tolua_readonly tolua_property__get_set UIElement* rootModalElement;
     tolua_property__get_set Cursor* cursor;
+    tolua_readonly tolua_property__get_set IntVector2 cursorPosition;
     tolua_readonly tolua_property__get_set UIElement* focusElement;
     tolua_readonly tolua_property__get_set UIElement* frontElement;
-    tolua_readonly tolua_property__get_set IntVector2 cursorPosition;
+
     tolua_property__get_set String& clipBoardText;
     tolua_property__get_set float doubleClickInterval;
     tolua_property__get_set int maxFontTextureSize;

+ 15 - 3
Source/Extras/LuaScript/pkgs/basic.lua

@@ -3,12 +3,24 @@ local currentString = ''
 local out
 local WRITE, OUTPUT = write, output
 
+_to_functions["HttpRequest"] = "tolua_tourho3dhttprequest"
+
+_is_functions["const PODVector<IntVector2>"] = "not implemented"
+_to_functions["const PODVector<IntVector2>"] = "not implemented"
+_push_functions["const PODVector<IntVector2>"] = "tolua_pushurho3dconstpodvectorintvector2"
+
+_is_functions["const PODVector<UIElement*>"] = "not implemented"
+_to_functions["const PODVector<UIElement*>"] = "not implemented"
+_push_functions["const PODVector<UIElement*>"] = "tolua_pushurho3dpodvectoruielement"
+
+_is_functions["const PODVector<unsigned>"] = "tolua_isurho3dconstpodvectorunsigned"
+_to_functions["const PODVector<unsigned>"] = "tolua_tourho3dconstpodvectorunsigned"
+_push_functions["const PODVector<unsigned>"] = "tolua_pushurho3dconstpodvectorunsigned"
+
 _is_functions["const Vector<String>"] = "tolua_isurho3dconstvectorstring"
 _to_functions["const Vector<String>"] = "tolua_tourho3dconstvectorstring"
 _push_functions["const Vector<String>"] = "tolua_pushurho3dconstvectorstring"
 
-_to_functions["HttpRequest"] = "tolua_tourho3dhttprequest"
-
 function output(s)
     out = _OUTPUT
     output = OUTPUT -- restore
@@ -74,7 +86,7 @@ function post_output_hook(package)
 //
 
 #include "Precompiled.h"
-#include "ToluaUrho3DEx.h"]]
+#include "tolua++urho3d.h"]]
       )
 
     WRITE(result)

+ 243 - 0
Source/Extras/LuaScript/tolua++urho3d.cpp

@@ -0,0 +1,243 @@
+//
+// Copyright (c) 2008-2013 the Urho3D project.
+//
+// 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 "Precompiled.h"
+#include "tolua++urho3d.h"
+
+#include "HttpRequest.h"
+#include "Ptr.h"
+
+const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def)
+{
+    const char* s = tolua_tostring(L, narg, def);
+    return s ? s : "";
+}
+
+const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def)
+{
+    return tolua_tourho3dstring(L, narg, def.CString());
+}
+
+void* tolua_tourho3dhttprequest(lua_State* L, int reg, void* def)
+{
+    void* userdata = tolua_tousertype(L, reg, def);
+
+    /// Use data type may be SharedPtr<HttpRequest>.
+    const char* type = tolua_typename(L, reg);
+    if (strcmp(type, "SharedPtr<HttpRequest>") == 0)
+        return *static_cast<SharedPtr<HttpRequest>*>(userdata);
+
+    return userdata;
+}
+
+int tolua_pushurho3dconstpodvectorintvector2(lua_State* L, void* data, const char* type)
+{
+#ifndef TOLUA_RELEASE
+    assert(strcmp(type, "const PODVector<IntVector2>") == 0);
+#endif
+
+    const PODVector<IntVector2>& vector = *((const PODVector<IntVector2>*)data);
+    lua_newtable(L);
+    for (unsigned i = 0; i < vector.Size(); ++i)
+    {
+        void* tolua_obj = Mtolua_new((IntVector2)(vector[i]));
+        tolua_pushusertype(L,tolua_obj,"IntVector2");
+        tolua_register_gc(L,lua_gettop(L));
+
+        lua_rawseti(L, -2, i + 1);
+    }
+
+    return 1;
+}
+
+int tolua_pushurho3dpodvectoruielement(lua_State* L, void* data, const char* type)
+{
+#ifndef TOLUA_RELEASE
+    assert(strcmp(type, "const PODVector<UIElement*>") == 0);
+#endif
+
+    const PODVector<UIElement*>& vector = *((const PODVector<UIElement*>*)data);
+    lua_newtable(L);
+    for (unsigned i = 0; i < vector.Size(); ++i)
+    {
+        tolua_pushusertype(L, vector[i], "UIElement");
+        lua_rawseti(L, -2, i + 1);
+    }
+
+    return 1;
+}
+
+int tolua_isurho3dconstpodvectorunsigned(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
+{
+    if (lua_istable(L, lo))
+    {
+        int length = lua_objlen(L, lo);
+        for (int i = 1; i <= length; ++i)
+        {
+            lua_pushinteger(L, i);
+            lua_gettable(L, lo);
+            if (!lua_isnumber(L, -1))
+            {
+                lua_pop(L, 1);
+
+                err->index = lo;
+                err->array = 0;
+                err->type = type;
+
+                return 0;
+            }
+
+            lua_pop(L, 1);
+        }
+
+        return 1;
+    }
+
+    err->index = lo;
+    err->array = 0;
+    err->type = type;
+    return 0;
+}
+
+void* tolua_tourho3dconstpodvectorunsigned(lua_State* L, int narg, void* def)
+{
+    if (!lua_istable(L, narg))
+        return 0;
+
+    static Vector<unsigned> result;
+    result.Clear();
+
+    int length = lua_objlen(L, narg);
+    for (int i = 1; i <= length; ++i)
+    {
+        lua_pushinteger(L, i);
+        lua_gettable(L, narg);
+
+        if (!lua_isnumber(L, -1))
+        {
+            lua_pop(L, 1);
+            return 0;
+        }
+
+        unsigned value = (unsigned)tolua_tonumber(L, -1, 0);
+        result.Push(value);
+
+        lua_pop(L, 1);
+    }
+
+    return &result;
+}
+
+int tolua_pushurho3dconstpodvectorunsigned(lua_State* L, void* data, const char* type)
+{
+#ifndef TOLUA_RELEASE
+    assert(strcmp(type, "const PODVector<unsigned>") == 0);
+#endif
+
+    const PODVector<unsigned>& vector = *((const PODVector<unsigned>*)data);
+    lua_newtable(L);
+    for (unsigned i = 0; i < vector.Size(); ++i)
+    {
+        lua_pushinteger(L, vector[i]);
+        lua_rawseti(L, -2, i + 1);
+    }
+
+    return 1;
+}
+
+int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
+{
+    if (lua_istable(L, lo))
+    {
+        int length = lua_objlen(L, lo);
+        for (int i = 1; i <= length; ++i)
+        {
+            lua_pushinteger(L, i);
+            lua_gettable(L, lo);
+            if (!lua_isstring(L, -1))
+            {
+                lua_pop(L, 1);
+
+                err->index = lo;
+                err->array = 0;
+                err->type = type;
+
+                return 0;
+            }
+
+            lua_pop(L, 1);
+        }
+
+        return 1;
+    }
+
+    err->index = lo;
+    err->array = 0;
+    err->type = type;
+    return 0;
+}
+
+void* tolua_tourho3dconstvectorstring(lua_State* L, int narg, void* def)
+{
+    if (!lua_istable(L, narg))
+        return 0;
+
+    static Vector<String> result;
+    result.Clear();
+
+    int length = lua_objlen(L, narg);
+    for (int i = 1; i <= length; ++i)
+    {
+        lua_pushinteger(L, i);
+        lua_gettable(L, narg);
+
+        if (!lua_isstring(L, -1))
+        {
+            lua_pop(L, 1);
+            return 0;
+        }
+
+        String string = tolua_tourho3dstring(L, -1, "");
+        result.Push(string);
+
+        lua_pop(L, 1);
+    }
+
+    return &result;
+}
+
+int tolua_pushurho3dconstvectorstring(lua_State*L, void* data, const char* type)
+{
+#ifndef TOLUA_RELEASE
+    assert(strcmp(type, "const Vector<String>") == 0);
+#endif
+
+    const Vector<String>& vectorstring = *((const Vector<String>*)data);
+    lua_newtable(L);
+    for (unsigned i = 0; i < vectorstring.Size(); ++i)
+    {
+        tolua_pushurho3dstring(L, vectorstring[i]);
+        lua_rawseti(L, -2, i + 1);
+    }
+    return 1;
+}
+

+ 27 - 3
Source/Extras/LuaScript/ToluaUrho3DEx.h → Source/Extras/LuaScript/tolua++urho3d.h

@@ -22,17 +22,41 @@
 
 #pragma once
 
+#include "Vector2.h"
+
 struct lua_State;
 
+namespace Urho3D
+{
+    class UIElement;
+}
+
 using namespace Urho3D;
 
+/// Check is String.
 #define tolua_isurho3dstring tolua_isstring
-#define tolua_pushurho3dstring(x,y)	tolua_pushstring(x,y.CString())
-
-/// Convert Lua string to Urho3D string.
+/// Push String.
+#define tolua_pushurho3dstring(x, y) tolua_pushstring(x, y.CString())
+/// Convert to String.
 const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def);
 const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def);
 
+/// Convert to HttpRequest.
+void* tolua_tourho3dhttprequest(lua_State* L, int reg, void* def);
+
+/// Push const PODVector<IntVector2>.
+int tolua_pushurho3dconstpodvectorintvector2(lua_State* L, void* data, const char* type);
+
+/// Push const PODVector<UIElement*>.
+int tolua_pushurho3dpodvectoruielement(lua_State* L, void* data, const char* type);
+
+/// Check is const PODVector<unsigned>.
+int tolua_isurho3dconstpodvectorunsigned(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
+/// Convert to const PODVector<unsigned>.
+void* tolua_tourho3dconstpodvectorunsigned(lua_State* L, int narg, void* def);
+/// Push const PODVector<unsigned>.
+int tolua_pushurho3dconstpodvectorunsigned(lua_State* L, void* data, const char* type);
+
 /// Check is const Vector<String>.
 int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
 /// Convert to const Vector<String>.