Core.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core.h"
  30. #include "EventSpecification.h"
  31. #include "FileInterfaceDefault.h"
  32. #include "FontEngineInterfaceDefault.h"
  33. #include "PluginRegistry.h"
  34. #include "StyleSheetFactory.h"
  35. #include "TemplateCache.h"
  36. #include "TextureDatabase.h"
  37. #include "EventSpecification.h"
  38. namespace Rml {
  39. namespace Core {
  40. // RmlUi's renderer interface.
  41. static RenderInterface* render_interface = nullptr;
  42. /// RmlUi's system interface.
  43. static SystemInterface* system_interface = nullptr;
  44. // RmlUi's file I/O interface.
  45. static FileInterface* file_interface = nullptr;
  46. // RmlUi's font engine interface.
  47. static FontEngineInterface* font_interface = nullptr;
  48. // Default interfaces should be created and destroyed on Initialise and Shutdown, respectively.
  49. static UniquePtr<FileInterface> default_file_interface;
  50. static UniquePtr<FontEngineInterface> default_font_interface;
  51. static bool initialised = false;
  52. using ContextMap = UnorderedMap< String, ContextPtr >;
  53. static ContextMap contexts;
  54. #ifndef RMLUI_VERSION
  55. #define RMLUI_VERSION "custom"
  56. #endif
  57. bool Initialise()
  58. {
  59. // Check for valid interfaces, or install default interfaces as appropriate.
  60. if (!system_interface)
  61. {
  62. Log::Message(Log::LT_ERROR, "No system interface set!");
  63. return false;
  64. }
  65. if (!file_interface)
  66. {
  67. #ifndef RMLUI_NO_FILE_INTERFACE_DEFAULT
  68. default_file_interface = std::make_unique<FileInterfaceDefault>();
  69. file_interface = default_file_interface.get();
  70. #else
  71. Log::Message(Log::LT_ERROR, "No file interface set!");
  72. return false;
  73. #endif
  74. }
  75. Log::Initialise();
  76. EventSpecificationInterface::Initialize();
  77. TextureDatabase::Initialise();
  78. if (!font_interface)
  79. {
  80. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  81. default_font_interface = std::make_unique<FontEngineInterfaceDefault>();
  82. font_interface = default_font_interface.get();
  83. #else
  84. Log::Message(Log::LT_ERROR, "No font interface set!");
  85. return false;
  86. #endif
  87. }
  88. StyleSheetSpecification::Initialise();
  89. StyleSheetFactory::Initialise();
  90. TemplateCache::Initialise();
  91. Factory::Initialise();
  92. // Notify all plugins we're starting up.
  93. PluginRegistry::NotifyInitialise();
  94. initialised = true;
  95. return true;
  96. }
  97. void Shutdown()
  98. {
  99. // Clear out all contexts, which should also clean up all attached elements.
  100. contexts.clear();
  101. // Notify all plugins we're being shutdown.
  102. PluginRegistry::NotifyShutdown();
  103. TemplateCache::Shutdown();
  104. StyleSheetFactory::Shutdown();
  105. StyleSheetSpecification::Shutdown();
  106. TextureDatabase::Shutdown();
  107. Factory::Shutdown();
  108. Log::Shutdown();
  109. initialised = false;
  110. render_interface = nullptr;
  111. file_interface = nullptr;
  112. system_interface = nullptr;
  113. font_interface = nullptr;
  114. default_file_interface.reset();
  115. default_font_interface.reset();
  116. }
  117. // Returns the version of this RmlUi library.
  118. String GetVersion()
  119. {
  120. return RMLUI_VERSION;
  121. }
  122. // Sets the interface through which all RmlUi messages will be routed.
  123. void SetSystemInterface(SystemInterface* _system_interface)
  124. {
  125. system_interface = _system_interface;
  126. }
  127. // Returns RmlUi's system interface.
  128. SystemInterface* GetSystemInterface()
  129. {
  130. return system_interface;
  131. }
  132. // Sets the interface through which all rendering requests are made.
  133. void SetRenderInterface(RenderInterface* _render_interface)
  134. {
  135. render_interface = _render_interface;
  136. }
  137. // Returns RmlUi's render interface.
  138. RenderInterface* GetRenderInterface()
  139. {
  140. return render_interface;
  141. }
  142. // Sets the interface through which all file I/O requests are made.
  143. void SetFileInterface(FileInterface* _file_interface)
  144. {
  145. file_interface = _file_interface;
  146. }
  147. // Returns RmlUi's file interface.
  148. FileInterface* GetFileInterface()
  149. {
  150. return file_interface;
  151. }
  152. // Sets the interface through which all font requests are made.
  153. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  154. {
  155. font_interface = _font_interface;
  156. }
  157. // Returns RmlUi's file interface.
  158. FontEngineInterface* GetFontEngineInterface()
  159. {
  160. return font_interface;
  161. }
  162. // Creates a new element context.
  163. Context* CreateContext(const String& name, const Vector2i& dimensions, RenderInterface* custom_render_interface)
  164. {
  165. if (!initialised)
  166. return nullptr;
  167. if (!custom_render_interface && !render_interface)
  168. {
  169. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.c_str());
  170. return nullptr;
  171. }
  172. if (GetContext(name))
  173. {
  174. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  175. return nullptr;
  176. }
  177. ContextPtr new_context = Factory::InstanceContext(name);
  178. if (!new_context)
  179. {
  180. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  181. return nullptr;
  182. }
  183. // Set the render interface on the context, and add a reference onto it.
  184. if (custom_render_interface)
  185. new_context->render_interface = custom_render_interface;
  186. else
  187. new_context->render_interface = render_interface;
  188. new_context->SetDimensions(dimensions);
  189. Context* new_context_raw = new_context.get();
  190. contexts[name] = std::move(new_context);
  191. PluginRegistry::NotifyContextCreate(new_context_raw);
  192. return new_context_raw;
  193. }
  194. bool RemoveContext(const String& name)
  195. {
  196. auto it = contexts.find(name);
  197. if (it != contexts.end())
  198. {
  199. contexts.erase(it);
  200. return true;
  201. }
  202. return false;
  203. }
  204. // Fetches a previously constructed context by name.
  205. Context* GetContext(const String& name)
  206. {
  207. ContextMap::iterator i = contexts.find(name);
  208. if (i == contexts.end())
  209. return nullptr;
  210. return i->second.get();
  211. }
  212. // Fetches a context by index.
  213. Context* GetContext(int index)
  214. {
  215. ContextMap::iterator i = contexts.begin();
  216. int count = 0;
  217. if (index >= GetNumContexts())
  218. index = GetNumContexts() - 1;
  219. while (count < index)
  220. {
  221. ++i;
  222. ++count;
  223. }
  224. if (i == contexts.end())
  225. return nullptr;
  226. return i->second.get();
  227. }
  228. // Returns the number of active contexts.
  229. int GetNumContexts()
  230. {
  231. return (int) contexts.size();
  232. }
  233. // Registers a generic rmlui plugin
  234. void RegisterPlugin(Plugin* plugin)
  235. {
  236. if (initialised)
  237. plugin->OnInitialise();
  238. PluginRegistry::RegisterPlugin(plugin);
  239. }
  240. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  241. {
  242. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  243. }
  244. // Forces all texture handles loaded and generated by RmlUi to be released.
  245. void ReleaseTextures()
  246. {
  247. TextureDatabase::ReleaseTextures();
  248. }
  249. }
  250. }