LuaScriptInstance.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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& objectType);
  41. /// Create script object.
  42. bool CreateObject(const String& fileName, const String& objectType);
  43. /// Script subscribe event.
  44. void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
  45. /// Script subscribe object's event.
  46. void ScriptSubscribeToEvent(void* object, const String& eventName, const String& functionName);
  47. private:
  48. /// Handle event.
  49. void HandleEvent(StringHash eventType, VariantMap& eventData);
  50. /// Handle object event.
  51. void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
  52. /// Call event handler.
  53. void CallEventHandler(int functionRef, StringHash eventType, VariantMap& eventData);
  54. /// Release the script object.
  55. void ReleaseObject();
  56. // Lua Script.
  57. LuaScript* luaScript_;
  58. /// Lua state.
  59. lua_State* luaState_;
  60. /// Object type.
  61. String objectType_;
  62. /// Script object ref.
  63. int scriptObjectRef_;
  64. /// Event type to function ref map.
  65. HashMap<StringHash, int> eventTypeToFunctionRefMap_;
  66. /// Object to event type to function ref map.
  67. HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
  68. };
  69. }