Core.cpp 7.3 KB

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