Core.cpp 8.4 KB

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