Skip to content

Commit

Permalink
feat(ui): The shop now has tooltips for viewing your unabbreviated cr…
Browse files Browse the repository at this point in the history
…edits and the names of your ships without needing to select them (#9825)
  • Loading branch information
Amazinite committed Feb 25, 2024
1 parent 340479a commit fffab90
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
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)
{
string text = Format::Number(player.Accounts().Credits()) + " credits";
DrawTooltip(text, hoverPoint, dim, *GameData::Colors().Get("tooltip background"));
}
}


Expand Down
8 changes: 5 additions & 3 deletions source/ShopPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ class ShopPanel : public Panel {
ShipInfoDisplay shipInfo;
OutfitInfoDisplay outfitInfo;

mutable Point warningPoint;
mutable std::string warningType;


private:
void DrawShipsSidebar();
Expand Down Expand Up @@ -214,6 +211,11 @@ class ShopPanel : public Panel {

private:
bool delayedAutoScroll = false;

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


Expand Down

0 comments on commit fffab90

Please sign in to comment.