EventManager.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "EventManager.h"
  28. #include <Rocket/Core/Context.h>
  29. #include <Rocket/Core/ElementDocument.h>
  30. #include <Rocket/Core/ElementUtilities.h>
  31. #include <Shell.h>
  32. #include "EventHandler.h"
  33. #include "GameDetails.h"
  34. #include <map>
  35. // The game's element context (declared in main.cpp).
  36. extern Rocket::Core::Context* context;
  37. // The event handler for the current screen. This may be NULL if the current screen has no specific functionality.
  38. static EventHandler* event_handler = NULL;
  39. // The event handlers registered with the manager.
  40. typedef std::map< Rocket::Core::String, EventHandler* > EventHandlerMap;
  41. EventHandlerMap event_handlers;
  42. EventManager::EventManager()
  43. {
  44. }
  45. EventManager::~EventManager()
  46. {
  47. }
  48. // Releases all event handlers registered with the manager.
  49. void EventManager::Shutdown()
  50. {
  51. for (EventHandlerMap::iterator i = event_handlers.begin(); i != event_handlers.end(); ++i)
  52. delete (*i).second;
  53. event_handlers.clear();
  54. event_handler = NULL;
  55. }
  56. // Registers a new event handler with the manager.
  57. void EventManager::RegisterEventHandler(const Rocket::Core::String& handler_name, EventHandler* handler)
  58. {
  59. // Release any handler bound under the same name.
  60. EventHandlerMap::iterator iterator = event_handlers.find(handler_name);
  61. if (iterator != event_handlers.end())
  62. delete (*iterator).second;
  63. event_handlers[handler_name] = handler;
  64. }
  65. // Processes an event coming through from Rocket.
  66. void EventManager::ProcessEvent(Rocket::Core::Event& event, const Rocket::Core::String& value)
  67. {
  68. Rocket::Core::StringList commands;
  69. Rocket::Core::StringUtilities::ExpandString(commands, value, ';');
  70. for (size_t i = 0; i < commands.size(); ++i)
  71. {
  72. // Check for a generic 'load' or 'exit' command.
  73. Rocket::Core::StringList values;
  74. Rocket::Core::StringUtilities::ExpandString(values, commands[i], ' ');
  75. if (values.empty())
  76. return;
  77. if (values[0] == "goto" &&
  78. values.size() > 1)
  79. {
  80. // Load the window, and if successful close the old window.
  81. if (LoadWindow(values[1]))
  82. event.GetTargetElement()->GetOwnerDocument()->Close();
  83. }
  84. else if (values[0] == "load" &&
  85. values.size() > 1)
  86. {
  87. // Load the window.
  88. LoadWindow(values[1]);
  89. }
  90. else if (values[0] == "close")
  91. {
  92. Rocket::Core::ElementDocument* target_document = NULL;
  93. if (values.size() > 1)
  94. target_document = context->GetDocument(values[1].CString());
  95. else
  96. target_document = event.GetTargetElement()->GetOwnerDocument();
  97. if (target_document != NULL)
  98. target_document->Close();
  99. }
  100. else if (values[0] == "exit")
  101. {
  102. Shell::RequestExit();
  103. }
  104. else if (values[0] == "pause")
  105. {
  106. GameDetails::SetPaused(true);
  107. }
  108. else if (values[0] == "unpause")
  109. {
  110. GameDetails::SetPaused(false);
  111. }
  112. else
  113. {
  114. if (event_handler != NULL)
  115. event_handler->ProcessEvent(event, commands[i]);
  116. }
  117. }
  118. }
  119. // Loads a window and binds the event handler for it.
  120. bool EventManager::LoadWindow(const Rocket::Core::String& window_name)
  121. {
  122. // Set the event handler for the new screen, if one has been registered.
  123. EventHandler* old_event_handler = event_handler;
  124. EventHandlerMap::iterator iterator = event_handlers.find(window_name);
  125. if (iterator != event_handlers.end())
  126. event_handler = (*iterator).second;
  127. else
  128. event_handler = NULL;
  129. // Attempt to load the referenced RML document.
  130. Rocket::Core::String document_path = Rocket::Core::String("data/") + window_name + Rocket::Core::String(".rml");
  131. Rocket::Core::ElementDocument* document = context->LoadDocument(document_path.CString());
  132. if (document == NULL)
  133. {
  134. event_handler = old_event_handler;
  135. return false;
  136. }
  137. // Set the element's title on the title; IDd 'title' in the RML.
  138. Rocket::Core::Element* title = document->GetElementById("title");
  139. if (title != NULL)
  140. title->SetInnerRML(document->GetTitle());
  141. document->Focus();
  142. document->Show();
  143. // Remove the caller's reference.
  144. document->RemoveReference();
  145. return true;
  146. }