| 1 |
#include "ConversationDialog.h" |
| 2 |
|
| 3 |
#include "i18n.h" |
| 4 |
#include "iregistry.h" |
| 5 |
#include "iundo.h" |
| 6 |
#include "ieclass.h" |
| 7 |
#include "ui/imainframe.h" |
| 8 |
#include "iscenegraph.h" |
| 9 |
#include "string/string.h" |
| 10 |
|
| 11 |
#include "wxutil/dataview/TreeModel.h" |
| 12 |
#include "wxutil/dialog/MessageBox.h" |
| 13 |
|
| 14 |
#include "RandomOrigin.h" |
| 15 |
#include "ConversationEntityFinder.h" |
| 16 |
#include "ConversationEditor.h" |
| 17 |
|
| 18 |
#include <wx/panel.h> |
| 19 |
#include <wx/button.h> |
| 20 |
#include <wx/sizer.h> |
| 21 |
|
| 22 |
#include <fmt/format.h> |
| 23 |
#include <iostream> |
| 24 |
|
| 25 |
namespace ui |
| 26 |
{ |
| 27 |
|
| 28 |
namespace |
| 29 |
{ |
| 30 |
const char* const WINDOW_TITLE = N_("Conversation Editor"); |
| 31 |
const std::string CONVERSATION_ENTITY_CLASS = "atdm:conversation_info"; |
| 32 |
} |
| 33 |
|
| 34 |
ConversationDialog::ConversationDialog() : |
| 35 |
DialogBase(_(WINDOW_TITLE)), |
| 36 |
_entityList(new wxutil::TreeModel(_convEntityColumns, true)), |
| 37 |
_entityView(nullptr), |
| 38 |
_convList(new wxutil::TreeModel(_convColumns, true)), |
| 39 |
_convView(nullptr), |
| 40 |
_addConvButton(nullptr), |
| 41 |
_editConvButton(nullptr), |
| 42 |
_deleteConvButton(nullptr), |
| 43 |
_moveUpConvButton(nullptr), |
| 44 |
_moveDownConvButton(nullptr), |
| 45 |
_clearConvButton(nullptr), |
| 46 |
_addEntityButton(nullptr), |
| 47 |
_deleteEntityButton(nullptr) |
| 48 |
{ |
| 49 |
// Create the widgets |
| 50 |
populateWindow(); |
| 51 |
|
| 52 |
FitToScreen(0.3f, 0.5f); |
| 53 |
} |
| 54 |
|
| 55 |
void ConversationDialog::populateWindow() |
| 56 |
{ |
| 57 |
loadNamedPanel(this, "ConvDialogMainPanel"); |
| 58 |
|
| 59 |
wxPanel* entityPanel = findNamedObject<wxPanel>(this, "ConvDialogEntityPanel"); |
| 60 |
|
| 61 |
// Entity Tree View |
| 62 |
_entityView = wxutil::TreeView::CreateWithModel(entityPanel, _entityList.get(), wxDV_NO_HEADER); |
| 63 |
entityPanel->GetSizer()->Add(_entityView, 1, wxEXPAND); |
| 64 |
|
| 65 |
_entityView->AppendTextColumn("", _convEntityColumns.displayName.getColumnIndex(), |
| 66 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE); |
| 67 |
|
| 68 |
_entityView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, |
| 69 |
wxDataViewEventHandler(ConversationDialog::onEntitySelectionChanged), NULL, this); |
| 70 |
|
| 71 |
// Wire up button signals |
| 72 |
_addEntityButton = findNamedObject<wxButton>(this, "ConvDialogAddEntityButton"); |
| 73 |
_addEntityButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onAddEntity), NULL, this); |
| 74 |
|
| 75 |
_deleteEntityButton = findNamedObject<wxButton>(this, "ConvDialogDeleteEntityButton"); |
| 76 |
_deleteEntityButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onDeleteEntity), NULL, this); |
| 77 |
_deleteEntityButton->Enable(false); |
| 78 |
|
| 79 |
wxPanel* convPanel = findNamedObject<wxPanel>(this, "ConvDialogConversationPanel"); |
| 80 |
|
| 81 |
_convView = wxutil::TreeView::CreateWithModel(convPanel, _convList.get()); |
| 82 |
|
| 83 |
// Key and value text columns |
| 84 |
_convView->AppendTextColumn("#", _convColumns.index.getColumnIndex(), |
| 85 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE); |
| 86 |
_convView->AppendTextColumn(_("Name"), _convColumns.name.getColumnIndex(), |
| 87 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE); |
| 88 |
|
| 89 |
_convView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ConversationDialog::onConversationSelectionChanged, this); |
| 90 |
_convView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [this](auto&) { editSelectedConversation(); }); |
| 91 |
|
| 92 |
convPanel->GetSizer()->Add(_convView, 1, wxEXPAND); |
| 93 |
|
| 94 |
convPanel->Enable(false); |
| 95 |
|
| 96 |
// Wire up button signals |
| 97 |
_addConvButton = findNamedObject<wxButton>(this, "ConvDialogAddConvButton"); |
| 98 |
_addConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onAddConversation, this); |
| 99 |
_addConvButton->Enable(false); // not enabled without selection |
| 100 |
|
| 101 |
_editConvButton = findNamedObject<wxButton>(this, "ConvDialogEditConvButton"); |
| 102 |
_editConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onEditConversation, this); |
| 103 |
_editConvButton->Enable(false); // not enabled without selection |
| 104 |
|
| 105 |
_deleteConvButton = findNamedObject<wxButton>(this, "ConvDialogDeleteConvButton"); |
| 106 |
_deleteConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onDeleteConversation, this); |
| 107 |
_deleteConvButton->Enable(false); // not enabled without selection |
| 108 |
|
| 109 |
_moveUpConvButton = findNamedObject<wxButton>(this, "ConvDialogMoveUpConvButton"); |
| 110 |
_moveUpConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onMoveConversationUpOrDown, this); |
| 111 |
_moveUpConvButton->Enable(false); // not enabled without selection |
| 112 |
|
| 113 |
_moveDownConvButton = findNamedObject<wxButton>(this, "ConvDialogMoveDownConvButton"); |
| 114 |
_moveDownConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onMoveConversationUpOrDown, this); |
| 115 |
_moveDownConvButton->Enable(false); // not enabled without selection |
| 116 |
|
| 117 |
_clearConvButton = findNamedObject<wxButton>(this, "ConvDialogClearConvButton"); |
| 118 |
_clearConvButton->Bind(wxEVT_BUTTON, &ConversationDialog::onClearConversations, this); |
| 119 |
_clearConvButton->Enable(false); // requires >0 conversations |
| 120 |
|
| 121 |
// Boldify a few labels |
| 122 |
makeLabelBold(this, "ConvDialogEntityLabel"); |
| 123 |
makeLabelBold(this, "ConvDialogConvLabel"); |
| 124 |
|
| 125 |
// Connect dialog buttons |
| 126 |
findNamedObject<wxButton>(this, "ConvDialogCancelButton")->Connect( |
| 127 |
wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onCancel), NULL, this); |
| 128 |
findNamedObject<wxButton>(this, "ConvDialogOkButton")->Connect( |
| 129 |
wxEVT_BUTTON, wxCommandEventHandler(ConversationDialog::onOK), NULL, this); |
| 130 |
} |
| 131 |
|
| 132 |
void ConversationDialog::save() |
| 133 |
{ |
| 134 |
// Consistency check can go here |
| 135 |
|
| 136 |
// Scoped undo object |
| 137 |
UndoableCommand command("editConversations"); |
| 138 |
|
| 139 |
// Save the working set to the entity |
| 140 |
for (conversation::ConversationEntityMap::iterator i = _entities.begin(); |
| 141 |
i != _entities.end(); |
| 142 |
++i) |
| 143 |
{ |
| 144 |
i->second->writeToEntity(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void ConversationDialog::clear() |
| 149 |
{ |
| 150 |
// Clear internal data |
| 151 |
_entities.clear(); |
| 152 |
_curEntity = _entities.end(); |
| 153 |
|
| 154 |
// Clear the list boxes |
| 155 |
_entityList->Clear(); |
| 156 |
_convList->Clear(); |
| 157 |
} |
| 158 |
|
| 159 |
void ConversationDialog::refreshConversationList() |
| 160 |
{ |
| 161 |
// Clear and refresh the conversation list |
| 162 |
_convList->Clear(); |
| 163 |
_curEntity->second->populateListStore(*_convList, _convColumns); |
| 164 |
|
| 165 |
// If there is at least one conversation, make the Clear button available |
| 166 |
_clearConvButton->Enable(!_curEntity->second->isEmpty()); |
| 167 |
|
| 168 |
handleConversationSelectionChange(); |
| 169 |
} |
| 170 |
|
| 171 |
void ConversationDialog::populateWidgets() |
| 172 |
{ |
| 173 |
// First clear the data |
| 174 |
clear(); |
| 175 |
|
| 176 |
// Use an ConversationEntityFinder to walk the map and add any conversation |
| 177 |
// entities to the liststore and entity map |
| 178 |
conversation::ConversationEntityFinder finder( |
| 179 |
_entityList, |
| 180 |
_convEntityColumns, |
| 181 |
_entities, |
| 182 |
CONVERSATION_ENTITY_CLASS |
| 183 |
); |
| 184 |
|
| 185 |
GlobalSceneGraph().root()->traverseChildren(finder); |
| 186 |
|
| 187 |
updateConversationPanelSensitivity(); |
| 188 |
} |
| 189 |
|
| 190 |
int ConversationDialog::ShowModal() |
| 191 |
{ |
| 192 |
populateWidgets(); |
| 193 |
|
| 194 |
int returnCode = DialogBase::ShowModal(); |
| 195 |
|
| 196 |
if (returnCode == wxID_OK) |
| 197 |
{ |
| 198 |
save(); |
| 199 |
} |
| 200 |
|
| 201 |
return returnCode; |
| 202 |
} |
| 203 |
|
| 204 |
// Static command target |
| 205 |
void ConversationDialog::ShowDialog(const cmd::ArgumentList& args) |
| 206 |
{ |
| 207 |
// Construct a new instance, this enters the main loop |
| 208 |
ConversationDialog* editor = new ConversationDialog; |
| 209 |
|
| 210 |
editor->ShowModal(); |
| 211 |
editor->Destroy(); |
| 212 |
} |
| 213 |
|
| 214 |
void ConversationDialog::updateConversationPanelSensitivity() |
| 215 |
{ |
| 216 |
// Clear the conversations list |
| 217 |
_convList->Clear(); |
| 218 |
|
| 219 |
wxDataViewItem item = _entityView->GetSelection(); |
| 220 |
|
| 221 |
if (item.IsOk()) |
| 222 |
{ |
| 223 |
// Get name of the entity and find the corresponding ConversationEntity in the map |
| 224 |
wxutil::TreeModel::Row row(item, *_entityList); |
| 225 |
|
| 226 |
std::string name = row[_convEntityColumns.entityName]; |
| 227 |
|
| 228 |
// Save the current selection and refresh the conversation list |
| 229 |
_curEntity = _entities.find(name); |
| 230 |
refreshConversationList(); |
| 231 |
|
| 232 |
// Enable the delete button and conversation panel |
| 233 |
_deleteEntityButton->Enable(true); |
| 234 |
|
| 235 |
findNamedObject<wxPanel>(this, "ConvDialogConversationPanel")->Enable(true); |
| 236 |
_addConvButton->Enable(true); |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
// No selection, disable the delete button and clear the conversation panel |
| 241 |
_deleteEntityButton->Enable(false); |
| 242 |
|
| 243 |
// Disable all the Conversation buttons |
| 244 |
findNamedObject<wxPanel>(this, "ConvDialogConversationPanel")->Enable(false); |
| 245 |
_addConvButton->Enable(false); |
| 246 |
_editConvButton->Enable(false); |
| 247 |
_deleteConvButton->Enable(false); |
| 248 |
_moveUpConvButton->Enable(false); |
| 249 |
_moveDownConvButton->Enable(false); |
| 250 |
_clearConvButton->Enable(false); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// Callback for conversation entity selection changed in list box |
| 255 |
void ConversationDialog::onEntitySelectionChanged(wxDataViewEvent& ev) |
| 256 |
{ |
| 257 |
updateConversationPanelSensitivity(); |
| 258 |
} |
| 259 |
|
| 260 |
void ConversationDialog::onOK(wxCommandEvent& ev) |
| 261 |
{ |
| 262 |
EndModal(wxID_OK); |
| 263 |
} |
| 264 |
|
| 265 |
void ConversationDialog::onCancel(wxCommandEvent& ev) |
| 266 |
{ |
| 267 |
EndModal(wxID_CANCEL); |
| 268 |
} |
| 269 |
|
| 270 |
// Add a new conversations entity button |
| 271 |
void ConversationDialog::onAddEntity(wxCommandEvent& ev) |
| 272 |
{ |
| 273 |
// Obtain the entity class object |
| 274 |
IEntityClassPtr eclass = |
| 275 |
GlobalEntityClassManager().findClass(CONVERSATION_ENTITY_CLASS); |
| 276 |
|
| 277 |
if (eclass) |
| 278 |
{ |
| 279 |
UndoableCommand addEntityCommand("addConversationEntity"); |
| 280 |
|
| 281 |
// Construct a Node of this entity type |
| 282 |
IEntityNodePtr node(GlobalEntityModule().createEntity(eclass)); |
| 283 |
|
| 284 |
// Create a random offset |
| 285 |
node->getEntity().setKeyValue( |
| 286 |
"origin", RandomOrigin::generate(128) |
| 287 |
); |
| 288 |
|
| 289 |
// Insert the node into the scene graph |
| 290 |
assert(GlobalSceneGraph().root()); |
| 291 |
GlobalSceneGraph().root()->addChildNode(node); |
| 292 |
|
| 293 |
// Refresh the widgets |
| 294 |
populateWidgets(); |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
// conversation entityclass was not found |
| 299 |
wxutil::Messagebox::ShowError( |
| 300 |
fmt::format(_("Unable to create conversation Entity: class '{0}' not found."), |
| 301 |
CONVERSATION_ENTITY_CLASS) |
| 302 |
); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// Delete entity button |
| 307 |
void ConversationDialog::onDeleteEntity(wxCommandEvent& ev) |
| 308 |
{ |
| 309 |
// Get the Node* from the tree model and remove it from the scenegraph |
| 310 |
wxDataViewItem item = _entityView->GetSelection(); |
| 311 |
|
| 312 |
if (item) |
| 313 |
{ |
| 314 |
// Get the name of the selected entity |
| 315 |
wxutil::TreeModel::Row row(item, *_entityList); |
| 316 |
std::string name = row[_convEntityColumns.entityName]; |
| 317 |
|
| 318 |
// Instruct the ConversationEntity to delete its world node, and then |
| 319 |
// remove it from the map |
| 320 |
_entities[name]->deleteWorldNode(); |
| 321 |
_entities.erase(name); |
| 322 |
|
| 323 |
// Update the widgets to remove the selection from the list |
| 324 |
populateWidgets(); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
// Callback for current conversation selection changed |
| 329 |
void ConversationDialog::onConversationSelectionChanged(wxDataViewEvent& ev) |
| 330 |
{ |
| 331 |
handleConversationSelectionChange(); |
| 332 |
} |
| 333 |
|
| 334 |
void ConversationDialog::handleConversationSelectionChange() |
| 335 |
{ |
| 336 |
// Get the selection |
| 337 |
_currentConversation = _convView->GetSelection(); |
| 338 |
int index = getSelectedConvIndex(); |
| 339 |
|
| 340 |
if (_currentConversation.IsOk()) |
| 341 |
{ |
| 342 |
// Enable the edit and delete buttons |
| 343 |
_editConvButton->Enable(true); |
| 344 |
_deleteConvButton->Enable(true); |
| 345 |
|
| 346 |
_moveUpConvButton->Enable(index > 1); |
| 347 |
_moveDownConvButton->Enable(index < _curEntity->second->getHighestIndex()); |
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
// Disable all manipulation buttons |
| 352 |
_editConvButton->Enable(false); |
| 353 |
_deleteConvButton->Enable(false); |
| 354 |
_moveUpConvButton->Enable(false); |
| 355 |
_moveDownConvButton->Enable(false); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
void ConversationDialog::onAddConversation(wxCommandEvent& ev) |
| 360 |
{ |
| 361 |
// Add a new conversation to the ConversationEntity and refresh the list store |
| 362 |
int newIndex = _curEntity->second->addConversation(); |
| 363 |
|
| 364 |
refreshConversationList(); |
| 365 |
|
| 366 |
selectConvByIndex(newIndex); |
| 367 |
} |
| 368 |
|
| 369 |
int ConversationDialog::getSelectedConvIndex() |
| 370 |
{ |
| 371 |
if (!_currentConversation.IsOk()) |
| 372 |
{ |
| 373 |
return -1; |
| 374 |
} |
| 375 |
|
| 376 |
// Retrieve the index of the current conversation |
| 377 |
wxutil::TreeModel::Row row(_currentConversation, *_convList); |
| 378 |
return row[_convColumns.index].getInteger(); |
| 379 |
} |
| 380 |
|
| 381 |
void ConversationDialog::selectConvByIndex(int index) |
| 382 |
{ |
| 383 |
auto item = _convList->FindInteger(index, _convColumns.index); |
| 384 |
_convView->Select(item); |
| 385 |
|
| 386 |
handleConversationSelectionChange(); |
| 387 |
} |
| 388 |
|
| 389 |
void ConversationDialog::editSelectedConversation() |
| 390 |
{ |
| 391 |
int index = getSelectedConvIndex(); |
| 392 |
|
| 393 |
if (index == -1) return; |
| 394 |
|
| 395 |
auto& conv = _curEntity->second->getConversation(index); |
| 396 |
|
| 397 |
// Display the edit dialog, blocks on construction |
| 398 |
auto editor = new ConversationEditor(this, conv); |
| 399 |
|
| 400 |
editor->ShowModal(); |
| 401 |
editor->Destroy(); |
| 402 |
|
| 403 |
// Repopulate the conversation list |
| 404 |
refreshConversationList(); |
| 405 |
} |
| 406 |
|
| 407 |
void ConversationDialog::onEditConversation(wxCommandEvent& ev) |
| 408 |
{ |
| 409 |
editSelectedConversation(); |
| 410 |
} |
| 411 |
|
| 412 |
void ConversationDialog::onDeleteConversation(wxCommandEvent& ev) |
| 413 |
{ |
| 414 |
int index = getSelectedConvIndex(); |
| 415 |
|
| 416 |
// Tell the ConversationEntity to delete this conversation |
| 417 |
_curEntity->second->deleteConversation(index); |
| 418 |
|
| 419 |
// Repopulate the conversation list |
| 420 |
refreshConversationList(); |
| 421 |
} |
| 422 |
|
| 423 |
void ConversationDialog::onMoveConversationUpOrDown(wxCommandEvent& ev) |
| 424 |
{ |
| 425 |
bool moveUp = ev.GetEventObject() == _moveUpConvButton; |
| 426 |
int index = getSelectedConvIndex(); |
| 427 |
|
| 428 |
int newIndex = _curEntity->second->moveConversation(index, moveUp); |
| 429 |
|
| 430 |
// Repopulate the conversation list |
| 431 |
refreshConversationList(); |
| 432 |
|
| 433 |
// Try to select the moved item |
| 434 |
selectConvByIndex(newIndex); |
| 435 |
} |
| 436 |
|
| 437 |
void ConversationDialog::onClearConversations(wxCommandEvent& ev) |
| 438 |
{ |
| 439 |
// Clear the entity and refresh the list |
| 440 |
_curEntity->second->clearConversations(); |
| 441 |
refreshConversationList(); |
| 442 |
} |
| 443 |
|
| 444 |
} // namespace ui |