瀏覽代碼

Replace require with ExecuteFile. Fixed property error, Add CreateRigidBody with mode paramter support.

Aster Jian 12 年之前
父節點
當前提交
38e2d98085

+ 1 - 1
Bin/Data/LuaScripts/TestScene.lua

@@ -1,4 +1,4 @@
-require "Data/LuaScripts/Utilities/Network"
+ExecuteFile("LuaScripts/Utilities/Network.lua")
 
 local testScene
 local camera

+ 21 - 27
Extras/LuaScript/LuaScript.cpp

@@ -57,13 +57,13 @@ extern int tolua_LuaScriptLuaAPI_open(lua_State*);
 namespace Urho3D
 {
 
-static Context* scriptContext_ = 0;
+static Context* currentContext_ = 0;
 
 LuaScript::LuaScript(Context* context) :
     Object(context),
     luaState_(0)
 {
-    scriptContext_ = context_;
+    currentContext_ = context_;
 
     luaState_ = luaL_newstate();
     if (!luaState_)
@@ -197,10 +197,15 @@ bool LuaScript::ExecuteFunction(const char* funcName)
     return true;
 }
 
-void LuaScript::SubscribeLuaEvent(const char* event, const char* funcName)
+void LuaScript::ScriptSendEvent(const char* eventName, VariantMap& eventData)
 {
-    StringHash eventType(event);
-    eventFunctionMap_[eventType].Push(String(funcName));
+	SendEvent(StringHash(eventName), eventData);
+}
+
+void LuaScript::ScriptSubscribeToEvent(const char* eventName, const char* functionName)
+{
+    StringHash eventType(eventName);
+    eventTypeToFunctionNameMap_[eventType].Push(String(functionName));
 
     SubscribeToEvent(eventType, HANDLER(LuaScript, HandleEvent));
 }
@@ -254,19 +259,20 @@ int LuaScript::PCallCallback(lua_State* L)
 
 void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
 {
-    HashMap<StringHash, Vector<String> >::ConstIterator it = eventFunctionMap_.Find(eventType);
-    if (it == eventFunctionMap_.End())
+    HashMap<StringHash, Vector<String> >::ConstIterator it = eventTypeToFunctionNameMap_.Find(eventType);
+    if (it == eventTypeToFunctionNameMap_.End())
         return;
 
     for (unsigned i = 0; i < it->second_.Size(); ++i)
     {
-        const String& funcName = it->second_[i];
-        int top = lua_gettop(luaState_);
-        lua_getglobal(luaState_, funcName.CString());
+        const String& functionName = it->second_[i];
+        
+		int top = lua_gettop(luaState_);
+        lua_getglobal(luaState_, functionName.CString());
         if (!lua_isfunction(luaState_, -1))
         {
             lua_settop(luaState_, top);
-            LOGRAW(String("Lua: Unable to get lua global: '") + funcName + "'.");
+            LOGRAW(String("Lua: Unable to get lua global: '") + functionName + "'.");
             return;
         }
 
@@ -274,10 +280,10 @@ void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
         tolua_pushusertype(luaState_, (void*)&eventData, "VariantMap");
         if (lua_pcall(luaState_, 2, 0, 0))
         {
-            String message = lua_tostring(luaState_, -1);
+            const char* message = lua_tostring(luaState_, -1);
             lua_settop(luaState_, top);
-            LOGRAW(String("Lua: Unable to execute function '") + funcName + "'.");
-            LOGRAW("Lua: " + message);
+            LOGRAW(String("Lua: Unable to execute function '") + functionName + "'.");
+            LOGRAW(String("Lua: ") + message);
             return;
         }
     }
@@ -291,19 +297,7 @@ void LuaScript::HandleConsoleCommand(StringHash eventType, VariantMap& eventData
 
 Context* GetContext()
 {
-    return scriptContext_;
-}
-
-void SendEvent(const char* eventType, VariantMap& eventData)
-{
-    LuaScript* luaScript = scriptContext_->GetSubsystem<LuaScript>();
-    luaScript->SendEvent(StringHash(eventType), eventData);
-}
-
-void SubscribeToEvent(const char* eventType, const char* funcName)
-{
-    LuaScript* luaScript = scriptContext_->GetSubsystem<LuaScript>();
-    luaScript->SubscribeLuaEvent(eventType, funcName);
+    return currentContext_;
 }
 
 }

+ 9 - 10
Extras/LuaScript/LuaScript.h

@@ -52,11 +52,14 @@ public:
     /// Execute lua function.
     bool ExecuteFunction(const char* funcName);
 
-    /// Return the lua state.
-    lua_State* GetLuaState() const { return luaState_; }
+    /// Script send event.
+	void ScriptSendEvent(const char* eventName, VariantMap& eventData);
 
-    /// Subscribe lua event.
-    void SubscribeLuaEvent(const char* event, const char* function);
+    /// Script subscribe event.
+    void ScriptSubscribeToEvent(const char* eventName, const char* functionName);
+
+	/// Return the lua state.
+	lua_State* GetLuaState() const { return luaState_; }
 
 private:
     /// Replace print function.
@@ -77,15 +80,11 @@ private:
 private:
     /// Lua state.
     lua_State* luaState_;
-    /// Event type to Lua function name map.
-    HashMap<StringHash, Vector<String> > eventFunctionMap_;
+    /// Event type to function name map.
+    HashMap<StringHash, Vector<String> > eventTypeToFunctionNameMap_;
 };
 
 /// Return context.
 Context* GetContext();
-///  Send event function for Lua script.
-void SendEvent(const char* eventType, VariantMap& eventData);
-/// Subscribe event function for Lua script.
-void SubscribeToEvent(const char* eventType, const char* funcName);
 
 }

