xoreos  0.0.5
partyselection.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/strutil.h"
26 
27 #include "src/aurora/2dareg.h"
28 #include "src/aurora/2dafile.h"
29 
32 
36 
38 
39 static const int kSlotCountK1 = 9;
40 static const int kSlotCountK2 = 10;
41 
42 namespace Engines {
43 
44 namespace KotOR {
45 
47  : _kotor2(k2),
48  _numSelectedSlots(0),
49  _activeSlot(-1) {
51  load("partyselection");
52 }
53 
55  _config = config;
57  _activeSlot = -1;
58 
59  if (_config.forceNPC1 != -1) {
62  }
63 
64  if (_config.forceNPC2 != -1) {
67  }
68 
69  const int slotCount = (_kotor2 ? kSlotCountK2 : kSlotCountK1);
70 
71  for (int i = 0; i < slotCount; ++i) {
72  if (_config.slotTemplate[i].empty())
73  toggleSlot(i, false);
74  else {
75  const Common::UString portrait(getPortrait(_config.slotTemplate[i]));
76  setSlotTexture(i, portrait);
77  toggleSlot(i, true);
78  }
79 
80  WidgetCheckBox *checkBox = getCheckBox("BTN_NPC" + Common::composeString(i));
81  const bool selected = _config.slotSelected[i];
82  checkBox->setState(selected);
83  checkBox->setHighlight(selected);
84  }
85 
86  getButton("BTN_BACK")->setDisabled(!config.canCancel);
87 
89 }
90 
92  const Common::UString &tag = widget.getTag();
93  if (tag == "BTN_BACK")
94  _returnCode = 1;
95  else if (tag == "BTN_DONE")
96  _returnCode = 2;
97  else if (tag.beginsWith("BTN_NPC")) {
98  Common::UString tmp(tag);
99  tmp.replaceAll("BTN_NPC", "");
100  int index;
101  Common::parseString(tmp, index);
102  _activeSlot = index;
104 
105  } else if (tag == "BTN_ACCEPT") {
106  if ((_activeSlot != _config.forceNPC1) &&
107  (_activeSlot != _config.forceNPC2)) {;
109  if (_numSelectedSlots < 2) {
112  }
113  } else {
116  }
117  }
118 
120  }
121 }
122 
126  MKTAG('U', 'T', 'C', ' ')));
127 
128  if (!utc)
129  throw Common::Exception("Invalid creature template: %s", templ.c_str());
130 
131  const Aurora::GFF3Struct &gff = utc->getTopLevel();
132  Common::UString portrait;
133 
134  uint32 portraitId = gff.getUint("PortraitId");
135  if (portraitId != 0) {
136  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("portraits");
137  portrait = twoda.getRow(portraitId).getString("BaseResRef");
138  }
139 
140  return gff.getString("Portrait", portrait);
141 }
142 
143 void PartySelectionGUI::setSlotTexture(int index, const Common::UString &texture) {
144  WidgetCheckBox *checkBox = getCheckBox("BTN_NPC" + Common::composeString(index));
145  checkBox->setFill(texture);
146  checkBox->setSelected(texture);
147  checkBox->setUnselected(texture);
148  checkBox->setSelectedHighlighted(texture);
149  checkBox->setUnselectedHighlighted(texture);
150 }
151 
152 void PartySelectionGUI::toggleSlot(int index, bool enabled) {
153  getLabel("LBL_NA" + Common::composeString(index))->setInvisible(enabled);
154  getLabel("LBL_CHAR" + Common::composeString(index))->setInvisible(enabled);
155 }
156 
159 
160  const int slotCount = (_kotor2 ? kSlotCountK2 : kSlotCountK1);
161 
162  for (int i = 0; i < slotCount; ++i) {
163  WidgetCheckBox *checkBox = getCheckBox("BTN_NPC" + Common::composeString(i));
164 
165  if (_config.slotSelected[i])
166  checkBox->setBorderColor(0.0f, 1.0f, 0.0f, 1.0f);
167  else if (_activeSlot == i)
168  checkBox->setBorderColor(1.0f, 1.0f, 0.0f, 1.0f);
169  else
170  checkBox->setBorderColor(0.0f, 0.0f, 0.0f, 0.0f);
171  }
172 
173  const bool activeSlotSelected = (_activeSlot != -1) && (_config.slotSelected[_activeSlot]);
174  getButton("BTN_ACCEPT")->setText(activeSlotSelected ? "Remove" : "Add");
175 }
176 
177 } // End of namespace KotOR
178 
179 } // End of namespace Engines
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void load(const Common::UString &resref)
Definition: gui.cpp:103
#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 setSelected(const Common::UString &selected)
Definition: checkbox.cpp:175
void setFill(const Common::UString &fill)
static const Common::UString & kBackgroundTypeMenu
Definition: guibackground.h:42
A label widget for Star Wars: Knights of the Old Republic and Jade Empire.
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
uint32 _returnCode
The GUI&#39;s return code.
Definition: gui.h:75
A class holding an UTF-8 string.
Definition: ustring.h:48
UString composeString(T value)
Convert any POD integer, float/double or bool type into a string.
Definition: strutil.cpp:276
bool beginsWith(const UString &with) const
Definition: ustring.cpp:295
void loadConfiguration(const PartyConfiguration &config)
A button widget for Star Wars: Knights of the Old Republic and Jade Empire.
void setHighlight(const Common::UString &hilight)
virtual void setDisabled(bool disabled)
Disable/Enable the widget.
Definition: widget.cpp:154
A checkbox widget for Star Wars: Knights of the Old Repulic and Jade Empire.
Aurora::GFF3File * loadOptionalGFF3(const Common::UString &gff3, Aurora::FileType type, uint32 id, bool repairNWNPremium)
Load a GFF3, but return 0 instead of throwing on error.
Definition: util.cpp:150
static const int kSlotCountK1
void setUnselectedHighlighted(const Common::UString &unselectedHighlighted)
Definition: checkbox.cpp:187
Utility templates and functions for working with strings and streams.
const Common::UString getPortrait(const Common::UString &templ)
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
void setSlotTexture(int index, const Common::UString &portrait)
WidgetButton * getButton(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:228
Common::UString slotTemplate[10]
Definition: partyconfig.h:37
const Common::UString & getTag() const
Get the widget&#39;s tag.
Definition: widget.cpp:45
Handling BioWare&#39;s 2DAs (two-dimensional array).
void callbackActive(Widget &widget)
Callback that&#39;s triggered when a widget was activated.
void setState(bool state)
Definition: checkbox.cpp:99
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
StackException Exception
Definition: error.h:59
The global 2DA registry.
void addBackground(const Common::UString &background, bool front=false)
Definition: gui.cpp:300
void setUnselected(const Common::UString &unselected)
Definition: checkbox.cpp:179
A widget in a GUI.
void toggleSlot(int index, bool enabled)
WidgetLabel * getLabel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:204
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A widget in a GUI.
Definition: widget.h:40
A struct within a GFF3.
Definition: gff3file.h:164
uint32_t uint32
Definition: types.h:204
static const int kSlotCountK2
virtual void setInvisible(bool invisible)
Make the widget invisible.
Party selection GUI for Star Wars: Knights of the Old Republic.
WidgetCheckBox * getCheckBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:240
void setText(const Common::UString &text)
Generic Aurora engines utility functions.
void setBorderColor(float r, float g, float b, float a)
void setSelectedHighlighted(const Common::UString &selectedHighlighted)
Definition: checkbox.cpp:183
Creature template (user), GFF.
Definition: types.h:93
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