26 #include <boost/filesystem/convenience.hpp>
27 #include <guichan/sdl/sdlinput.hpp>
28 #include <guichan/key.hpp>
29 #include <guichan/focushandler.hpp>
30 #include <guichan.hpp>
36 #include "util/base/exception.h"
37 #include "util/log/logger.h"
38 #include "video/renderbackend.h"
39 #include "gui/base/gui_imageloader.h"
40 #include "gui/base/gui_font.h"
41 #include "gui/console/console.h"
42 #include "video/fonts/fontbase.h"
43 #include "video/fonts/truetypefont.h"
44 #include "video/fonts/subimagefont.h"
45 #include "eventchannel/key/ec_key.h"
46 #include "eventchannel/key/ec_keyevent.h"
47 #include "eventchannel/mouse/ec_mouseevent.h"
49 #include "guimanager.h"
53 static Logger _log(LM_GUI);
55 GUIManager::GUIManager(ImagePool& pool) :
56 m_gcn_gui(new gcn::Gui()),
58 m_gcn_topcontainer(new gcn::Container()),
59 m_imgloader(new GuiImageLoader(pool)) ,
60 m_input(new gcn::SDLInput()),
64 m_logic_executed(false) {
66 m_gcn_gui->setInput(m_input);
67 gcn::Image::setImageLoader(m_imgloader);
69 m_gcn_gui->setTop(m_gcn_topcontainer);
70 m_focushandler = m_gcn_topcontainer->_getFocusHandler();
72 m_gcn_topcontainer->setOpaque(
false);
73 m_gcn_topcontainer->setFocusable(
false);
77 GUIManager::~GUIManager() {
79 delete m_gcn_topcontainer;
83 std::vector<GuiFont*>::iterator i = m_fonts.begin();
84 while (i != m_fonts.end()) {
90 bool GUIManager::onSdlEvent(SDL_Event& evt) {
92 FL_WARN(_log,
"GUIManager, GuichanGUI->getInput == 0 ... discarding events!");
97 case SDL_MOUSEBUTTONDOWN:
98 case SDL_MOUSEBUTTONUP:
99 if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
100 m_input->pushInput(evt);
103 m_focushandler->focusNone();
106 case SDL_MOUSEMOTION:
107 if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
109 m_input->pushInput(evt);
115 m_had_mouse = bool(m_focushandler->getDraggedWidget());
116 m_input->pushInput(evt);
123 if(m_focushandler->getFocused()) {
124 m_input->pushInput(evt);
129 case SDL_ACTIVEEVENT:
139 void GUIManager::resizeTopContainer(
unsigned int x,
unsigned int y,
unsigned int width,
unsigned int height) {
140 m_gcn_topcontainer->setDimension(gcn::Rectangle(x, y, width, height));
143 gcn::Gui* GUIManager::getGuichanGUI()
const {
147 void GUIManager::add(gcn::Widget* widget) {
148 if( !m_widgets.count(widget) ) {
149 m_gcn_topcontainer->add(widget);
150 m_widgets.insert(widget);
154 void GUIManager::remove(gcn::Widget* widget) {
155 if( m_widgets.count(widget) ) {
156 m_widgets.erase(widget);
157 m_gcn_topcontainer->remove(widget);
161 void GUIManager::init(gcn::Graphics* graphics,
int screenWidth,
int screenHeight) {
162 m_gcn_gui->setGraphics(graphics);
163 resizeTopContainer(0, 0, screenWidth, screenHeight);
164 m_console =
new Console();
167 GuiFont* GUIManager::createFont(
const std::string& path,
unsigned int size,
const std::string& glyphs) {
168 std::string fontpath = path;
169 std::string fontglyphs = glyphs;
174 fontpath = m_fontpath;
177 fontsize = m_fontsize;
179 if(fontglyphs ==
"") {
180 fontglyphs = m_fontglyphs;
183 AbstractFont* font = NULL;
184 GuiFont* guifont = NULL;
185 if( boost::filesystem::extension(fontpath) ==
".ttf" ) {
186 font =
new TrueTypeFont(fontpath, fontsize);
188 font =
new SubImageFont(fontpath, fontglyphs, m_pool);
190 guifont =
new GuiFont(font);
192 m_fonts.push_back(guifont);
196 void GUIManager::releaseFont(GuiFont* font) {
197 std::vector<GuiFont*>::iterator i = m_fonts.begin();
198 while (i != m_fonts.end()) {
208 void GUIManager::invalidateFonts() {
209 std::vector<GuiFont*>::iterator it = m_fonts.begin();
210 while (it != m_fonts.end()) {
216 GuiFont* GUIManager::setDefaultFont(
const std::string& path,
unsigned int size,
const std::string& glyphs) {
219 m_fontglyphs = glyphs;
221 GuiFont* defaultfont = createFont();
222 gcn::Widget::setGlobalFont(defaultfont);
224 m_console->reLayout();
230 void GUIManager::turn() {
231 if (!m_logic_executed)
233 m_logic_executed =
false;
237 KeyEvent GUIManager::translateKeyEvent(
const gcn::KeyEvent& gcnevt) {
239 if(gcnevt.getType() == gcn::KeyEvent::PRESSED)
240 keyevt.setType(KeyEvent::PRESSED);
241 else if(gcnevt.getType() == gcn::KeyEvent::RELEASED)
242 keyevt.setType(KeyEvent::RELEASED);
244 throw EventException(
"Invalid event type in fillKeyEvent");
245 keyevt.setShiftPressed(gcnevt.isShiftPressed());
246 keyevt.setControlPressed(gcnevt.isControlPressed());
247 keyevt.setAltPressed(gcnevt.isAltPressed());
248 keyevt.setMetaPressed(gcnevt.isMetaPressed());
249 keyevt.setNumericPad(gcnevt.isNumericPad());
252 int keyval = gcnevt.getKey().getValue();
253 keyval = convertGuichanKeyToFifeKey(keyval);
255 keyevt.setKey(Key(static_cast<Key::KeyType>(keyval), keyval));
260 MouseEvent GUIManager::translateMouseEvent(
const gcn::MouseEvent& gcnevt) {
262 mouseevt.setShiftPressed(gcnevt.isShiftPressed());
263 mouseevt.setControlPressed(gcnevt.isControlPressed());
264 mouseevt.setAltPressed(gcnevt.isAltPressed());
265 mouseevt.setMetaPressed(gcnevt.isMetaPressed());
266 mouseevt.setX(gcnevt.getX());
267 mouseevt.setY(gcnevt.getY());
269 switch(gcnevt.getType()) {
270 case gcn::MouseEvent::PRESSED:
271 mouseevt.setType(MouseEvent::PRESSED);
273 case gcn::MouseEvent::RELEASED:
274 mouseevt.setType(MouseEvent::RELEASED);
276 case gcn::MouseEvent::MOVED:
277 mouseevt.setType(MouseEvent::MOVED);
279 case gcn::MouseEvent::CLICKED:
280 mouseevt.setType(MouseEvent::CLICKED);
282 case gcn::MouseEvent::ENTERED:
283 mouseevt.setType(MouseEvent::ENTERED);
285 case gcn::MouseEvent::EXITED:
286 mouseevt.setType(MouseEvent::EXITED);
288 case gcn::MouseEvent::DRAGGED:
289 mouseevt.setType(MouseEvent::DRAGGED);
291 case gcn::MouseEvent::WHEEL_MOVED_DOWN:
292 mouseevt.setType(MouseEvent::WHEEL_MOVED_DOWN);
294 case gcn::MouseEvent::WHEEL_MOVED_UP:
295 mouseevt.setType(MouseEvent::WHEEL_MOVED_UP);
298 mouseevt.setType(MouseEvent::UNKNOWN_EVENT);
301 switch(gcnevt.getButton()) {
302 case gcn::MouseInput::LEFT:
303 mouseevt.setButton(MouseEvent::LEFT);
305 case gcn::MouseInput::RIGHT:
306 mouseevt.setButton(MouseEvent::RIGHT);
308 case gcn::MouseInput::MIDDLE:
309 mouseevt.setButton(MouseEvent::MIDDLE);
312 mouseevt.setButton(MouseEvent::UNKNOWN_BUTTON);
319 int GUIManager::convertGuichanKeyToFifeKey(
int value) {
325 case gcn::Key::LEFT_ALT:
326 value = Key::LEFT_ALT;
328 case gcn::Key::RIGHT_ALT:
329 value = Key::RIGHT_ALT;
331 case gcn::Key::LEFT_SHIFT:
332 value = Key::LEFT_SHIFT;
334 case gcn::Key::RIGHT_SHIFT:
335 value = Key::RIGHT_SHIFT;
337 case gcn::Key::LEFT_CONTROL:
338 value = Key::LEFT_CONTROL;
340 case gcn::Key::RIGHT_CONTROL:
341 value = Key::RIGHT_CONTROL;
343 case gcn::Key::BACKSPACE:
344 value = Key::BACKSPACE;
346 case gcn::Key::PAUSE:
349 case gcn::Key::SPACE:
352 case gcn::Key::ESCAPE:
355 case gcn::Key::DELETE:
358 case gcn::Key::INSERT:
367 case gcn::Key::PAGE_UP:
368 value = Key::PAGE_UP;
370 case gcn::Key::PRINT_SCREEN:
371 value = Key::PRINT_SCREEN;
373 case gcn::Key::PAGE_DOWN:
374 value = Key::PAGE_DOWN;
421 case gcn::Key::NUM_LOCK:
422 value = Key::NUM_LOCK;
424 case gcn::Key::CAPS_LOCK:
425 value = Key::CAPS_LOCK;
427 case gcn::Key::SCROLL_LOCK:
428 value = Key::SCROLL_LOCK;
430 case gcn::Key::RIGHT_META:
431 value = Key::RIGHT_META;
433 case gcn::Key::LEFT_META:
434 value = Key::LEFT_META;
436 case gcn::Key::LEFT_SUPER:
437 value = Key::LEFT_SUPER;
439 case gcn::Key::RIGHT_SUPER:
440 value = Key::RIGHT_SUPER;
442 case gcn::Key::ALT_GR:
454 case gcn::Key::RIGHT:
457 case gcn::Key::ENTER:
463 if (value >= 1 && value <= 26) {
465 value = value - 1 +
'a';
466 }
else if (value >=
'A' && value <=
'Z') {
467 value = value -
'A' +
'a';