ManaPlus
textcommandeditor.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 Andrei Karas
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "spellmanager.h"
26 #include "textcommand.h"
27 
28 #include "input/keyboardconfig.h"
29 
30 #include "gui/models/iconsmodel.h"
32 
33 #include "gui/widgets/button.h"
35 #include "gui/widgets/dropdown.h"
37 #include "gui/widgets/label.h"
38 #include "gui/widgets/layoutcell.h"
39 
40 #ifdef TMWA_SUPPORT
42 
44 #endif // TMWA_SUPPORT
45 
46 #include "utils/delete2.h"
47 
48 #include "debug.h"
49 
50 #ifdef TMWA_SUPPORT
51 const unsigned int MAGIC_START_ID = 340;
52 #endif // TMWA_SUPPORT
53 
55  // TRANSLATORS: command editor name
56  Window(_("Command Editor"), Modal_false, nullptr, "commandeditor.xml"),
58 #ifdef TMWA_SUPPORT
59  mIsMagicCommand(command != nullptr ?
60  (command->getCommandType() == TextCommandType::Magic) : false),
61 #endif // TMWA_SUPPORT
62  mCommand(command),
63 #ifdef TMWA_SUPPORT
64  // TRANSLATORS: command editor button
65  mIsMagic(new RadioButton(this, _("magic"), "magic", mIsMagicCommand)),
66  // TRANSLATORS: command editor button
67  mIsOther(new RadioButton(this, _("other"), "magic", !mIsMagicCommand)),
68 #endif // TMWA_SUPPORT
69  // TRANSLATORS: command editor label
70  mSymbolLabel(new Label(this, _("Symbol:"))),
71  mSymbolTextField(new TextField(this, std::string(),
72  LoseFocusOnTab_true, nullptr, std::string(), false)),
73  // TRANSLATORS: command editor label
74  mCommandLabel(new Label(this, _("Command:"))),
75  mCommandTextField(new TextField(this, std::string(),
76  LoseFocusOnTab_true, nullptr, std::string(), false)),
77  // TRANSLATORS: command editor label
78  mCommentLabel(new Label(this, _("Comment:"))),
79  mCommentTextField(new TextField(this, std::string(),
80  LoseFocusOnTab_true, nullptr, std::string(), false)),
81  mTargetTypeModel(new TargetTypeModel),
82  // TRANSLATORS: command editor label
83  mTypeLabel(new Label(this, _("Target Type:"))),
84  mTypeDropDown(new DropDown(this, mTargetTypeModel,
85  false, Modal_false, nullptr, std::string())),
86  mIconsModel(new IconsModel),
87  // TRANSLATORS: command editor label
88  mIconLabel(new Label(this, _("Icon:"))),
89  mIconDropDown(new DropDown(this, mIconsModel,
90  false, Modal_false, nullptr, std::string())),
91 #ifdef TMWA_SUPPORT
92  // TRANSLATORS: command editor label
93  mManaLabel(new Label(this, _("Mana:"))),
94  mManaField(new IntTextField(this, 0, 0, 0, Enable_true, 0)),
95  // TRANSLATORS: command editor label
96  mMagicLvlLabel(new Label(this, _("Magic level:"))),
97  mMagicLvlField(new IntTextField(this, 0, 0, 0, Enable_true, 0)),
98  mMagicSchoolModel(new MagicSchoolModel),
99  // TRANSLATORS: command editor label
100  mSchoolLabel(new Label(this, _("Magic School:"))),
101  mSchoolDropDown(new DropDown(this, mMagicSchoolModel,
102  false, Modal_false, nullptr, std::string())),
103  // TRANSLATORS: command editor label
104  mSchoolLvlLabel(new Label(this, _("School level:"))),
105  mSchoolLvlField(new IntTextField(this, 0, 0, 0, Enable_true, 0)),
106 #endif // TMWA_SUPPORT
107  // TRANSLATORS: command editor button
108  mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
109  // TRANSLATORS: command editor button
110  mSaveButton(new Button(this, _("Save"), "save", this)),
111  // TRANSLATORS: command editor button
112  mDeleteButton(new Button(this, _("Delete"), "delete", this)),
113  mEnabledKeyboard(keyboard.isEnabled())
114 {
115  const int w = 350;
116  const int h = 370;
117 
118  keyboard.setEnabled(false);
119 
120  setWindowName("TextCommandEditor");
122 
123 #ifdef TMWA_SUPPORT
124  mIsMagic->setActionEventId("magic");
126 
127  mIsOther->setActionEventId("other");
129 
130  mManaField->setRange(0, 500);
131  mManaField->setWidth(20);
132 
133  mMagicLvlField->setRange(0, 5);
135 
139 
140  mSchoolLvlField->setRange(0, 5);
142 #endif // TMWA_SUPPORT
143 
146 
149  if (mCommand != nullptr)
151 
155 
156  if (command != nullptr)
157  {
158 #ifdef TMWA_SUPPORT
159  if (command->getCommandType() == TextCommandType::Magic)
161  else
163 
164  mManaField->setValue(command->getMana());
165  mMagicLvlField->setValue(command->getBaseLvl());
167  - MAGIC_START_ID);
169 #endif // TMWA_SUPPORT
170  }
171 
172  ContainerPlacer placer(nullptr, nullptr);
173  placer = getPlacer(0, 0);
174 
175 #ifdef TMWA_SUPPORT
176  placer(0, 0, mIsMagic, 1, 1);
177  placer(2, 0, mIsOther, 1, 1);
178  placer(0, 1, mSymbolLabel, 2, 1).setPadding(3);
179  placer(2, 1, mSymbolTextField, 3, 1).setPadding(3);
180  placer(0, 2, mCommandLabel, 2, 1).setPadding(3);
181  placer(2, 2, mCommandTextField, 4, 1).setPadding(3);
182 
183  placer(0, 3, mCommentLabel, 2, 1).setPadding(3);
184  placer(2, 3, mCommentTextField, 4, 1).setPadding(3);
185 
186  placer(0, 4, mTypeLabel, 2, 1).setPadding(3);
187  placer(2, 4, mTypeDropDown, 3, 1).setPadding(3);
188 
189  placer(0, 5, mIconLabel, 2, 1).setPadding(3);
190  placer(2, 5, mIconDropDown, 3, 1).setPadding(3);
191 
192  placer(0, 6, mManaLabel, 2, 1).setPadding(3);
193  placer(2, 6, mManaField, 3, 1).setPadding(3);
194  placer(0, 7, mMagicLvlLabel, 2, 1).setPadding(3);
195  placer(2, 7, mMagicLvlField, 3, 1).setPadding(3);
196 
197  placer(0, 8, mSchoolLabel, 2, 1).setPadding(3);
198  placer(2, 8, mSchoolDropDown, 3, 1).setPadding(3);
199  placer(0, 9, mSchoolLvlLabel, 2, 1).setPadding(3);
200  placer(2, 9, mSchoolLvlField, 3, 1).setPadding(3);
201 
202  placer(0, 10, mSaveButton, 2, 1).setPadding(3);
203  placer(2, 10, mCancelButton, 2, 1).setPadding(3);
204  placer(4, 10, mDeleteButton, 2, 1).setPadding(3);
205 #else // TMWA_SUPPORT
206 
207  placer(0, 0, mSymbolLabel, 2, 1).setPadding(3);
208  placer(2, 0, mSymbolTextField, 3, 1).setPadding(3);
209  placer(0, 1, mCommandLabel, 2, 1).setPadding(3);
210  placer(2, 1, mCommandTextField, 4, 1).setPadding(3);
211 
212  placer(0, 2, mCommentLabel, 2, 1).setPadding(3);
213  placer(2, 2, mCommentTextField, 4, 1).setPadding(3);
214 
215  placer(0, 3, mTypeLabel, 2, 1).setPadding(3);
216  placer(2, 3, mTypeDropDown, 3, 1).setPadding(3);
217 
218  placer(0, 4, mIconLabel, 2, 1).setPadding(3);
219  placer(2, 4, mIconDropDown, 3, 1).setPadding(3);
220 
221  placer(0, 5, mSaveButton, 2, 1).setPadding(3);
222  placer(2, 5, mCancelButton, 2, 1).setPadding(3);
223  placer(4, 5, mDeleteButton, 2, 1).setPadding(3);
224 #endif // TMWA_SUPPORT
225 
226  setWidth(w);
227  setHeight(h);
228 
229  reflowLayout(w, 0);
230 
231  center();
232 }
233 
235 {
237  enableVisibleSound(true);
239 }
240 
242 {
245 #ifdef TMWA_SUPPORT
247 #endif // TMWA_SUPPORT
248 }
249 
251 {
252  const std::string &eventId = event.getId();
253  if (eventId == "save")
254  {
255  save();
256  scheduleDelete();
257  }
258  else if (eventId == "cancel")
259  {
260  scheduleDelete();
261  }
262  else if (eventId == "delete")
263  {
264  deleteCommand();
265  scheduleDelete();
266  }
267 #ifdef TMWA_SUPPORT
268  else if (eventId == "magic")
269  {
270  mIsMagicCommand = true;
272  }
273  else if (eventId == "other")
274  {
275  mIsMagicCommand = false;
277  }
278 #endif // TMWA_SUPPORT
279 }
280 
281 #ifdef TMWA_SUPPORT
283 {
284  mManaField->setVisible(show);
285  mManaLabel->setVisible(show);
286  mMagicLvlLabel->setVisible(show);
287  mMagicLvlField->setVisible(show);
288  mSchoolLabel->setVisible(show);
292 }
293 #endif // TMWA_SUPPORT
294 
296 {
299 }
300 
302 {
303  if (mCommand == nullptr)
304  return;
305 #ifdef TMWA_SUPPORT
306  if (mIsMagicCommand)
308  else
310 #endif // TMWA_SUPPORT
311 
316  static_cast<CommandTargetT>(mTypeDropDown->getSelected()));
318 
319 #ifdef TMWA_SUPPORT
322  mCommand->setSchool(static_cast<MagicSchoolT>(
325 #endif // TMWA_SUPPORT
326 
327  if (spellManager != nullptr)
328  spellManager->save();
329 }
330 
332 {
333  if (mCommand == nullptr)
334  return;
335  mCommand->setSymbol("");
336  mCommand->setCommand("");
337  mCommand->setComment("");
339  mCommand->setIcon("");
340 #ifdef TMWA_SUPPORT
342  mCommand->setMana(0);
343  mCommand->setBaseLvl(0);
346 #endif // TMWA_SUPPORT
347 
348  if (spellManager != nullptr)
349  spellManager->save();
350 }
unsigned getSchoolLvl() const
Definition: textcommand.h:113
DropDown * mIconDropDown
void setDefaultSize()
Definition: window.cpp:1193
#define _(s)
Definition: gettext.h:34
TextField * mSymbolTextField
void setValue(const int value)
const bool LoseFocusOnTab_true
void setWidth(const int width)
Definition: widget.cpp:132
unsigned getBaseLvl() const
Definition: textcommand.h:110
const bool Visible_true
Definition: visible.h:29
SpellManager * spellManager
std::string getIcon() const
Definition: textcommand.h:100
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
IntTextField * mSchoolLvlField
void setVisible(Visible visible)
Definition: widget.cpp:224
void showControls(const Visible show)
void setComment(const std::string &comment)
Definition: textcommand.h:138
DropDown * mSchoolDropDown
Definition: window.h:98
Definition: button.h:94
void save() const
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
RadioButton * mIsMagic
#define delete2(var)
Definition: delete2.h:24
IntTextField * mMagicLvlField
const bool Enable_true
Definition: enable.h:29
void action(const ActionEvent &event)
TextCommand * mCommand
void adjustSize()
Definition: button.cpp:829
void setCommand(const std::string &command)
Definition: textcommand.h:135
void setCommandType(const TextCommandTypeT commandType)
Definition: textcommand.h:131
#define new
Definition: debug_new.h:147
void setSelectedString(const std::string &str)
Definition: dropdown.cpp:445
IntTextField * mManaField
#define CAST_S32
Definition: cast.h:29
void setTargetType(const CommandTargetT targetType)
Definition: textcommand.h:147
const unsigned int MAGIC_START_ID
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
IconsModel * mIconsModel
RadioButton * mIsOther
void setRange(const int minimum, const int maximum)
void setMana(const unsigned int mana)
Definition: textcommand.h:116
#define nullptr
Definition: localconsts.h:44
TextField * mCommentTextField
TargetTypeModel * mTargetTypeModel
int getValue() const
void enableVisibleSound(bool b)
Definition: window.h:480
void setSchool(const MagicSchoolT school)
Definition: textcommand.h:119
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1386
std::string getSelectedString() const
Definition: dropdown.cpp:461
void setBaseLvl(const unsigned int baseLvl)
Definition: textcommand.h:122
virtual void scheduleDelete()
Definition: window.cpp:826
DropDown * mTypeDropDown
void setHeight(const int height)
Definition: widget.cpp:139
void setSymbol(const std::string &symbol)
Definition: textcommand.h:141
unsigned int getMana() const
Definition: textcommand.h:104
virtual void setVisible(Visible visible)
Definition: window.cpp:773
void center()
Definition: window.cpp:1412
TextCommandTypeT getCommandType() const
Definition: textcommand.h:128
TextCommandEditor(TextCommand *const command)
TextField * mCommandTextField
KeyboardConfig keyboard
void setSelected(int selected)
Definition: dropdown.cpp:514
const std::string & getText() const
Definition: textfield.h:223
int getSelected() const
Definition: dropdown.cpp:509
void setEnabled(const bool flag)
MagicSchoolT getSchool() const
Definition: textcommand.h:107
void setSchoolLvl(const unsigned int schoolLvl)
Definition: textcommand.h:125
void reflowLayout(int w, int h)
Definition: window.cpp:1391
void postInit()
Definition: window.cpp:248
void setIcon(const std::string &icon)
Definition: textcommand.h:150
void setWindowName(const std::string &name)
Definition: window.h:354
MagicSchoolModel * mMagicSchoolModel