Skip to content

Commit

Permalink
Allow base_item item to be defined after toggle_badged
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Feb 18, 2025
1 parent 9c6caf3 commit 58d6e8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/core/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ bool Tracker::AddItems(const std::string& file) {
}
item.setState(1, n);
}

// connect base item onChange to badged (if base was defined first)
if (!item.getBaseItem().empty()) {
// fire event for toggle_badged when base item changed
auto id = item.getID();
Expand All @@ -308,6 +310,31 @@ bool Tracker::AddItems(const std::string& file) {
o.luaItem->onChange += {this, [update](void* sender) {
update();
}};
} else {
auto& connectionList = _missingBaseItemConnection[JsonItem::toLower(item.getBaseItem())];
connectionList.push_back(id);
}
}
// connect item onChange to badged (if badged was defined first)
if (item.getType() == BaseItem::Type::TOGGLE_BADGED) {
fprintf(stderr, "WARNING: ignored %s as base_item of badged\n",
BaseItem::Type2Str(item.getType()).c_str());
} else {
for (const auto& code: item.getCodes(0)) {
auto it = _missingBaseItemConnection.find(JsonItem::toLower(code));
if (it != _missingBaseItemConnection.end()) {
for (const auto& targetId: it->second) {
item.onChange += {this, [this, targetId](void* sender) {
for (auto& i : _jsonItems) {
if (i.getID() == targetId) {
i.onChange.emit(&i);
break;
}
}
}};
}
_missingBaseItemConnection.erase(it);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Tracker final : public LuaInterface<Tracker> {
bool _isIndirectConnection = false; /// flag to skip cache for codes that depend on locations
bool _updatingCache = false; /// true while cache*() is running
std::set<std::string> _itemChangesDuringCacheUpdate;
std::map<std::string, std::vector<std::string>> _missingBaseItemConnection;

std::map<std::string, std::vector<std::string>> _sectionNameRefs;
std::map<std::reference_wrapper<const LocationSection>,
Expand Down

0 comments on commit 58d6e8e

Please # to comment.