+ 27 - 2
Extras/LuaScript/pkgs/LuaScriptLuaAPI.pkg

@@ -2,8 +2,33 @@ $#define TOLUA_RELEASE
 
 $#include "LuaScript.h"
 
-void SendEvent(const char* eventType, VariantMap& eventData);
-void SubscribeToEvent(const char* eventType, const char* funcName);
+void ExecuteFile(const char* fileName);
+void SendEvent(const char* eventName, VariantMap& eventData);
+void SubscribeToEvent(const char* eventName, const char* functionName);
 
 $using namespace Urho3D;
 $#pragma warning(disable:4800)
+
+${
+
+static LuaScript* GetLuaScript()
+{
+    return GetContext()->GetSubsystem<LuaScript>();
+}
+
+static bool ExecuteFile(const char* fileName)
+{
+    return GetLuaScript()->ExecuteFile(fileName);
+}
+
+static void SendEvent(const char* eventName, VariantMap& eventData)
+{
+    GetLuaScript()->ScriptSendEvent(eventName, eventData);
+}
+
+static void SubscribeToEvent(const char* eventName, const char* functionName)
+{
+    GetLuaScript()->ScriptSubscribeToEvent(eventName, functionName);
+}
+
+$}

+ 8 - 1
Extras/LuaScript/pkgs/Physics/Constraint.pkg

@@ -82,7 +82,7 @@ public:
     tolua_property__get_set const Quaternion& rotation;
     tolua_property__get_set const Vector3& otherPosition;
     tolua_property__get_set const Quaternion& otherRotation;
-    tolua_property__get_set const Vector3& worldPosition;
+    tolua_property__get_set Vector3 worldPosition;
     tolua_property__get_set const Vector2& highLimit;
     tolua_property__get_set const Vector2& lowLimit;
     tolua_property__get_set float ERP;
@@ -91,3 +91,10 @@ public:
     tolua_readonly tolua_property__get_set RigidBody* ownBody;
     tolua_property__get_set RigidBody* otherBody;
 };
+
+${
+
+#define TOLUA_DISABLE_tolua_get_Constraint_worldPosition
+#define tolua_get_Constraint_worldPosition tolua_PhysicsLuaAPI_Constraint_GetWorldPosition00
+
+$}

