xoreos  0.0.5
charportrait.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/2dareg.h"
26 #include "src/aurora/2dafile.h"
27 
29 
31 
36 
38 
39 namespace Engines {
40 
41 namespace NWN {
42 
44  WidgetListItem(gui) {
45 
46  _portrait = portrait;
47 
48  _portraitWidget = new PortraitWidget(gui, "portrait#" + portrait, portrait + "m",
49  Portrait::kSizeMedium, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
50 
53 }
54 
57 }
58 
60  return _portraitWidget->getHeight();
61 }
62 
64  return _portraitWidget->getWidth();
65 }
66 
67 void WidgetGridItemPortrait::mouseDown(uint8 state, float x, float y) {
68  activate();
69 
70  dynamic_cast<CharPortrait &>(*_gui).setMainTexture(_portrait);
71 
72  _owner->mouseDown(state, x, y);
73 }
74 
77  return false;
78 
79  _portraitWidget->setBorderColor(1.0f, 1.0f, 1.0f, 1.0f);
80  return true;
81 }
84  return false;
85 
86  _portraitWidget->setBorderColor(1.0f, 1.0f, 1.0f, 0.0f);
87  return true;
88 }
89 
91  CharGenBase(console), _selectedPortrait("gui_po_nwnlogo_") {
92 
93  _choices = &choices;
94  load("cg_portrait");
95 
96  // Init portraits widget.
97  float pX, pY, pZ;
98  getPanel("PNL_CHR_GENM", true)->getNode("portraits")->getPosition(pX, pY, pZ);
99 
100  _portraitsBox = new WidgetGridBox(*this, "portraitsGridBox", "ctl_cg_portraits", 5.0f, 6.0f);
101  _portraitsBox->setPosition(pX, pY, -200.0f);
104 
105  // Adjust main portrait.
106  getPanel("Portrait", true)->getNode("PortraitPlane")->move(0.0f, 0.0f, pZ - 50.0f);
107 
108  reset();
109 }
110 
112  _portraitsBox->lock();
113  _portraitsBox->clear();
115 }
116 
118  // Set default portrait.
119  _choices->setPortrait("gui_po_nwnlogo_");
120  // Disable the OK button.
121  getButton("OkButton", true)->setDisabled(true);
122 }
123 
125  // Rebuild the portrait list as gender or race could have change.
127 
129  if (_selectedPortrait == "gui_po_nwnlogo_")
130  getButton("OkButton", true)->setDisabled(true);
131 
133 }
134 
136  if (texture != "gui_po_nwnlogo_")
137  getButton("OkButton", true)->setDisabled(false);
138 
139  std::vector<Common::UString> textures;
140  textures.push_back(texture + "h");
141  getPanel("Portrait", true)->getNode("PortraitPlane")->setTextures(textures);
142  _selectedPortrait = texture;
143 }
144 
146  if (widget.getTag() == "OkButton") {
148  _returnCode = 2;
149  return;
150  }
151 
152  if (widget.getTag() == "CancelButton") {
153  _returnCode = 1;
154  return;
155  }
156 }
157 
158 const std::vector<Common::UString> CharPortrait::initPortraitList() {
159  const Aurora::TwoDAFile &twodaPortraits = TwoDAReg.get2DA("portraits");
160 
161  uint32 race = _choices->getCharacter().getRace();
162  // Portraits for half-elf and human are the same.
163  if (race == 4)
164  race = 6;
165 
166  std::vector<Common::UString> portraits;
167  std::vector<Common::UString> racePortraits;
168 
169  for (size_t po = 0; po < twodaPortraits.getRowCount(); ++po) {
170  const Aurora::TwoDARow &rowPortrait = twodaPortraits.getRow(po);
171 
172  if (rowPortrait.empty("plot"))
173  continue;
174 
175  if (rowPortrait.getInt("plot"))
176  continue;
177 
178  if ((uint32)rowPortrait.getInt("Sex") != (uint32)_choices->getCharacter().getGender())
179  continue;
180 
181  if (rowPortrait.empty("Race"))
182  continue;
183 
184  // TODO Implement Low Gore option.
185  // TODO Add user portraits.
186  if ((uint32) rowPortrait.getInt("Race") == race)
187  racePortraits.push_back("po_" + rowPortrait.getString("BaseResRef"));
188  else
189  portraits.push_back("po_" + rowPortrait.getString("BaseResRef"));
190  }
191 
192  // Show the race's character portrait first.
193  portraits.insert(portraits.begin(), racePortraits.begin(), racePortraits.end());
194 
195  return portraits;
196 }
197 
199  const std::vector<Common::UString> portraits = initPortraitList();
200 
201  _portraitsBox->lock();
202 
203  _portraitsBox->clear();
204  for (std::vector<Common::UString>::const_iterator it = portraits.begin();
205  it != portraits.end(); ++it) {
206  WidgetGridItemPortrait *portraitItem = new WidgetGridItemPortrait(*this, *it);
207  _portraitsBox->add(portraitItem);
208  }
209 
211 }
212 
213 } // End of namespace NWN
214 
215 } // End of namespace Engines
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void setBorderColor(float bR, float bG, float bB, float bA)
Definition: portrait.cpp:344
A NWN button widget.
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
Widget * _owner
The widget&#39;s owner, if any.
Definition: widget.h:113
A class holding an UTF-8 string.
Definition: ustring.h:48
Common::UString _selectedPortrait
Definition: charportrait.h:73
void add(WidgetListItem *item, bool noTag=false)
Definition: listbox.cpp:411
void mouseDown(uint8 state, float x, float y)
A mouse button was pressed on the widget.
uint8_t uint8
Definition: types.h:200
const Creature & getCharacter()
void setDisabled(bool disabled)
Disable/Enable the widget.
Definition: button.cpp:107
WidgetGridBox * _portraitsBox
Definition: charportrait.h:72
void remove()
Remove the widget from the GUI.
Definition: widget.cpp:258
float getHeight() const
Get the widget&#39;s height.
Definition: portrait.cpp:352
void getPosition(float &x, float &y, float &z) const
Get the position of the node.
Definition: modelnode.cpp:181
size_t getRowCount() const
Return the number of rows in the array.
Definition: 2dafile.cpp:400
An item widget within a NWN listbox widget.
Definition: listbox.h:51
const std::vector< Common::UString > initPortraitList()
WidgetButton * getButton(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:306
virtual void mouseDown(uint8 state, float x, float y)
A mouse button was pressed on the widget.
Definition: widget.cpp:191
void callbackActive(Widget &widget)
Callback that&#39;s triggered when a widget was activated.
virtual void show()
Show the GUI.
Definition: gui.cpp:62
float getHeight() const
Get the widget&#39;s height.
A NWN GUI.
Definition: gui.h:54
void show()
Show the GUI.
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).
A node within a 3D model.
WidgetPanel * getPanel(const Common::UString &tag, bool vital=false)
Definition: gui.cpp:258
void load(const Common::UString &resref)
Definition: gui.cpp:77
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: listbox.cpp:347
The global 2DA registry.
virtual void addChild(Widget &widget)
Add a child to the widget.
Definition: widget.cpp:215
virtual bool deactivate()
Definition: listbox.cpp:124
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
void setPortrait(const Common::UString &portrait)
bool empty(size_t column) const
Check if the cell is empty.
Definition: 2dafile.cpp:107
uint32 getRace() const
Return the creature&#39;s race value.
Definition: creature.cpp:205
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
WidgetGridItemPortrait(GUI &gui, const Common::UString &portrait)
A widget in a GUI.
Definition: widget.h:40
Graphics::Aurora::ModelNode * getNode(const Common::UString &nodeName) const
Definition: modelwidget.cpp:92
float getWidth() const
Get the widget&#39;s width.
uint32_t uint32
Definition: types.h:204
Gender getGender() const
Get the creature&#39;s gender.
Definition: creature.cpp:190
A creature in a Neverwinter Nights area.
const Common::UString & getPortrait() const
Get the creature&#39;s portrait.
Definition: creature.cpp:222
void setMainTexture(const Common::UString &texture)
void move(float x, float y, float z)
Move the node, relative to its current position.
Definition: modelnode.cpp:251
The portrait chooser in CharGen.
A portrait model and widget.
A NWN portrait widget.
Definition: portrait.h:121
CharPortrait(CharGenChoices &choices, ::Engines::Console *console=0)
void addWidget(Widget *widget)
Add a widget.
Definition: gui.cpp:250
A row within a 2DA file.
Definition: 2dafile.h:61
virtual void addSub(Widget &widget)
Add a sub-widget to the widget.
Definition: widget.cpp:206
A NWN panel widget.
float getWidth() const
Get the widget&#39;s width.
Definition: portrait.cpp:348
A NWN gridbox widget.
void setTextures(const std::vector< Common::UString > &textures)
Set textures to the node.
Definition: modelnode.cpp:297