| 1 |
#include "StimTypes.h" |
| 2 |
|
| 3 |
#include "itextstream.h" |
| 4 |
#include "string/string.h" |
| 5 |
#include "wxutil/dataview/TreeModel.h" |
| 6 |
#include "entitylib.h" |
| 7 |
#include "registry/registry.h" |
| 8 |
#include "SREntity.h" |
| 9 |
#include "gamelib.h" |
| 10 |
#include "i18n.h" |
| 11 |
#include "igame.h" |
| 12 |
|
| 13 |
#include "wxutil/Bitmap.h" |
| 14 |
#include <wx/bmpcbox.h> |
| 15 |
#include <wx/combobox.h> |
| 16 |
|
| 17 |
#include "string/predicate.h" |
| 18 |
#include "wxutil/Icon.h" |
| 19 |
|
| 20 |
namespace { |
| 21 |
const std::string RKEY_STIM_DEFINITIONS = |
| 22 |
"/stimResponseSystem/stims//stim"; |
| 23 |
const std::string GKEY_STORAGE_ECLASS = |
| 24 |
"/stimResponseSystem/customStimStorageEClass"; |
| 25 |
const std::string GKEY_STORAGE_PREFIX = |
| 26 |
"/stimResponseSystem/customStimKeyPrefix"; |
| 27 |
const std::string GKEY_LOWEST_CUSTOM_STIM_ID = |
| 28 |
"/stimResponseSystem/lowestCustomStimId"; |
| 29 |
const std::string RKEY_SHOW_STIM_TYPE_IDS = |
| 30 |
"user/ui/stimResponseEditor/showStimTypeIDs"; |
| 31 |
|
| 32 |
/* greebo: Finds an entity with the given classname |
| 33 |
*/ |
| 34 |
Entity* findEntityByClass(const std::string& className) { |
| 35 |
// Instantiate a walker to find the entity |
| 36 |
EntityNodeFindByClassnameWalker walker(className); |
| 37 |
|
| 38 |
// Walk the scenegraph |
| 39 |
GlobalSceneGraph().root()->traverse(walker); |
| 40 |
|
| 41 |
return walker.getEntity(); |
| 42 |
} |
| 43 |
|
| 44 |
// Helper visitor class to remove custom stim definitions from |
| 45 |
// the storage entity. First, all the keys are gathered and |
| 46 |
// on destruction the keys are deleted. The deletion may not |
| 47 |
// happen during the visit process (due to iterators becoming invalid). |
| 48 |
class CustomStimRemover |
| 49 |
{ |
| 50 |
// This list will be populated with all the keys that |
| 51 |
// should be removed. |
| 52 |
typedef std::vector<std::string> RemoveList; |
| 53 |
RemoveList _removeList; |
| 54 |
|
| 55 |
Entity* _entity; |
| 56 |
|
| 57 |
public: |
| 58 |
CustomStimRemover(Entity* entity) : |
| 59 |
_entity(entity) |
| 60 |
{} |
| 61 |
|
| 62 |
~CustomStimRemover() { |
| 63 |
// Delete all the keys that are tagged for deletion |
| 64 |
for (std::size_t i = 0; i < _removeList.size(); ++i) |
| 65 |
{ |
| 66 |
_entity->setKeyValue(_removeList[i], ""); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void operator()(const std::string& key, const std::string& value) |
| 71 |
{ |
| 72 |
std::string prefix = game::current::getValue<std::string>(GKEY_STORAGE_PREFIX); |
| 73 |
|
| 74 |
if (string::starts_with(key, prefix)) |
| 75 |
{ |
| 76 |
// We have a match, add the key to the removal list |
| 77 |
_removeList.push_back(key); |
| 78 |
} |
| 79 |
} |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
StimTypes::StimTypes() : |
| 84 |
_listStore(new wxutil::TreeModel(_columns, true)) |
| 85 |
{} |
| 86 |
|
| 87 |
StimTypes::~StimTypes() |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
void StimTypes::reload() |
| 92 |
{ |
| 93 |
_stimTypes.clear(); |
| 94 |
_listStore->Clear(); |
| 95 |
|
| 96 |
// Find all the relevant nodes |
| 97 |
xml::NodeList stimNodes = GlobalGameManager().currentGame()->getLocalXPath(RKEY_STIM_DEFINITIONS); |
| 98 |
|
| 99 |
for (std::size_t i = 0; i < stimNodes.size(); ++i) |
| 100 |
{ |
| 101 |
// Add the new stim type |
| 102 |
add(string::convert<int>(stimNodes[i].getAttributeValue("id")), |
| 103 |
stimNodes[i].getAttributeValue("name"), |
| 104 |
stimNodes[i].getAttributeValue("caption"), |
| 105 |
stimNodes[i].getAttributeValue("description"), |
| 106 |
stimNodes[i].getAttributeValue("icon"), |
| 107 |
false // non-custom stim |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
// Load the custom stims from the storage entity |
| 112 |
std::string storageEClass = game::current::getValue<std::string>(GKEY_STORAGE_ECLASS); |
| 113 |
Entity* storageEntity = findEntityByClass(storageEClass); |
| 114 |
|
| 115 |
if (storageEntity != NULL) |
| 116 |
{ |
| 117 |
// Visit each keyvalue |
| 118 |
storageEntity->forEachKeyValue([&](const std::string& key, const std::string& value) |
| 119 |
{ |
| 120 |
visitKeyValue(key, value); |
| 121 |
}); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void StimTypes::save() |
| 126 |
{ |
| 127 |
// Find the storage entity |
| 128 |
std::string storageEClass = game::current::getValue<std::string>(GKEY_STORAGE_ECLASS); |
| 129 |
Entity* storageEntity = findEntityByClass(storageEClass); |
| 130 |
|
| 131 |
if (storageEntity != NULL) |
| 132 |
{ |
| 133 |
std::string prefix = game::current::getValue<std::string>(GKEY_STORAGE_PREFIX); |
| 134 |
|
| 135 |
// Clean the storage entity from any previous definitions |
| 136 |
{ |
| 137 |
// Instantiate a visitor to gather the keys to delete |
| 138 |
CustomStimRemover remover(storageEntity); |
| 139 |
// Visit each keyvalue with the <self> class as visitor |
| 140 |
storageEntity->forEachKeyValue(remover); |
| 141 |
// Scope ends here, the keys are deleted now |
| 142 |
// as the CustomStimRemover gets destructed |
| 143 |
} |
| 144 |
|
| 145 |
// Now store all custom stim types to the storage entity |
| 146 |
for (StimTypeMap::iterator i = _stimTypes.begin(); i != _stimTypes.end(); ++i) |
| 147 |
{ |
| 148 |
StimType& s = i->second; |
| 149 |
std::string idStr = string::to_string(i->first); |
| 150 |
|
| 151 |
if (s.custom) { |
| 152 |
// spawnarg is something like "editor_dr_stim_1002" => "MyStim" |
| 153 |
storageEntity->setKeyValue(prefix + idStr, s.caption); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void StimTypes::remove(int id) |
| 160 |
{ |
| 161 |
StimTypeMap::iterator found = _stimTypes.find(id); |
| 162 |
|
| 163 |
if (found != _stimTypes.end()) |
| 164 |
{ |
| 165 |
// Erase the item from the map |
| 166 |
_stimTypes.erase(found); |
| 167 |
|
| 168 |
// Erase the row in the liststore |
| 169 |
wxDataViewItem item = getIterForId(id); |
| 170 |
|
| 171 |
if (item.IsOk()) |
| 172 |
{ |
| 173 |
_listStore->RemoveItem(item); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
wxDataViewItem StimTypes::getIterForId(int id) |
| 179 |
{ |
| 180 |
// Setup the selectionfinder to search for the id |
| 181 |
return _listStore->FindInteger(id, _columns.id); |
| 182 |
} |
| 183 |
|
| 184 |
void StimTypes::setStimTypeCaption(int id, const std::string& caption) |
| 185 |
{ |
| 186 |
StimTypeMap::iterator found = _stimTypes.find(id); |
| 187 |
|
| 188 |
if (found != _stimTypes.end()) |
| 189 |
{ |
| 190 |
_stimTypes[id].caption = caption; |
| 191 |
|
| 192 |
// Combine the ID and the caption |
| 193 |
std::string captionPlusId = caption; |
| 194 |
bool showStimTypeIds = registry::getValue<bool>(RKEY_SHOW_STIM_TYPE_IDS); |
| 195 |
captionPlusId += showStimTypeIds ? " (" + string::to_string(id) + ")" : ""; |
| 196 |
|
| 197 |
// Update the list store |
| 198 |
wxutil::TreeModel::Row row(getIterForId(id), *_listStore); |
| 199 |
|
| 200 |
wxutil::Icon stimIcon(wxutil::GetLocalBitmap(_stimTypes[id].icon)); |
| 201 |
|
| 202 |
row[_columns.caption] = wxVariant(wxDataViewIconText(_stimTypes[id].caption, stimIcon)); |
| 203 |
row[_columns.captionPlusID] = captionPlusId; |
| 204 |
|
| 205 |
row.SendItemChanged(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void StimTypes::add(int id, |
| 210 |
const std::string& name, |
| 211 |
const std::string& caption, |
| 212 |
const std::string& description, |
| 213 |
const std::string& icon, |
| 214 |
bool custom) |
| 215 |
{ |
| 216 |
StimType newStimType; |
| 217 |
newStimType.name = name; |
| 218 |
newStimType.caption = caption; |
| 219 |
newStimType.description = description; |
| 220 |
newStimType.icon = icon; |
| 221 |
newStimType.custom = custom; |
| 222 |
|
| 223 |
// Add the stim to the map |
| 224 |
_stimTypes[id] = newStimType; |
| 225 |
|
| 226 |
// Combine the ID and the caption |
| 227 |
std::string captionPlusId = caption; |
| 228 |
bool showStimTypeIds = registry::getValue<bool>(RKEY_SHOW_STIM_TYPE_IDS); |
| 229 |
captionPlusId += showStimTypeIds ? " (" + string::to_string(id) + ")" : ""; |
| 230 |
|
| 231 |
wxutil::TreeModel::Row row = _listStore->AddItem(); |
| 232 |
|
| 233 |
wxutil::Icon stimIcon(wxutil::GetLocalBitmap(newStimType.icon)); |
| 234 |
|
| 235 |
row[_columns.id] = id; |
| 236 |
row[_columns.caption] = wxVariant(wxDataViewIconText(_stimTypes[id].caption, stimIcon)); |
| 237 |
row[_columns.captionPlusID] = captionPlusId; |
| 238 |
row[_columns.name] = _stimTypes[id].name; |
| 239 |
row[_columns.isCustom] = custom; |
| 240 |
|
| 241 |
row.SendItemAdded(); |
| 242 |
} |
| 243 |
|
| 244 |
void StimTypes::populateComboBox(wxComboBox* combo) const |
| 245 |
{ |
| 246 |
combo->Clear(); |
| 247 |
|
| 248 |
for (const auto& pair : _stimTypes) |
| 249 |
{ |
| 250 |
// Add the name (e.g. "STIM_FIRE") as client data to this option, for later retrieval |
| 251 |
combo->Append(pair.second.caption, new wxStringClientData(pair.second.name)); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void StimTypes::populateComboBox(wxBitmapComboBox* combo) const |
| 256 |
{ |
| 257 |
combo->Clear(); |
| 258 |
|
| 259 |
for (const auto& pair : _stimTypes) |
| 260 |
{ |
| 261 |
wxBitmap icon = wxutil::GetLocalBitmap(pair.second.icon); |
| 262 |
|
| 263 |
// Add the name (e.g. "STIM_FIRE") as client data to this option, for later retrieval |
| 264 |
combo->Append(pair.second.caption, icon, new wxStringClientData(pair.second.name)); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void StimTypes::visitKeyValue(const std::string& key, const std::string& value) |
| 269 |
{ |
| 270 |
std::string prefix = game::current::getValue<std::string>(GKEY_STORAGE_PREFIX); |
| 271 |
int lowestCustomId = game::current::getValue<int>(GKEY_LOWEST_CUSTOM_STIM_ID); |
| 272 |
|
| 273 |
if (string::starts_with(key, prefix)) |
| 274 |
{ |
| 275 |
// Extract the stim name from the key (the part after the prefix) |
| 276 |
std::string idStr = key.substr(prefix.size()); |
| 277 |
int id = string::convert<int>(idStr); |
| 278 |
std::string stimCaption= value; |
| 279 |
|
| 280 |
if (id < lowestCustomId) |
| 281 |
{ |
| 282 |
rError() << "Warning: custom stim Id " << id << " is lower than " |
| 283 |
<< lowestCustomId << "\n"; |
| 284 |
} |
| 285 |
|
| 286 |
// Add this as new stim type |
| 287 |
add(id, |
| 288 |
idStr, // The name is the id in string format: e.g. "1002" |
| 289 |
stimCaption, // The caption |
| 290 |
_("Custom Stim"), |
| 291 |
ICON_CUSTOM_STIM, |
| 292 |
true // custom stim |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
const StimTypes::Columns& StimTypes::getColumns() const |
| 298 |
{ |
| 299 |
return _columns; |
| 300 |
} |
| 301 |
|
| 302 |
wxutil::TreeModel::Ptr StimTypes::getListStore() const |
| 303 |
{ |
| 304 |
return _listStore; |
| 305 |
} |
| 306 |
|
| 307 |
StimTypeMap& StimTypes::getStimMap() |
| 308 |
{ |
| 309 |
return _stimTypes; |
| 310 |
} |
| 311 |
|
| 312 |
int StimTypes::getFreeCustomStimId() |
| 313 |
{ |
| 314 |
int freeId = game::current::getValue<int>(GKEY_LOWEST_CUSTOM_STIM_ID); |
| 315 |
|
| 316 |
StimTypeMap::iterator found = _stimTypes.find(freeId); |
| 317 |
|
| 318 |
while (found != _stimTypes.end()) |
| 319 |
{ |
| 320 |
freeId++; |
| 321 |
found = _stimTypes.find(freeId); |
| 322 |
} |
| 323 |
|
| 324 |
return freeId; |
| 325 |
} |
| 326 |
|
| 327 |
wxDataViewItem StimTypes::getIterForName(const std::string& name) |
| 328 |
{ |
| 329 |
return _listStore->FindString(name, _columns.name); |
| 330 |
} |
| 331 |
|
| 332 |
StimType StimTypes::get(int id) const |
| 333 |
{ |
| 334 |
StimTypeMap::const_iterator i = _stimTypes.find(id); |
| 335 |
|
| 336 |
return i != _stimTypes.end() ? i->second : _emptyStimType; |
| 337 |
} |
| 338 |
|
| 339 |
std::string StimTypes::getFirstName() |
| 340 |
{ |
| 341 |
StimTypeMap::iterator i = _stimTypes.begin(); |
| 342 |
|
| 343 |
return (i != _stimTypes.end()) ? i->second.name : "noname"; |
| 344 |
} |
| 345 |
|
| 346 |
StimType StimTypes::get(const std::string& name) const |
| 347 |
{ |
| 348 |
for (StimTypeMap::const_iterator i = _stimTypes.begin(); i != _stimTypes.end(); ++i) |
| 349 |
{ |
| 350 |
if (i->second.name == name) |
| 351 |
{ |
| 352 |
return i->second; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
return _emptyStimType; // Nothing found |
| 357 |
} |
| 358 |
|
| 359 |
int StimTypes::getIdForName(const std::string& name) const |
| 360 |
{ |
| 361 |
for (StimTypeMap::const_iterator i = _stimTypes.begin(); i != _stimTypes.end(); ++i) |
| 362 |
{ |
| 363 |
if (i->second.name == name) |
| 364 |
{ |
| 365 |
return i->first; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return -1; // Nothing found |
| 370 |
} |