Core.cpp 8.8 KB

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