xoreos  0.0.5
charattributes.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/aurora/talkman.h"
26 #include "src/aurora/2dareg.h"
27 #include "src/aurora/2dafile.h"
28 
29 #include "src/common/strutil.h"
30 #include "src/common/util.h"
31 
33 
38 
40 
41 namespace Engines {
42 
43 namespace NWN {
44 
46  CharGenBase(console) {
47 
48  _choices = &choices;
49  load("cg_attributes");
50 
51  getButton("OkButton", true)->setDisabled(true);
52 
53  // Init attribute values.
54  _attributes.assign(6, 8);
55  _attrAdjust.assign(6, 0);
56  _pointLeft = 30;
57 
58  init();
59 }
60 
62 }
63 
65  getEditBox("HelpEdit", true)->setTitle("fnt_galahad14", TalkMan.getString(261));
66  getEditBox("HelpEdit", true)->setText("fnt_galahad14", TalkMan.getString(457));
67 
68  _attributes.assign(6, 8);
69  _attrAdjust.assign(6, 0);
70  _pointLeft = 30;
71 
72  getButton("PointsEdit", true)->setText(Common::composeString<uint32>(_pointLeft));
73  getButton("CostEdit", true)->setText("");
74 
75  getButton("OkButton", true)->setDisabled(true);
76 }
77 
79  if (widget.getTag() == "OkButton") {
81  _returnCode = 2;
82  return;
83  }
84 
85  if (widget.getTag() == "CancelButton") {
86  _returnCode = 1;
87  return;
88  }
89 
90  if (widget.getTag() == "RecommendButton") {
91  setRecommend();
92  return;
93  }
94 
95  const std::vector< WidgetButton* > buttonsList = _attrButtons->getButtonsList();
96 
97  // Update texts when attribute buttons are clicked.
98  if (widget.getTag().endsWith("Label")) {
99  _attrButtons->setActive(dynamic_cast<WidgetButton *>(&widget));
100 
101  for (uint b = 0; b < 6; ++b) {
102  if (buttonsList[b] != &widget)
103  continue;
104 
105  updateText(b);
106  break;
107  }
108  }
109 
110  // Compute and update attributes when any Up and Down attribute buttons are triggered.
111  for (size_t a = 0; a< 6; ++a) {
112  if (buttonsList[a] != widget.getParent())
113  continue;
114 
115  if (widget.getTag().endsWith("UpButton")) {
116  uint cost = pointCost(_attributes[a] + 1);
117  if ((_attributes[a] == 18) || (cost > _pointLeft))
118  break;
119 
120  _pointLeft -= cost;
121  _attributes[a]++;
122  } else if (widget.getTag().endsWith("DownButton")) {
123  if (_attributes[a] == 8)
124  break;
125 
127  _attributes[a]--;
128  }
129 
131  _attrButtons->setActive(a);
132  updateText(a);
133 
134  if (_pointLeft == 0)
135  getButton("OkButton", true)->setDisabled(false);
136  else
137  getButton("OkButton", true)->setDisabled(true);
138 
139  break;
140  }
141 }
142 
144  // Set default text.
145  getEditBox("HelpEdit", true)->setTitle("fnt_galahad14", TalkMan.getString(261));
146  getEditBox("HelpEdit", true)->setText("fnt_galahad14", TalkMan.getString(457));
147 
148  // Get WidgetLabels of the attribute values.
149  _labelAttributes.push_back(getLabel("StrEdit", true));
150  _labelAttributes.push_back(getLabel("DexEdit", true));
151  _labelAttributes.push_back(getLabel("ConEdit", true));
152  _labelAttributes.push_back(getLabel("WisEdit", true));
153  _labelAttributes.push_back(getLabel("IntEdit", true));
154  _labelAttributes.push_back(getLabel("ChaEdit", true));
155 }
156 
158  //TODO Help text should implement racial bonus as well as current attribute value.
159 
160  _attrButtons.reset(new ButtonsGroup(getEditBox("HelpEdit", true)));
161  _attrButtons->addButton(getButton("StrLabel", true), TalkMan.getString(473), TalkMan.getString(459));
162  _attrButtons->addButton(getButton("DexLabel", true), TalkMan.getString(474), TalkMan.getString(460));
163  _attrButtons->addButton(getButton("ConLabel", true), TalkMan.getString(475), TalkMan.getString(461));
164  _attrButtons->addButton(getButton("WisLabel", true), TalkMan.getString(476), TalkMan.getString(462));
165  _attrButtons->addButton(getButton("IntLabel", true), TalkMan.getString(477), TalkMan.getString(463));
166  _attrButtons->addButton(getButton("ChaLabel", true), TalkMan.getString(479), TalkMan.getString(478));
167 
168  // Set buttons to unchanged mode.
169  const std::vector< WidgetButton* > buttonsList = _attrButtons->getButtonsList();
170  for (std::vector<WidgetButton *>::const_iterator it = buttonsList.begin(); it != buttonsList.end(); ++it)
171  (*it)->setMode(WidgetButton::kModeUnchanged);
172 }
173 
175  // Check attribute adjustment from racial type.
176  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("racialtypes");
177  const Aurora::TwoDARow &row = twoda.getRow(_choices->getRace());
178  _attrAdjust.clear();
179  _attrAdjust.push_back(row.getInt(8));
180  _attrAdjust.push_back(row.getInt(9));
181  _attrAdjust.push_back(row.getInt(13));
182  _attrAdjust.push_back(row.getInt(12));
183  _attrAdjust.push_back(row.getInt(10));
184  _attrAdjust.push_back(row.getInt(11));
185 
186  // Update attribute text accordingly.
187  for (size_t it = 0; it < 6; ++it) {
188  genTextAttributes(it);
189  }
190 
191  // Init attribute buttons.
193 
195 }
196 
199 
200  if (_returnCode == 1) {
201  // Get previous choice.
202  uint sum = 0;
203  for (size_t ab = 0; ab < 6; ++ab) {
204  _attributes[ab] = _choices->getAbility(static_cast<Ability>(ab));
205  sum += _attributes[ab];
206  }
207  _pointLeft = 0;
208  getButton("PointsEdit", true)->setText("0");
209 
210  // Attributes were never saved if sum equals 48.
211  if (sum == 48)
212  reset();
213  }
214 }
215 
216 void CharAttributes::genTextAttributes(size_t attribute) {
217  // Compute modifier.
218  uint8 realValue = _attributes[attribute] + _attrAdjust.at(attribute);
219  realValue -= 6;
220  int8 modifier = (realValue - realValue % 2) / 2;
221  modifier -= 2;
222 
223  // Transform numbers into strings.
224  Common::UString sign = (modifier < 0) ? "" : "+";
225  Common::UString attr = Common::composeString<uint8>(_attributes[attribute]
226  + _attrAdjust.at(attribute));
227  Common::UString modifStr = Common::composeString<int8>(modifier);
228 
229  Common::UString output = attr + " (" + sign + modifStr + ")";
230 
231  // Update text.
232  _labelAttributes[attribute]->setText(output);
233 }
234 
236  if (attrValue < 15) {
237  getButton("CostEdit", true)->setText("1");
238  return 1;
239  }
240 
241  attrValue -= 15;
242  uint8 cost = ((attrValue - attrValue % 2) / 2) + 2;
243 
244  getButton("CostEdit", true)->setText(Common::composeString<uint8>(cost));
245 
246  return cost;
247 }
248 
250  getButton("PointsEdit", true)->setText(Common::composeString<uint8>(_pointLeft));
251  pointCost(_attributes[attribute] + 1);
252 }
253 
255  _pointLeft = 0;
256  const Aurora::TwoDAFile &twodaClasses = TwoDAReg.get2DA("classes");
257  const Aurora::TwoDARow &row = twodaClasses.getRow(_choices->getClass());
258  for (uint it = 0; it < 6; ++it) {
259  _attributes.at(it) = row.getInt(17 + it);
260  genTextAttributes(it);
261  }
262 
263  getButton("PointsEdit", true)->setText("0");
264 
265  getButton("OkButton", true)->setDisabled(false);
266 }
267 
268 } // End of namespace NWN
269 
270 } // End of namespace Engines
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void genTextAttributes(size_t attribute)
A NWN editbox widget.
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
A NWN button widget.
uint32 _returnCode
The GUI&#39;s return code.
Definition: gui.h:75
A class holding an UTF-8 string.
Definition: ustring.h:48
uint8_t uint8
Definition: types.h:200
void setText(const Common::UString &text)
void setDisabled(bool disabled)
Disable/Enable the widget.
Definition: button.cpp:107
bool endsWith(const UString &with) const
Definition: ustring.cpp:315
Utility class that handles group of WidgetButton where only one button can be toggled.
Utility templates and functions for working with strings and streams.
std::vector< uint8 > _attrAdjust
void callbackActive(Widget &widget)
Callback that&#39;s triggered when a widget was activated.
WidgetButton * getButton(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:306
void setText(const Common::UString &font, const Common::UString &text, float spacing=0.0f)
Definition: listbox.cpp:523
virtual void show()
Show the GUI.
Definition: gui.cpp:62
WidgetLabel * getLabel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:270
Widget * getParent()
Definition: widget.cpp:102
uint8 getAbility(Ability ability) const
Utility templates and functions.
const Common::UString & getTag() const
Get the widget&#39;s tag.
Definition: widget.cpp:45
Common::ScopedPtr< ButtonsGroup > _attrButtons
CharGenChoices * _choices
Definition: chargenbase.h:45
Handling BioWare&#39;s 2DAs (two-dimensional array).
std::vector< WidgetLabel * > _labelAttributes
A node within a 3D model.
uint8 pointCost(uint8 attrValue)
void load(const Common::UString &resref)
Definition: gui.cpp:77
CharAttributes(CharGenChoices &choices, ::Engines::Console *console=0)
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
The global 2DA registry.
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
void updateText(uint8 attribute)
WidgetEditBox * getEditBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:294
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A widget in a GUI.
Definition: widget.h:40
int8_t int8
Definition: types.h:199
void setTitle(const Common::UString &font, const Common::UString &title)
Set title text if available.
Definition: editbox.cpp:59
The attributes chooser in CharGen.
The global talk manager for Aurora strings.
std::vector< uint8 > _attributes
A row within a 2DA file.
Definition: 2dafile.h:61
void setAbilities(std::vector< uint8 > abilities, std::vector< uint8 > racialAbilities)
virtual void hide()
Hide the GUI.
Definition: gui.cpp:80
A NWN label widget.
unsigned int uint
Definition: types.h:211