Core.cpp 8.1 KB

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