2
0

LuaPlugin.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "LuaPlugin.h"
  2. #include "LuaDocumentElementInstancer.h"
  3. #include "LuaEventListenerInstancer.h"
  4. #include "RmlUi.h"
  5. #include <RmlUi/Core/Factory.h>
  6. #include <RmlUi/Core/FileInterface.h>
  7. #include <RmlUi/Core/Log.h>
  8. #include <RmlUi/Lua/Lua.h>
  9. #include <RmlUi/Lua/LuaType.h>
  10. #include <RmlUi/Lua/Utilities.h>
  11. // the types I made
  12. #include "Colourb.h"
  13. #include "Colourf.h"
  14. #include "Context.h"
  15. #include "ContextDocumentsProxy.h"
  16. #include "Document.h"
  17. #include "Element.h"
  18. #include "ElementAttributesProxy.h"
  19. #include "ElementChildNodesProxy.h"
  20. #include "ElementInstancer.h"
  21. #include "ElementStyleProxy.h"
  22. #include "ElementText.h"
  23. #include "Event.h"
  24. #include "EventParametersProxy.h"
  25. #include "GlobalLuaFunctions.h"
  26. #include "Log.h"
  27. #include "RmlUiContextsProxy.h"
  28. #include "Vector2f.h"
  29. #include "Vector2i.h"
  30. // Control types
  31. #include "Elements/ElementForm.h"
  32. #include "Elements/ElementFormControl.h"
  33. #include "Elements/ElementFormControlInput.h"
  34. #include "Elements/ElementFormControlSelect.h"
  35. #include "Elements/ElementFormControlTextArea.h"
  36. #include "Elements/ElementTabSet.h"
  37. #include "Elements/SelectOptionsProxy.h"
  38. namespace Rml {
  39. namespace Lua {
  40. static lua_State* g_L = nullptr;
  41. /** This will populate the global Lua table with all of the Lua core types by calling LuaType<T>::Register
  42. @remark This is called automatically by LuaPlugin::OnInitialise(). */
  43. static void RegisterTypes();
  44. LuaPlugin::LuaPlugin(lua_State* lua_state)
  45. {
  46. RMLUI_ASSERT(g_L == nullptr);
  47. g_L = lua_state;
  48. }
  49. int LuaPlugin::GetEventClasses()
  50. {
  51. return EVT_BASIC;
  52. }
  53. void LuaPlugin::OnInitialise()
  54. {
  55. if (g_L == nullptr)
  56. {
  57. Log::Message(Log::LT_INFO, "Loading Lua plugin using a new Lua state.");
  58. g_L = luaL_newstate();
  59. luaL_openlibs(g_L);
  60. owns_lua_state = true;
  61. }
  62. else
  63. {
  64. Log::Message(Log::LT_INFO, "Loading Lua plugin using the provided Lua state.");
  65. owns_lua_state = false;
  66. }
  67. RegisterTypes();
  68. lua_document_element_instancer = new LuaDocumentElementInstancer();
  69. lua_event_listener_instancer = new LuaEventListenerInstancer();
  70. Factory::RegisterElementInstancer("body", lua_document_element_instancer);
  71. Factory::RegisterEventListenerInstancer(lua_event_listener_instancer);
  72. }
  73. void LuaPlugin::OnShutdown()
  74. {
  75. delete lua_document_element_instancer;
  76. delete lua_event_listener_instancer;
  77. lua_document_element_instancer = nullptr;
  78. lua_event_listener_instancer = nullptr;
  79. if (owns_lua_state)
  80. lua_close(g_L);
  81. g_L = nullptr;
  82. delete this;
  83. }
  84. static void RegisterTypes()
  85. {
  86. RMLUI_ASSERT(g_L);
  87. lua_State* L = g_L;
  88. LuaType<Vector2i>::Register(L);
  89. LuaType<Vector2f>::Register(L);
  90. LuaType<Colourf>::Register(L);
  91. LuaType<Colourb>::Register(L);
  92. LuaType<Log>::Register(L);
  93. LuaType<ElementStyleProxy>::Register(L);
  94. LuaType<Element>::Register(L);
  95. // things that inherit from Element
  96. LuaType<Document>::Register(L);
  97. LuaType<ElementText>::Register(L);
  98. LuaType<ElementPtr>::Register(L);
  99. LuaType<Event>::Register(L);
  100. LuaType<Context>::Register(L);
  101. LuaType<LuaRmlUi>::Register(L);
  102. LuaType<ElementInstancer>::Register(L);
  103. // Proxy tables
  104. LuaType<ContextDocumentsProxy>::Register(L);
  105. LuaType<EventParametersProxy>::Register(L);
  106. LuaType<ElementAttributesProxy>::Register(L);
  107. LuaType<ElementChildNodesProxy>::Register(L);
  108. LuaType<RmlUiContextsProxy>::Register(L);
  109. OverrideLuaGlobalFunctions(L);
  110. // push the global variable "rmlui" to use the "RmlUi" methods
  111. LuaRmlUiPushrmluiGlobal(L);
  112. // Control types
  113. LuaType<ElementForm>::Register(L);
  114. LuaType<ElementFormControl>::Register(L);
  115. // Inherits from ElementFormControl
  116. LuaType<ElementFormControlSelect>::Register(L);
  117. LuaType<ElementFormControlInput>::Register(L);
  118. LuaType<ElementFormControlTextArea>::Register(L);
  119. LuaType<ElementTabSet>::Register(L);
  120. // proxy tables
  121. LuaType<SelectOptionsProxy>::Register(L);
  122. }
  123. lua_State* LuaPlugin::GetLuaState()
  124. {
  125. return g_L;
  126. }
  127. } // namespace Lua
  128. } // namespace Rml