Core.cpp 9.1 KB

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