Core.cpp 8.1 KB

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