xoreos  0.0.5
charpremade.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 <cstdio>
26 
27 #include "src/common/util.h"
28 #include "src/common/error.h"
29 
30 #include "src/events/requests.h"
31 
35 
37 
39 #include "src/engines/nwn/game.h"
40 #include "src/engines/nwn/module.h"
41 
44 
46 
48 
49 namespace Engines {
50 
51 namespace NWN {
52 
54  const Common::UString &name,
55  const Common::UString &classes,
56  const Common::UString &portrait, float spacing) :
57  WidgetListItemBaseButton(gui, "ctl_pre_btn_char",
58  spacing) {
59 
60  _portrait.reset (new Portrait(portrait, Portrait::kSizeTiny, 1.0f));
61  _textName.reset (new Graphics::Aurora::Text(FontMan.get(font), name));
62  _textClass.reset(new Graphics::Aurora::Text(FontMan.get(font), classes));
63 }
64 
66 }
67 
70 
71  _textName->show();
72  _textClass->show();
73  _portrait->show();
74 }
75 
78 
79  _textClass->hide();
80  _textName->hide();
81  _portrait->hide();
82 }
83 
84 void WidgetListItemCharacter::setPosition(float x, float y, float z) {
86 
87  getPosition(x, y, z);
88 
89  z -= 5.0f;
90  _portrait->setPosition(x + 8.0f, y + 7.0f, z);
91 
92  x += 32.0f;
93 
94  _textName->setPosition (x, y + getHeight() - _textName->getHeight() - 4.0f, z);
95  _textClass->setPosition(x, y + getHeight() - 2 * _textName->getHeight() - 4.0f, z);
96 }
97 
99  int cmp = name.strcmp(c.name);
100 
101  if (cmp < 0)
102  return true;
103  if (cmp > 0)
104  return false;
105 
106  return number < c.number;
107 }
108 
109 
111  _module(&module) {
112 
113  load("pre_playmod");
114 
115  // TODO: "SaveLabel" should wrap!
116 
117  // TODO: Customize character
118  Widget *customChar = getWidget("CustomCharButton");
119  if (customChar)
120  customChar->setDisabled(true);
121 
122  // TODO: Show pregenerated characters
123  getWidget("SaveGameBox" , true)->setDisabled(true);
124 
125  // TODO: Delete character
126  getWidget("DeleteCharButton", true)->setDisabled(true);
127 
128  _charGen.reset(new CharGenMenu(*_module, _console));
129 }
130 
132 }
133 
135  if (_characters.empty())
137 
138  getWidget("PlayButton", true)->setDisabled(_characters.empty());
139 
140  GUI::show();
141 }
142 
144  if (tag == "ButtonList")
145  type = kWidgetTypeListBox;
146 }
147 
149  if (widget.getTag() == "CancelButton") {
150  _returnCode = 1;
151  return;
152  }
153 
154  if (widget.getTag() == "NewCharButton") {
155  sub(*_charGen);
156  return;
157  }
158 
159  if (widget.getTag() == "PlayButton") {
160  playCharacter();
161  return;
162  }
163 
164  if (widget.getTag() == "ButtonList") {
165  // selectedCharacter();
166 
167  if (dynamic_cast<WidgetListBox &>(widget).wasDblClicked())
168  playCharacter();
169 
170  return;
171  }
172 }
173 
175  status("Creating character list");
176 
177  std::vector<Common::UString> characters;
178  Game::getCharacters(characters, true);
179 
180  WidgetListBox &charList = *getListBox("ButtonList", true);
181 
182  charList.lock();
183 
184  charList.clear();
186 
187  // Get the character display info
188  _characters.reserve(characters.size());
189  for (std::vector<Common::UString>::iterator c = characters.begin();
190  c != characters.end(); ++c) {
191 
192  _characters.push_back(Character());
193 
194  Character &ch = _characters.back();
195 
196  ch.file = *c;
197 
198  try {
199  Creature::getPCListInfo(ch.file, true, ch.name, ch.classes, ch.portrait);
200  } catch (...) {
201  _characters.pop_back();
202 
203  Common::exceptionDispatcherWarning("Can't read PC \"%s\"", c->c_str());
204  continue;
205  }
206 
207  if (std::sscanf(ch.file.c_str(), "%*[^0-9]%u", &ch.number) != 1)
208  ch.number = 0;
209 
210  ch.displayName = ch.name;
211  if (ch.number > 0)
212  ch.displayName += Common::UString::format(" (%u)", ch.number);
213  }
214 
215  std::sort(_characters.begin(), _characters.end());
216 
217  // Create the listbox items
218  charList.reserve(characters.size());
219  for (std::vector<Character>::iterator c = _characters.begin();
220  c != _characters.end(); ++c)
221  charList.add(new WidgetListItemCharacter(*this, "fnt_galahad14",
222  c->displayName, c->classes, c->portrait, 2.0f));
223 
224  charList.unlock();
225 
226  charList.select(0);
227 }
228 
231  size_t n = getListBox("ButtonList", true)->getSelected();
232  if (n >= _characters.size())
233  return kStringEmpty;
234 
235  return _characters[n].file;
236 }
237 
239  try {
241  } catch (...) {
243  return;
244  }
245 
246  _returnCode = 2;
247 }
248 
249 } // End of namespace NWN
250 
251 } // End of namespace Engines
void fixWidgetType(const Common::UString &tag, WidgetType &type)
Widget * getWidget(const Common::UString &tag, bool vital=false)
Return a widget in the GUI.
Definition: gui.cpp:314
Inter-thread request events.
WidgetListItemCharacter(::Engines::GUI &gui, const Common::UString &font, const Common::UString &name, const Common::UString &classes, const Common::UString &portrait, float spacing=0.0f)
Definition: charpremade.cpp:53
uint32 _returnCode
The GUI&#39;s return code.
Definition: gui.h:75
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 reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
void add(WidgetListItem *item, bool noTag=false)
Definition: listbox.cpp:411
void show()
Show the GUI.
static void getCharacters(std::vector< Common::UString > &characters, bool local)
Return a list of local player characters.
Definition: game.cpp:190
Common::ScopedPtr< Portrait > _portrait
Definition: charpremade.h:62
A text object.
virtual void setDisabled(bool disabled)
Disable/Enable the widget.
Definition: widget.cpp:154
Console * _console
Definition: gui.h:70
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
Common::ScopedPtr< Graphics::Aurora::Text > _textName
Definition: charpremade.h:60
static const Common::UString kStringEmpty
The Aurora font manager.
A NWN listbox widget.
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
The context needed to run a Neverwinter Nights module.
bool operator<(const Character &c) const
Definition: charpremade.cpp:98
A GUI.
Definition: gui.h:43
size_t getSelected() const
Definition: listbox.cpp:626
Basic exceptions to throw.
static void getPCListInfo(const Common::UString &bic, bool local, Common::UString &name, Common::UString &classes, Common::UString &portrait)
Return the information needed for a character list.
Definition: creature.cpp:1151
float getHeight() const
Get the widget&#39;s height.
A text object.
Definition: text.h:42
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
virtual void show()
Show the GUI.
Definition: gui.cpp:62
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
A NWN GUI.
Definition: gui.h:54
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
The premade character chooser.
A 3D model of an object.
uint32 sub(GUI &gui, uint32 startCode=kStartCodeNone, bool showSelf=true, bool hideSelf=true)
Open up a sub GUI.
Definition: gui.cpp:349
void load(const Common::UString &resref)
Definition: gui.cpp:77
The NWN character generator.
Definition: chargen.h:41
A NWN listbox widget.
Definition: listbox.h:116
Common::ScopedPtr< GUI > _charGen
Definition: charpremade.h:95
const Common::UString & getSelectedCharacter()
void reserve(size_t n)
Definition: listbox.cpp:405
void usePC(const Common::UString &bic, bool local)
Use this character as the player character.
Definition: module.cpp:268
int strcmp(const UString &str) const
Definition: ustring.cpp:170
A NWN portrait model.
Definition: portrait.h:47
A widget in a GUI.
Definition: widget.h:40
void select(size_t item)
Definition: listbox.cpp:618
void callbackActive(Widget &widget)
Callback that&#39;s triggered when a widget was activated.
A creature in a Neverwinter Nights area.
Common::ScopedPtr< Graphics::Aurora::Text > _textClass
Definition: charpremade.h:61
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: charpremade.cpp:84
void status(const char *s,...)
Definition: util.cpp:52
std::vector< Character > _characters
Definition: charpremade.h:97
A portrait model and widget.
The context handling the gameplay in Neverwinter Nights.
The NWN character generator.
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
CharPremadeMenu(Module &module, ::Engines::Console *console=0)
#define FontMan
Shortcut for accessing the font manager.
Definition: fontman.h:105
Generic Aurora engines model functions.