LuaScript.h 4.1 KB

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