| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 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" |
| 38 |
#include "base/librocketinputprocessor.h" |
| 39 |
#include "base/librocketrenderinterface.h" |
| 40 |
|
| 41 |
namespace FIFE { |
| 42 |
|
| 43 |
LibRocketManager::LibRocketManager() |
| 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 |
|
| 53 |
LibRocketManager::~LibRocketManager() { |
| 54 |
unloadDocuments(); |
| 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 |
|
| 70 |
m_inputProcessor = new LibRocketInputProcessor(m_context); |
| 71 |
} |
| 72 |
|
| 73 |
float LibRocketManager::GetElapsedTime() { |
| 74 |
static TimeManager *timeManager = TimeManager::instance(); |
| 75 |
|
| 76 |
return timeManager->getTime() / 1000.0f; |
| 77 |
} |
| 78 |
|
| 79 |
void LibRocketManager::turn() { |
| 80 |
m_inputProcessor->turn(); |
| 81 |
|
| 82 |
m_context->Update(); |
| 83 |
m_context->Render(); |
| 84 |
|
| 85 |
m_renderInterface->render(); |
| 86 |
m_renderInterface->freeTextures(); |
| 87 |
//m_renderInterface->reset(); |
| 88 |
} |
| 89 |
|
| 90 |
void LibRocketManager::resizeTopContainer(uint32_t x, uint32_t y, uint32_t width, uint32_t height) { |
| 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 |
|
| 159 |
void LibRocketManager::showDebugger() const { |
| 160 |
#ifdef _DEBUG |
| 161 |
if(!Rocket::Debugger::IsVisible()) { |
| 162 |
Rocket::Debugger::SetVisible(true); |
| 163 |
} |
| 164 |
#endif |
| 165 |
} |
| 166 |
|
| 167 |
void LibRocketManager::hideDebugger() const { |
| 168 |
#ifdef _DEBUG |
| 169 |
if(Rocket::Debugger::IsVisible()) { |
| 170 |
Rocket::Debugger::SetVisible(false); |
| 171 |
} |
| 172 |
#endif |
| 173 |
} |
| 174 |
|
| 175 |
void LibRocketManager::unloadDocuments() { |
| 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 |
}; |