Debian Codegraph @

plugins/dm.conversation/CommandEditor.cpp

raw
1 #include "CommandEditor.h"
2
3 #include "i18n.h"
4 #include "string/string.h"
5 #include "string/convert.h"
6
7 #include <fmt/format.h>
8 #include "itextstream.h"
9
10 #include <wx/sizer.h>
11 #include <wx/panel.h>
12 #include <wx/checkbox.h>
13 #include <wx/choice.h>
14 #include <wx/button.h>
15
16 #include "wxutil/ChoiceHelper.h"
17 #include "ConversationCommandLibrary.h"
18
19 namespace ui
20 {
21
22 namespace
23 {
24 const char* const WINDOW_TITLE = N_("Edit Command");
25 }
26
27 CommandEditor::CommandEditor(wxWindow* parent, conversation::ConversationCommand& command, const conversation::Conversation& conv) :
28 DialogBase(_(WINDOW_TITLE), parent),
29 _conversation(conv),
30 _command(command), // copy the conversation command to a local object
31 _targetCommand(command)
32 {
33 // Create all widgets
34 populateWindow();
35
36 // Fill the actor dropdown
37 for (conversation::Conversation::ActorMap::const_iterator i = _conversation.actors.begin();
38 i != _conversation.actors.end(); ++i)
39 {
40 std::string actorStr = fmt::format(_("Actor {0:d} ({1})"), i->first, i->second);
41
42 // Store the actor ID into a client data object and pass it along
43 findNamedObject<wxChoice>(this, "ConvCmdEditorActorChoice")->Append(actorStr,
44 new wxStringClientData(string::to_string(i->first)));
45 }
46
47 // Let the command library fill the command dropdown
48 conversation::ConversationCommandLibrary::Instance().populateChoice(
49 findNamedObject<wxChoice>(this, "ConvCmdEditorCommandChoice"));
50
51 // Fill the values
52 updateWidgets();
53 }
54
55 conversation::ConversationCommand& CommandEditor::getCommand()
56 {
57 return _command;
58 }
59
60 const conversation::Conversation& CommandEditor::getConversation()
61 {
62 return _conversation;
63 }
64
65 void CommandEditor::updateWidgets()
66 {
67 // Select the actor passed from the command
68 wxutil::ChoiceHelper::SelectItemByStoredId(
69 findNamedObject<wxChoice>(this, "ConvCmdEditorActorChoice"), _command.actor);
70
71 // Select the command type
72 wxutil::ChoiceHelper::SelectItemByStoredId(
73 findNamedObject<wxChoice>(this, "ConvCmdEditorCommandChoice"), _command.type);
74
75 // Populate the correct command argument widgets
76 createArgumentWidgets(_command.type);
77
78 // Pre-fill the values
79 for (conversation::ConversationCommand::ArgumentMap::const_iterator i = _command.arguments.begin();
80 i != _command.arguments.end(); ++i)
81 {
82 int argIndex = i->first;
83
84 if (argIndex > static_cast<int>(_argumentItems.size()) || argIndex < 0)
85 {
86 // Invalid command argument index
87 rError() << "Invalid command argument index " << argIndex << std::endl;
88 continue;
89 }
90
91 // Load the value into the argument item
92 _argumentItems[argIndex - 1]->setValueFromString(i->second);
93 }
94
95 // Update the "wait until finished" flag
96 findNamedObject<wxCheckBox>(this, "ConvCmdEditorWaitUntilFinished")->SetValue(_command.waitUntilFinished);
97
98 // Update the sensitivity of the correct flag
99 updateWaitUntilFinished(_command.type);
100 }
101
102 void CommandEditor::save()
103 {
104 _command.actor = wxutil::ChoiceHelper::GetSelectionId(
105 findNamedObject<wxChoice>(this, "ConvCmdEditorActorChoice"));
106
107 _command.type = wxutil::ChoiceHelper::GetSelectionId(
108 findNamedObject<wxChoice>(this, "ConvCmdEditorCommandChoice"));
109
110 // Clear the existing arguments
111 _command.arguments.clear();
112
113 int index = 1;
114
115 for (ArgumentItemList::iterator i = _argumentItems.begin();
116 i != _argumentItems.end(); ++i, ++index)
117 {
118 _command.arguments[index] = (*i)->getValue();
119 }
120
121 // Get the value of the "wait until finished" flag
122 try
123 {
124 const conversation::ConversationCommandInfo& cmdInfo =
125 conversation::ConversationCommandLibrary::Instance().findCommandInfo(_command.type);
126
127 if (cmdInfo.waitUntilFinishedAllowed)
128 {
129 // Load the value
130 _command.waitUntilFinished =
131 findNamedObject<wxCheckBox>(this, "ConvCmdEditorWaitUntilFinished")->GetValue();
132 }
133 else
134 {
135 // Command doesn't support "wait until finished", set to default == true
136 _command.waitUntilFinished = true;
137 }
138 }
139 catch (std::runtime_error&)
140 {
141 rError() << "Cannot find conversation command info for index " << _command.type << std::endl;
142 }
143
144 // Copy the command over the target object
145 _targetCommand = _command;
146 }
147
148 void CommandEditor::populateWindow()
149 {
150 loadNamedPanel(this, "ConvCmdEditorMainPanel");
151
152 makeLabelBold(this, "ConvCmdEditorActorLabel");
153 makeLabelBold(this, "ConvCmdEditorCommandLabel");
154 makeLabelBold(this, "ConvCmdEditorCmdArgLabel");
155 makeLabelBold(this, "ConvCmdEditorPropertiesLabel");
156
157 wxChoice* cmdDropDown = findNamedObject<wxChoice>(this, "ConvCmdEditorCommandChoice");
158 cmdDropDown->Connect(wxEVT_CHOICE, wxCommandEventHandler(CommandEditor::onCommandTypeChange), NULL, this);
159
160 // Wire up button events
161 findNamedObject<wxButton>(this, "ConvCmdEditorCancelButton")->Connect(
162 wxEVT_BUTTON, wxCommandEventHandler(CommandEditor::onCancel), NULL, this);
163 findNamedObject<wxButton>(this, "ConvCmdEditorOkButton")->Connect(
164 wxEVT_BUTTON, wxCommandEventHandler(CommandEditor::onSave), NULL, this);
165 }
166
167 void CommandEditor::commandTypeChanged()
168 {
169 int newCommandTypeID = -1;
170
171 wxChoice* cmdDropDown = findNamedObject<wxChoice>(this, "ConvCmdEditorCommandChoice");
172 int selectedItem = cmdDropDown->GetSelection();
173
174 wxStringClientData* cmdIdStr = static_cast<wxStringClientData*>(cmdDropDown->GetClientObject(selectedItem));
175 newCommandTypeID = string::convert<int>(cmdIdStr->GetData().ToStdString(), -1);
176
177 // Create the argument widgets for this new command type
178 createArgumentWidgets(newCommandTypeID);
179
180 // Update the sensitivity of the correct flag
181 updateWaitUntilFinished(newCommandTypeID);
182 }
183
184 void CommandEditor::updateWaitUntilFinished(int commandTypeID)
185 {
186 // Update the sensitivity of the correct flag
187 try
188 {
189 const conversation::ConversationCommandInfo& cmdInfo =
190 conversation::ConversationCommandLibrary::Instance().findCommandInfo(commandTypeID);
191
192 findNamedObject<wxCheckBox>(this, "ConvCmdEditorWaitUntilFinished")->Enable(cmdInfo.waitUntilFinishedAllowed);
193 }
194 catch (std::runtime_error&)
195 {
196 rError() << "Cannot find conversation command info for index " << commandTypeID << std::endl;
197 }
198 }
199
200 void CommandEditor::createArgumentWidgets(int commandTypeID)
201 {
202 try
203 {
204 const conversation::ConversationCommandInfo& cmdInfo =
205 conversation::ConversationCommandLibrary::Instance().findCommandInfo(commandTypeID);
206
207 // Remove all possible previous items from the list
208 _argumentItems.clear();
209
210 // Clear the panel and add a new table
211 wxPanel* argPanel = findNamedObject<wxPanel>(this, "ConvCmdEditorArgPanel");
212 argPanel->DestroyChildren();
213
214 // Create the table
215 wxFlexGridSizer* table = new wxFlexGridSizer(static_cast<int>(cmdInfo.arguments.size()), 3, 6, 12);
216 table->AddGrowableCol(1);
217
218 argPanel->SetSizer(table);
219
220 if (cmdInfo.arguments.empty())
221 {
222 wxStaticText* noneText = new wxStaticText(argPanel, wxID_ANY, _("None"));
223 noneText->SetFont(noneText->GetFont().Italic());
224 argPanel->GetSizer()->Add(noneText, 0, wxLEFT, 6);
225 return;
226 }
227
228 // Setup the table with default spacings
229 for (const conversation::ArgumentInfo& argInfo : cmdInfo.arguments)
230 {
231 CommandArgumentItemPtr item = createCommandArgumentItem(argInfo, argPanel);
232
233 if (item)
234 {
235 _argumentItems.push_back(item);
236
237 // The label
238 table->Add(item->getLabelWidget(), 0, wxALIGN_CENTER_VERTICAL | wxLEFT, 6);
239
240 // The edit widgets
241 table->Add(item->getEditWidget(), 1, wxEXPAND, wxALIGN_CENTER_VERTICAL);
242
243 // The help widgets
244 table->Add(item->getHelpWidget(), 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxRIGHT, 6);
245 }
246 }
247 }
248 catch (std::runtime_error&)
249 {
250 rError() << "Cannot find conversation command info for index " << commandTypeID << std::endl;
251 }
252
253 wxPanel* mainPanel = findNamedObject<wxPanel>(this, "ConvCmdEditorMainPanel");
254 mainPanel->Fit();
255 mainPanel->Layout();
256 Fit();
257 }
258
259 CommandArgumentItemPtr CommandEditor::createCommandArgumentItem(const conversation::ArgumentInfo& argInfo, wxWindow* parent)
260 {
261 // Unfortunately we don't have a type declaration for animation in the .def file (didn't think about that back then)
262 // so let's detect the "Anim" title of the argument and construct an animation picker in this case
263 if (argInfo.title == "Anim")
264 {
265 return std::make_shared<AnimationArgument>(*this, parent, argInfo);
266 }
267
268 switch (argInfo.type)
269 {
270 case conversation::ArgumentInfo::ARGTYPE_BOOL:
271 // Create a new bool argument item
272 return std::make_shared<BooleanArgument>(*this, parent, argInfo);
273 case conversation::ArgumentInfo::ARGTYPE_INT:
274 case conversation::ArgumentInfo::ARGTYPE_FLOAT:
275 case conversation::ArgumentInfo::ARGTYPE_STRING:
276 // Create a new string argument item
277 return std::make_shared<StringArgument>(*this, parent, argInfo);
278 case conversation::ArgumentInfo::ARGTYPE_VECTOR:
279 // Create a new string argument item
280 return std::make_shared<StringArgument>(*this, parent, argInfo);
281 case conversation::ArgumentInfo::ARGTYPE_SOUNDSHADER:
282 // Create a new sound shader argument item
283 return std::make_shared<SoundShaderArgument>(*this, parent, argInfo);
284 case conversation::ArgumentInfo::ARGTYPE_ACTOR:
285 // Create a new actor argument item
286 return std::make_shared<ActorArgument>(*this, parent, argInfo, _conversation.actors);
287 case conversation::ArgumentInfo::ARGTYPE_ENTITY:
288 // Create a new string argument item
289 return std::make_shared<StringArgument>(*this, parent, argInfo);
290 default:
291 rError() << "Unknown command argument type: " << argInfo.type << std::endl;
292 break;
293 };
294
295 return CommandArgumentItemPtr();
296 }
297
298 void CommandEditor::onSave(wxCommandEvent& ev)
299 {
300 save();
301 EndModal(wxID_OK);
302 }
303
304 void CommandEditor::onCancel(wxCommandEvent& ev)
305 {
306 // Just close the window without writing the values
307 EndModal(wxID_CANCEL);
308 }
309
310 void CommandEditor::onCommandTypeChange(wxCommandEvent& ev)
311 {
312 commandTypeChanged();
313 }
314
315 } // namespace ui