Core.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // Notify all plugins we're being shutdown.
  89. PluginRegistry::NotifyShutdown();
  90. contexts.clear();
  91. TemplateCache::Shutdown();
  92. StyleSheetFactory::Shutdown();
  93. StyleSheetSpecification::Shutdown();
  94. FontDatabase::Shutdown();
  95. TextureDatabase::Shutdown();
  96. Factory::Shutdown();
  97. Log::Shutdown();
  98. initialised = false;
  99. if (render_interface != NULL)
  100. render_interface->RemoveReference();
  101. if (file_interface != NULL)
  102. file_interface->RemoveReference();
  103. if (system_interface != NULL)
  104. system_interface->RemoveReference();
  105. render_interface = NULL;
  106. file_interface = NULL;
  107. system_interface = NULL;
  108. }
  109. // Returns the version of this RmlUi library.
  110. String GetVersion()
  111. {
  112. return RMLUI_VERSION;
  113. }
  114. // Sets the interface through which all RmlUi messages will be routed.
  115. void SetSystemInterface(SystemInterface* _system_interface)
  116. {
  117. if (system_interface == _system_interface)
  118. return;
  119. if (system_interface != NULL)
  120. system_interface->RemoveReference();
  121. system_interface = _system_interface;
  122. if (system_interface != NULL)
  123. system_interface->AddReference();
  124. }
  125. // Returns RmlUi's system interface.
  126. SystemInterface* GetSystemInterface()
  127. {
  128. return system_interface;
  129. }
  130. // Sets the interface through which all rendering requests are made.
  131. void SetRenderInterface(RenderInterface* _render_interface)
  132. {
  133. if (render_interface == _render_interface)
  134. return;
  135. if (render_interface != NULL)
  136. render_interface->RemoveReference();
  137. render_interface = _render_interface;
  138. if (render_interface != NULL)
  139. render_interface->AddReference();
  140. }
  141. // Returns RmlUi's render interface.
  142. RenderInterface* GetRenderInterface()
  143. {
  144. return render_interface;
  145. }
  146. // Sets the interface through which all file I/O requests are made.
  147. void SetFileInterface(FileInterface* _file_interface)
  148. {
  149. if (file_interface == _file_interface)
  150. return;
  151. if (file_interface != NULL)
  152. file_interface->RemoveReference();
  153. file_interface = _file_interface;
  154. if (file_interface != NULL)
  155. file_interface->AddReference();
  156. }
  157. // Returns RmlUi's file interface.
  158. FileInterface* GetFileInterface()
  159. {
  160. return file_interface;
  161. }
  162. // Creates a new element context.
  163. Context* CreateContext(const String& name, const Vector2i& dimensions, RenderInterface* custom_render_interface)
  164. {
  165. if (!initialised)
  166. return nullptr;
  167. if (!custom_render_interface && !render_interface)
  168. {
  169. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.c_str());
  170. return nullptr;
  171. }
  172. if (GetContext(name))
  173. {
  174. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  175. return nullptr;
  176. }
  177. UniquePtr<Context> new_context = Factory::InstanceContext(name);
  178. if (!new_context)
  179. {
  180. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned NULL.", name.c_str());
  181. return nullptr;
  182. }
  183. // Set the render interface on the context, and add a reference onto it.
  184. if (custom_render_interface)
  185. new_context->render_interface = custom_render_interface;
  186. else
  187. new_context->render_interface = render_interface;
  188. new_context->render_interface->AddReference();
  189. new_context->SetDimensions(dimensions);
  190. if (dimensions.x > 0 && dimensions.y > 0)
  191. {
  192. // install an orthographic projection, by default
  193. Matrix4f P = Matrix4f::ProjectOrtho(0, (float)dimensions.x, (float)dimensions.y, 0, -1, 1);
  194. new_context->ProcessProjectionChange(P);
  195. // install an identity view, by default
  196. new_context->ProcessViewChange(Matrix4f::Identity());
  197. }
  198. Context* new_context_raw = new_context.get();
  199. contexts[name] = std::move(new_context);
  200. PluginRegistry::NotifyContextCreate(new_context_raw);
  201. return new_context_raw;
  202. }
  203. bool RemoveContext(const String& name)
  204. {
  205. auto it = contexts.find(name);
  206. if (it != contexts.end())
  207. {
  208. contexts.erase(it);
  209. return true;
  210. }
  211. return false;
  212. }
  213. // Fetches a previously constructed context by name.
  214. Context* GetContext(const String& name)
  215. {
  216. ContextMap::iterator i = contexts.find(name);
  217. if (i == contexts.end())
  218. return nullptr;
  219. return i->second.get();
  220. }
  221. // Fetches a context by index.
  222. Context* GetContext(int index)
  223. {
  224. ContextMap::iterator i = contexts.begin();
  225. int count = 0;
  226. if (index >= GetNumContexts())
  227. index = GetNumContexts() - 1;
  228. while (count < index)
  229. {
  230. ++i;
  231. ++count;
  232. }
  233. if (i == contexts.end())
  234. return nullptr;
  235. return i->second.get();
  236. }
  237. // Returns the number of active contexts.
  238. int GetNumContexts()
  239. {
  240. return (int) contexts.size();
  241. }
  242. // Registers a generic rmlui plugin
  243. void RegisterPlugin(Plugin* plugin)
  244. {
  245. if (initialised)
  246. plugin->OnInitialise();
  247. PluginRegistry::RegisterPlugin(plugin);
  248. }
  249. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  250. {
  251. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  252. }
  253. // Forces all compiled geometry handles generated by RmlUi to be released.
  254. void ReleaseCompiledGeometries()
  255. {
  256. GeometryDatabase::ReleaseGeometries();
  257. }
  258. // Forces all texture handles loaded and generated by RmlUi to be released.
  259. void ReleaseTextures()
  260. {
  261. TextureDatabase::ReleaseTextures();
  262. }
  263. }
  264. }