1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "../LuaScript/LuaScriptEventListener.h"
26 #include "../Scene/Component.h"
27 
28 struct lua_State;
29 
30 namespace Urho3D
31 {
32 
33 class LuaFile;
34 class LuaFunction;
35 class LuaScript;
36 class LuaScriptEventInvoker;
37 
38 /// Lua Script object methods.
39 enum LuaScriptObjectMethod
40 {
41     LSOM_START = 0,
42     LSOM_STOP,
43     LSOM_DELAYEDSTART,
44     LSOM_UPDATE,
45     LSOM_POSTUPDATE,
46     LSOM_FIXEDUPDATE,
47     LSOM_FIXEDPOSTUPDATE,
48     LSOM_LOAD,
49     LSOM_SAVE,
50     LSOM_READNETWORKUPDATE,
51     LSOM_WRITENETWORKUPDATE,
52     LSOM_APPLYATTRIBUTES,
53     LSOM_TRANSFORMCHANGED,
54     MAX_LUA_SCRIPT_OBJECT_METHODS
55 };
56 
57 /// Lua script object component.
58 class URHO3D_API LuaScriptInstance : public Component, public LuaScriptEventListener
59 {
60     URHO3D_OBJECT(LuaScriptInstance, Component);
61 
62 public:
63     /// Construct.
64     LuaScriptInstance(Context* context);
65     /// Destruct.
66     ~LuaScriptInstance();
67     /// Register object factory.
68     static void RegisterObject(Context* context);
69 
70     /// Handle attribute write access.
71     virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
72     /// Handle attribute read access.
73     virtual void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const;
74 
75     /// Return attribute descriptions, or null if none defined.
GetAttributes()76     virtual const Vector<AttributeInfo>* GetAttributes() const { return &attributeInfos_; }
77 
78     /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
79     virtual void ApplyAttributes();
80     /// Handle enabled/disabled state change.
81     virtual void OnSetEnabled();
82 
83     /// Add a scripted event handler by function.
84     virtual void AddEventHandler(const String& eventName, int functionIndex);
85     /// Add a scripted event handler by function name.
86     virtual void AddEventHandler(const String& eventName, const String& functionName);
87     /// Add a scripted event handler by function for a specific sender.
88     virtual void AddEventHandler(Object* sender, const String& eventName, int functionIndex);
89     /// Add a scripted event handler by function name for a specific sender.
90     virtual void AddEventHandler(Object* sender, const String& eventName, const String& functionName);
91     /// Remove a scripted event handler.
92     virtual void RemoveEventHandler(const String& eventName);
93     /// Remove a scripted event handler for a specific sender.
94     virtual void RemoveEventHandler(Object* sender, const String& eventName);
95     /// Remove all scripted event handlers for a specific sender.
96     virtual void RemoveEventHandlers(Object* sender);
97     /// Remove all scripted event handlers.
98     virtual void RemoveAllEventHandlers();
99     /// Remove all scripted event handlers, except those listed.
100     virtual void RemoveEventHandlersExcept(const Vector<String>& exceptionNames);
101     /// Return whether has subscribed to an event.
102     virtual bool HasEventHandler(const String& eventName) const;
103     /// Return whether has subscribed to a specific sender's event.
104     virtual bool HasEventHandler(Object* sender, const String& eventName) const;
105 
106     /// Create script object. Return true if successful.
107     bool CreateObject(const String& scriptObjectType);
108     /// Create script object. Return true if successful.
109     bool CreateObject(LuaFile* scriptFile, const String& scriptObjectType);
110     /// Set script file.
111     void SetScriptFile(LuaFile* scriptFile);
112     /// Set script object type.
113     void SetScriptObjectType(const String& scriptObjectType);
114     /// Set script file serialization attribute by calling a script function.
115     void SetScriptDataAttr(const PODVector<unsigned char>& data);
116     /// Set script network serialization attribute by calling a script function.
117     void SetScriptNetworkDataAttr(const PODVector<unsigned char>& data);
118 
119     /// Return script file.
120     LuaFile* GetScriptFile() const;
121 
122     /// Return script object type.
GetScriptObjectType()123     const String& GetScriptObjectType() const { return scriptObjectType_; }
124 
125     /// Return Lua reference to script object.
GetScriptObjectRef()126     int GetScriptObjectRef() const { return scriptObjectRef_; }
127 
128     /// Get script file serialization attribute by calling a script function.
129     PODVector<unsigned char> GetScriptDataAttr() const;
130     /// Get script network serialization attribute by calling a script function.
131     PODVector<unsigned char> GetScriptNetworkDataAttr() const;
132     /// Return script object's funcition.
133     LuaFunction* GetScriptObjectFunction(const String& functionName) const;
134 
135     /// Set script file attribute.
136     void SetScriptFileAttr(const ResourceRef& value);
137     /// Return script file attribute.
138     ResourceRef GetScriptFileAttr() const;
139 
140 protected:
141     /// Handle scene being assigned.
142     virtual void OnSceneSet(Scene* scene);
143     /// Handle node transform being dirtied.
144     virtual void OnMarkedDirty(Node* node);
145 
146 private:
147     /// Find script object attributes.
148     void GetScriptAttributes();
149     /// Find script object method refs.
150     void FindScriptObjectMethodRefs();
151     /// Subscribe to script method events.
152     void SubscribeToScriptMethodEvents();
153     /// Unsubscribe from script method events.
154     void UnsubscribeFromScriptMethodEvents();
155     /// Handle the logic update event.
156     void HandleUpdate(StringHash eventType, VariantMap& eventData);
157     /// Handle the logic post update event.
158     void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
159 #if defined(URHO3D_PHYSICS) || defined(URHO3D_URHO2D)
160     /// Handle the physics update event.
161     void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
162     /// Handle the physics post update event.
163     void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
164 #endif
165     /// Release the script object.
166     void ReleaseObject();
167 
168     /// Lua Script subsystem.
169     LuaScript* luaScript_;
170     /// Lua state.
171     lua_State* luaState_;
172     /// Event invoker.
173     SharedPtr<LuaScriptEventInvoker> eventInvoker_;
174     /// Script file.
175     SharedPtr<LuaFile> scriptFile_;
176     /// Script object type.
177     String scriptObjectType_;
178     /// Attributes, including script object variables.
179     Vector<AttributeInfo> attributeInfos_;
180     /// Lua reference to script object.
181     int scriptObjectRef_;
182     /// Script object method.
183     LuaFunction* scriptObjectMethods_[MAX_LUA_SCRIPT_OBJECT_METHODS];
184 };
185 
186 }
187