Core.cpp 7.4 KB

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