From 4930c59979a99ac2ba943ccbe0cc750b2f4950b3 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:11:50 +0200 Subject: [PATCH] Settings: show correct tooltip for progressive --- src/ui/settingswindow.cpp | 29 +++++++++++++++++++++++------ src/ui/settingswindow.h | 3 +++ src/uilib/tooltip.cpp | 9 +++++++++ src/uilib/tooltip.h | 2 ++ src/uilib/window.h | 2 -- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/ui/settingswindow.cpp b/src/ui/settingswindow.cpp index eedfedaa..e61a9fd4 100644 --- a/src/ui/settingswindow.cpp +++ b/src/ui/settingswindow.cpp @@ -19,15 +19,32 @@ void SettingsWindow::setTracker(Tracker* tracker) TrackerWindow::setTracker(tracker, "settings_popup"); if (_view && tracker) { // Future improvement: widgets could store a ref to the parent and create the tooltips themselves - _view->onItemTooltip += {this, [this, tracker](void*, const std::string& id) { - if (id.empty()) { + _view->onItemTooltip += {this, [this, tracker](void*, const std::string& itemid) { + if (itemid.empty()) { showTooltip(nullptr); + auto& item = tracker->getItemById(_tooltipItemId); + item.onChange -= this; + _tooltipItemId.clear(); } else { - auto& item = tracker->getItemById(id); - if (item.getName().empty()) + auto& item = tracker->getItemById(itemid); + const auto& text = (item.getBaseItem().empty() || item.getState()) ? + item.getCurrentName() : tracker->getItemByCode(item.getBaseItem()).getCurrentName(); + if (text.empty()) { showTooltip(nullptr); - else - showTooltip(item.getName()); + _tooltipItemId.clear(); + } else { + showTooltip(text); + _tooltipItemId = itemid; + item.onChange += {this, [this, tracker, itemid](void* sender) { + const auto& item = tracker->getItemById(itemid); + const auto& text = (item.getBaseItem().empty() || item.getState()) ? + item.getCurrentName() : tracker->getItemByCode(item.getBaseItem()).getCurrentName(); + if (auto tooltip = dynamic_cast(_tooltip)) { + tooltip->setText(text); + tooltip->setSize(tooltip->getMinSize()); + } + }}; + } } }}; } diff --git a/src/ui/settingswindow.h b/src/ui/settingswindow.h index bc93f0b0..18c725c8 100644 --- a/src/ui/settingswindow.h +++ b/src/ui/settingswindow.h @@ -14,6 +14,9 @@ class SettingsWindow : public TrackerWindow { virtual void setTracker(Tracker *tracker) override; virtual void render(Renderer renderer, int offX, int offY) override; + +private: + std::string _tooltipItemId; }; } // namespace Ui diff --git a/src/uilib/tooltip.cpp b/src/uilib/tooltip.cpp index e971eafd..db6ddf61 100644 --- a/src/uilib/tooltip.cpp +++ b/src/uilib/tooltip.cpp @@ -23,4 +23,13 @@ Tooltip::Tooltip(FONT font, const std::string& text) addChild(label); } +void Tooltip::setText(const std::string &text) +{ + if (auto label = dynamic_cast(_children.front())) { + label->setText(text); + label->setSize(label->getAutoSize()); + relayout(); + } +} + } // namespace Ui diff --git a/src/uilib/tooltip.h b/src/uilib/tooltip.h index 6d964670..0bae4e57 100644 --- a/src/uilib/tooltip.h +++ b/src/uilib/tooltip.h @@ -24,6 +24,8 @@ class Tooltip : public HBox { static constexpr int PADDING=8; static constexpr int OFFSET=2; static constexpr tick_t delay = DEFAULT_TOOLTIP_DELAY; + + void setText(const std::string& text); private: }; diff --git a/src/uilib/window.h b/src/uilib/window.h index e284b1b9..a6713e09 100644 --- a/src/uilib/window.h +++ b/src/uilib/window.h @@ -25,11 +25,9 @@ class Window : public Container { FontStore *_fontStore = nullptr; // TODO; pass as argument to window constructor? FONT _font = nullptr; -private: Position _lastMousePos; Widget* _tooltip = nullptr; -protected: void clear(); void present(); virtual void render(Renderer renderer, int offX, int offY) override;