Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions libs/s25main/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,21 @@ class WindowManager::Tooltip
const glFont* font;
std::vector<std::string> lines;
unsigned width = 0, height = 0;
unsigned short maxWidth;

public:
Tooltip(const ctrlBaseTooltip* showingCtrl, const std::string& text, unsigned short maxWidth)
: showingCtrl(showingCtrl), font(NormalFont),
lines(font->GetWrapInfo(text, maxWidth, maxWidth).CreateSingleStrings(text))
: showingCtrl(showingCtrl), font(NormalFont), maxWidth(maxWidth)
{
setText(text);
}

auto getShowingCtrl() const { return showingCtrl; }
auto getWidth() const { return width; }

void setText(const std::string& text)
{
lines = font->GetWrapInfo(text, maxWidth, maxWidth).CreateSingleStrings(text);
if(lines.empty())
return;
width = 0;
Expand All @@ -813,9 +822,6 @@ class WindowManager::Tooltip
height = lines.size() * font->getHeight() + BORDER_SIZE * 2;
}

auto getShowingCtrl() const { return showingCtrl; }
auto getWidth() const { return width; }

void draw(DrawPoint pos) const
{
Window::DrawRectangle(Rect(pos, width, height), 0x9F000000);
Expand All @@ -829,7 +835,7 @@ class WindowManager::Tooltip
}
};

void WindowManager::SetToolTip(const ctrlBaseTooltip* ttw, const std::string& tooltip)
void WindowManager::SetToolTip(const ctrlBaseTooltip* ttw, const std::string& tooltip, bool updateCurrent)
{
// Max width of tooltip
constexpr unsigned short MAX_TOOLTIP_WIDTH = 260;
Expand All @@ -838,6 +844,10 @@ void WindowManager::SetToolTip(const ctrlBaseTooltip* ttw, const std::string& to
{
if(curTooltip && (!ttw || curTooltip->getShowingCtrl() == ttw))
curTooltip.reset();
} else if(updateCurrent)
{
if(curTooltip && curTooltip->getShowingCtrl() == ttw)
curTooltip->setText(tooltip);
} else
curTooltip = std::make_unique<Tooltip>(ttw, tooltip, MAX_TOOLTIP_WIDTH);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class WindowManager : public Singleton<WindowManager>, public VideoDriverLoaderI
/// Verarbeitung Keyboard-Event
void Msg_KeyDown(const KeyEvent& ke) override;
// setzt den Tooltip
void SetToolTip(const ctrlBaseTooltip* ttw, const std::string& tooltip);
void SetToolTip(const ctrlBaseTooltip* ttw, const std::string& tooltip, bool updateCurrent = false);

/// Verarbeitung Spielfenstergröße verändert (vom Betriebssystem aus)
void WindowResized() override;
Expand Down
6 changes: 6 additions & 0 deletions libs/s25main/controls/ctrlBaseTooltip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ ctrlBaseTooltip::~ctrlBaseTooltip()
HideTooltip();
}

void ctrlBaseTooltip::SetTooltip(const std::string& tooltip)
{
tooltip_ = tooltip;
WINDOWMANAGER.SetToolTip(this, tooltip_, true);
}

void ctrlBaseTooltip::SwapTooltip(ctrlBaseTooltip& other)
{
std::swap(tooltip_, other.tooltip_);
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/controls/ctrlBaseTooltip.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ctrlBaseTooltip
ctrlBaseTooltip(std::string tooltip = "") : tooltip_(std::move(tooltip)) {}
virtual ~ctrlBaseTooltip();

void SetTooltip(const std::string& tooltip) { tooltip_ = tooltip; }
void SetTooltip(const std::string& tooltip);
const std::string& GetTooltip() const { return tooltip_; }
/// Swap the tooltips of those controls
void SwapTooltip(ctrlBaseTooltip& other);
Expand Down