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", BUTTON_SKIN, this)),
109  // TRANSLATORS: command editor button
110  mSaveButton(new Button(this, _("Save"), "save", BUTTON_SKIN, this)),
111  // TRANSLATORS: command editor button
112  mDeleteButton(new Button(this, _("Delete"), "delete", BUTTON_SKIN, 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  mSymbolTextField->setText(command->getSymbol());
172  mCommandTextField->setText(command->getCommand());
173  mCommentTextField->setText(command->getComment());
175  }
176 
177  ContainerPlacer placer(nullptr, nullptr);
178  placer = getPlacer(0, 0);
179 
180 #ifdef TMWA_SUPPORT
181  placer(0, 0, mIsMagic, 1, 1);
182  placer(2, 0, mIsOther, 1, 1);
183  placer(0, 1, mSymbolLabel, 2, 1).setPadding(3);
184  placer(2, 1, mSymbolTextField, 3, 1).setPadding(3);
185  placer(0, 2, mCommandLabel, 2, 1).setPadding(3);
186  placer(2, 2, mCommandTextField, 4, 1).setPadding(3);
187 
188  placer(0, 3, mCommentLabel, 2, 1).setPadding(3);
189  placer(2, 3, mCommentTextField, 4, 1).setPadding(3);
190 
191  placer(0, 4, mTypeLabel, 2, 1).setPadding(3);
192  placer(2, 4, mTypeDropDown, 3, 1).setPadding(3);
193 
194  placer(0, 5, mIconLabel, 2, 1).setPadding(3);
195  placer(2, 5, mIconDropDown, 3, 1).setPadding(3);
196 
197  placer(0, 6, mManaLabel, 2, 1).setPadding(3);
198  placer(2, 6, mManaField, 3, 1).setPadding(3);
199  placer(0, 7, mMagicLvlLabel, 2, 1).setPadding(3);
200  placer(2, 7, mMagicLvlField, 3, 1).setPadding(3);
201 
202  placer(0, 8, mSchoolLabel, 2, 1).setPadding(3);
203  placer(2, 8, mSchoolDropDown, 3, 1).setPadding(3);
204  placer(0, 9, mSchoolLvlLabel, 2, 1).setPadding(3);
205  placer(2, 9, mSchoolLvlField, 3, 1).setPadding(3);
206 
207  placer(0, 10, mSaveButton, 2, 1).setPadding(3);
208  placer(2, 10, mCancelButton, 2, 1).setPadding(3);
209  placer(4, 10, mDeleteButton, 2, 1).setPadding(3);
210 #else // TMWA_SUPPORT
211 
212  placer(0, 0, mSymbolLabel, 2, 1).setPadding(3);
213  placer(2, 0, mSymbolTextField, 3, 1).setPadding(3);
214  placer(0, 1, mCommandLabel, 2, 1).setPadding(3);
215  placer(2, 1, mCommandTextField, 4, 1).setPadding(3);
216 
217  placer(0, 2, mCommentLabel, 2, 1).setPadding(3);
218  placer(2, 2, mCommentTextField, 4, 1).setPadding(3);
219 
220  placer(0, 3, mTypeLabel, 2, 1).setPadding(3);
221  placer(2, 3, mTypeDropDown, 3, 1).setPadding(3);
222 
223  placer(0, 4, mIconLabel, 2, 1).setPadding(3);
224  placer(2, 4, mIconDropDown, 3, 1).setPadding(3);
225 
226  placer(0, 5, mSaveButton, 2, 1).setPadding(3);
227  placer(2, 5, mCancelButton, 2, 1).setPadding(3);
228  placer(4, 5, mDeleteButton, 2, 1).setPadding(3);
229 #endif // TMWA_SUPPORT
230 
231  setWidth(w);
232  setHeight(h);
233 
234  reflowLayout(w, 0);
235 
236  center();
237 }
238 
240 {
242  enableVisibleSound(true);
244 }
245 
247 {
250 #ifdef TMWA_SUPPORT
252 #endif // TMWA_SUPPORT
253 }
254 
256 {
257  const std::string &eventId = event.getId();
258  if (eventId == "save")
259  {
260  save();
261  scheduleDelete();
262  }
263  else if (eventId == "cancel")
264  {
265  scheduleDelete();
266  }
267  else if (eventId == "delete")
268  {
269  deleteCommand();
270  scheduleDelete();
271  }
272 #ifdef TMWA_SUPPORT
273  else if (eventId == "magic")
274  {
275  mIsMagicCommand = true;
277  }
278  else if (eventId == "other")
279  {
280  mIsMagicCommand = false;
282  }
283 #endif // TMWA_SUPPORT
284 }
285 
286 #ifdef TMWA_SUPPORT
288 {
289  mManaField->setVisible(show);
290  mManaLabel->setVisible(show);
291  mMagicLvlLabel->setVisible(show);
292  mMagicLvlField->setVisible(show);
293  mSchoolLabel->setVisible(show);
297 }
298 #endif // TMWA_SUPPORT
299 
301 {
304 }
305 
307 {
308  if (mCommand == nullptr)
309  return;
310 #ifdef TMWA_SUPPORT
311  if (mIsMagicCommand)
313  else
315 #endif // TMWA_SUPPORT
316 
321  static_cast<CommandTargetT>(mTypeDropDown->getSelected()));
323 
324 #ifdef TMWA_SUPPORT
327  mCommand->setSchool(static_cast<MagicSchoolT>(
330 #endif // TMWA_SUPPORT
331 
332  if (spellManager != nullptr)
333  spellManager->save();
334 }
335 
337 {
338  if (mCommand == nullptr)
339  return;
340  mCommand->setSymbol("");
341  mCommand->setCommand("");
342  mCommand->setComment("");
344  mCommand->setIcon("");
345 #ifdef TMWA_SUPPORT
347  mCommand->setMana(0);
348  mCommand->setBaseLvl(0);
351 #endif // TMWA_SUPPORT
352 
353  if (spellManager != nullptr)
354  spellManager->save();
355 }
unsigned getSchoolLvl() const
Definition: textcommand.h:113
DropDown * mIconDropDown
void setDefaultSize()
Definition: window.cpp:1197
#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:96
void save() const
CommandTargetT getTargetType() const
Definition: textcommand.h:97
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)
void setText(const std::string &text)
Definition: textfield.cpp:802
TextCommand * mCommand
void adjustSize()
Definition: button.cpp:798
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 std::string BUTTON_SKIN
Definition: button.h:88
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
IconsModel * mIconsModel
std::string getComment() const
Definition: textcommand.h:88
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:1390
std::string getSelectedString() const
Definition: dropdown.cpp:461
void setBaseLvl(const unsigned int baseLvl)
Definition: textcommand.h:122
virtual void scheduleDelete()
Definition: window.cpp:830
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:777
void center()
Definition: window.cpp:1416
std::string getCommand() const
Definition: textcommand.h:85
TextCommandTypeT getCommandType() const
Definition: textcommand.h:128
std::string getSymbol() const
Definition: textcommand.h:91
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:1395
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