Core.cpp 8.9 KB

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