FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
librocketmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // 3rd party library includes
23 #include <Rocket/Core.h>
24 #include <Rocket/Controls.h>
25 
26 #ifdef _DEBUG
27 #include <Rocket/Debugger.h>
28 #endif
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "util/base/exception.h"
35 #include "util/time/timemanager.h"
36 
37 #include "librocketmanager.h"
40 
41 namespace FIFE {
42 
44  :
45  m_renderInterface(new LibRocketRenderInterface),
46  m_inputProcessor(NULL) {
47  Rocket::Core::SetSystemInterface(this);
48  Rocket::Core::SetRenderInterface(m_renderInterface);
49  Rocket::Core::Initialise();
50  Rocket::Controls::Initialise();
51  }
52 
55  m_context->RemoveReference();
56  Rocket::Core::Shutdown();
57 
58  delete m_renderInterface;
59  delete m_inputProcessor;
60  }
61 
62  void LibRocketManager::init(const std::string& backend, int32_t screenWidth, int32_t screenHeight) {
63 
64  m_context = Rocket::Core::CreateContext("default", Rocket::Core::Vector2i(screenWidth, screenHeight));
65 
66 #ifdef _DEBUG
67  Rocket::Debugger::Initialise(m_context);
68 #endif
69 
71  }
72 
74  static TimeManager *timeManager = TimeManager::instance();
75 
76  return timeManager->getTime() / 1000.0f;
77  }
78 
81 
82  m_context->Update();
83  m_context->Render();
84 
87  //m_renderInterface->reset();
88  }
89 
91  m_context->SetDimensions(Rocket::Core::Vector2i(width, height));
92  }
93 
94  Rocket::Core::ElementDocument* LibRocketManager::loadDocument(const std::string& id, const std::string& documentPath) {
95 
96  if(m_openDocuments.find(id) != m_openDocuments.end()) {
97  throw GuiException("Id: " + id + " already used!");
98  }
99 
100  Rocket::Core::String rocketDocumentPath(documentPath.c_str());
101 
102  Rocket::Core::ElementDocument* document = m_context->LoadDocument(rocketDocumentPath);
103 
104  if(document != NULL) {
105  m_openDocuments[id] = document;
106  } else {
107  throw GuiException("Could not load document " + documentPath + "!");
108  }
109 
110  return document;
111  }
112 
113  Rocket::Core::ElementDocument* LibRocketManager::getDocument(const std::string& id) {
114  std::map<std::string, Rocket::Core::ElementDocument*>::iterator doc(m_openDocuments.find(id));
115  if(doc == m_openDocuments.end()) {
116  throw GuiException("Rocket document with id " + id + " doesn't exist!");
117  }
118  return doc->second;
119  }
120 
121  void LibRocketManager::unloadDocument(Rocket::Core::ElementDocument* document) {
122  std::map<std::string, Rocket::Core::ElementDocument*>::iterator currDoc(m_openDocuments.begin());
123  std::map<std::string, Rocket::Core::ElementDocument*>::iterator endDocs(m_openDocuments.end());
124 
125  for(; currDoc != endDocs;) {
126  if(currDoc->second == document) {
127  m_context->UnloadDocument(currDoc->second);
128  m_openDocuments.erase(currDoc);
129  break;
130  }
131 
132  ++currDoc;
133  }
134  }
135 
136  void LibRocketManager::unloadDocument(const std::string& id) {
137  std::map<std::string, Rocket::Core::ElementDocument*>::iterator doc(m_openDocuments.find(id));
138 
139  if(doc != m_openDocuments.end()) {
140  m_context->UnloadDocument(doc->second);
141  m_openDocuments.erase(doc);
142  }
143  }
144 
145  void LibRocketManager::loadFont(const std::string& filepath) {
146  Rocket::Core::String rocketFontPath(filepath.c_str());
147 
148  bool succeeded = Rocket::Core::FontDatabase::LoadFontFace(rocketFontPath);
149  if(!succeeded) {
150  throw GuiException("Librocket cannot open font: " + filepath);
151  }
152  }
153 
154  bool LibRocketManager::onSdlEvent(SDL_Event& evt) {
155  return (m_inputProcessor != NULL) ? m_inputProcessor->onSdlEvent(evt)
156  : false;
157  }
158 
160 #ifdef _DEBUG
161  if(!Rocket::Debugger::IsVisible()) {
162  Rocket::Debugger::SetVisible(true);
163  }
164 #endif
165  }
166 
168 #ifdef _DEBUG
169  if(Rocket::Debugger::IsVisible()) {
170  Rocket::Debugger::SetVisible(false);
171  }
172 #endif
173  }
174 
176 
177  std::map<std::string, Rocket::Core::ElementDocument*>::iterator currDoc(m_openDocuments.begin());
178  std::map<std::string, Rocket::Core::ElementDocument*>::iterator endDocs(m_openDocuments.end());
179 
180  for(; currDoc != endDocs; ++currDoc) {
181  m_context->UnloadDocument(currDoc->second);
182  }
183 
184  m_openDocuments.clear();
185  }
186 };
virtual void turn()
Updates and renders the gui.
virtual Rocket::Core::ElementDocument * loadDocument(const std::string &id, const std::string &documentPath)
Loads a rocket .rml file and shows it.
bool onSdlEvent(SDL_Event &evt)
Processes SDL input and converts it to librocket input, then forwards it to the librocket context...
virtual void unloadDocument(Rocket::Core::ElementDocument *document)
Unloads a rocket document.
virtual float GetElapsedTime()
NOTE There is an inconsistency in the naming of this method.
void init(const std::string &backend, int32_t screenWidth, int32_t screenHeight)
Initializes the librocket manager.
LibRocketInputProcessor * m_inputProcessor
Input processor for librocket.
std::map< std::string, Rocket::Core::ElementDocument * > m_openDocuments
A set of all open documents.
static TimeManager * instance()
Definition: singleton.h:84
void freeTextures()
Frees all textures that are no longer needed by librocket.
void hideDebugger() const
Hides the librocket debugger.
LibRocketRenderInterface * m_renderInterface
Render Interface for librocket.
uint32_t getTime() const
Get the time.
virtual void loadFont(const std::string &filepath)
Loads a font to be used by librocket.
void showDebugger() const
Shows the librocket debugger.
LibRocketManager()
Constructor.
virtual void resizeTopContainer(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
Resizes the top container.
virtual bool onSdlEvent(SDL_Event &evt)
Receives input and converts it to librocket format, then it forwards it to librocket.
void unloadDocuments()
Unloads documents opened by librocket.
virtual Rocket::Core::ElementDocument * getDocument(const std::string &id)
void turn()
Called each frame to perform update operations.
Time Manager.
Definition: timemanager.h:50
Rocket::Core::Context * m_context
Librocket&#39;s context.
unsigned int uint32_t
Definition: core.h:40
void render()
Renders librocket gui.
virtual ~LibRocketManager()
Destructor.