Debian Codegraph @

plugins/dm.conversation/ConversationEditor.cpp

raw
1 #include "ConversationEditor.h"
2
3 #include "i18n.h"
4 #include "string/string.h"
5
6 #include <fmt/format.h>
7 #include "string/join.h"
8 #include <regex>
9
10 #include "CommandEditor.h"
11 #include "ActorNodeFinder.h"
12 #include "wxutil/dialog/MessageBox.h"
13
14 #include <wx/button.h>
15 #include <wx/sizer.h>
16 #include <wx/panel.h>
17 #include <wx/textctrl.h>
18 #include <wx/checkbox.h>
19 #include <wx/stattext.h>
20 #include <wx/spinctrl.h>
21
22 namespace ui
23 {
24
25 namespace
26 {
27 const char* const WINDOW_TITLE = N_("Edit Conversation");
28 }
29
30 ConversationEditor::ConversationEditor(wxWindow* parent, conversation::Conversation& conversation) :
31 DialogBase(_(WINDOW_TITLE), parent),
32 _actorStore(new wxutil::TreeModel(_actorColumns, true)),
33 _commandStore(new wxutil::TreeModel(_commandColumns, true)),
34 _conversation(conversation), // copy the conversation to a local object
35 _targetConversation(conversation),
36 _updateInProgress(false)
37 {
38 // Create the widgets
39 populateWindow();
40
41 // Load the conversation values into the widgets
42 updateWidgets();
43
44 // Clear the button sensitivity in the command actions panel
45 updateCmdActionSensitivity(false);
46
47 SetSize(500, 680);
48 }
49
50 void ConversationEditor::populateWindow()
51 {
52 loadNamedPanel(this, "ConvEditorMainPanel");
53
54 makeLabelBold(this, "ConvEditorPropertyLabel");
55 makeLabelBold(this, "ConvEditorActorLabel");
56 makeLabelBold(this, "ConvEditorCommandLabel");
57
58 findNamedObject<wxTextCtrl>(this, "ConvEditorNameEntry")->Bind(wxEVT_TEXT, [&](wxCommandEvent& ev)
59 {
60 if (!_updateInProgress)
61 {
62 _conversation.name = ev.GetString();
63 }
64 });
65
66 findNamedObject<wxCheckBox>(this, "ConvEditorRepeatCheckbox")->Connect(
67 wxEVT_CHECKBOX, wxCommandEventHandler(ConversationEditor::onMaxPlayCountEnabled), NULL, this);
68
69 findNamedObject<wxCheckBox>(this, "ConvEditorActorsWithinTalkDistance")->Bind(wxEVT_CHECKBOX,
70 [&](wxCommandEvent& ev) { _conversation.actorsMustBeWithinTalkdistance = ev.IsChecked(); });
71 findNamedObject<wxCheckBox>(this, "ConvEditorActorsMustFace")->Bind(wxEVT_CHECKBOX,
72 [&](wxCommandEvent& ev) { _conversation.actorsAlwaysFaceEachOther = ev.IsChecked(); });
73
74 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->Bind(wxEVT_SPINCTRL, [&](wxSpinEvent& ev)
75 {
76 if (!_updateInProgress)
77 {
78 _conversation.maxPlayCount = ev.GetValue();
79 updateWidgets();
80 }
81 });
82
83 // Actor Panel
84 wxPanel* actorPanel = findNamedObject<wxPanel>(this, "ConvEditorActorPanel");
85
86 _actorView = wxutil::TreeView::CreateWithModel(actorPanel, _actorStore.get());
87 _actorView->SetSize(wxSize(350, 160));
88 actorPanel->GetSizer()->Add(_actorView, 1, wxEXPAND);
89
90 _actorView->AppendTextColumn("#", _actorColumns.actorNumber.getColumnIndex(),
91 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
92 _actorView->AppendTextColumn(_("Actor (click to edit)"), _actorColumns.displayName.getColumnIndex(),
93 wxDATAVIEW_CELL_EDITABLE, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
94
95 _actorView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
96 wxDataViewEventHandler(ConversationEditor::onActorSelectionChanged), NULL, this);
97 _actorView->Connect(wxEVT_DATAVIEW_ITEM_EDITING_DONE,
98 wxDataViewEventHandler(ConversationEditor::onActorEdited), NULL, this);
99
100 // Wire up button signals
101 _addActorButton = findNamedObject<wxButton>(this, "ConvEditorAddActorButton");
102 _addActorButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onAddActor), NULL, this);
103
104 _delActorButton = findNamedObject<wxButton>(this, "ConvEditorDeleteActorButton");
105 _delActorButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onDeleteActor), NULL, this);
106 _delActorButton->Enable(false);
107
108 _validateActorsButton = findNamedObject<wxButton>(this, "ConvEditorValidateActorsButton");
109 _validateActorsButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onValidateActors), NULL, this);
110 _validateActorsButton->Enable(false);
111
112 // Command Panel
113 wxPanel* commandPanel = findNamedObject<wxPanel>(this, "ConvEditorCommandPanel");
114
115 _commandView = wxutil::TreeView::CreateWithModel(commandPanel, _commandStore.get());
116 _commandView->SetSize(wxSize(350, 200));
117 commandPanel->GetSizer()->Add(_commandView, 1, wxEXPAND);
118
119 _commandView->AppendTextColumn("#", _commandColumns.cmdNumber.getColumnIndex(),
120 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
121 _commandView->AppendTextColumn(_("Actor"), _commandColumns.actorName.getColumnIndex(),
122 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
123 _commandView->AppendTextColumn(_("Command"), _commandColumns.sentence.getColumnIndex(),
124 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
125 _commandView->AppendTextColumn(_("Wait"), _commandColumns.wait.getColumnIndex(),
126 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
127
128 _commandView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
129 wxDataViewEventHandler(ConversationEditor::onCommandSelectionChanged), NULL, this);
130
131 // Wire up buttons
132 _addCmdButton = findNamedObject<wxButton>(this, "ConvEditorAddCommandButton");
133 _addCmdButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onAddCommand), NULL, this);
134
135 _delCmdButton = findNamedObject<wxButton>(this, "ConvEditorDeleteCommandButton");
136 _delCmdButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onDeleteCommand), NULL, this);
137 _delCmdButton->Enable(false);
138
139 _editCmdButton = findNamedObject<wxButton>(this, "ConvEditorEditCommandButton");
140 _editCmdButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onEditCommand), NULL, this);
141 _editCmdButton->Enable(false);
142
143 _moveUpCmdButton = findNamedObject<wxButton>(this, "ConvEditorMoveUpCommandButton");
144 _moveUpCmdButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onMoveUpCommand), NULL, this);
145 _moveUpCmdButton->Enable(false);
146
147 _moveDownCmdButton = findNamedObject<wxButton>(this, "ConvEditorMoveDownCommandButton");
148 _moveDownCmdButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onMoveDownCommand), NULL, this);
149 _moveDownCmdButton->Enable(false);
150
151 // Dialog buttons
152 findNamedObject<wxButton>(this, "ConvEditorCancelButton")->Connect(
153 wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onCancel), NULL, this);
154 findNamedObject<wxButton>(this, "ConvEditorOkButton")->Connect(
155 wxEVT_BUTTON, wxCommandEventHandler(ConversationEditor::onSave), NULL, this);
156 }
157
158 void ConversationEditor::updateWidgets()
159 {
160 _updateInProgress = true;
161
162 // Clear the liststore first
163 _actorStore->Clear();
164
165 _currentActor = wxDataViewItem();
166 _currentCommand = wxDataViewItem();
167
168 updateCmdActionSensitivity(false);
169 _delActorButton->Enable(false);
170
171 _validateActorsButton->Enable(!_conversation.actors.empty());
172
173 // Name
174 findNamedObject<wxTextCtrl>(this, "ConvEditorNameEntry")->SetValue(_conversation.name);
175
176 findNamedObject<wxCheckBox>(this, "ConvEditorActorsWithinTalkDistance")->SetValue(
177 _conversation.actorsMustBeWithinTalkdistance);
178 findNamedObject<wxCheckBox>(this, "ConvEditorActorsMustFace")->SetValue(
179 _conversation.actorsAlwaysFaceEachOther);
180
181 // Update the max play count
182 if (_conversation.maxPlayCount != -1)
183 {
184 // Max play count is enabled
185 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->Enable(true);
186 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->SetValue(_conversation.maxPlayCount);
187
188 findNamedObject<wxStaticText>(this, "ConvEditorRepeatAdditionalText")->Enable(true);
189 findNamedObject<wxCheckBox>(this, "ConvEditorRepeatCheckbox")->SetValue(true);
190 }
191 else
192 {
193 // Max play count disabled
194 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->Enable(false);
195 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->SetValue(-1);
196
197 findNamedObject<wxStaticText>(this, "ConvEditorRepeatAdditionalText")->Enable(false);
198 findNamedObject<wxCheckBox>(this, "ConvEditorRepeatCheckbox")->SetValue(false);
199 }
200
201 // Actors
202 for (conversation::Conversation::ActorMap::const_iterator i = _conversation.actors.begin();
203 i != _conversation.actors.end(); ++i)
204 {
205 wxutil::TreeModel::Row row = _actorStore->AddItem();
206
207 row[_actorColumns.actorNumber] = i->first;
208 row[_actorColumns.displayName] = i->second;
209
210 row.SendItemAdded();
211 }
212
213 // Commands
214 updateCommandList();
215
216 _updateInProgress = false;
217 }
218
219 void ConversationEditor::updateCommandList()
220 {
221 _commandStore->Clear();
222
223 // Commands
224 for (conversation::Conversation::CommandMap::const_iterator i = _conversation.commands.begin();
225 i != _conversation.commands.end(); ++i)
226 {
227 const conversation::ConversationCommand& cmd = *(i->second);
228
229 wxutil::TreeModel::Row row = _commandStore->AddItem();
230
231 row[_commandColumns.cmdNumber] = i->first;
232 row[_commandColumns.actorName] = fmt::format(_("Actor {0:d}"), cmd.actor);
233 row[_commandColumns.sentence] = removeMarkup(cmd.getSentence());
234 row[_commandColumns.wait] = cmd.waitUntilFinished ? _("yes") : _("no");
235
236 row.SendItemAdded();
237 }
238 }
239
240 void ConversationEditor::selectCommand(int index)
241 {
242 // Select the actor passed from the command
243 wxDataViewItem found = _commandStore->FindInteger(index, _commandColumns.cmdNumber);
244 _commandView->Select(found);
245
246 // Update sensitivity based on the new selection
247 _currentCommand = _commandView->GetSelection();
248 updateCmdActionSensitivity(_currentCommand.IsOk());
249 }
250
251 void ConversationEditor::moveSelectedCommand(int delta)
252 {
253 // Get the index of the currently selected command
254 wxutil::TreeModel::Row row(_currentCommand, *_commandStore);
255 int index = row[_commandColumns.cmdNumber].getInteger();
256
257 int targetIndex = index + delta;
258
259 if (targetIndex <= 0)
260 {
261 return; // can't move any more upwards
262 }
263
264 // Try to look up the command indices in the conversation
265 conversation::Conversation::CommandMap::iterator oldCmd = _conversation.commands.find(index);
266 conversation::Conversation::CommandMap::iterator newCmd = _conversation.commands.find(targetIndex);
267
268 if (oldCmd != _conversation.commands.end() && newCmd != _conversation.commands.end())
269 {
270 // There is a command at this position, swap it
271 conversation::ConversationCommandPtr temp = newCmd->second;
272 newCmd->second = oldCmd->second;
273 oldCmd->second = temp;
274
275 updateWidgets();
276
277 // Select the moved command, for the user's convenience
278 selectCommand(newCmd->first);
279 }
280 }
281
282 void ConversationEditor::save()
283 {
284 // Name
285 _conversation.name = findNamedObject<wxTextCtrl>(this, "ConvEditorNameEntry")->GetValue();
286
287 _conversation.actorsMustBeWithinTalkdistance =
288 findNamedObject<wxCheckBox>(this, "ConvEditorActorsWithinTalkDistance")->GetValue();
289 _conversation.actorsAlwaysFaceEachOther =
290 findNamedObject<wxCheckBox>(this, "ConvEditorActorsMustFace")->GetValue();
291
292 if (findNamedObject<wxCheckBox>(this, "ConvEditorRepeatCheckbox")->GetValue())
293 {
294 _conversation.maxPlayCount = findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->GetValue();
295 }
296 else
297 {
298 _conversation.maxPlayCount = -1;
299 }
300
301 // Copy the working copy over the actual object
302 _targetConversation = _conversation;
303 }
304
305 void ConversationEditor::onSave(wxCommandEvent& ev)
306 {
307 // First, save to the conversation object
308 save();
309
310 // Then close the window
311 EndModal(wxID_OK);
312 }
313
314 void ConversationEditor::onCancel(wxCommandEvent& ev)
315 {
316 EndModal(wxID_CANCEL);
317 }
318
319 void ConversationEditor::onActorSelectionChanged(wxDataViewEvent& ev)
320 {
321 if (_updateInProgress) return;
322
323 // Get the selection
324 _currentActor = _actorView->GetSelection();
325
326 // Enable the delete buttons if we have a selection
327 _delActorButton->Enable(_currentActor.IsOk());
328 }
329
330 void ConversationEditor::updateCmdActionSensitivity(bool hasSelection)
331 {
332 // Enable the edit and delete buttons if we have a selection
333 _editCmdButton->Enable(hasSelection);
334 _delCmdButton->Enable(hasSelection);
335
336 if (hasSelection)
337 {
338 // Check if this is the first command in the list, get the ID of the selected item
339 wxutil::TreeModel::Row row(_currentCommand, *_commandStore);
340 int index = row[_commandColumns.cmdNumber].getInteger();
341
342 bool hasNext = _conversation.commands.find(index+1) != _conversation.commands.end();
343 bool hasPrev = index > 1;
344
345 _moveUpCmdButton->Enable(hasPrev);
346 _moveDownCmdButton->Enable(hasNext);
347 }
348 else
349 {
350 _moveUpCmdButton->Enable(false);
351 _moveDownCmdButton->Enable(false);
352 }
353 }
354
355 void ConversationEditor::onCommandSelectionChanged(wxDataViewEvent& ev)
356 {
357 if (_updateInProgress) return;
358
359 // Get the selection
360 _currentCommand = _commandView->GetSelection();
361
362 updateCmdActionSensitivity(_currentCommand.IsOk());
363 }
364
365 void ConversationEditor::onMaxPlayCountEnabled(wxCommandEvent& ev)
366 {
367 if (_updateInProgress) return;
368
369 if (findNamedObject<wxCheckBox>(this, "ConvEditorRepeatCheckbox")->GetValue())
370 {
371 // Enabled, write a new value in the spin button
372 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->SetValue(1);
373 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->Enable(true);
374 findNamedObject<wxStaticText>(this, "ConvEditorRepeatAdditionalText")->Enable(true);
375 }
376 else
377 {
378 // Disabled, write a -1 in the spin button
379 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->SetValue(-1);
380
381 findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->Enable(false);
382 findNamedObject<wxStaticText>(this, "ConvEditorRepeatAdditionalText")->Enable(false);
383 }
384
385 // Update the value in the conversation as well
386 _conversation.maxPlayCount = findNamedObject<wxSpinCtrl>(this, "ConvEditorRepeatTimes")->GetValue();
387 }
388
389 void ConversationEditor::onAddActor(wxCommandEvent& ev)
390 {
391 // Get the lowest available actor ID
392 int idx = 1;
393
394 for (idx = 1; idx < INT_MAX; idx++)
395 {
396 if (_conversation.actors.find(idx) == _conversation.actors.end())
397 {
398 break;
399 }
400 }
401
402 // Add the new actor to the map
403 _conversation.actors[idx] = _("New Actor");
404
405 // Update the widgets
406 updateWidgets();
407 }
408
409 void ConversationEditor::onDeleteActor(wxCommandEvent& ev)
410 {
411 // Get the index of the currently selected actor
412 wxutil::TreeModel::Row row(_currentActor, *_actorStore);
413 int index = row[_actorColumns.actorNumber].getInteger();
414
415 // Add the new actor to the map
416 conversation::Conversation::ActorMap::iterator i = _conversation.actors.find(index);
417
418 if (i != _conversation.actors.end())
419 {
420 // Remove the specified actor
421 _conversation.actors.erase(index);
422 }
423 else
424 {
425 // Index not found, quit here
426 return;
427 }
428
429 // Adjust the numbers of all other actors with higher numbers
430 while (_conversation.actors.find(index + 1) != _conversation.actors.end())
431 {
432 // Move the actor with the higher index "down" by one number...
433 _conversation.actors[index] = _conversation.actors[index + 1];
434 // ...and remove it from the old location
435 _conversation.actors.erase(index + 1);
436
437 index++;
438 }
439
440 // Update the widgets
441 updateWidgets();
442 }
443
444 void ConversationEditor::onValidateActors(wxCommandEvent& ev)
445 {
446 std::vector<std::string> errors;
447
448 for (const conversation::Conversation::ActorMap::value_type& i : _conversation.actors)
449 {
450 scene::ActorNodeFinder finder(i.second);
451 GlobalSceneGraph().root()->traverse(finder);
452
453 if (!finder.getFoundNode())
454 {
455 errors.push_back(fmt::format(_("The actor {0} cannot be found in the current map."), i.second));
456 }
457 }
458
459 if (!errors.empty())
460 {
461 std::string message = string::join(errors, "\n");
462 wxutil::Messagebox::Show(_("Actors missing"), message, IDialog::MESSAGE_ERROR, this);
463 }
464 else
465 {
466 std::string message = _("All actors are correctly referring to entities in the map.");
467 wxutil::Messagebox::Show(_("Actors OK"), message, IDialog::MESSAGE_CONFIRM, this);
468 }
469 }
470
471 void ConversationEditor::onActorEdited(wxDataViewEvent& ev)
472 {
473 if (ev.IsEditCancelled())
474 {
475 return;
476 }
477
478 wxutil::TreeModel::Row row(ev.GetItem(), *_actorStore);
479
480 // The iter points to the edited cell now, get the actor number
481 int actorNum = row[_actorColumns.actorNumber].getInteger();
482
483 #if wxCHECK_VERSION(3, 1, 0)
484 // wx 3.1+ delivers the new value through the event
485 std::string actor = ev.GetValue().GetString().ToStdString();
486 #else
487 // wx 3.0.x already has the value set in the model
488 std::string actor = row[_actorColumns.displayName];
489 #endif
490 _conversation.actors[actorNum] = actor;
491
492 // Update all command widgets
493 updateCommandList();
494 }
495
496 void ConversationEditor::onAddCommand(wxCommandEvent& ev)
497 {
498 conversation::Conversation& conv = _conversation; // shortcut
499
500 // Create a new command
501 conversation::ConversationCommandPtr command(new conversation::ConversationCommand);
502
503 // Construct a command editor (blocks on construction)
504 CommandEditor* editor = new CommandEditor(this, *command, conv);
505
506 if (editor->ShowModal() == wxID_OK)
507 {
508 // The user hit ok, insert the command, find the first free index
509 int index = 1;
510 while (conv.commands.find(index) != conv.commands.end())
511 {
512 index++;
513 }
514
515 // Insert the command at the new location
516 conv.commands[index] = command;
517
518 updateWidgets();
519 }
520
521 editor->Destroy();
522 }
523
524 void ConversationEditor::onEditCommand(wxCommandEvent& ev)
525 {
526 // Get the index of the currently selected command
527 wxutil::TreeModel::Row row(_currentCommand, *_commandStore);
528 int index = row[_commandColumns.cmdNumber].getInteger();
529
530 // Try to look up the command in the conversation
531 conversation::Conversation::CommandMap::iterator i = _conversation.commands.find(index);
532
533 if (i != _conversation.commands.end())
534 {
535 // Get the command reference
536 conversation::ConversationCommandPtr command = i->second;
537
538 // Construct a command editor (blocks on construction)
539 CommandEditor* editor = new CommandEditor(this, *command, _conversation);
540
541 if (editor->ShowModal() == wxID_OK)
542 {
543 updateWidgets();
544 }
545
546 editor->Destroy();
547 }
548 }
549
550 void ConversationEditor::onMoveUpCommand(wxCommandEvent& ev)
551 {
552 // Pass the call
553 moveSelectedCommand(-1);
554 }
555
556 void ConversationEditor::onMoveDownCommand(wxCommandEvent& ev)
557 {
558 // Pass the call
559 moveSelectedCommand(+1);
560 }
561
562 void ConversationEditor::onDeleteCommand(wxCommandEvent& ev)
563 {
564 // Get the index of the currently selected command
565 wxutil::TreeModel::Row row(_currentCommand, *_commandStore);
566 int index = row[_commandColumns.cmdNumber].getInteger();
567
568 // Add the new command to the map
569 conversation::Conversation::CommandMap::iterator i = _conversation.commands.find(index);
570
571 if (i != _conversation.commands.end()) {
572 // Remove the specified command
573 _conversation.commands.erase(index);
574 }
575 else {
576 // Index not found, quit here
577 return;
578 }
579
580 // Adjust the numbers of all other commands with higher numbers
581 while (_conversation.commands.find(index + 1) != _conversation.commands.end()) {
582 // Move the commands with the higher index "down" by one number...
583 _conversation.commands[index] = _conversation.commands[index + 1];
584 // ...and remove it from the old location
585 _conversation.commands.erase(index + 1);
586
587 index++;
588 }
589
590 // Update the widgets
591 updateWidgets();
592 }
593
594 std::string ConversationEditor::removeMarkup(const std::string& input)
595 {
596 std::regex expr("(<[A-Za-z]+>)|(</[A-Za-z]+>)");
597 return std::regex_replace(input, expr, "");
598 }
599
600 } // namespace ui