+ 29 - 6
Extras/LuaScript/pkgs/Physics/RigidBody.pkg

@@ -155,14 +155,14 @@ public:
     
     // Properties:
     tolua_property__get_set float mass;
-    tolua_property__get_set const Vector3& position;
-    tolua_property__get_set const Quaternion& rotation;
-    tolua_property__get_set const Vector3& linearVelocity;
-    tolua_property__get_set const Vector3& linearFactor;
+    tolua_property__get_set Vector3 position;
+    tolua_property__get_set Quaternion rotation;
+    tolua_property__get_set Vector3 linearVelocity;
+    tolua_property__get_set Vector3 linearFactor;
     tolua_property__get_set float linearRestThreshold;
     tolua_property__get_set float linearDamping;
-    tolua_property__get_set const Vector3& angularVelocity;
-    tolua_property__get_set const Vector3& angularFactor;
+    tolua_property__get_set Vector3 angularVelocity;
+    tolua_property__get_set Vector3 angularFactor;
     tolua_property__get_set float angularRestThreshold;
     tolua_property__get_set float angularDamping;
     tolua_property__get_set float friction;
@@ -180,3 +180,26 @@ public:
     tolua_property__get_set unsigned collisionMask;
     tolua_property__get_set CollisionEventMode collisionEventMode;
 };
+
+${
+
+#define TOLUA_DISABLE_tolua_get_RigidBody_position
+#define tolua_get_RigidBody_position tolua_PhysicsLuaAPI_RigidBody_GetPosition00
+
+
+#define TOLUA_DISABLE_tolua_get_RigidBody_rotation
+#define tolua_get_RigidBody_rotation tolua_PhysicsLuaAPI_RigidBody_GetRotation00
+
+#define TOLUA_DISABLE_tolua_get_RigidBody_linearVelocity
+#define tolua_get_RigidBody_linearVelocity tolua_PhysicsLuaAPI_RigidBody_GetLinearVelocity00
+
+#define TOLUA_DISABLE_tolua_get_RigidBody_linearFactor
+#define tolua_get_RigidBody_linearFactor tolua_PhysicsLuaAPI_RigidBody_GetLinearFactor00
+
+#define TOLUA_DISABLE_tolua_get_RigidBody_angularVelocity
+#define tolua_get_RigidBody_angularVelocity tolua_PhysicsLuaAPI_RigidBody_GetAngularVelocity00
+
+#define TOLUA_DISABLE_tolua_get_RigidBody_angularFactor
+#define tolua_get_RigidBody_angularFactor tolua_PhysicsLuaAPI_RigidBody_GetAngularFactor00
+
+$}

+ 48 - 23
Extras/LuaScript/pkgs/Scene/Node.pkg

@@ -133,6 +133,7 @@ public:
     
     /// Template version of creating a component.
     // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
+    
     AnimationController* CreateComponent<AnimationController> @ CreateAnimationController();
     AnimatedModel* CreateComponent<AnimatedModel> @ CreateAnimatedModel();
     Camera* CreateComponent<Camera> @ CreateCamera();
@@ -155,32 +156,56 @@ public:
     StaticModel* CreateComponent<StaticModel> @ CreateStaticModel();
     Terrain* CreateComponent<Terrain> @ CreateTerrain();
     Zone* CreateComponent<Zone> @ CreateZone();
