xoreos  0.0.5
charskills.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/util.h"
26 #include "src/common/strutil.h"
27 
28 #include "src/aurora/talkman.h"
29 #include "src/aurora/2dareg.h"
30 #include "src/aurora/2dafile.h"
31 
32 #include "src/graphics/graphics.h"
33 
37 
45 
46 namespace Engines {
47 
48 namespace NWN {
49 
51  WidgetListItemButton(gui, "ctl_cg_btn_skill", skill.name, skill.icon),
52  _upButton(0), _downButton(0), _skillPointPanel(0), _skill(skill) {
53 
54  setTag("Item#" + skill.name);
55 
56  // Set maximum width for text.
57  _text->set(skill.name, 230.f);
58 
59  // Up and down buttons.
60  _upButton = new WidgetButton(gui, _tag + "#Up", "ctl_cg_btn_skup");
62  addSub(*_upButton);
63 
64  _downButton = new WidgetButton(gui, _tag + "#Down", "ctl_cg_btn_skdn");
67 
68  _skillPointPanel = new WidgetPanel(gui, _tag + "#SkillPointPanel", "ctl_cg_numbox3");
69  _skillPointPanel->initCaption("fnt_galahad14", "0", 1.0f, 1.0f, 1.0f, 1.0f,
72 }
73 
76 }
77 
78 void WidgetListItemSkill::setPosition(float x, float y, float z) {
80 
81  float pX, pY, pZ;
82  _button->getNode("skillup")->getPosition(pX, pY, pZ);
83  _upButton->setPosition(pX + x, pY + y, z - 5.f);
84  _button->getNode("skilldown")->getPosition(pX, pY, pZ);
85  _downButton->setPosition(pX + x, pY + y, z - 5.f);
86 
87  _button->getNode("skillrank")->getPosition(pX, pY, pZ);
88  _skillPointPanel->setPosition(pX + x, pY + y, z - pZ);
89 }
90 
93  _skillPointPanel->setText(Common::composeString<uint8>(_skill.rank));
94 }
95 
96 void WidgetListItemSkill::mouseDown(uint8 state, float x, float y) {
97  WidgetListItem::mouseDown(state, x, y);
98 
99  uint8 cost = (_skill.isClassSkill) ? 1 : 2;
100  dynamic_cast<CharSkills &>(*_gui).setSkillCost(cost);
101 }
102 
104  select();
105 
106  uint8 cost = (_skill.isClassSkill) ? 1 : 2;
107  dynamic_cast<CharSkills &>(*_gui).setSkillCost(cost);
108  dynamic_cast<CharSkills &>(*_gui).setHelpText(_skill.name, _skill.help);
109 
110  if (widget.getTag().endsWith("#Up"))
111  changeRank(true);
112 
113  if (widget.getTag().endsWith("#Down"))
114  changeRank(false);
115 }
116 
117 bool WidgetListItemSkill::changeRank(bool isIncreasing) {
118  uint8 classRankFactor = (_skill.isClassSkill) ? 1 : 2;
119 
120  if (isIncreasing) {
121  if (_skill.maxRank == _skill.rank)
122  return false;
123  if (!dynamic_cast<CharSkills &>(*_gui).changeAvailableSkillRank(-1 * classRankFactor))
124  return false;
125 
126  ++_skill.rank;
127  } else {
128  if (_skill.rank == _skill.minRank)
129  return false;
130  dynamic_cast<CharSkills &>(*_gui).changeAvailableSkillRank(1 * classRankFactor);
131  --_skill.rank;
132  }
133 
134  _skillPointPanel->setText(Common::composeString<uint8>(_skill.rank));
135  return true;
136 }
137 
139  _choices = &choices;
140  load("cg_skills");
141 
142  _helpTextBox = getEditBox("HelpBox", true);
143 
144  // Adjust panel position.
145  WidgetLabel *costBox = getLabel("CostBox", true);
146  costBox->movePosition(costBox->getWidth(), 0.f, 0.f);
147 
149 
150  getLabel("PtsRemainingBox", true)->setText(Common::composeString<uint8>(_availableSkillRank));
151 
152  // Default help texts.
153  getEditBox("HelpBox", true)->setTitle("fnt_galahad14", TalkMan.getString(58250));
154  getEditBox("HelpBox", true)->setText("fnt_galahad14", TalkMan.getString(58251));
155 }
156 
158 }
159 
161  WidgetListBox *skillBox = getListBox("SkillsButtonBox", true);
162  skillBox->deselect();
163 
164  uint8 spentPoint = 0;
165  for (std::vector<WidgetListItem *>::iterator it = skillBox->begin();
166  it != skillBox->end(); ++it) {
167 
168  WidgetListItemSkill &item = dynamic_cast<WidgetListItemSkill &>(**it);
169  spentPoint += (item._skill.isClassSkill) ? item._skill.rank : item._skill.rank * 2;
170  _choices->setSkill(item._skill.skillID, 0);
171  item.reset();
172  }
173  changeAvailableSkillRank(spentPoint);
174 
175  // Default help texts.
176  getEditBox("HelpBox", true)->setTitle("fnt_galahad14", TalkMan.getString(58250));
177  getEditBox("HelpBox", true)->setText("fnt_galahad14", TalkMan.getString(58251));
178 
180  for (size_t sk = 0; sk < 28; ++sk)
181  _choices->setSkill(sk, 0);
182 }
183 
186 
187  if (_returnCode == 1)
188  reset();
189 }
190 
192  if ((int) _availableSkillRank + changeRank < 0)
193  return false;
194 
195  _availableSkillRank += changeRank;
196  getLabel("PtsRemainingBox", true)->setText(Common::composeString<uint8>(_availableSkillRank));
197  return true;
198 }
199 
201  getLabel("CostBox")->setText(Common::composeString<uint8>(cost));
202 }
203 
205  if (widget.getTag() == "SkillsButtonBox") {
206  WidgetListBox *skillBox = getListBox("SkillsButtonBox", true);
207  WidgetListItemSkill *item = dynamic_cast<WidgetListItemSkill *>(skillBox->getSelectedItem());
208 
209  if (item)
210  setHelpText(item->_skill.name, item->_skill.help);
211  }
212 
213  if (widget.getTag() == "OkButton") {
214  // Retrieve all skills rank.
215  WidgetListBox *skillBox = getListBox("SkillsButtonBox", true);
216  for (std::vector<WidgetListItem *>::iterator it = skillBox->begin();
217  it != skillBox->end(); ++it) {
218  WidgetListItemSkill &item = dynamic_cast<WidgetListItemSkill &>(**it);
219  size_t skillIndex = item._skill.skillID;
220  uint8 skillRank = item._skill.rank;
221 
222  _choices->setSkill(skillIndex, skillRank);
223  }
224 
226 
227  _returnCode = 2;
228  return;
229  }
230 
231  if (widget.getTag() == "CancelButton") {
232  _returnCode = 1;
233  return;
234  }
235 
236  if (widget.getTag() == "RecommendButton") {
237  GfxMan.lockFrame();
239  GfxMan.unlockFrame();
240  return;
241  }
242 }
243 
244 void CharSkills::setHelpText(const Common::UString &title, const Common::UString &text) {
245  _helpTextBox->setTitle("fnt_galahad14", title);
246  _helpTextBox->setText("fnt_galahad14", text);
247 }
248 
251 
252  std::vector<SkillItem> skills;
253  _choices->getSkillItems(skills);
254 
255  WidgetListBox *skillListBox = getListBox("SkillsButtonBox");
257  skillListBox->lock();
258 
259  for (std::vector<SkillItem>::iterator s = skills.begin(); s != skills.end(); ++s)
260  skillListBox->add(new WidgetListItemSkill(*this, *s), true);
261 
262  skillListBox->sortByTag();
263  skillListBox->unlock();
264 }
265 
267  reset();
268 
269  std::vector<uint8> prefSkills;
270  _choices->getPrefSkills(prefSkills);
271 
272  WidgetListBox *listBox = getListBox("SkillsButtonBox");
273 
274  // Fill preferred skills
275  for (std::vector<uint8>::iterator pS = prefSkills.begin(); pS != prefSkills.end(); ++pS) {
276  if (_availableSkillRank == 0)
277  break;
278 
279  for (std::vector<WidgetListItem *>::iterator i = listBox->begin(); i != listBox->end(); ++i) {
280  WidgetListItemSkill &item = dynamic_cast<WidgetListItemSkill &>(**i);
281 
282  if (item._skill.skillID != *pS)
283  continue;
284 
285  // Increase skill rank to the maximum.
286  bool skillFilled = false;
287  while (!skillFilled) {
288  if (!item.changeRank(true))
289  skillFilled = true;
290  }
291 
292  // Continue to the next preferred skill.
293  break;
294  }
295  }
296 }
297 
298 } // End of namespace NWN
299 
300 } // End of namespace Engines
void setSkill(size_t skillIndex, uint8 rank)
A NWN panel widget.
Definition: panel.h:41
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: button.cpp:72
void hide()
Hide the GUI.
Definition: charskills.cpp:184
A NWN editbox widget.
The global graphics manager.
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
A NWN button widget.
void setSkillCost(uint8 cost)
Definition: charskills.cpp:200
uint32 _returnCode
The GUI&#39;s return code.
Definition: gui.h:75
Common::UString help
A class holding an UTF-8 string.
Definition: ustring.h:48
WidgetListBox * getListBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:318
void add(WidgetListItem *item, bool noTag=false)
Definition: listbox.cpp:411
uint8_t uint8
Definition: types.h:200
CharSkills(CharGenChoices &choices, ::Engines::Console *console=0)
Definition: charskills.cpp:138
void setNotUsedSkills(uint8 notUsedSkills)
A text object.
void setText(const Common::UString &text)
void setTag(const Common::UString &tag)
Set the widget&#39;s tag.
bool endsWith(const UString &with) const
Definition: ustring.cpp:315
void remove()
Remove the widget from the GUI.
Definition: widget.cpp:258
std::vector< WidgetListItem * >::iterator begin()
Get begin iterator from all the items in WidgetListBox.
Definition: listbox.cpp:654
WidgetListItem * getSelectedItem() const
Definition: listbox.cpp:630
Utility templates and functions for working with strings and streams.
A NWN listbox widget.
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: charskills.cpp:78
A NWN button widget.
Definition: button.h:39
A GUI.
Definition: gui.h:43
bool changeRank(bool isIncreasing)
Definition: charskills.cpp:117
virtual void mouseDown(uint8 state, float x, float y)
A mouse button was pressed on the widget.
Definition: widget.cpp:191
void setText(const Common::UString &font, const Common::UString &text, float spacing=0.0f)
Definition: listbox.cpp:523
Common::UString _tag
The widget&#39;s tag.
Definition: widget.h:110
WidgetLabel * getLabel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:270
Common::UString name
Utility templates and functions.
const Common::UString & getTag() const
Get the widget&#39;s tag.
Definition: widget.cpp:45
void setMode(Mode mode)
Definition: listbox.cpp:313
CharGenChoices * _choices
Definition: chargenbase.h:45
Handling BioWare&#39;s 2DAs (two-dimensional array).
virtual void initCaption(const Common::UString &font, const Common::UString &text, float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f, float halign=Graphics::Aurora::kHAlignLeft, float valign=Graphics::Aurora::kVAlignTop)
A node within a 3D model.
A 3D model of an object.
void getPrefSkills(std::vector< uint8 > &skills)
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
void load(const Common::UString &resref)
Definition: gui.cpp:77
std::vector< WidgetListItem * >::iterator end()
Get end iterator from all the items in WidgetListBox.
Definition: listbox.cpp:658
A NWN listbox widget.
Definition: listbox.h:116
WidgetEditBox * _helpTextBox
Definition: charskills.h:81
The global 2DA registry.
virtual void movePosition(float x, float y, float z)
Move the widget, relative to its current position.
Definition: widget.cpp:136
virtual void addChild(Widget &widget)
Add a child to the widget.
Definition: widget.cpp:215
WidgetListItemSkill(::Engines::GUI &gui, SkillItem skill)
Definition: charskills.cpp:50
const float kHAlignCenter
Definition: types.h:43
WidgetEditBox * getEditBox(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:294
A NWN label widget.
Definition: label.h:41
const float kVAlignMiddle
Definition: types.h:47
void subActive(Widget &widget)
A sub-widget was activated.
Definition: charskills.cpp:103
bool changeAvailableSkillRank(int rankChange)
Definition: charskills.cpp:191
Common::ScopedPtr< Graphics::Aurora::Text > _text
A widget in a GUI.
Definition: widget.h:40
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: modelwidget.cpp:71
void setTitle(const Common::UString &font, const Common::UString &title)
Set title text if available.
Definition: editbox.cpp:59
The global talk manager for Aurora strings.
A creature in a Neverwinter Nights area.
void mouseDown(uint8 state, float x, float y)
A mouse button was pressed on the widget.
Definition: charskills.cpp:96
The skills selection GUI.
float getWidth() const
Get the widget&#39;s width.
Definition: label.cpp:84
void callbackActive(Widget &widget)
Callback that&#39;s triggered when a widget was activated.
Definition: charskills.cpp:204
virtual void addSub(Widget &widget)
Add a sub-widget to the widget.
Definition: widget.cpp:206
A NWN panel widget.
Common::ScopedPtr< Graphics::Aurora::Model > _button
void getSkillItems(std::vector< SkillItem > &skills)
void setHelpText(const Common::UString &title, const Common::UString &text)
Definition: charskills.cpp:244
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
virtual void hide()
Hide the GUI.
Definition: gui.cpp:80
A NWN label widget.
void setText(const Common::UString &text)
Definition: label.cpp:67