Core.cpp 8.5 KB

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