+    
+    AnimationController* CreateComponent<AnimationController> @ CreateAnimationController(CreateMode mode);
+    AnimatedModel* CreateComponent<AnimatedModel> @ CreateAnimatedModel(CreateMode mode);
+    Camera* CreateComponent<Camera> @ CreateCamera(CreateMode mode);
+    CollisionShape* CreateComponent<CollisionShape> @ CreateCollisionShape(CreateMode mode);
+    Constraint* CreateComponent<Constraint> @ CreateConstraint(CreateMode mode);
+    DebugRenderer* CreateComponent<DebugRenderer> @ CreateDebugRenderer(CreateMode mode);
+    DecalSet* CreateComponent<DecalSet> @ CreateDecalSet(CreateMode mode);
+    Drawable* CreateComponent<Drawable> @ CreateDrawable(CreateMode mode);
+    Light* CreateComponent<Light> @ CreateLight(CreateMode mode);
+    Navigable* CreateComponent<Navigable> @ CreateNavigable(CreateMode mode);
+    NavigationMesh* CreateComponent<NavigationMesh> @ CreateNavigationMesh(CreateMode mode);
+    NetworkPriority* CreateComponent<NetworkPriority> @ CreateNetworkPriority(CreateMode mode);
+    Octree* CreateComponent<Octree> @ CreateOctree(CreateMode mode);
+    OffMeshConnection* CreateComponent<OffMeshConnection> @ CreateOffMeshConnection(CreateMode mode);
+    PhysicsWorld* CreateComponent<PhysicsWorld> @ CreatePhysicsWorld(CreateMode mode);
+    RigidBody* CreateComponent<RigidBody> @ CreateRigidBody(CreateMode mode);
+    SmoothedTransform* CreateComponent<SmoothedTransform> @ CreateSmoothedTransform(CreateMode mode);
+    SoundListener* CreateComponent<SoundListener> @ CreateSoundListener(CreateMode mode);
+    SoundSource* CreateComponent<SoundSource> @ CreateSoundSource(CreateMode mode);
+    StaticModel* CreateComponent<StaticModel> @ CreateStaticModel(CreateMode mode);
+    Terrain* CreateComponent<Terrain> @ CreateTerrain(CreateMode mode);
+    Zone* CreateComponent<Zone> @ CreateZone(CreateMode mode);
+    
+    AnimationController* CreateComponent<AnimationController> @ CreateAnimationController(CreateMode mode, unsigned id);
+    AnimatedModel* CreateComponent<AnimatedModel> @ CreateAnimatedModel(CreateMode mode, unsigned id);
+    Camera* CreateComponent<Camera> @ CreateCamera(CreateMode mode, unsigned id);
+    CollisionShape* CreateComponent<CollisionShape> @ CreateCollisionShape(CreateMode mode, unsigned id);
+    Constraint* CreateComponent<Constraint> @ CreateConstraint(CreateMode mode, unsigned id);
+    DebugRenderer* CreateComponent<DebugRenderer> @ CreateDebugRenderer(CreateMode mode, unsigned id);
+    DecalSet* CreateComponent<DecalSet> @ CreateDecalSet(CreateMode mode, unsigned id);
+    Drawable* CreateComponent<Drawable> @ CreateDrawable(CreateMode mode, unsigned id);
+    Light* CreateComponent<Light> @ CreateLight(CreateMode mode, unsigned id);
+    Navigable* CreateComponent<Navigable> @ CreateNavigable(CreateMode mode, unsigned id);
+    NavigationMesh* CreateComponent<NavigationMesh> @ CreateNavigationMesh(CreateMode mode, unsigned id);
+    NetworkPriority* CreateComponent<NetworkPriority> @ CreateNetworkPriority(CreateMode mode, unsigned id);
+    Octree* CreateComponent<Octree> @ CreateOctree(CreateMode mode, unsigned id);
+    OffMeshConnection* CreateComponent<OffMeshConnection> @ CreateOffMeshConnection(CreateMode mode, unsigned id);
+    PhysicsWorld* CreateComponent<PhysicsWorld> @ CreatePhysicsWorld(CreateMode mode, unsigned id);
+    RigidBody* CreateComponent<RigidBody> @ CreateRigidBody(CreateMode mode, unsigned id);
+    SmoothedTransform* CreateComponent<SmoothedTransform> @ CreateSmoothedTransform(CreateMode mode, unsigned id);
+    SoundListener* CreateComponent<SoundListener> @ CreateSoundListener(CreateMode mode, unsigned id);
+    SoundSource* CreateComponent<SoundSource> @ CreateSoundSource(CreateMode mode, unsigned id);
+    StaticModel* CreateComponent<StaticModel> @ CreateStaticModel(CreateMode mode, unsigned id);
+    Terrain* CreateComponent<Terrain> @ CreateTerrain(CreateMode mode, unsigned id);
+    Zone* CreateComponent<Zone> @ CreateZone(CreateMode mode, unsigned id);
 
     /// Template version of getting or creating a component.
     // template <class T> T* GetOrCreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
