Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shop tooltips for total credits and ship names #9825

Merged
merged 8 commits into from
Feb 25, 2024
73 changes: 52 additions & 21 deletions source/ShopPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ namespace {
{
return ship.GetPlanet() == here;
}

const int HOVER_TIME = 60;

void DrawTooltip(const string &text, const Point &hoverPoint, const Color &textColor, const Color &backColor)
{
constexpr int WIDTH = 250;
constexpr int PAD = 10;
WrappedText wrap(FontSet::Get(14));
wrap.SetWrapWidth(WIDTH - 2 * PAD);
wrap.Wrap(text);
int longest = wrap.LongestLineWidth();
if(longest < wrap.WrapWidth())
{
wrap.SetWrapWidth(longest);
wrap.Wrap(text);
}

Point textSize(wrap.WrapWidth() + 2 * PAD, wrap.Height() + 2 * PAD - wrap.ParagraphBreak());
Point anchor = Point(hoverPoint.X(), min<double>(hoverPoint.Y() + textSize.Y(), Screen::Bottom()));
FillShader::Fill(anchor - .5 * textSize, textSize, backColor);
wrap.Draw(anchor - textSize + Point(PAD, PAD), textColor);
}
}


Expand Down Expand Up @@ -105,23 +127,15 @@ void ShopPanel::Draw()
shipInfo.DrawTooltips();
outfitInfo.DrawTooltips();

if(!warningType.empty())
if(!shipName.empty())
{
constexpr int WIDTH = 250;
constexpr int PAD = 10;
const string &text = GameData::Tooltip(warningType);
WrappedText wrap(FontSet::Get(14));
wrap.SetWrapWidth(WIDTH - 2 * PAD);
wrap.Wrap(text);

bool isError = (warningType.back() == '!');
string text = shipName;
if(!warningType.empty())
text += "\n" + GameData::Tooltip(warningType);
const Color &textColor = *GameData::Colors().Get("medium");
const Color &backColor = *GameData::Colors().Get(isError ? "error back" : "warning back");

Point size(WIDTH, wrap.Height() + 2 * PAD);
Point anchor = Point(warningPoint.X(), min<double>(warningPoint.Y() + size.Y(), Screen::Bottom()));
FillShader::Fill(anchor - .5 * size, size, backColor);
wrap.Draw(anchor - size + Point(PAD, PAD), textColor);
const Color &backColor = *GameData::Colors().Get(warningType.empty() ? "tooltip background"
: (warningType.back() == '!' ? "error back" : "warning back"));
DrawTooltip(text, hoverPoint, textColor, backColor);
}

if(dragShip && isDraggingShip && dragShip->GetSprite())
Expand Down Expand Up @@ -527,7 +541,7 @@ bool ShopPanel::Click(int x, int y, int /* clicks */)

bool ShopPanel::Hover(int x, int y)
{
Point point(x, y);
hoverPoint = Point(x, y);
// Check that the point is not in the button area.
hoverButton = CheckButton(x, y);
if(hoverButton)
Expand All @@ -537,8 +551,8 @@ bool ShopPanel::Hover(int x, int y)
}
else
{
shipInfo.Hover(point);
outfitInfo.Hover(point);
shipInfo.Hover(hoverPoint);
outfitInfo.Hover(hoverPoint);
}

activePane = ShopPane::Main;
Expand Down Expand Up @@ -710,6 +724,7 @@ void ShopPanel::DrawShipsSidebar()
const auto flightChecks = player.FlightCheck();
Point mouse = UI::GetMouse();
warningType.clear();
shipName.clear();
shipZones.clear();

static const Color selected(.8f, 1.f);
Expand Down Expand Up @@ -752,17 +767,20 @@ void ShopPanel::DrawShipsSidebar()

shipZones.emplace_back(point, Point(ICON_TILE, ICON_TILE), ship.get());

if(shipZones.back().Contains(mouse))
{
shipName = ship->Name();
hoverPoint = shipZones.back().TopLeft();
}

const auto checkIt = flightChecks.find(ship);
if(checkIt != flightChecks.end())
{
const string &check = (*checkIt).second.front();
const Sprite *icon = SpriteSet::Get(check.back() == '!' ? "ui/error" : "ui/warning");
SpriteShader::Draw(icon, point + .5 * Point(ICON_TILE - icon->Width(), ICON_TILE - icon->Height()));
if(shipZones.back().Contains(mouse))
{
warningType = check;
warningPoint = shipZones.back().TopLeft();
}
}

if(isSelected && playerShips.size() > 1 && ship->OutfitCount(selectedOutfit))
Expand Down Expand Up @@ -907,6 +925,19 @@ void ShopPanel::DrawButtons()
if(CanSellMultiple())
font.Draw(mod, sellCenter + Point(-.5 * modWidth, 10.), dim);
}

// Draw the tooltip for your full number of credits.
const Rectangle creditsBox = Rectangle::FromCorner(creditsPoint, Point(SIDEBAR_WIDTH - 20, 15));
if(creditsBox.Contains(hoverPoint))
hoverCount += hoverCount < HOVER_TIME;
else if(hoverCount)
--hoverCount;

if(hoverCount == HOVER_TIME)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we recreate a box every frame? ig it's fine given we're in the shop panel and all
mb this means it even moves around with your mouse?

Copy link
Collaborator Author

@Amazinite Amazinite Feb 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mb this means it even moves around with your mouse?

Yeah. That's how tooltips normally work. The only exception is actually the ship name/warning tooltip.

Shouldnt we clear overcount when you move out of the hoverzone tho?

Tooltips also normally decay their count instead of clearing it.

{
string text = Format::Number(player.Accounts().Credits()) + " credits";
DrawTooltip(text, hoverPoint, dim, *GameData::Colors().Get("tooltip background"));
}
}


Expand Down
4 changes: 4 additions & 0 deletions source/ShopPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ class ShopPanel : public Panel {

private:
bool delayedAutoScroll = false;

Point hoverPoint;
std::string shipName;
int hoverCount = 0;
};


Expand Down
Loading