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 Jade {
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.setState("down");
97 }
98 
99 void GUI::mouseUp() {
100  CursorMan.setState("up");
101 }
102 
103 void GUI::load(const Common::UString &resref) {
104  // This is only relevant to Jade Empire.
105  // LTI prefixed GUI definitions for the Windows version of Jade Empire
106  // with lti_ to support mouse and keyboard control.
107  _name = ResMan.hasResource("lti_" + resref, Aurora::kFileTypeGUI) ? _name = "lti_" + resref : resref;
108 
109  try {
110  _gff.reset(new Aurora::GFF3File(_name, Aurora::kFileTypeGUI, MKTAG('G', 'U', 'I', ' ')));
111 
112  loadWidget(_gff->getTopLevel(), 0);
113 
114  } catch (Common::Exception &e) {
115  e.add("Can't load GUI \"%s\"", _name.c_str());
116  throw;
117  }
118 }
119 
120 void GUI::loadWidget(const Aurora::GFF3Struct &strct, Widget *parent) {
121 
122  WidgetContext ctx(strct, parent);
123 
124  createWidget(ctx);
125 
126  if (_guiWidth <= 0.0f)
127  _guiWidth = ctx.widget->getWidth();
128  if (_guiHeight <= 0.0f)
129  _guiHeight = ctx.widget->getHeight();
130 
131  float wX, wY, wZ;
132  ctx.widget->getPosition(wX, wY, wZ);
133 
134  convertToXoreos(wX, wY, ctx.widget->getHeight());
135  wZ = _widgetZ + wZ;
136 
137  ctx.widget->setPosition(wX, wY, wZ);
138 
139  _widgetZ -= 1.0f;
140 
141  addWidget(ctx.widget);
142 
143  // Go down to the children
144  if (ctx.strct->hasField("CONTROLS")) {
145  const Aurora::GFF3List &children = ctx.strct->getList("CONTROLS");
146 
147  for (Aurora::GFF3List::const_iterator c = children.begin(); c != children.end(); ++c)
148  loadWidget(**c, ctx.widget);
149  }
150 }
151 
153  if (ctx.type == kWidgetTypePanel)
154  ctx.widget = new WidgetPanel(*this, ctx.tag);
155  else if (ctx.type == kWidgetTypeLabel)
156  ctx.widget = new WidgetLabel(*this, ctx.tag);
157  else if (ctx.type == kWidgetTypeProtoItem)
158  ctx.widget = new WidgetProtoItem(*this, ctx.tag);
159  else if (ctx.type == kWidgetTypeButton)
160  ctx.widget = new WidgetButton(*this, ctx.tag);
161  else if (ctx.type == kWidgetTypeCheckBox)
162  ctx.widget = new WidgetCheckBox(*this, ctx.tag);
163  else if (ctx.type == kWidgetTypeSlider)
164  ctx.widget = new WidgetSlider(*this, ctx.tag);
165  else if (ctx.type == kWidgetTypeScrollbar)
166  ctx.widget = new WidgetScrollbar(*this, ctx.tag);
167  else if (ctx.type == kWidgetTypeProgressbar)
168  ctx.widget = new WidgetProgressbar(*this, ctx.tag);
169  else if (ctx.type == kWidgetTypeListBox)
170  ctx.widget = new WidgetListBox(*this, ctx.tag);
171  else
172  throw Common::Exception("No such widget type %d", ctx.type);
173 
174  ctx.widget->load(*ctx.strct);
175 
176  initWidget(*ctx.widget);
177 }
178 
179 void GUI::initWidget(Widget &UNUSED(widget)) {
180 }
181 
182 WidgetPanel *GUI::getPanel(const Common::UString &tag, bool vital) {
183  Widget *widget = getWidget(tag, vital);
184  if (!widget)
185  return 0;
186 
187  WidgetPanel *panel = dynamic_cast<WidgetPanel *>(widget);
188  if (!panel && vital)
189  throw Common::Exception("Vital panel widget \"%s\" doesn't exist", tag.c_str());
190 
191  return panel;
192 }
193 
194 WidgetLabel *GUI::getLabel(const Common::UString &tag, bool vital) {
195  Widget *widget = getWidget(tag, vital);
196  if (!widget)
197  return 0;
198 
199  WidgetLabel *label = dynamic_cast<WidgetLabel *>(widget);
200  if (!label && vital)
201  throw Common::Exception("Vital label widget \"%s\" doesn't exist", tag.c_str());
202 
203  return label;
204 }
205 
207  Widget *widget = getWidget(tag, vital);
208  if (!widget)
209  return 0;
210 
211  WidgetProtoItem *protoItem = dynamic_cast<WidgetProtoItem *>(widget);
212  if (!protoItem && vital)
213  throw Common::Exception("Vital protoItem widget \"%s\" doesn't exist", tag.c_str());
214 
215  return protoItem;
216 }
217 
218 WidgetButton *GUI::getButton(const Common::UString &tag, bool vital) {
219  Widget *widget = getWidget(tag, vital);
220  if (!widget)
221  return 0;
222 
223  WidgetButton *button = dynamic_cast<WidgetButton *>(widget);
224  if (!button && vital)
225  throw Common::Exception("Vital button widget \"%s\" doesn't exist", tag.c_str());
226 
227  return button;
228 }
229 
231  Widget *widget = getWidget(tag, vital);
232  if (!widget)
233  return 0;
234 
235  WidgetCheckBox *checkBox = dynamic_cast<WidgetCheckBox *>(widget);
236  if (!checkBox && vital)
237  throw Common::Exception("Vital checkBox widget \"%s\" doesn't exist", tag.c_str());
238 
239  return checkBox;
240 }
241 
242 WidgetSlider *GUI::getSlider(const Common::UString &tag, bool vital) {
243  Widget *widget = getWidget(tag, vital);
244  if (!widget)
245  return 0;
246 
247  WidgetSlider *slider = dynamic_cast<WidgetSlider *>(widget);
248  if (!slider && vital)
249  throw Common::Exception("Vital slider widget \"%s\" doesn't exist", tag.c_str());
250 
251  return slider;
252 }
253 
255  Widget *widget = getWidget(tag, vital);
256  if (!widget)
257  return 0;
258 
259  WidgetScrollbar *scrollbar = dynamic_cast<WidgetScrollbar *>(widget);
260  if (!scrollbar && vital)
261  throw Common::Exception("Vital scrollbar widget \"%s\" doesn't exist", tag.c_str());
262 
263  return scrollbar;
264 }
265 
267  Widget *widget = getWidget(tag, vital);
268  if (!widget)
269  return 0;
270 
271  WidgetProgressbar *progressbar = dynamic_cast<WidgetProgressbar *>(widget);
272  if (!progressbar && vital)
273  throw Common::Exception("Vital progressbar widget \"%s\" doesn't exist", tag.c_str());
274 
275  return progressbar;
276 }
277 
279  Widget *widget = getWidget(tag, vital);
280  if (!widget)
281  return 0;
282 
283  WidgetListBox *listBox = dynamic_cast<WidgetListBox *>(widget);
284  if (!listBox && vital)
285  throw Common::Exception("Vital listBox widget \"%s\" doesn't exist", tag.c_str());
286 
287  return listBox;
288 }
289 
290 void GUI::addBackground(const Common::UString &background, bool front) {
291  if (!_background)
292  _background.reset(new GUIBackground(background, front));
293  else
294  _background->setType(background);
295 }
296 
297 void GUI::setCheckBoxState(const Common::UString &tag, bool state) {
298  WidgetCheckBox &checkbox = *getCheckBox(tag, true);
299  checkbox.setState(state);
300 }
301 
303  WidgetCheckBox &checkbox = *getCheckBox(tag, true);
304  return checkbox.getState();
305 }
306 
307 } // End of namespace Jade
308 
309 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
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
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
WidgetButton * getButton(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:218
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
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.
WidgetScrollbar * getScrollbar(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:254
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
Common::UString _name
Definition: gui.h:139
void loadWidget(const Aurora::GFF3Struct &strct, Widget *parent)
Definition: gui.cpp:120
virtual void hide()
Hide the GUI.
Definition: gui.cpp:75
const Aurora::GFF3Struct * strct
Definition: gui.h:118
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
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
Common::UString getName() const
Definition: gui.cpp:91
KotORJadeWidget * widget
Definition: gui.h:123
A scrollbar widget for Star Wars: Knights of the Old Republic and Jade Empire.
WidgetContext(const Aurora::GFF3Struct &s, Widget *p)
Definition: gui.cpp:49
WidgetProtoItem * getProtoItem(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:206
Basic exceptions to throw.
float _guiWidth
Definition: gui.h:133
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
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
void addBackground(const Common::UString &background, bool front=false)
Definition: gui.cpp:290
A Jade Empire GUI.
Definition: gui.h:57
WidgetSlider * getSlider(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:242
A list box widget for Star Wars: Knights of the Old Republic and Jade Empire.
GUI(::Engines::Console *console=0)
Definition: gui.cpp:63
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
StackException Exception
Definition: error.h:59
A slider widget for Star Wars: Knights of the Old Republic and Jade Empire.
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
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
Common::ScopedPtr< Aurora::GFF3File > _gff
Definition: gui.h:137
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 Jade Empire&#39;s GUI coordinates ...
Definition: gui.cpp:86
A Jade Empire GUI.
const GFF3List & getList(const Common::UString &field) const
Definition: gff3file.cpp:741
void load(const Common::UString &resref)
Definition: gui.cpp:103
virtual void show()
Show the GUI.
Definition: gui.cpp:69
A widget in a GUI.
Definition: widget.h:40
void setCheckBoxState(const Common::UString &tag, bool state)
Definition: gui.cpp:297
void createWidget(WidgetContext &ctx)
Definition: gui.cpp:152
A struct within a GFF3.
Definition: gff3file.h:164
virtual void mouseDown()
Definition: gui.cpp:95
virtual void initWidget(Widget &widget)
Definition: gui.cpp:179
void convertToXoreos(float &x, float &y, const float widgetHeight) const
Converts Jade Empire&#39; GUI coordinates with a coordinate origin in the upper left corner to the Xoreos...
Definition: gui.cpp:81
A protoitem widget for Star Wars: Knights of the Old Republic and Jade Empire.
float _widgetZ
Definition: gui.h:130
WidgetListBox * getListBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:278
GUI definition, GFF.
Definition: types.h:113
WidgetCheckBox * getCheckBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:230
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Common::ScopedPtr< GUIBackground > _background
Definition: gui.h:135
void addWidget(Widget *widget)
Add a widget.
Definition: gui.cpp:250
virtual void mouseUp()
The mouse state has changed.
Definition: gui.cpp:99
virtual void load(const Aurora::GFF3Struct &gff)
bool getCheckBoxState(const Common::UString &tag)
Definition: gui.cpp:302
WidgetPanel * getPanel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:182
float getWidth() const
Get the widget&#39;s width.
WidgetProgressbar * getProgressbar(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:266
bool getState() const
Definition: checkbox.cpp:124
virtual void hide()
Hide the GUI.
Definition: gui.cpp:80
The global resource manager for Aurora resources.
float _guiHeight
Definition: gui.h:132
virtual void setPosition(float x, float y, float z)
Set the widget&#39;s position.
float getHeight() const
Get the widget&#39;s height.
WidgetLabel * getLabel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:194