Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions code/missioneditor/sexp_tree_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,39 @@ int SexpTreeModel::get_loadout_variable_count(int var_index)
return count;
}

void SexpTreeModel::rename_variable_references(const SCP_unordered_map<SCP_string, SCP_string>& 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
Expand Down
2 changes: 2 additions & 0 deletions code/missioneditor/sexp_tree_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SCP_string, SCP_string>& 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
Expand Down
1 change: 1 addition & 0 deletions fred2/sexp_tree_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
3 changes: 2 additions & 1 deletion qtfred/src/mission/dialogs/FictionViewerDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 51 additions & 31 deletions qtfred/src/mission/dialogs/VariableDialogModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "VariableDialogModel.h"

#include "missioneditor/sexp_tree_model.h"
#include "parse/sexp.h"

#include <algorithm>
Expand Down Expand Up @@ -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<int> 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;

Expand All @@ -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<SCP_string, SCP_string> 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<sexp_container> new_containers;
SCP_unordered_map<SCP_string, SCP_string, SCP_string_lcase_hash, SCP_string_lcase_equal_to> renamed_containers;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions qtfred/src/mission/dialogs/WingEditorDialogModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 13 additions & 2 deletions qtfred/src/ui/FredView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions qtfred/src/ui/dialogs/ShipEditor/ShipEditorDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion qtfred/src/ui/dialogs/WingEditorDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading