Skip to content

Commit

Permalink
add storage and accessors for pins
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Nov 18, 2023
1 parent 28b72dc commit 1d4ced7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
50 changes: 45 additions & 5 deletions src/core/tracker.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tracker.h"
#include <algorithm>
#include <luaglue/luamethod.h>
#include <cstring>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -426,11 +427,11 @@ const BaseItem& Tracker::getItemByCode(const std::string& code) const
for (const auto& item: _jsonItems) {
if (item.canProvideCode(code)) return item;
}

for (const auto& item: _luaItems) {
if (item.canProvideCode(code)) return item;
}

return blankItem;
}
BaseItem& Tracker::getItemById(const std::string& id)
Expand Down Expand Up @@ -498,6 +499,29 @@ LocationSection& Tracker::getLocationSection(const std::string& id)
return blankLocationSection;
}

bool Tracker::isPinned(const std::string& id) const
{
return std::find(_pins.begin(), _pins.end(), id) != _pins.end();
}

void Tracker::setPinned(const std::string& id, bool pinned)
{
auto it = std::find(_pins.begin(), _pins.end(), id);
if (pinned && it == _pins.end()) {
_pins.push_back(id);
onPinChanged.emit(this, id, pinned);
}
else if (!pinned && it != _pins.end()) {
onPinChanged.emit(this, id, pinned);
_pins.erase(it);
}
}

const std::list<std::string>& Tracker::getPins() const
{
return _pins;
}

const Pack* Tracker::getPack() const
{
return _pack;
Expand Down Expand Up @@ -831,16 +855,17 @@ json Tracker::saveState() const
jSections[id] = sec.save();
}
}

json state = { {
"tracker", {
{"format_version", 1},
{"json_items", jJsonItems},
{"lua_items", jLuaItems},
{"sections", jSections}
{"sections", jSections},
{"pins", _pins},
}
} };

return state;
}

Expand All @@ -849,6 +874,10 @@ bool Tracker::loadState(nlohmann::json& state)
_reachableCache.clear();
_providerCountCache.clear();
_bulkItemUpdates.clear();
for (const auto& pin: _pins)
onPinChanged.emit(this, pin, false);
_pins.clear();

if (state.type() != json::value_t::object) return false;
auto& j = state["tracker"]; // state's tracker data
if (j["format_version"] != 1) return false; // incompatible state format
Expand Down Expand Up @@ -885,6 +914,17 @@ bool Tracker::loadState(nlohmann::json& state)
}
}
}
auto& jPins = j["pins"];
if (jPins.type() == json::value_t::array) {
try {
_pins = jPins.get<std::list<std::string>>();
} catch (std::exception& ex) {
fprintf(stderr, "Could not load pins: %s\n", ex.what());
}
}
for (const auto& pin: _pins)
onPinChanged.emit(this, pin, true);

for (const auto& id: _bulkItemUpdates)
onStateChanged.emit(this, id);
_bulkItemUpdates.clear();
Expand Down
8 changes: 7 additions & 1 deletion src/core/tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Tracker final : public LuaInterface<Tracker> {
Signal<const std::string&> onStateChanged;
Signal<const std::string&> onDisplayChanged; // changed display of an item
Signal<const std::string&, const std::string&> onUiHint;

Signal<const std::string&, bool> onPinChanged;

const LayoutNode& getLayout(const std::string& name) const;
bool hasLayout(const std::string& name) const;
const BaseItem& getItemByCode(const std::string& code) const;
Expand All @@ -83,6 +84,10 @@ class Tracker final : public LuaInterface<Tracker> {
bool isVisible(const Location& location);
bool isVisible(const Location::MapLocation& mapLoc);

bool isPinned(const std::string& id) const;
void setPinned(const std::string& id, bool pinned);
const std::list<std::string>& getPins() const;

const Pack* getPack() const;

bool changeItemState(const std::string& id, BaseItem::Action action);
Expand All @@ -102,6 +107,7 @@ class Tracker final : public LuaInterface<Tracker> {
std::map<std::string, int> _providerCountCache;
std::list<std::string> _bulkItemUpdates;
bool _bulkUpdate = false;
std::list<std::string> _pins;

std::list<std::string>* _parents = nullptr;

Expand Down

0 comments on commit 1d4ced7

Please sign in to comment.