Core.cpp 8.1 KB

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