LuaScript.h 4.0 KB

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