25 #include <guichan/sdl/sdlinput.hpp>
26 #include <guichan/key.hpp>
27 #include <guichan/focushandler.hpp>
28 #include <guichan.hpp>
35 #include "gui/guichan/base/opengl/opengl_gui_graphics.h"
36 #include "gui/guichan/base/opengle/opengle_gui_graphics.h"
38 #include "gui/guichan/base/sdl/sdl_gui_graphics.h"
39 #include "util/base/exception.h"
40 #include "util/log/logger.h"
41 #include "video/renderbackend.h"
42 #include "gui/guichan/base/gui_imageloader.h"
43 #include "gui/guichan/base/gui_font.h"
44 #include "gui/guichan/console/console.h"
45 #include "video/fonts/fontbase.h"
46 #include "video/fonts/truetypefont.h"
47 #include "video/fonts/subimagefont.h"
48 #include "eventchannel/key/ec_key.h"
49 #include "eventchannel/key/ec_keyevent.h"
50 #include "eventchannel/mouse/ec_mouseevent.h"
51 #include "vfs/fife_boost_filesystem.h"
53 #include "guichanmanager.h"
56 static Logger _log(LM_GUI);
58 GUIChanManager::GUIChanManager() :
59 m_gcn_gui(new
gcn::Gui()),
61 m_gcn_topcontainer(new
gcn::Container()),
62 m_imgloader(new GuiImageLoader()) ,
63 m_input(new
gcn::SDLInput()),
67 m_logic_executed(false) {
69 m_gcn_gui->setInput(m_input);
70 gcn::Image::setImageLoader(m_imgloader);
72 m_gcn_gui->setTop(m_gcn_topcontainer);
73 m_focushandler = m_gcn_topcontainer->_getFocusHandler();
75 m_gcn_topcontainer->setOpaque(
false);
76 m_gcn_topcontainer->setFocusable(
false);
81 GUIChanManager::~GUIChanManager() {
83 delete m_gcn_topcontainer;
87 delete m_gui_graphics;
88 std::vector<GuiFont*>::iterator i = m_fonts.begin();
89 while (i != m_fonts.end()) {
95 bool GUIChanManager::onSdlEvent(SDL_Event& evt) {
97 FL_WARN(_log,
"GUIChanManager, GuichanGUI->getInput == 0 ... discarding events!");
101 bool overWidget = m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) != 0;
104 case SDL_MOUSEBUTTONDOWN:
105 m_had_widget = overWidget;
106 case SDL_MOUSEBUTTONUP:
108 m_input->pushInput(evt);
112 if( m_had_widget && overWidget ) {
119 m_focushandler->focusNone();
127 case SDL_MOUSEMOTION:
128 if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
130 m_input->pushInput(evt);
136 m_had_mouse = m_focushandler->getDraggedWidget() != 0;
137 m_input->pushInput(evt);
144 if(m_focushandler->getFocused()) {
145 m_input->pushInput(evt);
150 case SDL_ACTIVEEVENT:
160 void GUIChanManager::resizeTopContainer(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
161 m_gcn_topcontainer->setDimension(gcn::Rectangle(x, y, width, height));
162 this->invalidateFonts();
163 this->m_console->reLayout();
166 gcn::Gui* GUIChanManager::getGuichanGUI()
const {
170 void GUIChanManager::add(gcn::Widget* widget) {
171 if( !m_widgets.count(widget) ) {
172 m_gcn_topcontainer->add(widget);
173 m_widgets.insert(widget);
177 void GUIChanManager::remove(gcn::Widget* widget) {
178 if( m_widgets.count(widget) ) {
179 m_widgets.erase(widget);
180 m_gcn_topcontainer->remove(widget);
184 void GUIChanManager::init(
const std::string& backend, int32_t screenWidth, int32_t screenHeight) {
185 if( backend ==
"SDL" ) {
186 m_gui_graphics =
new SdlGuiGraphics();
188 else if (backend ==
"OpenGL") {
189 m_gui_graphics =
new OpenGLGuiGraphics();
191 else if (backend ==
"OpenGLe") {
192 m_gui_graphics =
new OpenGLeGuiGraphics();
198 m_gcn_gui->setGraphics(m_gui_graphics);
199 m_console =
new Console();
201 resizeTopContainer(0, 0, screenWidth, screenHeight);
204 GuiFont* GUIChanManager::createFont(
const std::string& path, uint32_t size,
const std::string& glyphs) {
205 std::string fontpath = path;
206 std::string fontglyphs = glyphs;
207 int32_t fontsize = size;
211 fontpath = m_fontpath;
214 fontsize = m_fontsize;
216 if(fontglyphs ==
"") {
217 fontglyphs = m_fontglyphs;
221 GuiFont* guifont = NULL;
222 if( bfs::extension(fontpath) ==
".ttf" || bfs::extension(fontpath) ==
".ttc" ) {
223 font =
new TrueTypeFont(fontpath, fontsize);
225 font =
new SubImageFont(fontpath, fontglyphs);
227 guifont =
new GuiFont(font);
229 m_fonts.push_back(guifont);
233 void GUIChanManager::releaseFont(GuiFont* font) {
234 std::vector<GuiFont*>::iterator i = m_fonts.begin();
235 while (i != m_fonts.end()) {
245 void GUIChanManager::invalidateFonts() {
246 std::vector<GuiFont*>::iterator it = m_fonts.begin();
247 while (it != m_fonts.end()) {
253 GuiFont* GUIChanManager::setDefaultFont(
const std::string& path, uint32_t size,
const std::string& glyphs) {
256 m_fontglyphs = glyphs;
258 m_defaultfont = createFont();
259 gcn::Widget::setGlobalFont(m_defaultfont);
261 m_console->reLayout();
264 return m_defaultfont;
267 void GUIChanManager::turn() {
268 if (!m_logic_executed)
270 m_logic_executed =
false;
274 KeyEvent GUIChanManager::translateKeyEvent(
const gcn::KeyEvent& gcnevt) {
276 if(gcnevt.getType() == gcn::KeyEvent::PRESSED)
277 keyevt.setType(KeyEvent::PRESSED);
278 else if(gcnevt.getType() == gcn::KeyEvent::RELEASED)
279 keyevt.setType(KeyEvent::RELEASED);
281 FL_WARN(_log, LMsg(
"GUIChanManager::translateKeyEvent() - ") <<
"Unknown event type: " << gcnevt.getType());
282 keyevt.setType(KeyEvent::UNKNOWN);
284 keyevt.setShiftPressed(gcnevt.isShiftPressed());
285 keyevt.setControlPressed(gcnevt.isControlPressed());
286 keyevt.setAltPressed(gcnevt.isAltPressed());
287 keyevt.setMetaPressed(gcnevt.isMetaPressed());
288 keyevt.setNumericPad(gcnevt.isNumericPad());
291 int32_t keyval = gcnevt.getKey().getValue();
292 keyval = convertGuichanKeyToFifeKey(keyval);
294 keyevt.setKey(Key(static_cast<Key::KeyType>(keyval), keyval));
299 MouseEvent GUIChanManager::translateMouseEvent(
const gcn::MouseEvent& gcnevt) {
301 mouseevt.setShiftPressed(gcnevt.isShiftPressed());
302 mouseevt.setControlPressed(gcnevt.isControlPressed());
303 mouseevt.setAltPressed(gcnevt.isAltPressed());
304 mouseevt.setMetaPressed(gcnevt.isMetaPressed());
305 mouseevt.setX(gcnevt.getX());
306 mouseevt.setY(gcnevt.getY());
308 switch(gcnevt.getType()) {
309 case gcn::MouseEvent::PRESSED:
310 mouseevt.setType(MouseEvent::PRESSED);
312 case gcn::MouseEvent::RELEASED:
313 mouseevt.setType(MouseEvent::RELEASED);
315 case gcn::MouseEvent::MOVED:
316 mouseevt.setType(MouseEvent::MOVED);
318 case gcn::MouseEvent::CLICKED:
319 mouseevt.setType(MouseEvent::CLICKED);
321 case gcn::MouseEvent::ENTERED:
322 mouseevt.setType(MouseEvent::ENTERED);
324 case gcn::MouseEvent::EXITED:
325 mouseevt.setType(MouseEvent::EXITED);
327 case gcn::MouseEvent::DRAGGED:
328 mouseevt.setType(MouseEvent::DRAGGED);
330 case gcn::MouseEvent::WHEEL_MOVED_DOWN:
331 mouseevt.setType(MouseEvent::WHEEL_MOVED_DOWN);
333 case gcn::MouseEvent::WHEEL_MOVED_UP:
334 mouseevt.setType(MouseEvent::WHEEL_MOVED_UP);
337 mouseevt.setType(MouseEvent::UNKNOWN_EVENT);
340 switch(gcnevt.getButton()) {
341 case gcn::MouseInput::LEFT:
342 mouseevt.setButton(MouseEvent::LEFT);
344 case gcn::MouseInput::RIGHT:
345 mouseevt.setButton(MouseEvent::RIGHT);
347 case gcn::MouseInput::MIDDLE:
348 mouseevt.setButton(MouseEvent::MIDDLE);
351 mouseevt.setButton(MouseEvent::UNKNOWN_BUTTON);
358 int32_t GUIChanManager::convertGuichanKeyToFifeKey(int32_t value) {
364 case gcn::Key::LEFT_ALT:
365 value = Key::LEFT_ALT;
367 case gcn::Key::RIGHT_ALT:
368 value = Key::RIGHT_ALT;
370 case gcn::Key::LEFT_SHIFT:
371 value = Key::LEFT_SHIFT;
373 case gcn::Key::RIGHT_SHIFT:
374 value = Key::RIGHT_SHIFT;
376 case gcn::Key::LEFT_CONTROL:
377 value = Key::LEFT_CONTROL;
379 case gcn::Key::RIGHT_CONTROL:
380 value = Key::RIGHT_CONTROL;
382 case gcn::Key::BACKSPACE:
383 value = Key::BACKSPACE;
385 case gcn::Key::PAUSE:
388 case gcn::Key::SPACE:
391 case gcn::Key::ESCAPE:
394 case gcn::Key::DELETE:
397 case gcn::Key::INSERT:
406 case gcn::Key::PAGE_UP:
407 value = Key::PAGE_UP;
409 case gcn::Key::PRINT_SCREEN:
410 value = Key::PRINT_SCREEN;
412 case gcn::Key::PAGE_DOWN:
413 value = Key::PAGE_DOWN;
460 case gcn::Key::NUM_LOCK:
461 value = Key::NUM_LOCK;
463 case gcn::Key::CAPS_LOCK:
464 value = Key::CAPS_LOCK;
466 case gcn::Key::SCROLL_LOCK:
467 value = Key::SCROLL_LOCK;
469 case gcn::Key::RIGHT_META:
470 value = Key::RIGHT_META;
472 case gcn::Key::LEFT_META:
473 value = Key::LEFT_META;
475 case gcn::Key::LEFT_SUPER:
476 value = Key::LEFT_SUPER;
478 case gcn::Key::RIGHT_SUPER:
479 value = Key::RIGHT_SUPER;
481 case gcn::Key::ALT_GR:
493 case gcn::Key::RIGHT:
496 case gcn::Key::ENTER:
502 if (value >= 1 && value <= 26) {
504 value = value - 1 +
'a';
505 }
else if (value >=
'A' && value <=
'Z') {
506 value = value -
'A' +
'a';
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...