diff --git a/plugins/dm.stimresponse/ClassEditor.cpp b/plugins/dm.stimresponse/ClassEditor.cpp index 1c0195a1b3..45ffe5cf77 100644 --- a/plugins/dm.stimresponse/ClassEditor.cpp +++ b/plugins/dm.stimresponse/ClassEditor.cpp @@ -64,29 +64,27 @@ void ClassEditor::setEntity(const SREntityPtr& entity) _entity = entity; } -int ClassEditor::getIdFromSelection() +int ClassEditor::getIndexFromSelection() { wxDataViewItem item = _list->GetSelection(); - if (item.IsOk() && _entity != NULL) + if (item.IsOk() && _entity != nullptr) { wxutil::TreeModel::Row row(item, *_list->GetModel()); - return row[SREntity::getColumns().id].getInteger(); - } - else - { - return -1; + return row[SREntity::getColumns().index].getInteger(); } + + return -1; } void ClassEditor::setProperty(const std::string& key, const std::string& value) { - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { // Don't edit inherited stims/responses - _entity->setProperty(id, key, value); + _entity->setProperty(index, key, value); } // Call the method of the child class to update the widgets @@ -156,22 +154,22 @@ void ClassEditor::reloadStimTypes() void ClassEditor::removeSR() { - // Get the selected stim ID - int id = getIdFromSelection(); + // Get the selected stim index + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { - _entity->remove(id); + _entity->remove(index); } } -void ClassEditor::selectId(int id) +void ClassEditor::selectIndex(int index) { // Setup the selectionfinder to search for the id wxutil::TreeModel* model = dynamic_cast(_list->GetModel()); assert(model != NULL); - wxDataViewItem item = model->FindInteger(id, SREntity::getColumns().id); + wxDataViewItem item = model->FindInteger(index, SREntity::getColumns().index); if (item.IsOk()) { @@ -184,13 +182,13 @@ void ClassEditor::selectId(int id) void ClassEditor::duplicateStimResponse() { - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { - int newId = _entity->duplicate(id); + int newIndex = _entity->duplicate(index); // Select the newly created stim - selectId(newId); + selectIndex(newIndex); } // Call the method of the child class to update the widgets diff --git a/plugins/dm.stimresponse/ClassEditor.h b/plugins/dm.stimresponse/ClassEditor.h index 8521250fe0..d6fd831e0c 100644 --- a/plugins/dm.stimresponse/ClassEditor.h +++ b/plugins/dm.stimresponse/ClassEditor.h @@ -118,13 +118,13 @@ class ClassEditor : /** greebo: Returns the ID of the currently selected stim/response * - * @returns: the id (number) of the selected stim or -1 on failure + * @returns: the index (number) of the selected stim or -1 on failure */ - int getIdFromSelection(); + int getIndexFromSelection(); - /** greebo: Selects the given ID in the S/R list + /** greebo: Selects the given index in the S/R list */ - void selectId(int id); + void selectIndex(int index); /** greebo: Gets called when the list selection changes */ diff --git a/plugins/dm.stimresponse/ResponseEditor.cpp b/plugins/dm.stimresponse/ResponseEditor.cpp index 2ead4dcd22..2f6d3a4b68 100644 --- a/plugins/dm.stimresponse/ResponseEditor.cpp +++ b/plugins/dm.stimresponse/ResponseEditor.cpp @@ -64,13 +64,13 @@ void ResponseEditor::update() wxPanel* mainPanel = findNamedObject(_mainPanel, "SREditorResponsePanel"); auto removeButton = findNamedObject(_mainPanel, "RemoveResponseButton"); - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0 && _entity != NULL) + if (index > 0 && _entity != NULL) { mainPanel->Enable(true); - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); // Get the iter into the liststore pointing at the correct STIM_YYYY type std::string typeToFind = sr.get("type"); @@ -305,13 +305,13 @@ void ResponseEditor::checkBoxToggled(wxCheckBox* toggleButton) void ResponseEditor::addEffect() { - if (_entity == NULL) return; + if (_entity == nullptr) return; - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); int effectIndex = getEffectIdFromSelection(); // Make sure we have a response @@ -325,13 +325,13 @@ void ResponseEditor::addEffect() void ResponseEditor::removeEffect() { - if (_entity == NULL) return; + if (_entity == nullptr) return; - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); int effectIndex = getEffectIdFromSelection(); // Make sure we have a response and anything selected @@ -346,13 +346,13 @@ void ResponseEditor::removeEffect() void ResponseEditor::editEffect() { - if (_entity == NULL) return; + if (_entity == nullptr) return; - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); int effectIndex = getEffectIdFromSelection(); // Make sure we have a response and anything selected @@ -371,11 +371,11 @@ void ResponseEditor::moveEffect(int direction) { if (_entity == NULL) return; - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); int effectIndex = getEffectIdFromSelection(); if (sr.get("class") == "R" && effectIndex > 0) @@ -397,11 +397,11 @@ void ResponseEditor::updateEffectContextMenu() bool anythingSelected = curEffectIndex >= 0; - int srId = getIdFromSelection(); + int index = getIndexFromSelection(); - if (srId > 0) + if (index > 0) { - StimResponse& sr = _entity->get(srId); + StimResponse& sr = _entity->get(index); highestEffectIndex = sr.highestEffectIndex(); } @@ -513,10 +513,10 @@ void ResponseEditor::addSR() if (_entity == NULL) return; // Create a new StimResponse object - int id = _entity->add(); + int index = _entity->add(); // Get a reference to the newly allocated object - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); sr.set("class", "R"); // Get the selected stim type name from the combo box @@ -529,7 +529,7 @@ void ResponseEditor::addSR() _entity->updateListStores(); // Select the newly created response - selectId(id); + selectIndex(index); } void ResponseEditor::onEffectItemContextMenu(wxDataViewEvent& ev) diff --git a/plugins/dm.stimresponse/SREntity.cpp b/plugins/dm.stimresponse/SREntity.cpp index 6068283279..58efe6ee80 100644 --- a/plugins/dm.stimresponse/SREntity.cpp +++ b/plugins/dm.stimresponse/SREntity.cpp @@ -28,21 +28,6 @@ SREntity::SREntity(Entity* source, StimTypes& stimTypes) : load(source); } -int SREntity::getHighestId() -{ - int id = 0; - - for (const auto& i : _list) - { - if (i.first > id) - { - id = i.first; - } - } - - return id; -} - int SREntity::getHighestIndex() { int index = 0; @@ -90,36 +75,43 @@ void SREntity::load(Entity* source) updateListStores(); } -void SREntity::remove(int id) +void SREntity::remove(int index) { - auto found = _list.find(id); + auto found = _list.find(index); - if (found != _list.end() && !found->second.inherited()) + if (found == _list.end() || found->second.inherited()) { - _list.erase(found); - updateListStores(); + return; } + + _list.erase(found); + + // Re-arrange the S/R indices. The parser in the game engine + // walks up the indices and stops on the first missing index + // not noticing there might be higher indices to follow (#5193) + // TODO + + updateListStores(); } -int SREntity::duplicate(int fromId) +int SREntity::duplicate(int fromIndex) { - auto found = _list.find(fromId); + auto found = _list.find(fromIndex); if (found != _list.end()) { - int id = getHighestId() + 1; int index = getHighestIndex() + 1; // Copy the object to the new id - _list[id] = found->second; + _list[index] = found->second; // Set the index and the inheritance status - _list[id].setInherited(false); - _list[id].setIndex(index); + _list[index].setInherited(false); + _list[index].setIndex(index); // Rebuild the liststores updateListStores(); - return id; + return index; } return -1; @@ -134,14 +126,14 @@ void SREntity::updateListStores() // Now populate the liststore for (auto& i : _list) { - int id = i.first; + int index = i.first; StimResponse& sr = i.second; wxutil::TreeModel::Row row = (sr.get("class") == "S") ? _stimStore->AddItem() : _responseStore->AddItem(); // Store the ID into the liststore - row[getColumns().id] = id; + row[getColumns().index] = index; writeToListRow(row, sr); @@ -151,17 +143,17 @@ void SREntity::updateListStores() int SREntity::add() { - int id = getHighestId() + 1; int index = getHighestIndex() + 1; // Create a new StimResponse object - _list[id] = StimResponse(); + _list[index] = StimResponse(); + // Set the index and the inheritance status - _list[id].setInherited(false); - _list[id].setIndex(index); - _list[id].set("class", "S"); + _list[index].setInherited(false); + _list[index].setIndex(index); + _list[index].set("class", "S"); - return id; + return index; } void SREntity::cleanEntity(Entity* target) @@ -179,7 +171,8 @@ void SREntity::cleanEntity(Entity* target) void SREntity::save(Entity* target) { - if (target == nullptr) { + if (target == nullptr) + { return; } @@ -188,15 +181,16 @@ void SREntity::save(Entity* target) // Setup the saver object SRPropertySaver saver(target, _keys); + for (auto& i : _list) { saver.visit(i.second); } } -wxDataViewItem SREntity::getIterForId(wxutil::TreeModel& targetStore, int id) +wxDataViewItem SREntity::getIterForIndex(wxutil::TreeModel& targetStore, int index) { - return targetStore.FindInteger(id, getColumns().id); + return targetStore.FindInteger(index, getColumns().index); } void SREntity::writeToListRow(wxutil::TreeModel::Row& row, StimResponse& sr) @@ -228,19 +222,19 @@ void SREntity::writeToListRow(wxutil::TreeModel::Row& row, StimResponse& sr) row[cols.inherited] = sr.inherited(); } -void SREntity::setProperty(int id, const std::string& key, const std::string& value) +void SREntity::setProperty(int index, const std::string& key, const std::string& value) { // First, propagate the SR set() call - StimResponse& sr = get(id); + StimResponse& sr = get(index); sr.set(key, value); wxutil::TreeModel::Ptr targetStore = (sr.get("class") == "S") ? _stimStore : _responseStore; - wxDataViewItem item = getIterForId(*targetStore, id); + wxDataViewItem item = getIterForIndex(*targetStore, index); if (!item.IsOk()) { - rError() << "Cannot find S/R ID in liststore: " << id << std::endl; + rError() << "Cannot find S/R index in liststore: " << index << std::endl; return; } @@ -249,9 +243,9 @@ void SREntity::setProperty(int id, const std::string& key, const std::string& va row.SendItemChanged(); } -StimResponse& SREntity::get(int id) +StimResponse& SREntity::get(int index) { - auto i = _list.find(id); + auto i = _list.find(index); return i != _list.end() ? i->second : _emptyStimResponse; } diff --git a/plugins/dm.stimresponse/SREntity.h b/plugins/dm.stimresponse/SREntity.h index 8abf2d3871..0cc0c36cd7 100644 --- a/plugins/dm.stimresponse/SREntity.h +++ b/plugins/dm.stimresponse/SREntity.h @@ -31,16 +31,13 @@ struct SRListColumns : index(add(wxutil::TreeModel::Column::Integer)), srClass(add(wxutil::TreeModel::Column::Icon)), caption(add(wxutil::TreeModel::Column::IconText)), - inherited(add(wxutil::TreeModel::Column::Boolean)), - id(add(wxutil::TreeModel::Column::Integer)) - { - } + inherited(add(wxutil::TreeModel::Column::Boolean)) + {} wxutil::TreeModel::Column index; // S/R index wxutil::TreeModel::Column srClass; // Type icon wxutil::TreeModel::Column caption; // Caption String wxutil::TreeModel::Column inherited; // Inheritance flag - wxutil::TreeModel::Column id; // ID (unique) }; /** @@ -92,42 +89,30 @@ class SREntity void cleanEntity(Entity* target); /** greebo: Retrieves the reference to the StimResponse object - * having the given integer . + * having the given integer . * * @returns: The ref to the StimResponse or an empty StimResponse object, - * if the id was not found. + * if the index was not found. */ - StimResponse& get(int id); + StimResponse& get(int index); - /** greebo: Adds a new StimResponse and returns the id of the new object. + /** greebo: Adds a new StimResponse and returns the index of the new object. * The ListStore is NOT updated with this call to allow setting of * the properties before refreshing the treeview. */ int add(); - /** greebo: Removes the StimResponse object with the given id. + /** greebo: Removes the StimResponse object with the given index. * This triggers a refresh of the liststores. */ - void remove(int id); + void remove(int index); - /** greebo: Duplicates the stim/response with the given id. + /** greebo: Duplicates the stim/response with the given index. * - * @fromId: The ID of the SR to copy from. - * @returns: the ID of the new duplicate. + * @fromId: The index of the SR to copy from. + * @returns: the index of the new duplicate. */ - int duplicate(int fromId); - - /** greebo: Overrides the "state" property of an inherited stim. - * As inherited spawnargs can't be altered, a sr_state_N - * key/value pair is added to the entity, overriding - * the inherited one. - */ - void setInheritedState(int id, bool enabled); - - /** greebo: Returns TRUE if the inherited stim/response is enabled, - * FALSE, if the inherited item is overridden. - */ - bool getInheritedState(int id); + int duplicate(int fromIndex); // Static column definition static const SRListColumns& getColumns(); @@ -140,9 +125,9 @@ class SREntity wxutil::TreeModel::Ptr getStimStore(); wxutil::TreeModel::Ptr getResponseStore(); - /** greebo: Sets the of the SR with the given to + /** greebo: Sets the of the SR with the given to */ - void setProperty(int id, const std::string& key, const std::string& value); + void setProperty(int index, const std::string& key, const std::string& value); /** greebo: Updates the ListStore according to the * values of the current StimResponseMap <_list> @@ -156,27 +141,23 @@ class SREntity /** * greebo: Returns the treeIter pointing to the row containing the - * StimResponse with the given + * StimResponse with the given * * @targetStore: The liststore where the iter should be searched */ - wxDataViewItem getIterForId(wxutil::TreeModel& targetStore, int id); + wxDataViewItem getIterForIndex(wxutil::TreeModel& targetStore, int index); private: /** greebo: Write the values of the passed StimResponse to the - * TreeModel using the passed Row. - * The ID stays untouched. + * TreeModel using the passed Row. + * The index stays untouched. * * @row: The row where the data should be inserted to * @sr: the StimResponse object containing the source data */ void writeToListRow(wxutil::TreeModel::Row& row, StimResponse& sr); - // Returns the highest currently assigned id - int getHighestId(); - // Returns the highest Stim/Response index number int getHighestIndex(); }; - typedef std::shared_ptr SREntityPtr; diff --git a/plugins/dm.stimresponse/SRPropertyLoader.cpp b/plugins/dm.stimresponse/SRPropertyLoader.cpp index 062d19db40..3f219c90b3 100644 --- a/plugins/dm.stimresponse/SRPropertyLoader.cpp +++ b/plugins/dm.stimresponse/SRPropertyLoader.cpp @@ -40,7 +40,7 @@ void SRPropertyLoader::parseAttribute( for (std::size_t i = 0; i < _keys.size(); i++) { // Construct a regex with the number as match variable - std::string exprStr = "^" + prefix + _keys[i].key + "_([0-9])+$"; + std::string exprStr = "^" + prefix + _keys[i].key + "_([0-9]+)$"; std::regex expr(exprStr); std::smatch matches; diff --git a/plugins/dm.stimresponse/StimEditor.cpp b/plugins/dm.stimresponse/StimEditor.cpp index 554726157b..f1ed2bd618 100644 --- a/plugins/dm.stimresponse/StimEditor.cpp +++ b/plugins/dm.stimresponse/StimEditor.cpp @@ -404,10 +404,10 @@ void StimEditor::addSR() if (!_entity) return; // Create a new StimResponse object - int id = _entity->add(); + int index = _entity->add(); // Get a reference to the newly allocated object - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); sr.set("class", "S"); // Get the selected stim type name from the combo box @@ -420,7 +420,7 @@ void StimEditor::addSR() _entity->updateListStores(); // Select the newly created stim - selectId(id); + selectIndex(index); } // Create the context menus @@ -456,14 +456,14 @@ void StimEditor::update() wxPanel* mainPanel = findNamedObject(_mainPanel, "SREditorStimPanel"); auto removeButton = findNamedObject(_mainPanel, "RemoveStimButton"); - int id = getIdFromSelection(); + int index = getIndexFromSelection(); - if (id > 0) + if (index > 0) { // Update all the widgets mainPanel->Enable(true); - StimResponse& sr = _entity->get(id); + StimResponse& sr = _entity->get(index); std::string typeToFind = sr.get("type");