xoreos  0.0.5
gui.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/error.h"
26 #include "src/common/util.h"
27 
28 #include "src/aurora/gff3file.h"
29 #include "src/aurora/resman.h"
30 
32 
42 
44 
45 namespace Engines {
46 
47 namespace KotOR {
48 
50  strct = &s;
51 
52  widget = 0;
53  parent = p;
54 
55  type = (WidgetType) strct->getUint("CONTROLTYPE", kWidgetTypeInvalid);
56  if (type == kWidgetTypeInvalid)
57  throw Common::Exception("Widget without a type");
58 
59  tag = strct->getString("TAG");
60 }
61 
62 
63 GUI::GUI(::Engines::Console *console) : ::Engines::GUI(console), _widgetZ(0), _guiHeight(0.0f), _guiWidth(0.0f) {
64 }
65 
67 }
68 
69 void GUI::show() {
70  if (_background)
71  _background->show();
73 }
74 
75 void GUI::hide() {
76  if (_background)
77  _background->hide();
79 }
80 
81 void GUI::convertToXoreos(float &x, float &y, const float widgetHeight) const {
82  x = x - (_guiWidth / 2.0f);
83  y = (_guiHeight / 2.0f) - y - widgetHeight;
84 }
85 
86 void GUI::convertToGUI(float &x, float &y, const float widgetHeight) const {
87  x = x + (_guiWidth / 2.0f);
88  y = widgetHeight + (-1.0f * (y - (_guiHeight / 2.0f)));
89 }
90 
92  return _name;
93 }
94 
96  CursorMan.set(CursorMan.getCurrentGroup(), "down");
97 }
98 
99 void GUI::mouseUp() {
100  CursorMan.set(CursorMan.getCurrentGroup(), "up");
101 }
102 
103 void GUI::load(const Common::UString &resref) {
104  if (!empty())
105  clearWidgets();
106 
107  _guiWidth = 0;
108  _guiHeight = 0;
109 
110  // This is only relevant to Jade Empire.
111  // LTI prefixed GUI definitions for the Windows version of Jade Empire
112  // with lti_ to support mouse and keyboard control.
113  _name = ResMan.hasResource("lti_" + resref, Aurora::kFileTypeGUI) ? _name = "lti_" + resref : resref;
114 
115  try {
116  _gff.reset(new Aurora::GFF3File(_name, Aurora::kFileTypeGUI, MKTAG('G', 'U', 'I', ' ')));
117 
118  loadWidget(_gff->getTopLevel(), 0);
119 
120  } catch (Common::Exception &e) {
121  e.add("Can't load GUI \"%s\"", _name.c_str());
122  throw;
123  }
124 }
125 
126 void GUI::loadWidget(const Aurora::GFF3Struct &strct, Widget *parent) {
127 
128  WidgetContext ctx(strct, parent);
129 
130  createWidget(ctx);
131 
132  if (_guiWidth <= 0.0f)
133  _guiWidth = ctx.widget->getWidth();
134  if (_guiHeight <= 0.0f)
135  _guiHeight = ctx.widget->getHeight();
136 
137  float wX, wY, wZ;
138  ctx.widget->getPosition(wX, wY, wZ);
139 
140  convertToXoreos(wX, wY, ctx.widget->getHeight());
141  wZ = _widgetZ + wZ;
142 
143  ctx.widget->setPosition(wX, wY, wZ);
144 
145  _widgetZ -= 1.0f;
146 
147  addWidget(ctx.widget);
148 
149  // Go down to the children
150  if (ctx.strct->hasField("CONTROLS")) {
151  const Aurora::GFF3List &children = ctx.strct->getList("CONTROLS");
152 
153  for (Aurora::GFF3List::const_iterator c = children.begin(); c != children.end(); ++c)
154  loadWidget(**c, ctx.widget);
155  }
156 }
157 
159  if (ctx.type == kWidgetTypePanel)
160  ctx.widget = new WidgetPanel(*this, ctx.tag);
161  else if (ctx.type == kWidgetTypeLabel)
162  ctx.widget = new WidgetLabel(*this, ctx.tag);
163  else if (ctx.type == kWidgetTypeProtoItem)
164  ctx.widget = new WidgetProtoItem(*this, ctx.tag);
165  else if (ctx.type == kWidgetTypeButton)
166  ctx.widget = new WidgetButton(*this, ctx.tag);
167  else if (ctx.type == kWidgetTypeCheckBox)
168  ctx.widget = new WidgetCheckBox(*this, ctx.tag);
169  else if (ctx.type == kWidgetTypeSlider)
170  ctx.widget = new WidgetSlider(*this, ctx.tag);
171  else if (ctx.type == kWidgetTypeScrollbar)
172  ctx.widget = new WidgetScrollbar(*this, ctx.tag);
173  else if (ctx.type == kWidgetTypeProgressbar)
174  ctx.widget = new WidgetProgressbar(*this, ctx.tag);
175  else if (ctx.type == kWidgetTypeListBox)
176  ctx.widget = new WidgetListBox(*this, ctx.tag);
177  else
178  throw Common::Exception("No such widget type %d", ctx.type);
179 
180  try {
181  ctx.widget->load(*ctx.strct);
182  } catch (...) {
183  Common::exceptionDispatcherWarning("Failed to load widget %s", ctx.tag.c_str());
184  }
185 
186  initWidget(*ctx.widget);
187 }
188 
189 void GUI::initWidget(Widget &UNUSED(widget)) {
190 }
191 
192 WidgetPanel *GUI::getPanel(const Common::UString &tag, bool vital) {
193  Widget *widget = getWidget(tag, vital);
194  if (!widget)
195  return 0;
196 
197  WidgetPanel *panel = dynamic_cast<WidgetPanel *>(widget);
198  if (!panel && vital)
199  throw Common::Exception("Vital panel widget \"%s\" doesn't exist", tag.c_str());
200 
201  return panel;
202 }
203 
204 WidgetLabel *GUI::getLabel(const Common::UString &tag, bool vital) {
205  Widget *widget = getWidget(tag, vital);
206  if (!widget)
207  return 0;
208 
209  WidgetLabel *label = dynamic_cast<WidgetLabel *>(widget);
210  if (!label && vital)
211  throw Common::Exception("Vital label widget \"%s\" doesn't exist", tag.c_str());
212 
213  return label;
214 }
215 
217  Widget *widget = getWidget(tag, vital);
218  if (!widget)
219  return 0;
220 
221  WidgetProtoItem *protoItem = dynamic_cast<WidgetProtoItem *>(widget);
222  if (!protoItem && vital)
223  throw Common::Exception("Vital protoItem widget \"%s\" doesn't exist", tag.c_str());
224 
225  return protoItem;
226 }
227 
228 WidgetButton *GUI::getButton(const Common::UString &tag, bool vital) {
229  Widget *widget = getWidget(tag, vital);
230  if (!widget)
231  return 0;
232 
233  WidgetButton *button = dynamic_cast<WidgetButton *>(widget);
234  if (!button && vital)
235  throw Common::Exception("Vital button widget \"%s\" doesn't exist", tag.c_str());
236 
237  return button;
238 }
239 
241  Widget *widget = getWidget(tag, vital);
242  if (!widget)
243  return 0;
244 
245  WidgetCheckBox *checkBox = dynamic_cast<WidgetCheckBox *>(widget);
246  if (!checkBox && vital)
247  throw Common::Exception("Vital checkBox widget \"%s\" doesn't exist", tag.c_str());
248 
249  return checkBox;
250 }
251 
252 WidgetSlider *GUI::getSlider(const Common::UString &tag, bool vital) {
253  Widget *widget = getWidget(tag, vital);
254  if (!widget)
255  return 0;
256 
257  WidgetSlider *slider = dynamic_cast<WidgetSlider *>(widget);
258  if (!slider && vital)
259  throw Common::Exception("Vital slider widget \"%s\" doesn't exist", tag.c_str());
260 
261  return slider;
262 }
263 
265  Widget *widget = getWidget(tag, vital);
266  if (!widget)
267  return 0;
268 
269  WidgetScrollbar *scrollbar = dynamic_cast<WidgetScrollbar *>(widget);
270  if (!scrollbar && vital)
271  throw Common::Exception("Vital scrollbar widget \"%s\" doesn't exist", tag.c_str());
272 
273  return scrollbar;
274 }
275 
277  Widget *widget = getWidget(tag, vital);
278  if (!widget)
279  return 0;
280 
281  WidgetProgressbar *progressbar = dynamic_cast<WidgetProgressbar *>(widget);
282  if (!progressbar && vital)
283  throw Common::Exception("Vital progressbar widget \"%s\" doesn't exist", tag.c_str());
284 
285  return progressbar;
286 }
287 
289  Widget *widget = getWidget(tag, vital);
290  if (!widget)
291  return 0;
292 
293  WidgetListBox *listBox = dynamic_cast<WidgetListBox *>(widget);
294  if (!listBox && vital)
295  throw Common::Exception("Vital listBox widget \"%s\" doesn't exist", tag.c_str());
296 
297  return listBox;
298 }
299 
300 void GUI::addBackground(const Common::UString &background, bool front) {
301  if (!_background)
302  _background.reset(new GUIBackground(background, front));
303  else
304  _background->setType(background);
305 }
306 
307 void GUI::setCheckBoxState(const Common::UString &tag, bool state) {
308  WidgetCheckBox &checkbox = *getCheckBox(tag, true);
309  checkbox.setState(state);
310 }
311 
313  WidgetCheckBox &checkbox = *getCheckBox(tag, true);
314  return checkbox.getState();
315 }
316 
317 } // End of namespace KotOR
318 
319 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
virtual void initWidget(Widget &widget)
Definition: gui.cpp:189
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
void load(const Common::UString &resref)
Definition: gui.cpp:103
void createWidget(WidgetContext &ctx)
Definition: gui.cpp:158
Widget * getWidget(const Common::UString &tag, bool vital=false)
Return a widget in the GUI.
Definition: gui.cpp:314
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like &#39;DATA&#39;, to ensure portability.
Definition: endianness.h:140
Common::UString _name
Definition: gui.h:139
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
A label widget for Star Wars: Knights of the Old Republic and Jade Empire.
A class holding an UTF-8 string.
Definition: ustring.h:48
WidgetContext(const Aurora::GFF3Struct &s, Widget *p)
Definition: gui.cpp:49
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
virtual void mouseDown()
Definition: gui.cpp:95
A button widget for Star Wars: Knights of the Old Republic and Jade Empire.
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
A checkbox widget for Star Wars: Knights of the Old Repulic and Jade Empire.
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
virtual void mouseUp()
The mouse state has changed.
Definition: gui.cpp:99
GUI(::Engines::Console *console=0)
Definition: gui.cpp:63
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
bool getCheckBoxState(const Common::UString &tag)
Definition: gui.cpp:312
virtual void hide()
Hide the GUI.
Definition: gui.cpp:75
A panel widget for Star Wars: Knights of the Old Republic and Jade Empire.
Exception that provides a stack of explanations.
Definition: error.h:36
float _widgetZ
Definition: gui.h:130
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
A scrollbar widget for Star Wars: Knights of the Old Republic and Jade Empire.
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
virtual void show()
Show the GUI.
Definition: gui.cpp:62
WidgetButton * getButton(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:228
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
WidgetScrollbar * getScrollbar(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:264
A list box widget for Star Wars: Knights of the Old Republic and Jade Empire.
Common::UString getName() const
Definition: gui.cpp:91
const Aurora::GFF3Struct * strct
Definition: gui.h:118
A GFF (generic file format) V3.2/V3.3 file, found in all Aurora games except Sonic Chronicles: The Da...
Definition: gff3file.h:85
void setState(bool state)
Definition: checkbox.cpp:99
float _guiWidth
Definition: gui.h:133
WidgetProtoItem * getProtoItem(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:216
void convertToXoreos(float &x, float &y, const float widgetHeight) const
Converts Kotor&#39; GUI coordinates with a coordinate origin in the upper left corner to the Xoreos coord...
Definition: gui.cpp:81
StackException Exception
Definition: error.h:59
A slider widget for Star Wars: Knights of the Old Republic and Jade Empire.
WidgetSlider * getSlider(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:252
void addBackground(const Common::UString &background, bool front=false)
Definition: gui.cpp:300
void loadWidget(const Aurora::GFF3Struct &strct, Widget *parent)
Definition: gui.cpp:126
A progressbar widget for Star Wars: Knights of the Old Republic and Jade Empire.
#define CursorMan
Shortcut for accessing the cursor manager.
Definition: cursorman.h:129
A KotOR GUI.
Common::ScopedPtr< GUIBackground > _background
Definition: gui.h:135
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
virtual void show()
Show the GUI.
Definition: gui.cpp:69
KotORJadeWidget * widget
Definition: gui.h:123
The Aurora cursor manager.
void convertToGUI(float &x, float &y, const float widgetHeight) const
Converts Xoreos&#39; coordinates with a coordinate origin in the center to Kotor&#39;s GUI coordinates with t...
Definition: gui.cpp:86
WidgetLabel * getLabel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:204
const GFF3List & getList(const Common::UString &field) const
Definition: gff3file.cpp:741
void clearWidgets()
Clear all widgets.
Definition: gui.cpp:295
A widget in a GUI.
Definition: widget.h:40
A struct within a GFF3.
Definition: gff3file.h:164
A protoitem widget for Star Wars: Knights of the Old Republic and Jade Empire.
WidgetProgressbar * getProgressbar(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:276
GUI definition, GFF.
Definition: types.h:113
WidgetCheckBox * getCheckBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:240
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
WidgetListBox * getListBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:288
A KotOR GUI.
Definition: gui.h:57
void addWidget(Widget *widget)
Add a widget.
Definition: gui.cpp:250
float _guiHeight
Definition: gui.h:132
WidgetPanel * getPanel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:192
virtual void load(const Aurora::GFF3Struct &gff)
bool empty()
Check if the gui is currently empty.
Definition: gui.cpp:306
Common::ScopedPtr< Aurora::GFF3File > _gff
Definition: gui.h:137
float getWidth() const
Get the widget&#39;s width.
bool getState() const
Definition: checkbox.cpp:124
virtual void hide()
Hide the GUI.
Definition: gui.cpp:80
The global resource manager for Aurora resources.
void setCheckBoxState(const Common::UString &tag, bool state)
Definition: gui.cpp:307
virtual void setPosition(float x, float y, float z)
Set the widget&#39;s position.
float getHeight() const
Get the widget&#39;s height.