Mercurial > fife-parpg
comparison engine/core/gui/guimanager.cpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | e84dccee1bb7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2005-2008 by the FIFE team * | |
3 * http://www.fifengine.de * | |
4 * This file is part of FIFE. * | |
5 * * | |
6 * FIFE is free software; you can redistribute it and/or modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * | |
20 ***************************************************************************/ | |
21 | |
22 // Standard C++ library includes | |
23 #include <iostream> | |
24 | |
25 // 3rd party library includes | |
26 #include <boost/filesystem/convenience.hpp> | |
27 #include <guichan/sdl/sdlinput.hpp> | |
28 #include <guichan/focushandler.hpp> | |
29 #include <guichan.hpp> | |
30 | |
31 // FIFE includes | |
32 // These includes are split up in two parts, separated by one empty line | |
33 // First block: files included from the FIFE root src directory | |
34 // Second block: files included from the same folder | |
35 #include "util/log/logger.h" | |
36 #include "video/renderbackend.h" | |
37 #include "gui/base/gui_imageloader.h" | |
38 #include "gui/base/gui_font.h" | |
39 #include "gui/console/console.h" | |
40 #include "video/fonts/fontbase.h" | |
41 #include "video/fonts/truetypefont.h" | |
42 #include "video/fonts/subimagefont.h" | |
43 #include "eventchannel/widget/ec_widgetevent.h" | |
44 #include "eventchannel/key/ec_keyevent.h" | |
45 #include "eventchannel/mouse/ec_mouseevent.h" | |
46 | |
47 #include "guimanager.h" | |
48 | |
49 | |
50 namespace FIFE { | |
51 static Logger _log(LM_GUI); | |
52 | |
53 GUIManager::GUIManager(IWidgetListener* widgetlistener, ImagePool& pool) : | |
54 m_gcn_gui(new gcn::Gui()), | |
55 m_focushandler(0), | |
56 m_gcn_topcontainer(new gcn::Container()), | |
57 m_imgloader(new GuiImageLoader(pool)) , | |
58 m_input(new gcn::SDLInput()), | |
59 m_console(0), | |
60 m_fonts(), | |
61 m_widgetlistener(widgetlistener), | |
62 m_pool(pool), | |
63 m_logic_executed(false) { | |
64 | |
65 m_gcn_gui->setTop(m_gcn_topcontainer); | |
66 m_gcn_topcontainer->setOpaque(false); | |
67 m_gcn_gui->setInput(m_input); | |
68 | |
69 gcn::Image::setImageLoader(m_imgloader); | |
70 m_focushandler = m_gcn_topcontainer->_getFocusHandler(); | |
71 } | |
72 | |
73 GUIManager::~GUIManager() { | |
74 delete m_console; | |
75 delete m_gcn_topcontainer; | |
76 delete m_imgloader; | |
77 delete m_input; | |
78 delete m_gcn_gui; | |
79 std::vector<GuiFont*>::iterator i = m_fonts.begin(); | |
80 while (i != m_fonts.end()) { | |
81 delete *i; | |
82 ++i; | |
83 } | |
84 } | |
85 | |
86 void GUIManager::onSdlEvent(SDL_Event& evt) { | |
87 gcn::SDLInput *input = dynamic_cast<gcn::SDLInput*>(m_gcn_gui->getInput()); | |
88 if (!input) { | |
89 FL_WARN(_log, "GUIManager, GuichanGUI->getInput == 0 ... discarding events!"); | |
90 return; | |
91 } | |
92 input->pushInput(evt); | |
93 } | |
94 | |
95 void GUIManager::resizeTopContainer(unsigned int x, unsigned int y, unsigned int width, unsigned int height) { | |
96 m_gcn_topcontainer->setDimension(gcn::Rectangle(x, y, width, height)); | |
97 } | |
98 | |
99 gcn::Gui* GUIManager::getGuichanGUI() const { | |
100 return m_gcn_gui; | |
101 } | |
102 | |
103 void GUIManager::add(gcn::Widget* widget) { | |
104 if( !m_widgets.count(widget) ) { | |
105 m_gcn_topcontainer->add(widget); | |
106 m_widgets.insert(widget); | |
107 } | |
108 } | |
109 | |
110 void GUIManager::remove(gcn::Widget* widget) { | |
111 if( m_widgets.count(widget) ) { | |
112 m_widgets.erase(widget); | |
113 m_gcn_topcontainer->remove(widget); | |
114 } | |
115 } | |
116 | |
117 void GUIManager::init(gcn::Graphics* graphics, int screenWidth, int screenHeight) { | |
118 m_gcn_gui->setGraphics(graphics); | |
119 resizeTopContainer(0, 0, screenWidth, screenHeight); | |
120 m_console = new Console(); | |
121 } | |
122 | |
123 GuiFont* GUIManager::createFont(const std::string& path, unsigned int size, const std::string& glyphs) { | |
124 std::string fontpath = path; | |
125 std::string fontglyphs = glyphs; | |
126 int fontsize = size; | |
127 | |
128 // Set default settings if necessary | |
129 if(fontpath == "") { | |
130 fontpath = m_fontpath; | |
131 } | |
132 if(fontsize == 0) { | |
133 fontsize = m_fontsize; | |
134 } | |
135 if(fontglyphs == "") { | |
136 fontglyphs = m_fontglyphs; | |
137 } | |
138 | |
139 AbstractFont* font = NULL; | |
140 GuiFont* guifont = NULL; | |
141 if( boost::filesystem::extension(fontpath) == ".ttf" ) { | |
142 font = new TrueTypeFont(fontpath, fontsize); | |
143 } else { | |
144 font = new SubImageFont(fontpath, fontglyphs, m_pool); | |
145 } | |
146 guifont = new GuiFont(font); | |
147 | |
148 m_fonts.push_back(guifont); | |
149 return guifont; | |
150 } | |
151 | |
152 void GUIManager::releaseFont(GuiFont* font) { | |
153 std::vector<GuiFont*>::iterator i = m_fonts.begin(); | |
154 while (i != m_fonts.end()) { | |
155 if ((*i) == font) { | |
156 m_fonts.erase(i); | |
157 delete font; | |
158 return; | |
159 } | |
160 ++i; | |
161 } | |
162 } | |
163 | |
164 GuiFont* GUIManager::setDefaultFont(const std::string& path, unsigned int size, const std::string& glyphs) { | |
165 m_fontpath = path; | |
166 m_fontsize = size; | |
167 m_fontglyphs = glyphs; | |
168 | |
169 GuiFont* defaultfont = createFont(); | |
170 gcn::Widget::setGlobalFont(defaultfont); | |
171 if (m_console) { | |
172 m_console->reLayout(); | |
173 } | |
174 | |
175 return defaultfont; | |
176 } | |
177 | |
178 void GUIManager::turn() { | |
179 if (!m_logic_executed) { | |
180 // Due to a BUG in Guichan we need to catch GCN exceptions | |
181 // This is a potentially dangerous workaround put in place | |
182 // until we upgrade to Guichan 0.8.0 | |
183 // See here: http://code.google.com/p/guichan/issues/detail?id=24 | |
184 try { | |
185 m_gcn_gui->logic(); | |
186 } catch( const gcn::Exception& e) { | |
187 FL_WARN(_log, LMsg("GUIManager, discarding gcn::Exception: ") << e.getMessage()); | |
188 } | |
189 } | |
190 m_gcn_gui->draw(); | |
191 m_logic_executed = false; | |
192 } | |
193 | |
194 void GUIManager::action(const gcn::ActionEvent & event) { | |
195 WidgetEvent wevt; | |
196 wevt.setId(event.getId()); | |
197 wevt.setSourceWidget(event.getSource()); | |
198 m_widgetlistener->onWidgetAction(wevt); | |
199 } | |
200 | |
201 void GUIManager::evaluateKeyEventConsumption(KeyEvent& evt) { | |
202 gcn::Widget* w = m_focushandler->getFocused(); | |
203 if (w) { | |
204 evt.consume(); | |
205 } | |
206 } | |
207 | |
208 void GUIManager::evaluateMouseEventConsumption(MouseEvent& evt) { | |
209 gcn::Widget* w = m_gcn_topcontainer->getWidgetAt(evt.getX(), evt.getY()); | |
210 if (w && w->isVisible()) { | |
211 // evt.consume(); | |
212 } | |
213 } | |
214 | |
215 void GUIManager::mousePressed(MouseEvent& evt) { | |
216 evaluateMouseEventConsumption(evt); | |
217 if (!evt.isConsumed()) { | |
218 m_focushandler->focusNone(); | |
219 } | |
220 } | |
221 | |
222 void GUIManager::mouseDragged(MouseEvent& evt) { | |
223 try { | |
224 m_gcn_gui->logic(); | |
225 m_logic_executed = true; | |
226 } catch( const gcn::Exception& e) { | |
227 FL_WARN(_log, LMsg("GUIManager, discarding gcn::Exception: ") << e.getMessage()); | |
228 } | |
229 evaluateMouseEventConsumption(evt); | |
230 } | |
231 } |