xoreos  0.0.5
listbox.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/system.h"
26 #include "src/common/strutil.h"
27 
28 #include "src/aurora/gff3file.h"
29 
30 #include "src/graphics/graphics.h"
31 
33 
40 
41 namespace Engines {
42 
44  : KotORJadeWidget(gui, tag),
45  _protoItem(0),
46  _scrollbar(0),
47  _itemType(kLBItemTypeDefault),
48  _padding(0),
49  _leftScrollbar(false),
50  _itemSelectionEnabled(false),
51  _adjustHeight(false),
52  _hideScrollbar(true),
53  _selectedIndex(-1),
54  _startIndex(0),
55  _numVisibleItems(0),
56  _textColorChanged(false),
57  _textR(0.0f), _textG(0.0f), _textB(0.0f), _textA(0.0f),
58  _borderColorChanged(false),
59  _borderR(0.0f), _borderG(0.0f), _borderB(0.0f), _borderA(0.0f) {
60 }
61 
63 }
64 
67 
68  _padding = gff.getSint("PADDING");
69  _leftScrollbar = gff.getBool("LEFTSCROLLBAR");
70 
71  if (gff.hasField("SCROLLBAR"))
72  createScrollbar(gff.getStruct("SCROLLBAR"));
73 
74  if (gff.hasField("PROTOITEM"))
75  _protoItem = &gff.getStruct("PROTOITEM");
76 }
77 
79  _itemType = itemType;
80 }
81 
82 void WidgetListBox::setItemSelectionEnabled(bool itemSelectionEnabled) {
83  _itemSelectionEnabled = itemSelectionEnabled;
85  _selectedIndex = -1;
86 
88 }
89 
90 void WidgetListBox::setAdjustHeight(bool adjustHeight) {
91  _adjustHeight = adjustHeight;
92 }
93 
94 void WidgetListBox::setHideScrollbar(bool hideScrollbar) {
95  _hideScrollbar = hideScrollbar;
96 }
97 
99  _padding = padding;
100 }
101 
102 void WidgetListBox::setItemTextColor(float r, float g, float b, float a) {
103  _textColorChanged = true;
104  _textR = r;
105  _textG = g;
106  _textB = b;
107  _textA = a;
108 
110 }
111 
112 void WidgetListBox::setItemBorderColor(float r, float g, float b, float a) {
113  _borderColorChanged = true;
114  _borderR = r;
115  _borderG = g;
116  _borderB = b;
117  _borderA = a;
118 
120 }
121 
123  _items.push_back(contents);
124 }
125 
127  _startIndex = 0;
128  _items.clear();
129 }
130 
132  if ((!_protoItem) || (!_itemWidgets.empty()))
133  return;
134 
135  for (uint32 i = 0; i < count; ++i) {
136  Common::UString tag = Common::UString::format("%s_ITEM_%u", _tag.c_str(), i);
137  WidgetProtoItem *item;
138 
139  switch (_itemType) {
141  item = new KotORInventoryItem(*_gui, tag);
142  break;
143  case kLBItemTypeDefault:
144  default:
145  item = new WidgetProtoItem(*_gui, tag, this);
146  break;
147  }
148 
149  item->load(*_protoItem);
150 
151  addChild(*item);
152  addSub(*item);
153 
154  _itemWidgets.push_back(item);
155  }
156 
159 }
160 
162  if (_itemWidgets.empty())
163  return;
164 
165  _numVisibleItems = 0;
166  float totalHeight = 0;
167 
168  float x, y, z;
169  getPosition(x, y, z);
170  y += _height;
171 
172  GfxMan.lockFrame();
173 
174  bool heightExceeded = false;
175 
176  for (size_t i = 0; i < _itemWidgets.size(); ++i) {
177  WidgetProtoItem *itemWidget = _itemWidgets[i];
178  bool visible = false;
179 
180  if (!heightExceeded) {
181  int itemIndex = _startIndex + i;
182  if (itemIndex < (int)_items.size()) { // have item to display?
183  Common::UString &contents = _items[itemIndex];
184 
185  if (_adjustHeight) {
186  float textHeight = itemWidget->getTextHeight(contents);
187  if (totalHeight + textHeight > getHeight())
188  heightExceeded = true;
189  else {
190  float iX, iY, iZ;
191  itemWidget->getPosition(iX, iY, iZ);
192  itemWidget->setPosition(iX, y -= textHeight + _padding, iZ);
193  itemWidget->setHeight(textHeight);
194  totalHeight += textHeight + _padding;
195  }
196  }
197 
198  if (!heightExceeded) {
199  itemWidget->setContents(contents);
200  itemWidget->setHighlight(itemIndex == _selectedIndex);
201  visible = true;
202  }
203  }
204  }
205 
206  if (visible) {
207  itemWidget->setInvisible(false);
208 
209  if (isVisible())
210  itemWidget->show();
212  } else {
213  if (isVisible())
214  itemWidget->hide();
215  itemWidget->setInvisible(true);
216  }
217  }
218 
219  if (_hideScrollbar) {
220  if (_numVisibleItems < (int)_items.size()) {
221  _scrollbar->setInvisible(false);
222  if (isVisible())
223  _scrollbar->show();
224  } else {
225  if (isVisible())
226  _scrollbar->hide();
227  _scrollbar->setInvisible(true);
228  }
229  }
230 
231  GfxMan.unlockFrame();
232 }
233 
235  if (!tag.beginsWith(_tag + "_ITEM_"))
236  return;
237 
238  Common::UString tmp(tag);
239  tmp.replaceAll(_tag + "_ITEM_", "");
240 
241  int index = -1;
242  Common::parseString(tmp, index);
243 
244  if ((index >= 0) && (_selectedIndex != _startIndex + index)) {
245  _selectedIndex = _startIndex + index;
248  }
249 }
250 
252  if (index < 0 || static_cast<size_t>(index) >= _items.size())
253  return;
254 
255  _selectedIndex = index;
256 }
257 
259  if (_itemSelectionEnabled) {
260  bool selectionChanged = false;
261 
262  if ((_selectedIndex < 0) && (!_items.empty())) {
263  _selectedIndex = 0;
264  selectionChanged = true;
266  } else if (_selectedIndex < (int)_items.size() - 1) {
267  ++_selectedIndex;
270  selectionChanged = true;
272  }
273 
274  if (selectionChanged)
276  } else if (_startIndex + _numVisibleItems < (int)_items.size()) {
277  ++_startIndex;
279  }
280 }
281 
283  if (_itemSelectionEnabled) {
284  bool selectionChanged = false;
285 
286  if ((_selectedIndex < 0) && (!_items.empty())) {
287  _selectedIndex = 0;
288  selectionChanged = true;
290  } else if (_selectedIndex > 0) {
291  --_selectedIndex;
294  selectionChanged = true;
296  }
297 
298  if (selectionChanged)
300  } else if (_startIndex > 0) {
301  --_startIndex;
303  }
304 }
305 
307  return _selectedIndex;
308 }
309 
310 void WidgetListBox::setHeight(float height) {
311  float deltaHeight = height - _height;
312 
314 
315  if (_scrollbar) {
316  height = _scrollbar->getHeight();
317  _scrollbar->setHeight(height + deltaHeight);
318  }
319 }
320 
323  selectItemByWidgetTag(widget.getTag());
324  else
325  raiseCallbackActive(widget);
326 }
327 
329  _soundSelectItem = resRef;
331 }
332 
334  _soundHoverItem = resRef;
336 }
337 
339  _soundClickItem = resRef;
341 }
342 
344  _scrollbar = new WidgetScrollbar(*_gui, _tag + "#" + gff.getString("TAG"));
345  _scrollbar->load(gff);
346 
347  float x, y, z;
348  getPosition(x, y, z);
349 
350  if (_leftScrollbar)
351  _scrollbar->setPosition(x, y, -1.0f);
352  else
353  _scrollbar->setPosition(x + _width - _scrollbar->getWidth(), y, -1.0f);
354 
356 
358  addSub(*_scrollbar);
359 }
360 
362  float x, y, z;
363  getPosition(x, y, z);
364 
365  if (_scrollbar && _leftScrollbar)
367 
368  y += _height;
369  z -= 1.0f;
370 
371  size_t count = _itemWidgets.size();
372  for (size_t i = 0; i < count; ++i) {
373  _itemWidgets[i]->setPosition(x, y -= _itemWidgets[i]->getHeight() + _padding, z);
374  }
375 }
376 
378  for (size_t i = 0; i < _itemWidgets.size(); ++i) {
379  _itemWidgets[i]->setDisableHighlight(_itemSelectionEnabled);
380 
381  if (_textColorChanged)
382  _itemWidgets[i]->setTextColor(_textR, _textG, _textB, _textA);
383 
385  _itemWidgets[i]->setBorderColor(_borderR, _borderG, _borderB, _borderA);
386 
387  _itemWidgets[i]->setSoundHover(_soundHoverItem);
388  _itemWidgets[i]->setSoundClick(_soundClickItem);
389  }
390 }
391 
392 void WidgetListBox::mouseWheel(uint8 UNUSED(state), int UNUSED(x), int y) {
393  if (y == 0 || !_adjustHeight)
394  return;
395 
396  _startIndex = MIN(
397  MAX(_startIndex - y, 0),
398  MAX<int>(_itemWidgets.size() - _numVisibleItems, 0)
399  );
401 }
402 
403 } // End of namespace Engines
int64 getSint(const Common::UString &field, int64 def=0) const
Definition: gff3file.cpp:473
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
virtual void setHeight(float height)
Set the height of the widget.
The global graphics manager.
WidgetListBox(GUI &gui, const Common::UString &tag)
Definition: listbox.cpp:43
void mouseWheel(uint8 state, int x, int y)
A mouse wheel was used on the widget.
Definition: listbox.cpp:392
A label widget for Star Wars: Knights of the Old Republic and Jade Empire.
bool getBool(const Common::UString &field, bool def=false) const
Definition: gff3file.cpp:510
virtual void hide()
Hide the widget.
A class holding an UTF-8 string.
Definition: ustring.h:48
void createItemWidgets(uint32 count)
Definition: listbox.cpp:131
WidgetScrollbar * _scrollbar
Definition: listbox.h:107
uint8_t uint8
Definition: types.h:200
bool beginsWith(const UString &with) const
Definition: ustring.cpp:295
A button widget for Star Wars: Knights of the Old Republic and Jade Empire.
void setHighlight(const Common::UString &hilight)
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
bool isVisible() const
Is the widget visible?
Definition: widget.cpp:59
void setSoundClickItem(const Common::UString &resRef)
Definition: listbox.cpp:338
Common::UString _soundClickItem
Definition: listbox.h:129
GUI * _gui
The GUI the widget belongs to.
Definition: widget.h:108
void setPadding(uint32 padding)
Definition: listbox.cpp:98
Utility templates and functions for working with strings and streams.
const Aurora::GFF3Struct * _protoItem
Definition: listbox.h:106
void load(const Aurora::GFF3Struct &gff)
Definition: scrollbar.cpp:44
A scrollbar widget for Star Wars: Knights of the Old Republic and Jade Empire.
Common::UString _soundHoverItem
Definition: listbox.h:128
A GUI.
Definition: gui.h:43
void raiseCallbackActive(Widget &widget)
Definition: widget.cpp:281
Common::UString _tag
The widget&#39;s tag.
Definition: widget.h:110
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
void replaceAll(uint32 what, uint32 with)
Replace all occurrences of a character with another character.
Definition: ustring.cpp:435
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
#define UNUSED(x)
Definition: system.h:170
void setItemTextColor(float r, float g, float b, float a)
Definition: listbox.cpp:102
float getTextHeight(const Common::UString &text) const
virtual void show()
Show the widget.
const Common::UString & getTag() const
Get the widget&#39;s tag.
Definition: widget.cpp:45
A list box widget for Star Wars: Knights of the Old Republic and Jade Empire.
void setItemSelectionEnabled(bool itemSelectionEnabled)
Toggle item selection mode.
Definition: listbox.cpp:82
T MIN(T a, T b)
Definition: util.h:70
void addItem(const Common::UString &contents)
Definition: listbox.cpp:122
void selectItemByWidgetTag(const Common::UString &tag)
Definition: listbox.cpp:234
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: scrollbar.cpp:119
virtual void addChild(Widget &widget)
Add a child to the widget.
Definition: widget.cpp:215
void subActive(Widget &widget)
A sub-widget was activated.
Definition: listbox.cpp:321
Common::UString _soundSelectItem
Definition: listbox.h:127
void setHideScrollbar(bool hideScrollbar)
Toggle scroll bar visibility mode.
Definition: listbox.cpp:94
void selectItemByIndex(int index)
Definition: listbox.cpp:251
void setAdjustHeight(bool adjustHeight)
Toggle height adjustment mode.
Definition: listbox.cpp:90
Sound::ChannelHandle playSound(const Common::UString &sound, Sound::SoundType soundType, bool loop, float volume, bool pitchVariance)
Play this sound resource.
Definition: util.cpp:81
void setItemBorderColor(float r, float g, float b, float a)
Definition: listbox.cpp:112
void show()
Show the widget.
Definition: scrollbar.cpp:91
void hide()
Hide the widget.
Definition: scrollbar.cpp:105
virtual void setContents(const Common::UString &contents)
Set item contents.
Definition: protoitem.cpp:44
void setHeight(float height)
Set the height of the widget.
Definition: scrollbar.cpp:169
A widget in a GUI.
Definition: widget.h:40
ListBoxItemType _itemType
Definition: listbox.h:111
A struct within a GFF3.
Definition: gff3file.h:164
uint32_t uint32
Definition: types.h:204
void load(const Aurora::GFF3Struct &gff)
Definition: listbox.cpp:65
const GFF3Struct & getStruct(const Common::UString &field) const
Definition: gff3file.cpp:728
A protoitem widget for Star Wars: Knights of the Old Republic and Jade Empire.
virtual void setInvisible(bool invisible)
Make the widget invisible.
Low-level detection of architecture/system properties.
void createScrollbar(const Aurora::GFF3Struct &gff)
Definition: listbox.cpp:343
An inventory item widget for Star Wars: Knights of the Old Republic.
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Sound effect.
Definition: types.h:45
Generic Aurora engines utility functions.
std::vector< WidgetProtoItem * > _itemWidgets
Definition: listbox.h:108
void setHeight(float height)
Set the height of the widget.
Definition: listbox.cpp:310
void setItemType(ListBoxItemType itemType)
Definition: listbox.cpp:78
void applyChangesToItemWidgets()
Definition: listbox.cpp:377
T MAX(T a, T b)
Definition: util.h:71
virtual void load(const Aurora::GFF3Struct &gff)
virtual void addSub(Widget &widget)
Add a sub-widget to the widget.
Definition: widget.cpp:206
float getWidth() const
Get the widget&#39;s width.
std::vector< Common::UString > _items
Definition: listbox.h:109
void setSoundHoverItem(const Common::UString &resRef)
Definition: listbox.cpp:333
void parseString(const UString &str, T &value, bool allowEmpty)
Parse a string into any POD integer, float/double or bool type.
Definition: strutil.cpp:215
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
virtual void setPosition(float x, float y, float z)
Set the widget&#39;s position.
float getHeight() const
Get the widget&#39;s height.
int getSelectedIndex() const
Definition: listbox.cpp:306
void setSoundSelectItem(const Common::UString &resRef)
Definition: listbox.cpp:328
ListBoxItemType
Definition: listbox.h:35