FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fifechanmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 // Standard C++ library includes
23 
24 // 3rd party library includes
25 #include <fifechan/sdl/sdlinput.hpp>
26 #include <fifechan/key.hpp>
27 #include <fifechan/focushandler.hpp>
28 #include <fifechan.hpp>
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 #ifdef HAVE_OPENGL
36 #endif
38 #include "util/base/exception.h"
39 #include "util/log/logger.h"
40 #include "video/renderbackend.h"
44 #include "video/fonts/fontbase.h"
51 
52 #include "fifechanmanager.h"
53 
54 namespace FIFE {
55  static Logger _log(LM_GUI);
56 
58  m_fcn_gui(new fcn::Gui()),
59  m_focushandler(0),
60  m_fcn_topcontainer(new fcn::Container()),
61  m_imgloader(new GuiImageLoader()) ,
62  m_input(new fcn::SDLInput()),
63  m_console(0),
64  m_defaultfont(0),
65  m_fonts(),
66  m_logic_executed(false) {
67 
68  m_fcn_gui->setInput(m_input);
69  fcn::Image::setImageLoader(m_imgloader);
70 
72  m_focushandler = m_fcn_topcontainer->_getFocusHandler();
73 
74  m_fcn_topcontainer->setOpaque(false);
75  m_fcn_topcontainer->setFocusable(false);
76  m_had_mouse = false;
77  m_had_widget = false;
78  }
79 
81  delete m_console;
82  delete m_fcn_topcontainer;
83  delete m_imgloader;
84  delete m_input;
85  delete m_fcn_gui;
86  delete m_gui_graphics;
87  std::vector<GuiFont*>::iterator i = m_fonts.begin();
88  while (i != m_fonts.end()) {
89  delete *i;
90  ++i;
91  }
92  }
93 
94  bool FifechanManager::onSdlEvent(SDL_Event& evt) {
95  if (!m_input) {
96  FL_WARN(_log, "FifechanManager, FifechanGUI->getInput == 0 ... discarding events!");
97  return false;
98  }
99 
100  bool overWidget = m_fcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) != 0;
101 
102  switch(evt.type) {
103  case SDL_MOUSEBUTTONDOWN:
104  m_had_widget = overWidget;
105  case SDL_MOUSEBUTTONUP:
106  // Always send the button up/down events to fifechan
107  m_input->pushInput(evt);
108 
109  // Button was pressed over a widget and still is over a widget
110  // so we mark the event as processed.
111  if( m_had_widget && overWidget ) {
112  return true;
113  }
114 
115  // Button wasn't pressed over a widget so we want to release focus
116  // no matter what.
117  if (!m_had_widget) {
118  m_focushandler->focusNone();
119  }
120 
121  // Button up was processed by fifechan but there was no widget under
122  // the mouse at the time. Don't mark it as processed here so the
123  // other listeners have a chance to process the event.
124  return false;
125 
126  case SDL_MOUSEMOTION:
127  if( m_fcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
128  m_had_mouse = true;
129  m_input->pushInput(evt);
130  return true;
131  }
132  if( m_had_mouse ) {
133  // We only keep the mouse if a widget/window has requested
134  // dragging.
135  m_had_mouse = m_focushandler->getDraggedWidget() != 0;
136  m_input->pushInput(evt);
137  return true;
138  }
139  return false;
140 
141  case SDL_KEYDOWN:
142  case SDL_KEYUP:
143  if(m_focushandler->getFocused()) {
144  m_input->pushInput(evt);
145  return true;
146  }
147  return false;
148 
149  case SDL_ACTIVEEVENT:
150  // Actually Guichan doesn't care (it should!)
151  // so at least don't swallow mouse_focus events up.
152  return false;
153 
154  default:
155  return false;
156  }
157  }
158 
160  m_fcn_topcontainer->setDimension(fcn::Rectangle(x, y, width, height));
161  this->invalidateFonts();
162  this->m_console->reLayout();
163  }
164 
166  return m_fcn_gui;
167  }
168 
169  void FifechanManager::add(fcn::Widget* widget) {
170  if( !m_widgets.count(widget) ) {
171  m_fcn_topcontainer->add(widget);
172  m_widgets.insert(widget);
173  }
174  }
175 
176  void FifechanManager::remove(fcn::Widget* widget) {
177  if( m_widgets.count(widget) ) {
178  m_widgets.erase(widget);
179  m_fcn_topcontainer->remove(widget);
180  }
181  }
182 
183  void FifechanManager::init(const std::string& backend, int32_t screenWidth, int32_t screenHeight) {
184  if( backend == "SDL" ) {
186  }
187 #ifdef HAVE_OPENGL
188  else if (backend == "OpenGL") {
190  }
191 #endif
192  else {
193  //should never get here
194  assert(0);
195  }
196 
197  m_fcn_gui->setGraphics(m_gui_graphics);
198  m_console = new Console();
199 
200  resizeTopContainer(0, 0, screenWidth, screenHeight);
201  }
202 
203  GuiFont* FifechanManager::createFont(const std::string& path, uint32_t size, const std::string& glyphs) {
204  std::string fontpath = path;
205  std::string fontglyphs = glyphs;
206  int32_t fontsize = size;
207 
208  // Set default settings if necessary
209  if(fontpath == "") {
210  fontpath = m_fontpath;
211  }
212  if(fontsize == 0) {
213  fontsize = m_fontsize;
214  }
215  if(fontglyphs == "") {
216  fontglyphs = m_fontglyphs;
217  }
218 
219  IFont* font = NULL;
220  GuiFont* guifont = NULL;
221  if( bfs::extension(fontpath) == ".ttf" || bfs::extension(fontpath) == ".ttc" ) {
222  font = new TrueTypeFont(fontpath, fontsize);
223  } else {
224  font = new SubImageFont(fontpath, fontglyphs);
225  }
226  guifont = new GuiFont(font);
227 
228  m_fonts.push_back(guifont);
229  return guifont;
230  }
231 
233  std::vector<GuiFont*>::iterator i = m_fonts.begin();
234  while (i != m_fonts.end()) {
235  if ((*i) == font) {
236  m_fonts.erase(i);
237  delete font;
238  return;
239  }
240  ++i;
241  }
242  }
243 
245  std::vector<GuiFont*>::iterator it = m_fonts.begin();
246  while (it != m_fonts.end()) {
247  (*it)->invalidate();
248  ++it;
249  }
250  }
251 
252  GuiFont* FifechanManager::setDefaultFont(const std::string& path, uint32_t size, const std::string& glyphs) {
253  m_fontpath = path;
254  m_fontsize = size;
255  m_fontglyphs = glyphs;
256 
258  fcn::Widget::setGlobalFont(m_defaultfont);
259  if (m_console) {
260  m_console->reLayout();
261  }
262 
263  return m_defaultfont;
264  }
265 
267  if (!m_logic_executed)
268  m_fcn_gui->logic();
269  m_logic_executed = false;
270  m_fcn_gui->draw();
271  }
272 
273  KeyEvent FifechanManager::translateKeyEvent(const fcn::KeyEvent& fcnevt) {
274  KeyEvent keyevt;
275  if(fcnevt.getType() == fcn::KeyEvent::Pressed)
276  keyevt.setType(KeyEvent::PRESSED);
277  else if(fcnevt.getType() == fcn::KeyEvent::Released)
278  keyevt.setType(KeyEvent::RELEASED);
279  else {
280  FL_WARN(_log, LMsg("FifechanManager::translateKeyEvent() - ") << "Unknown event type: " << fcnevt.getType());
281  keyevt.setType(KeyEvent::UNKNOWN);
282  }
283  keyevt.setShiftPressed(fcnevt.isShiftPressed());
284  keyevt.setControlPressed(fcnevt.isControlPressed());
285  keyevt.setAltPressed(fcnevt.isAltPressed());
286  keyevt.setMetaPressed(fcnevt.isMetaPressed());
287  keyevt.setNumericPad(fcnevt.isNumericPad());
288 
289  // Convert from fifechan keyval to FIFE keyval
290  int32_t keyval = fcnevt.getKey().getValue();
291  keyval = convertFifechanKeyToFifeKey(keyval);
292 
293  keyevt.setKey(Key(static_cast<Key::KeyType>(keyval), keyval));
294 
295  return keyevt;
296  }
297 
298  MouseEvent FifechanManager::translateMouseEvent(const fcn::MouseEvent& fcnevt) {
299  MouseEvent mouseevt;
300  mouseevt.setShiftPressed(fcnevt.isShiftPressed());
301  mouseevt.setControlPressed(fcnevt.isControlPressed());
302  mouseevt.setAltPressed(fcnevt.isAltPressed());
303  mouseevt.setMetaPressed(fcnevt.isMetaPressed());
304  mouseevt.setX(fcnevt.getX());
305  mouseevt.setY(fcnevt.getY());
306 
307  switch(fcnevt.getType()) {
308  case fcn::MouseEvent::Pressed:
309  mouseevt.setType(MouseEvent::PRESSED);
310  break;
311  case fcn::MouseEvent::Released:
312  mouseevt.setType(MouseEvent::RELEASED);
313  break;
314  case fcn::MouseEvent::Moved:
315  mouseevt.setType(MouseEvent::MOVED);
316  break;
317  case fcn::MouseEvent::Clicked:
318  mouseevt.setType(MouseEvent::CLICKED);
319  break;
320  case fcn::MouseEvent::Entered:
321  mouseevt.setType(MouseEvent::ENTERED);
322  break;
323  case fcn::MouseEvent::Exited:
324  mouseevt.setType(MouseEvent::EXITED);
325  break;
326  case fcn::MouseEvent::Dragged:
327  mouseevt.setType(MouseEvent::DRAGGED);
328  break;
329  case fcn::MouseEvent::WheelMovedDown:
331  break;
332  case fcn::MouseEvent::WheelMovedUp:
334  break;
335  default:
337  }
338 
339  switch(fcnevt.getButton()) {
340  case fcn::MouseInput::Left:
341  mouseevt.setButton(MouseEvent::LEFT);
342  break;
343  case fcn::MouseInput::Right:
344  mouseevt.setButton(MouseEvent::RIGHT);
345  break;
346  case fcn::MouseInput::Middle:
347  mouseevt.setButton(MouseEvent::MIDDLE);
348  break;
349  default:
351  break;
352  }
353  return mouseevt;
354  }
355 
356 
358 
359  switch (value) {
360  case fcn::Key::Tab:
361  value = Key::TAB;
362  break;
363  case fcn::Key::LeftAlt:
364  value = Key::LEFT_ALT;
365  break;
366  case fcn::Key::RightAlt:
367  value = Key::RIGHT_ALT;
368  break;
369  case fcn::Key::LeftShift:
370  value = Key::LEFT_SHIFT;
371  break;
372  case fcn::Key::RightShift:
373  value = Key::RIGHT_SHIFT;
374  break;
375  case fcn::Key::LeftControl:
376  value = Key::LEFT_CONTROL;
377  break;
378  case fcn::Key::RightControl:
379  value = Key::RIGHT_CONTROL;
380  break;
381  case fcn::Key::Backspace:
382  value = Key::BACKSPACE;
383  break;
384  case fcn::Key::Pause:
385  value = Key::PAUSE;
386  break;
387  case fcn::Key::Space:
388  value = Key::SPACE;
389  break;
390  case fcn::Key::Escape:
391  value = Key::ESCAPE;
392  break;
393  case fcn::Key::Delete:
394  value = Key::DELETE;
395  break;
396  case fcn::Key::Insert:
397  value = Key::INSERT;
398  break;
399  case fcn::Key::Home:
400  value = Key::HOME;
401  break;
402  case fcn::Key::End:
403  value = Key::END;
404  break;
405  case fcn::Key::PageUp:
406  value = Key::PAGE_UP;
407  break;
408  case fcn::Key::PrintScreen:
409  value = Key::PRINT_SCREEN;
410  break;
411  case fcn::Key::PageDown:
412  value = Key::PAGE_DOWN;
413  break;
414  case fcn::Key::F1:
415  value = Key::F1;
416  break;
417  case fcn::Key::F2:
418  value = Key::F2;
419  break;
420  case fcn::Key::F3:
421  value = Key::F3;
422  break;
423  case fcn::Key::F4:
424  value = Key::F4;
425  break;
426  case fcn::Key::F5:
427  value = Key::F5;
428  break;
429  case fcn::Key::F6:
430  value = Key::F6;
431  break;
432  case fcn::Key::F7:
433  value = Key::F7;
434  break;
435  case fcn::Key::F8:
436  value = Key::F8;
437  break;
438  case fcn::Key::F9:
439  value = Key::F9;
440  break;
441  case fcn::Key::F10:
442  value = Key::F10;
443  break;
444  case fcn::Key::F11:
445  value = Key::F11;
446  break;
447  case fcn::Key::F12:
448  value = Key::F12;
449  break;
450  case fcn::Key::F13:
451  value = Key::F13;
452  break;
453  case fcn::Key::F14:
454  value = Key::F14;
455  break;
456  case fcn::Key::F15:
457  value = Key::F15;
458  break;
459  case fcn::Key::NumLock:
460  value = Key::NUM_LOCK;
461  break;
462  case fcn::Key::CapsLock:
463  value = Key::CAPS_LOCK;
464  break;
465  case fcn::Key::ScrollLock:
466  value = Key::SCROLL_LOCK;
467  break;
468  case fcn::Key::RightMeta:
469  value = Key::RIGHT_META;
470  break;
471  case fcn::Key::LeftMeta:
472  value = Key::LEFT_META;
473  break;
474  case fcn::Key::LeftSuper:
475  value = Key::LEFT_SUPER;
476  break;
477  case fcn::Key::RightSuper:
478  value = Key::RIGHT_SUPER;
479  break;
480  case fcn::Key::AltGr:
481  value = Key::ALT_GR;
482  break;
483  case fcn::Key::Up:
484  value = Key::UP;
485  break;
486  case fcn::Key::Down:
487  value = Key::DOWN;
488  break;
489  case fcn::Key::Left:
490  value = Key::LEFT;
491  break;
492  case fcn::Key::Right:
493  value = Key::RIGHT;
494  break;
495  case fcn::Key::Enter:
496  value = Key::ENTER;
497  break;
498 
499  default:
500  // Convert from unicode to lowercase letters
501  if (value >= 1 && value <= 26) {
502  // Control characters
503  value = value - 1 + 'a';
504  } else if (value >= 'A' && value <= 'Z') {
505  value = value - 'A' + 'a';
506  }
507 
508  // FIXME: Accented characters (รก) will not get converted properly.
509  break;
510  }
511 
512  return value;
513  }
514 }
Definition: modules.h:41
#define FL_WARN(logger, msg)
Definition: logger.h:72
KeyEvent translateKeyEvent(const fcn::KeyEvent &evt)
void add(fcn::Widget *widget)
Adds a new widget.
virtual void turn()
Performs the GUI logic and draws the GUI accordingly.
void setY(int32_t y)
Class for mouse events.
Definition: ec_mouseevent.h:42
fcn::FocusHandler * m_focushandler
void setKey(const Key &key)
Definition: ec_keyevent.h:72
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
virtual void setControlPressed(bool pressed)
FifechanManager()
Constructor.
virtual void setShiftPressed(bool pressed)
void resizeTopContainer(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
Resizes the top container.
GuiFont * setDefaultFont(const std::string &path, uint32_t size, const std::string &glyphs)
Set the global font properties.
virtual ~FifechanManager()
Destructor.
static Logger _log(LM_AUDIO)
virtual bool onSdlEvent(SDL_Event &evt)
Called when an SDL event is received from SDL.
virtual void setControlPressed(bool pressed)
Definition: ec_keyevent.h:77
virtual void setAltPressed(bool pressed)
GuiFont * createFont(const std::string &path="", uint32_t size=0, const std::string &glyphs="")
Gets font with given properties.
virtual void setShiftPressed(bool pressed)
Definition: ec_keyevent.h:81
fcn::SDLInput * m_input
virtual void setMetaPressed(bool pressed)
Definition: ec_keyevent.h:79
fcn::Container * m_fcn_topcontainer
GuiImageLoader * m_imgloader
SDL True Type Font implementation of Font.
Definition: truetypefont.h:51
std::vector< GuiFont * > m_fonts
void remove(fcn::Widget *widget)
Removes a widget.
Represents a key or a character.
Definition: ec_key.h:41
std::set< fcn::Widget * > m_widgets
void releaseFont(GuiFont *font)
Releases given font.
void setX(int32_t x)
void setNumericPad(bool ispad)
Definition: ec_keyevent.h:69
void setButton(MouseButtonType type)
Definition: ec_mouseevent.h:92
virtual void setMetaPressed(bool pressed)
fcn::Graphics * m_gui_graphics
void setType(KeyEventType type)
Definition: ec_keyevent.h:66
void init(const std::string &backend, int32_t screenWidth, int32_t screenHeight)
Inits the Fifechan GUI Manager.
Pure abstract Font interface.
Definition: ifont.h:43
Overrides Guichan Graphics to enable usage of normal fife images & related facilities.
Imagefont that is able to read glyphs from single image sheet, see e.g.
Definition: subimagefont.h:44
Class for key events.
Definition: ec_keyevent.h:45
MouseEvent translateMouseEvent(const fcn::MouseEvent &evt)
static int32_t convertFifechanKeyToFifeKey(int32_t value)
void setType(MouseEventType type)
Definition: ec_mouseevent.h:99
void reLayout()
Layouts the console to match e.g.
Definition: console.cpp:89
Ingame Console.
Definition: console.h:64
unsigned int uint32_t
Definition: core.h:40
virtual void setAltPressed(bool pressed)
Definition: ec_keyevent.h:75
fcn::Gui * getFifechanGUI() const
Gets the member pointer to the Fifechan GUI.
Overrides Guichan Graphics to enable usage of normal fife images & related facilities.