Browse Source

Support call Lua function in a table. Add CreateChild support in UIElement.

Aster Jian 12 years ago
parent
commit
bbfdc536ad
3 changed files with 75 additions and 31 deletions
  1. 50 24
      Extras/LuaScript/LuaScript.cpp
  2. 8 5
      Extras/LuaScript/LuaScript.h
  3. 17 2
      Extras/LuaScript/pkgs/UI/UIElement.pkg

+ 50 - 24
Extras/LuaScript/LuaScript.cpp

@@ -101,11 +101,8 @@ LuaScript::~LuaScript()
     lua_close(luaState_);
 }
 
-bool LuaScript::ExecuteFile(const char* fileName)
+bool LuaScript::ExecuteFile(const String& fileName)
 {
-    if (!fileName)
-        return false;
-
     PROFILE(ExecuteFile);
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
@@ -122,7 +119,7 @@ bool LuaScript::ExecuteFile(const char* fileName)
 
     int top = lua_gettop(luaState_);
 
-    int error = luaL_loadbuffer(luaState_, buffer, size, fileName);
+    int error = luaL_loadbuffer(luaState_, buffer, size, fileName.CString());
     delete [] buffer;
 
     if (error)
@@ -148,16 +145,13 @@ bool LuaScript::ExecuteFile(const char* fileName)
     return true;
 }
 
-bool LuaScript::ExecuteString(const char* string)
+bool LuaScript::ExecuteString(const String& string)
 {
-    if (string == 0)
-        return false;
-
     PROFILE(ExecuteString);
 
     int top = lua_gettop(luaState_);
 
-    if (luaL_dostring(luaState_, string) != 0)
+    if (luaL_dostring(luaState_, string.CString()) != 0)
     {
         const char* message = lua_tostring(luaState_, -1);
         lua_settop(luaState_, top);
@@ -171,20 +165,15 @@ bool LuaScript::ExecuteString(const char* string)
     return true;
 }
 
-bool LuaScript::ExecuteFunction(const char* funcName)
+bool LuaScript::ExecuteFunction(const String& functionName)
 {
-    if (funcName == 0)
-        return false;
-
     PROFILE(ExecuteFunction);
 
     int top = lua_gettop(luaState_);
 
-    lua_getglobal(luaState_, funcName);
-    if (!lua_isfunction(luaState_, -1))
+    if (!FindFunction(functionName))
     {
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to get Lua global: '") + funcName + "'.");
         return false;
     }
 
@@ -192,7 +181,7 @@ bool LuaScript::ExecuteFunction(const char* funcName)
     {
         const char* message = lua_tostring(luaState_, -1);
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to execute Lua function '") + funcName + "'.");
+        LOGRAW(String("Lua: Unable to execute Lua function '") + functionName + "'.");
         LOGRAW(String("Lua: ") + message);
         return false;
     }
@@ -200,19 +189,19 @@ bool LuaScript::ExecuteFunction(const char* funcName)
     return true;
 }
 
-void LuaScript::ScriptSendEvent(const char* eventName, VariantMap& eventData)
+void LuaScript::ScriptSendEvent(const String& eventName, VariantMap& eventData)
 {
 	SendEvent(StringHash(eventName), eventData);
 }
 
-void LuaScript::ScriptSubscribeToEvent(const char* eventName, const char* functionName)
+void LuaScript::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
 {
     StringHash eventType(eventName);
 
 	if (!eventTypeToFunctionNameMap_.Contains(eventType))
 		SubscribeToEvent(eventType, HANDLER(LuaScript, HandleEvent));
 
-    eventTypeToFunctionNameMap_[eventType].Push(String(functionName));
+    eventTypeToFunctionNameMap_[eventType].Push(functionName);
 }
 
 void LuaScript::ReplacePrintFunction()
@@ -255,6 +244,44 @@ int LuaScript::Print(lua_State *L)
     return 0;
 }
 
