Core.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core.h"
  29. #include "EventSpecification.h"
  30. #include "FileInterfaceDefault.h"
  31. #include "GeometryDatabase.h"
  32. #include "PluginRegistry.h"
  33. #include "StyleSheetFactory.h"
  34. #include "TemplateCache.h"
  35. #include "TextureDatabase.h"
  36. #include "EventSpecification.h"
  37. namespace Rocket {
  38. namespace Core {
  39. // Rocket's renderer interface.
  40. static RenderInterface* render_interface = NULL;
  41. /// Rocket's system interface.
  42. static SystemInterface* system_interface = NULL;
  43. // Rocket's file I/O interface.
  44. FileInterface* file_interface = NULL;
  45. #ifndef ROCKET_NO_FILE_INTERFACE_DEFAULT
  46. static FileInterfaceDefault file_interface_default;
  47. #endif
  48. static bool initialised = false;
  49. typedef UnorderedMap< String, Context* > ContextMap;
  50. static ContextMap contexts;
  51. #ifndef ROCKET_VERSION
  52. #define ROCKET_VERSION "custom"
  53. #endif
  54. /**
  55. A 'plugin' for unobtrusively intercepting the destruction of contexts.
  56. */
  57. class PluginContextRelease : public Plugin
  58. {
  59. public:
  60. virtual void OnShutdown()
  61. {
  62. delete this;
  63. }
  64. virtual void OnContextDestroy(Context* context)
  65. {
  66. contexts.erase(context->GetName());
  67. }
  68. };
  69. bool Initialise()
  70. {
  71. // Check for valid interfaces, or install default interfaces as appropriate.
  72. if (system_interface == NULL)
  73. {
  74. Log::Message(Log::LT_ERROR, "No system interface set!");
  75. return false;
  76. }
  77. if (file_interface == NULL)
  78. {
  79. #ifndef ROCKET_NO_FILE_INTERFACE_DEFAULT
  80. file_interface = &file_interface_default;
  81. file_interface->AddReference();
  82. #else
  83. Log::Message(Log::LT_ERROR, "No file interface set!");
  84. return false;
  85. #endif
  86. }
  87. Log::Initialise();
  88. EventSpecificationInterface::Initialize();
  89. TextureDatabase::Initialise();
  90. FontDatabase::Initialise();
  91. StyleSheetSpecification::Initialise();
  92. StyleSheetFactory::Initialise();
  93. TemplateCache::Initialise();
  94. Factory::Initialise();
  95. // Notify all plugins we're starting up.
  96. PluginRegistry::RegisterPlugin(new PluginContextRelease());
  97. PluginRegistry::NotifyInitialise();
  98. initialised = true;
  99. return true;
  100. }
  101. void Shutdown()
  102. {
  103. // Notify all plugins we're being shutdown.
  104. PluginRegistry::NotifyShutdown();
  105. // Release all remaining contexts.
  106. for (ContextMap::iterator itr = contexts.begin(); itr != contexts.end(); ++itr)
  107. Core::Log::Message(Log::LT_WARNING, "Context '%s' still active on shutdown.", (*itr).first.c_str());
  108. contexts.clear();
  109. TemplateCache::Shutdown();
  110. StyleSheetFactory::Shutdown();
  111. StyleSheetSpecification::Shutdown();
  112. FontDatabase::Shutdown();
  113. TextureDatabase::Shutdown();
  114. Factory::Shutdown();
  115. Log::Shutdown();
  116. initialised = false;
  117. if (render_interface != NULL)
  118. render_interface->RemoveReference();
  119. if (file_interface != NULL)
  120. file_interface->RemoveReference();
  121. if (system_interface != NULL)
  122. system_interface->RemoveReference();
  123. render_interface = NULL;
  124. file_interface = NULL;
  125. system_interface = NULL;
  126. }
  127. // Returns the version of this Rocket library.
  128. String GetVersion()
  129. {
  130. return ROCKET_VERSION;
  131. }
  132. // Sets the interface through which all Rocket messages will be routed.
  133. void SetSystemInterface(SystemInterface* _system_interface)
  134. {
  135. if (system_interface == _system_interface)
  136. return;
  137. if (system_interface != NULL)
  138. system_interface->RemoveReference();
  139. system_interface = _system_interface;
  140. if (system_interface != NULL)
  141. system_interface->AddReference();
  142. }
  143. // Returns Rocket's system interface.
  144. SystemInterface* GetSystemInterface()
  145. {
  146. return system_interface;
  147. }
  148. // Sets the interface through which all rendering requests are made.
  149. void SetRenderInterface(RenderInterface* _render_interface)
  150. {
  151. if (render_interface == _render_interface)
  152. return;
  153. if (render_interface != NULL)
  154. render_interface->RemoveReference();
  155. render_interface = _render_interface;
  156. if (render_interface != NULL)
  157. render_interface->AddReference();
  158. }
  159. // Returns Rocket's render interface.
  160. RenderInterface* GetRenderInterface()
  161. {
  162. return render_interface;
  163. }
  164. // Sets the interface through which all file I/O requests are made.
  165. void SetFileInterface(FileInterface* _file_interface)
  166. {
  167. if (file_interface == _file_interface)
  168. return;
  169. if (file_interface != NULL)
  170. file_interface->RemoveReference();
  171. file_interface = _file_interface;
  172. if (file_interface != NULL)
  173. file_interface->AddReference();
  174. }
  175. // Returns Rocket's file interface.
  176. FileInterface* GetFileInterface()
  177. {
  178. return file_interface;
  179. }
  180. // Creates a new element context.
  181. Context* CreateContext(const String& name, const Vector2i& dimensions, RenderInterface* custom_render_interface)
  182. {
  183. if (!initialised)
  184. return NULL;
  185. if (custom_render_interface == NULL &&
  186. render_interface == NULL)
  187. {
  188. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.c_str());
  189. return NULL;
  190. }
  191. if (GetContext(name) != NULL)
  192. {
  193. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  194. return NULL;
  195. }
  196. Context* new_context = Factory::InstanceContext(name);
  197. if (new_context == NULL)
  198. {
  199. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned NULL.", name.c_str());
  200. return NULL;
  201. }
  202. // Set the render interface on the context, and add a reference onto it.
  203. if (custom_render_interface)
  204. new_context->render_interface = custom_render_interface;
  205. else
  206. new_context->render_interface = render_interface;
  207. new_context->render_interface->AddReference();
  208. new_context->SetDimensions(dimensions);
  209. if (dimensions.x > 0 && dimensions.y > 0)
  210. {
  211. // install an orthographic projection, by default
  212. Matrix4f P = Matrix4f::ProjectOrtho(0, (float)dimensions.x, (float)dimensions.y, 0, -1, 1);
  213. new_context->ProcessProjectionChange(P);
  214. // install an identity view, by default
  215. new_context->ProcessViewChange(Matrix4f::Identity());
  216. }
  217. contexts[name] = new_context;
  218. PluginRegistry::NotifyContextCreate(new_context);
  219. return new_context;
  220. }
  221. // Fetches a previously constructed context by name.
  222. Context* GetContext(const String& name)
  223. {
  224. ContextMap::iterator i = contexts.find(name);
  225. if (i == contexts.end())
  226. return NULL;
  227. return (*i).second;
  228. }
  229. // Fetches a context by index.
  230. Context* GetContext(int index)
  231. {
  232. ContextMap::iterator i = contexts.begin();
  233. int count = 0;
  234. if (index >= GetNumContexts())
  235. index = GetNumContexts() - 1;
  236. while (count < index)
  237. {
  238. ++i;
  239. ++count;
  240. }
  241. if (i == contexts.end())
  242. return NULL;
  243. return (*i).second;
  244. }
  245. // Returns the number of active contexts.
  246. int GetNumContexts()
  247. {
  248. return (int) contexts.size();
  249. }
  250. // Registers a generic rocket plugin
  251. void RegisterPlugin(Plugin* plugin)
  252. {
  253. if (initialised)
  254. plugin->OnInitialise();
  255. PluginRegistry::RegisterPlugin(plugin);
  256. }
  257. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  258. {
  259. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  260. }
  261. // Forces all compiled geometry handles generated by libRocket to be released.
  262. void ReleaseCompiledGeometries()
  263. {
  264. GeometryDatabase::ReleaseGeometries();
  265. }
  266. // Forces all texture handles loaded and generated by libRocket to be released.
  267. void ReleaseTextures()
  268. {
  269. TextureDatabase::ReleaseTextures();
  270. }
  271. }
  272. }