LuaPlugin.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LuaPlugin.h"
  29. #include "LuaDocumentElementInstancer.h"
  30. #include "LuaEventListenerInstancer.h"
  31. #include "RmlUi.h"
  32. #include <RmlUi/Core/Factory.h>
  33. #include <RmlUi/Core/FileInterface.h>
  34. #include <RmlUi/Core/Log.h>
  35. #include <RmlUi/Lua/Lua.h>
  36. #include <RmlUi/Lua/LuaType.h>
  37. #include <RmlUi/Lua/Utilities.h>
  38. // the types I made
  39. #include "Colourb.h"
  40. #include "Colourf.h"
  41. #include "Context.h"
  42. #include "ContextDocumentsProxy.h"
  43. #include "Document.h"
  44. #include "Element.h"
  45. #include "ElementAttributesProxy.h"
  46. #include "ElementChildNodesProxy.h"
  47. #include "ElementInstancer.h"
  48. #include "ElementStyleProxy.h"
  49. #include "ElementText.h"
  50. #include "Event.h"
  51. #include "EventParametersProxy.h"
  52. #include "GlobalLuaFunctions.h"
  53. #include "Log.h"
  54. #include "RmlUiContextsProxy.h"
  55. #include "Vector2f.h"
  56. #include "Vector2i.h"
  57. // Control types
  58. #include "Elements/ElementForm.h"
  59. #include "Elements/ElementFormControl.h"
  60. #include "Elements/ElementFormControlInput.h"
  61. #include "Elements/ElementFormControlSelect.h"
  62. #include "Elements/ElementFormControlTextArea.h"
  63. #include "Elements/ElementTabSet.h"
  64. #include "Elements/SelectOptionsProxy.h"
  65. namespace Rml {
  66. namespace Lua {
  67. static lua_State* g_L = nullptr;
  68. /** This will populate the global Lua table with all of the Lua core types by calling LuaType<T>::Register
  69. @remark This is called automatically by LuaPlugin::OnInitialise(). */
  70. static void RegisterTypes();
  71. LuaPlugin::LuaPlugin(lua_State* lua_state)
  72. {
  73. RMLUI_ASSERT(g_L == nullptr);
  74. g_L = lua_state;
  75. }
  76. int LuaPlugin::GetEventClasses()
  77. {
  78. return EVT_BASIC;
  79. }
  80. void LuaPlugin::OnInitialise()
  81. {
  82. if (g_L == nullptr)
  83. {
  84. Log::Message(Log::LT_INFO, "Loading Lua plugin using a new Lua state.");
  85. g_L = luaL_newstate();
  86. luaL_openlibs(g_L);
  87. owns_lua_state = true;
  88. }
  89. else
  90. {
  91. Log::Message(Log::LT_INFO, "Loading Lua plugin using the provided Lua state.");
  92. owns_lua_state = false;
  93. }
  94. RegisterTypes();
  95. lua_document_element_instancer = new LuaDocumentElementInstancer();
  96. lua_event_listener_instancer = new LuaEventListenerInstancer();
  97. Factory::RegisterElementInstancer("body", lua_document_element_instancer);
  98. Factory::RegisterEventListenerInstancer(lua_event_listener_instancer);
  99. }
  100. void LuaPlugin::OnShutdown()
  101. {
  102. delete lua_document_element_instancer;
  103. delete lua_event_listener_instancer;
  104. lua_document_element_instancer = nullptr;
  105. lua_event_listener_instancer = nullptr;
  106. if (owns_lua_state)
  107. lua_close(g_L);
  108. g_L = nullptr;
  109. delete this;
  110. }
  111. static void RegisterTypes()
  112. {
  113. RMLUI_ASSERT(g_L);
  114. lua_State* L = g_L;
  115. LuaType<Vector2i>::Register(L);
  116. LuaType<Vector2f>::Register(L);
  117. LuaType<Colourf>::Register(L);
  118. LuaType<Colourb>::Register(L);
  119. LuaType<Log>::Register(L);
  120. LuaType<ElementStyleProxy>::Register(L);
  121. LuaType<Element>::Register(L);
  122. // things that inherit from Element
  123. LuaType<Document>::Register(L);
  124. LuaType<ElementText>::Register(L);
  125. LuaType<ElementPtr>::Register(L);
  126. LuaType<Event>::Register(L);
  127. LuaType<Context>::Register(L);
  128. LuaType<LuaRmlUi>::Register(L);
  129. LuaType<ElementInstancer>::Register(L);
  130. // Proxy tables
  131. LuaType<ContextDocumentsProxy>::Register(L);
  132. LuaType<EventParametersProxy>::Register(L);
  133. LuaType<ElementAttributesProxy>::Register(L);
  134. LuaType<ElementChildNodesProxy>::Register(L);
  135. LuaType<RmlUiContextsProxy>::Register(L);
  136. OverrideLuaGlobalFunctions(L);
  137. // push the global variable "rmlui" to use the "RmlUi" methods
  138. LuaRmlUiPushrmluiGlobal(L);
  139. // Control types
  140. LuaType<ElementForm>::Register(L);
  141. LuaType<ElementFormControl>::Register(L);
  142. // Inherits from ElementFormControl
  143. LuaType<ElementFormControlSelect>::Register(L);
  144. LuaType<ElementFormControlInput>::Register(L);
  145. LuaType<ElementFormControlTextArea>::Register(L);
  146. LuaType<ElementTabSet>::Register(L);
  147. // proxy tables
  148. LuaType<SelectOptionsProxy>::Register(L);
  149. }
  150. lua_State* LuaPlugin::GetLuaState()
  151. {
  152. return g_L;
  153. }
  154. } // namespace Lua
  155. } // namespace Rml