Core.cpp 8.6 KB

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