-    AnimationController* GetOrCreateComponent<AnimationController> @ GetOrCreateAnimationController();
-    AnimatedModel* GetOrCreateComponent<AnimatedModel> @ GetOrCreateAnimatedModel();
-    Camera* GetOrCreateComponent<Camera> @ GetOrCreateCamera();
-    CollisionShape* GetOrCreateComponent<CollisionShape> @ GetOrCreateCollisionShape();
-    Constraint* GetOrCreateComponent<Constraint> @ GetOrCreateConstraint();
-    DebugRenderer* GetOrCreateComponent<DebugRenderer> @ GetOrCreateDebugRenderer();
-    DecalSet* GetOrCreateComponent<DecalSet> @ GetOrCreateDecalSet();
-    Drawable* GetOrCreateComponent<Drawable> @ GetOrCreateDrawable();
-    Light* GetOrCreateComponent<Light> @ GetOrCreateLight();
-    Navigable* GetOrCreateComponent<Navigable> @ GetOrCreateNavigable();
-    NavigationMesh* GetOrCreateComponent<NavigationMesh> @ GetOrCreateNavigationMesh();
-    NetworkPriority* GetOrCreateComponent<NetworkPriority> @ GetOrCreateNetworkPriority();
-    Octree* GetOrCreateComponent<Octree> @ GetOrCreateOctree();
-    OffMeshConnection* GetOrCreateComponent<OffMeshConnection> @ GetOrCreateOffMeshConnection();
-    PhysicsWorld* GetOrCreateComponent<PhysicsWorld> @ GetOrCreatePhysicsWorld();
-    RigidBody* GetOrCreateComponent<RigidBody> @ GetOrCreateRigidBody();
-    SmoothedTransform* GetOrCreateComponent<SmoothedTransform> @ GetOrCreateSmoothedTransform();
-    SoundListener* GetOrCreateComponent<SoundListener> @ GetOrCreateSoundListener();
-    SoundSource* GetOrCreateComponent<SoundSource> @ GetOrCreateSoundSource();
-    StaticModel* GetOrCreateComponent<StaticModel> @ GetOrCreateStaticModel();
-    Terrain* GetOrCreateComponent<Terrain> @ GetOrCreateTerrain();
-    Zone* GetOrCreateComponent<Zone> @ GetOrCreateZone();
-
+    
     /// Return ID.
     unsigned GetID() const { return id_; }
     /// Return name.

+ 12 - 13
Extras/LuaScript/pkgs/basic.lua

@@ -21,19 +21,6 @@ function write(a)
     end
 end
 
-
-function get_property_methods_hook(ptype, name)
-    if ptype == "get_set" then
-        local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
-        return "Get"..Name, "Set"..Name
-    end
-    
-    if ptype == "is_set" then
-        local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
-        return "Is"..Name, "Set"..Name
-    end
-end
-
 function post_output_hook(package)
     local result = table.concat(toWrite)
     local function replace(pattern, replacement)
@@ -83,3 +70,15 @@ function post_output_hook(package)
 
     WRITE(result)
 end
+
+function get_property_methods_hook(ptype, name)
+    if ptype == "get_set" then
+        local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
+        return "Get"..Name, "Set"..Name
+    end
+    
+    if ptype == "is_set" then
+        local Name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
+        return "Is"..Name, "Set"..Name
+    end
+end