LuaScriptInstance.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Copyright (c) 2008-2013 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. #pragma once
  23. #include "Component.h"
  24. struct lua_State;
  25. namespace Urho3D
  26. {
  27. class LuaScript;
  28. /// Lua instance.
  29. class URHO3D_API LuaScriptInstance : public Component
  30. {
  31. OBJECT(LuaScriptInstance);
  32. public:
  33. /// Construct.
  34. LuaScriptInstance(Context* context);
  35. /// Destruct.
  36. ~LuaScriptInstance();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Create script object.
  40. bool CreateObject(const String& scriptObjectType);
  41. /// Create script object.
  42. bool CreateObject(const String& fileName, const String& scriptObjectType);
  43. /// Script subscribe to an event that can by send by any sender.
  44. void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
  45. /// Script unsubscribe from an event.
  46. void ScriptUnsubscribeFromEvent(const String& eventName);
  47. /// Script unsubscribe from all events.
  48. void ScriptUnsubscribeFromAllEvents();
  49. /// Script subscribe to a specific sender's event.
  50. void ScriptSubscribeToEvent(void* object, const String& eventName, const String& functionName);
  51. /// Script unsubscribe from a specific sender's event.
  52. void ScriptUnsubscribeFromEvent(void* object, const String& eventName);
  53. /// Script unsubscribe from a specific sender's all events.
  54. void ScriptUnsubscribeFromEvents(void* object);
  55. /// Return script object type.
  56. const String& GetScriptObjectType() const { return scriptObjectType_; }
  57. /// Return script object ref.
  58. int GetScriptObjectRef() const { return scriptObjectRef_; }
  59. private:
  60. /// Handle event.
  61. void HandleEvent(StringHash eventType, VariantMap& eventData);
  62. /// Handle object event.
  63. void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
  64. /// Call event handler.
  65. void CallEventHandler(int functionRef, StringHash eventType, VariantMap& eventData);
  66. /// Release the script object.
  67. void ReleaseObject();
  68. // Lua Script.
  69. LuaScript* luaScript_;
  70. /// Lua state.
  71. lua_State* luaState_;
  72. /// Script object type.
  73. String scriptObjectType_;
  74. /// Script object ref.
  75. int scriptObjectRef_;
  76. /// Event type to function ref map.
  77. HashMap<StringHash, int> eventTypeToFunctionRefMap_;
  78. /// Object to event type to function ref map.
  79. HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
  80. };
  81. }