diff --git a/code/missioneditor/sexp_tree_model.cpp b/code/missioneditor/sexp_tree_model.cpp index 7e45afc5b89..7a5e396e2c7 100644 --- a/code/missioneditor/sexp_tree_model.cpp +++ b/code/missioneditor/sexp_tree_model.cpp @@ -1175,6 +1175,39 @@ int SexpTreeModel::get_loadout_variable_count(int var_index) return count; } +void SexpTreeModel::rename_variable_references(const SCP_unordered_map& new_names) +{ + // all renames are looked up in one pass, so two variables that trade names do not collide + for (int i = 0; i < Num_sexp_nodes; i++) { + auto& node = Sexp_nodes[i]; + if (node.type & SEXP_FLAG_VARIABLE) { + const auto it = new_names.find(node.text); + if (it != new_names.cend()) { + strcpy_s(node.text, it->second.c_str()); + } + } + } + + const auto follow_rename = [&new_names](SCP_string& name) { + const auto it = new_names.find(name); + if (it != new_names.cend()) { + name = it->second; + } + }; + + for (auto& team_datum : Team_data) { + for (auto& entry : team_datum.ship_choices) { + follow_rename(entry.class_variable); + follow_rename(entry.count_variable); + } + + for (auto& entry : team_datum.weapon_choices) { + follow_rename(entry.class_variable); + follow_rename(entry.count_variable); + } + } +} + // Count how many tree nodes are container name or container data references // matching the given container_name. int SexpTreeModel::get_container_usage_count(const SCP_string& container_name) const diff --git a/code/missioneditor/sexp_tree_model.h b/code/missioneditor/sexp_tree_model.h index 1a2fb7b8003..0314af9542d 100644 --- a/code/missioneditor/sexp_tree_model.h +++ b/code/missioneditor/sexp_tree_model.h @@ -573,6 +573,8 @@ class SexpTreeModel { int get_variable_count(const char* var_name) const; // Count how many times a variable is used in player loadout data (Team_data) static int get_loadout_variable_count(int var_index); + // Point every sexp node and loadout entry naming a renamed variable (old name -> new name) at its new name + static void rename_variable_references(const SCP_unordered_map& new_names); // Count how many tree nodes reference the given container name int get_container_usage_count(const SCP_string& container_name) const; // Check if a specific node is a valid, matching container reference for the given name diff --git a/fred2/sexp_tree_view.cpp b/fred2/sexp_tree_view.cpp index 35f3ae1e7ab..9372feff0fa 100644 --- a/fred2/sexp_tree_view.cpp +++ b/fred2/sexp_tree_view.cpp @@ -921,6 +921,7 @@ BOOL sexp_tree_view::OnCommand(WPARAM wParam, LPARAM lParam) // Don't sort until after modify, since modify uses index if (dlg.m_modified_name) { + SexpTreeModel::rename_variable_references({{old_name, Sexp_variables[sexp_var_index].variable_name}}); sexp_variable_sort(); } diff --git a/qtfred/src/mission/dialogs/FictionViewerDialogModel.cpp b/qtfred/src/mission/dialogs/FictionViewerDialogModel.cpp index f8fede9b783..6044b3ea802 100644 --- a/qtfred/src/mission/dialogs/FictionViewerDialogModel.cpp +++ b/qtfred/src/mission/dialogs/FictionViewerDialogModel.cpp @@ -16,7 +16,8 @@ bool FictionViewerDialogModel::apply() { const bool empty = stage.story_filename[0] == '\0'; if (empty) { - _fictionViewerStages.clear(); + // only the first stage is edited here, so only it goes; a mission can carry more + _fictionViewerStages.erase(_fictionViewerStages.begin()); Mission_music[SCORE_FICTION_VIEWER] = -1; } else { // Keep whatever you've edited in _fictionViewerStages diff --git a/qtfred/src/mission/dialogs/VariableDialogModel.cpp b/qtfred/src/mission/dialogs/VariableDialogModel.cpp index fe04994b987..96a1beae7d9 100644 --- a/qtfred/src/mission/dialogs/VariableDialogModel.cpp +++ b/qtfred/src/mission/dialogs/VariableDialogModel.cpp @@ -1,5 +1,6 @@ #include "VariableDialogModel.h" +#include "missioneditor/sexp_tree_model.h" #include "parse/sexp.h" #include @@ -32,42 +33,43 @@ bool VariableDialogModel::apply() // Loop through our new list of deleted variables and remove them from the global state. for (const auto& original_name : m_deleted_variables) { - for (int i = 0; i < MAX_SEXP_VARIABLES; ++i) { - if (!stricmp(Sexp_variables[i].variable_name, original_name.c_str())) { - sexp_variable_delete(i); - break; - } + const int i = get_index_sexp_variable_name(original_name.c_str()); + if (i >= 0) { + sexp_variable_delete(i); } } - for (const auto& var_info : m_variables) { - bool found = false; - // Existing variable that might be modified or deleted - if (!var_info.originalName.empty()) { - for (int i = 0; i < MAX_SEXP_VARIABLES; ++i) { - if (!stricmp(Sexp_variables[i].variable_name, var_info.originalName.c_str())) { - if (var_info.name != var_info.originalName) { - renamedVariables.emplace_back(i, var_info.originalName); - } - strcpy_s(Sexp_variables[i].variable_name, var_info.name.c_str()); - Sexp_variables[i].type = var_info.flags; - - if (var_info.is_string) { - strcpy_s(Sexp_variables[i].text, var_info.stringValue.c_str()); - Sexp_variables[i].type |= SEXP_VARIABLE_STRING; - } else { - strcpy_s(Sexp_variables[i].text, std::to_string(var_info.numberValue).c_str()); - Sexp_variables[i].type |= SEXP_VARIABLE_NUMBER; - } - found = true; - break; - } - } + // find every existing variable by its old name before any is renamed, or two variables that swap names + // would both find the first one + SCP_vector existing_index(m_variables.size(), -1); + for (size_t v = 0; v < m_variables.size(); ++v) { + if (!m_variables[v].originalName.empty()) { + existing_index[v] = get_index_sexp_variable_name(m_variables[v].originalName.c_str()); } + } - // New variable - if (!found) { - int flags = var_info.flags; + for (size_t v = 0; v < m_variables.size(); ++v) { + const auto& var_info = m_variables[v]; + const int i = existing_index[v]; + // Existing variable that might be modified + if (i >= 0) { + if (var_info.name != var_info.originalName) { + renamedVariables.emplace_back(i, var_info.originalName); + } + strcpy_s(Sexp_variables[i].variable_name, var_info.name.c_str()); + // the stored flags still hold the type the variable was loaded with; the dialog's type is set below + Sexp_variables[i].type = var_info.flags & ~(SEXP_VARIABLE_NUMBER | SEXP_VARIABLE_STRING); + + if (var_info.is_string) { + strcpy_s(Sexp_variables[i].text, var_info.stringValue.c_str()); + Sexp_variables[i].type |= SEXP_VARIABLE_STRING; + } else { + strcpy_s(Sexp_variables[i].text, std::to_string(var_info.numberValue).c_str()); + Sexp_variables[i].type |= SEXP_VARIABLE_NUMBER; + } + } else { + // New variable + int flags = var_info.flags & ~(SEXP_VARIABLE_NUMBER | SEXP_VARIABLE_STRING); const char* value_str; SCP_string num_val_str; @@ -83,6 +85,16 @@ bool VariableDialogModel::apply() } } + // events, cues and the team loadout refer to a variable by name, so a rename has to reach them too + if (!renamedVariables.empty()) { + SCP_unordered_map new_names; + for (const auto& renamed : renamedVariables) { + new_names[renamed.second] = Sexp_variables[renamed.first].variable_name; + } + + SexpTreeModel::rename_variable_references(new_names); + } + // Apply Container Changes SCP_vector new_containers; SCP_unordered_map renamed_containers; @@ -238,6 +250,14 @@ bool VariableDialogModel::checkValidity() if (!names_taken.insert(var.name).second) { error_message += "Duplicate variable name found: \"" + var.name + "\"\n"; } + + // the team loadout reads its variables by type, and a mission saved with the wrong one does not load + const int idx = var.originalName.empty() ? -1 : get_index_sexp_variable_name(var.originalName.c_str()); + if (idx >= 0 && var.is_string != ((Sexp_variables[idx].type & SEXP_VARIABLE_STRING) != 0) && + SexpTreeModel::get_loadout_variable_count(idx) > 0) { + error_message += + "The type of variable \"" + var.name + "\" cannot change while the team loadout uses it.\n"; + } } if (m_variables.size() > MAX_SEXP_VARIABLES) { diff --git a/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp b/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp index 38ab3537d58..3ff6c08e856 100644 --- a/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp +++ b/qtfred/src/mission/dialogs/WingEditorDialogModel.cpp @@ -451,6 +451,10 @@ void WingEditorDialogModel::setWingName(const SCP_string& name) if (!wingIsValid()) return; + // rename_wing resets the display name, so it only runs for a real rename + if (name == _currentWingName) + return; + if (_editor->rename_wing(_currentWingIndex, name)) { modify(_currentWingName, name); Q_EMIT modelChanged(); diff --git a/qtfred/src/ui/FredView.cpp b/qtfred/src/ui/FredView.cpp index a17fef0d342..9987618e46a 100644 --- a/qtfred/src/ui/FredView.cpp +++ b/qtfred/src/ui/FredView.cpp @@ -1180,7 +1180,8 @@ void FredView::onUpdateContextToolbar() { const bool anythingSelected = valid || numMarked > 0; if (effectiveIsShip) { - if (numMarked <= 1) + // a wing member is named by its wing, so it gets no Rename + if (numMarked <= 1 && !effectiveInWing) addBtn(tr("Rename"), &FredView::quickRenameCurrentObject); addBtn(tr("Edit Ship"), &FredView::on_actionShips_triggered); if (effectiveInWing) { @@ -1220,7 +1221,17 @@ void FredView::quickRenameCurrentObject() { if (!ok || newName.isEmpty() || newName == current) return; if (Objects[obj].type == OBJ_SHIP || Objects[obj].type == OBJ_START) { - fred->rename_ship(Objects[obj].instance, newName.toUtf8().constData()); + // the ship editor refuses these names as well, and a longer one does not fit the ship's name field + const SCP_string name = newName.toUtf8().constData(); + SCP_string reason = check_name_conflict("ship", name.c_str(), Objects[obj].instance); + if (reason.empty() && name.size() >= NAME_LENGTH) { + reason = "This ship name is too long"; + } + if (!reason.empty()) { + showButtonDialog(DialogType::Error, "Ship Name Error", reason, { DialogButton::Ok }); + return; + } + fred->rename_ship(Objects[obj].instance, name.c_str()); } // Waypoints, props, and jump nodes open their editor (name field is front-and-center) // Wing rename goes through Edit Wing since wings have no standalone scene object diff --git a/qtfred/src/ui/dialogs/ShipEditor/ShipEditorDialog.cpp b/qtfred/src/ui/dialogs/ShipEditor/ShipEditorDialog.cpp index 0f649313f4c..1a4a94fceb9 100644 --- a/qtfred/src/ui/dialogs/ShipEditor/ShipEditorDialog.cpp +++ b/qtfred/src/ui/dialogs/ShipEditor/ShipEditorDialog.cpp @@ -768,11 +768,14 @@ void ShipEditorDialog::on_shipNameEdit_editingFinished() if (!entry.isEmpty() && entry != _model->getShipName().c_str()) { const auto textBytes = entry.toUtf8(); const std::string NewShipName = textBytes.toStdString(); + const SCP_string oldShipName = _model->getShipName(); _model->setShipName(NewShipName); - } - // automatically determine or reset the display name - _model->setShipDisplayName(Editor::get_display_name_for_text_box(_model->getShipName())); + // a refused rename leaves the ship name as it was, and its display name with it + if (_model->getShipName() != oldShipName) { + _model->setShipDisplayName(Editor::get_display_name_for_text_box(_model->getShipName())); + } + } // sync the variable to the edit box ui->shipDisplayNameEdit->setText(_model->getShipDisplayName().c_str()); diff --git a/qtfred/src/ui/dialogs/WingEditorDialog.cpp b/qtfred/src/ui/dialogs/WingEditorDialog.cpp index 5bb423f4bf5..c1290d02bbf 100644 --- a/qtfred/src/ui/dialogs/WingEditorDialog.cpp +++ b/qtfred/src/ui/dialogs/WingEditorDialog.cpp @@ -405,10 +405,13 @@ void WingEditorDialog::on_hideCuesButton_clicked() void WingEditorDialog::on_wingNameEdit_editingFinished() { const auto newName = ui->wingNameEdit->text().toStdString(); + const SCP_string oldName = _model->getWingName(); _model->setWingName(newName); // rename_wing already auto-sets the display name from the hash; just sync the edit box - ui->wingDisplayNameEdit->setText(Editor::get_display_name_for_text_box(_model->getWingName()).c_str()); + if (_model->getWingName() != oldName) { + ui->wingDisplayNameEdit->setText(Editor::get_display_name_for_text_box(_model->getWingName()).c_str()); + } } void WingEditorDialog::on_wingDisplayNameEdit_editingFinished()