+bool LuaScript::FindFunction(const String& functionName)
+{
+    Vector<String> splitedNames = functionName.Split('.');
+    
+    String currentName = splitedNames.Front();
+    lua_getglobal(luaState_, currentName.CString());
+
+    if (splitedNames.Size() > 1)
+    {       
+        if (!lua_istable(luaState_, -1))
+        {
+            LOGRAW(String("Lua: Unable to find Lua table: '") + currentName + "'.");
+            return false;
+        }
+
+        for (unsigned i = 1; i < splitedNames.Size() - 1; ++i)
+        {
+            currentName = currentName + "." + splitedNames[i];
+            lua_getfield(luaState_, -1, splitedNames[i].CString());
+            if (!lua_istable(luaState_, -1))
+            {
+                LOGRAW(String("Lua: Unable to find Lua table: '") + currentName + "'.");
+                return false;
+            }
+        }
+
+        lua_getfield(luaState_, -1, splitedNames.Back().CString());
+    }
+
+    if (!lua_isfunction(luaState_, -1))
+    {
+        LOGRAW(String("Lua: Unable to find Lua function: '") + currentName + "'.");
+        return false;
+    }
+
+    return true;
+}
+
 void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
 {
     HashMap<StringHash, Vector<String> >::ConstIterator it = eventTypeToFunctionNameMap_.Find(eventType);
@@ -268,11 +295,9 @@ void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
 
 		int top = lua_gettop(luaState_);
 
-        lua_getglobal(luaState_, functionName.CString());
-        if (!lua_isfunction(luaState_, -1))
+        if (!FindFunction(functionName))
         {
             lua_settop(luaState_, top);
-            LOGRAW(String("Lua: Unable to get Lua global: '") + functionName + "'.");
             return;
         }
 
@@ -296,6 +321,7 @@ void LuaScript::HandleConsoleCommand(StringHash eventType, VariantMap& eventData
     ExecuteString(eventData[P_COMMAND].GetString().CString());
 }
 
+
 Context* GetContext()
 {
     return currentContext_;

+ 8 - 5
Extras/LuaScript/LuaScript.h

@@ -44,19 +44,19 @@ public:
     ~LuaScript();
 
     /// Execute script file.
-    bool ExecuteFile(const char* fileName);
+    bool ExecuteFile(const String& fileName);
 
     /// Execute script string.
-    bool ExecuteString(const char* string);
+    bool ExecuteString(const String& string);
 
     /// Execute script function.
-    bool ExecuteFunction(const char* funcName);
+    bool ExecuteFunction(const String& functionName);
 
     /// Script send event.
-	void ScriptSendEvent(const char* eventName, VariantMap& eventData);
+	void ScriptSendEvent(const String& eventName, VariantMap& eventData);
 
     /// Script subscribe event.
-    void ScriptSubscribeToEvent(const char* eventName, const char* functionName);
+    void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
 
 private:
     /// Replace print function.
@@ -65,6 +65,9 @@ private:
     /// Print function.
     static int Print(lua_State* L);
 
+    /// Find Lua function.
+    bool FindFunction(const String& functionName);
+
     /// Handle event.
     void HandleEvent(StringHash eventType, VariantMap& eventData);
 

+ 17 - 2
Extras/LuaScript/pkgs/UI/UIElement.pkg

@@ -126,8 +126,8 @@ class UIElement : public Serializable
     void EnableLayoutUpdate();
     void BringToFront();
     
-    UIElement* CreateChild(ShortStringHash type, const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
-    UIElement* CreateChild(const char* type, const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    // UIElement* CreateChild(ShortStringHash type, const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
+    // UIElement* CreateChild(const char* type, const char* name = 0, unsigned index = M_MAX_UNSIGNED);
     
     void AddChild(UIElement* element);
     void InsertChild(unsigned index, UIElement* element);
@@ -142,6 +142,21 @@ class UIElement : public Serializable
     void SetElementEventSender(bool flag);
 
     // template <class T> T* CreateChild(const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
+    BorderImage* CreateChild<BorderImage> @ CreateBorderImage(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Button* CreateChild<Button> @ CreateButton(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    CheckBox* CreateChild<CheckBox> @ CreateCheckBox(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Cursor* CreateChild<Cursor> @ CreateCursor(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    DropDownList* CreateChild<DropDownList> @ CreateDropDownList(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    LineEdit* CreateChild<LineEdit> @ CreateLineEdit(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    ListView* CreateChild<ListView> @ CreateListView(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Menu* CreateChild<Menu> @ CreateMenu(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    ScrollBar* CreateChild<ScrollBar> @ CreateScrollBar(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    ScrollView* CreateChild<ScrollView> @ CreateScrollView(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Slider* CreateChild<Slider> @ CreateSlider(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Sprite* CreateChild<Sprite> @ CreateSprite(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Text* CreateChild<Text> @ CreateText(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    UIElement* CreateChild<UIElement> @ CreateUIElement(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
+    Window* CreateChild<Window> @ CreateWindow(const char* name = 0, unsigned index = M_MAX_UNSIGNED);
 
     const String& GetName() const;
     const IntVector2& GetPosition() const;