Skip to content

Commit

Permalink
Settings: show correct tooltip for progressive
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Aug 29, 2024
1 parent 57b271c commit 4930c59
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
29 changes: 23 additions & 6 deletions src/ui/settingswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
tooltip->setText(text);
tooltip->setSize(tooltip->getMinSize());
}
}};
}
}
}};
}
Expand Down
3 changes: 3 additions & 0 deletions src/ui/settingswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/uilib/tooltip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Label*>(_children.front())) {
label->setText(text);
label->setSize(label->getAutoSize());
relayout();
}
}

} // namespace Ui
2 changes: 2 additions & 0 deletions src/uilib/tooltip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

};
Expand Down
2 changes: 0 additions & 2 deletions src/uilib/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4930c59

Please sign in to comment.