LuaScript.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "Context.h"
  24. #include "Object.h"
  25. struct lua_State;
  26. namespace Urho3D
  27. {
  28. class Scene;
  29. /// Lua script subsystem.
  30. class URHO3D_API LuaScript : public Object
  31. {
  32. OBJECT(LuaScript);
  33. public:
  34. /// Construct.
  35. LuaScript(Context* context);
  36. /// Destruct.
  37. ~LuaScript();
  38. /// Execute script file.
  39. bool ExecuteFile(const String& fileName);
  40. /// Execute script string.
  41. bool ExecuteString(const String& string);
  42. /// Execute script function.
  43. bool ExecuteFunction(const String& functionName);
  44. /// Script send event.
  45. void ScriptSendEvent(const String& eventName, VariantMap& eventData);
  46. /// Script subscribe to an event that can by send by any sender.
  47. void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
  48. /// Script unsubscribe from an event.
  49. void ScriptUnsubscribeFromEvent(const String& eventName);
  50. /// Script unsubscribe from all events.
  51. void ScriptUnsubscribeFromAllEvents();
  52. /// Script subscribe to a specific sender's event.
  53. void ScriptSubscribeToEvent(void* object, const String& eventName, const String& functionName);
  54. /// Script unsubscribe from a specific sender's event.
  55. void ScriptUnsubscribeFromEvent(void* object, const String& eventName);
  56. /// Script unsubscribe from a specific sender's all events.
  57. void ScriptUnsubscribeFromEvents(void* object);
  58. /// Return Lua state.
  59. lua_State* GetLuaState() const { return luaState_; }
  60. /// Find Lua function.
  61. bool FindFunction(const String& functionName);
  62. private:
  63. /// Register loader.
  64. void RegisterLoader();
  65. /// Loader.
  66. static int Loader(lua_State* L);
  67. /// Replace print.
  68. void ReplacePrint();
  69. /// Print function.
  70. static int Print(lua_State* L);
  71. /// Handle event.
  72. void HandleEvent(StringHash eventType, VariantMap& eventData);
  73. /// Handle object event.
  74. void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
  75. /// Handle a console command event.
  76. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  77. private:
  78. /// Call Lua event handler.
  79. void CallEventHandler(int functionRef, StringHash eventType, VariantMap& eventData);
  80. /// Lua state.
  81. lua_State* luaState_;
  82. /// Event type to function ref map.
  83. HashMap<StringHash, int> eventTypeToFunctionRefMap_;
  84. /// Object to event type to function ref map.
  85. HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
  86. };
  87. /// Register Lua script library objects.
  88. void RegisterLuaScriptLibrary(Context* context);
  89. /// Return context.
  90. Context* GetContext();
  91. }