1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* MapPlanetCard.cpp
Copyright (c) 2022 by Hurleveur
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "MapPlanetCard.h"
#include "Color.h"
#include "text/DisplayText.h"
#include "shader/FillShader.h"
#include "text/Font.h"
#include "text/FontSet.h"
#include "GameData.h"
#include "Government.h"
#include "Interface.h"
#include "MapDetailPanel.h"
#include "Planet.h"
#include "Point.h"
#include "shader/PointerShader.h"
#include "Screen.h"
#include "image/Sprite.h"
#include "shader/SpriteShader.h"
#include "StellarObject.h"
#include "System.h"
#include "text/WrappedText.h"
using namespace std;
namespace {
bool hasGovernments = false;
}
MapPlanetCard::MapPlanetCard(const StellarObject &object, unsigned number, bool hasVisited,
const MapDetailPanel *parent)
: parent(parent), number(number), hasVisited(hasVisited), planetName(object.DisplayName())
{
planet = object.GetPlanet();
hasSpaceport = planet->HasServices();
hasShipyard = planet->HasShipyard();
hasOutfitter = planet->HasOutfitter();
governmentName = planet->GetGovernment()->DisplayName();
string systemGovernmentName = planet->GetSystem()->GetGovernment()->DisplayName();
if(governmentName != "Uninhabited" && governmentName != systemGovernmentName)
hasGovernments = true;
if(!hasSpaceport)
reputationLabel = "No Spaceport";
else
{
switch(planet->GetFriendliness())
{
case Planet::Friendliness::FRIENDLY:
reputationLabel = "Friendly";
break;
case Planet::Friendliness::RESTRICTED:
reputationLabel = "Restricted";
break;
case Planet::Friendliness::HOSTILE:
reputationLabel = "Hostile";
break;
case Planet::Friendliness::DOMINATED:
reputationLabel = "Dominated";
break;
}
}
sprite = object.GetSprite();
const Interface *planetCardInterface = GameData::Interfaces().Get("map planet card");
const float planetIconMaxSize = static_cast<float>(planetCardInterface->GetValue("planet icon max size"));
spriteScale = min(.5f, min(planetIconMaxSize / sprite->Width(), planetIconMaxSize / sprite->Height()));
}
MapPlanetCard::ClickAction MapPlanetCard::Click(int x, int y, int clicks)
{
ClickAction clickAction = ClickAction::NONE;
// The isShown variable should have already updated by the drawing of this item.
if(isShown)
{
const Interface *planetCardInterface = GameData::Interfaces().Get("map planet card");
// Point at which the text starts (after the top margin), at first there is the planet's name,
// and then it is divided into clickable categories of the same size.
const double textStart = planetCardInterface->GetValue("text start");
const double categorySize = planetCardInterface->GetValue("category size");
const double categories = planetCardInterface->GetValue("categories");
// The maximum possible size for the sprite of the planet.
const double planetIconMaxSize = planetCardInterface->GetValue("planet icon max size");
// The yCoordinate refers to the center of this object.
double relativeY = (y - yCoordinate);
if(relativeY > 0. && relativeY < AvailableSpace())
{
// The first category is the planet name and is not selectable.
if(x > Screen::Left() + planetIconMaxSize &&
relativeY > textStart + categorySize && relativeY < textStart + categorySize * (categories + hasGovernments))
selectedCategory = (relativeY - textStart - categorySize) / categorySize;
else
clickAction = ClickAction::SELECTED;
static const int SHOW[5] = {MapPanel::SHOW_GOVERNMENT, MapPanel::SHOW_REPUTATION,
MapPanel::SHOW_SHIPYARD, MapPanel::SHOW_OUTFITTER,
MapPanel::SHOW_VISITED};
if(clickAction != ClickAction::SELECTED)
{
// If there are no governments shown, the first category is the reputation.
clickAction = static_cast<ClickAction>(SHOW[selectedCategory + !hasGovernments]);
// Double clicking results in going to the shipyard/outfitter.
if(clickAction == ClickAction::SHOW_SHIPYARD && clicks > 1)
clickAction = ClickAction::GOTO_SHIPYARD;
else if(clickAction == ClickAction::SHOW_OUTFITTER && clicks > 1)
clickAction = ClickAction::GOTO_OUTFITTER;
}
}
}
isSelected = (clickAction != ClickAction::NONE);
return clickAction;
}
bool MapPlanetCard::DrawIfFits(const Point &uiPoint)
{
// Need to update this before checking if the element fits.
yCoordinate = uiPoint.Y();
isShown = IsShown();
if(isShown)
{
const Font &font = FontSet::Get(14);
const Color &faint = *GameData::Colors().Get("faint");
const Color &dim = *GameData::Colors().Get("dim");
const Color &medium = *GameData::Colors().Get("medium");
const Interface *planetCardInterface = GameData::Interfaces().Get("map planet card");
// The maximum possible size for the sprite of the planet.
const double planetIconMaxSize = planetCardInterface->GetValue("planet icon max size");
const auto alignLeft = Layout(planetCardInterface->GetValue("width") - planetIconMaxSize, Truncate::BACK);
// Height of one MapPlanetCard element.
const double height = Height();
// Point at which the text starts (after the top margin), at first there is the planet's name,
// and then it is divided into clickable categories of the same size.
const double textStart = planetCardInterface->GetValue("text start");
const double categorySize = planetCardInterface->GetValue("category size");
const double categories = planetCardInterface->GetValue("categories");
// Available space, limited by the space there is between the top of this item,
// and the end of the panel below.
const double availableBottomSpace = AvailableBottomSpace();
// The top part goes out of the screen so we can draw there. The bottom would go out of this panel.
const Interface *mapInterface = GameData::Interfaces().Get("map detail panel");
auto spriteItem = SpriteShader::Prepare(sprite, Point(Screen::Left() + planetIconMaxSize / 2.,
uiPoint.Y() + height / 2.), spriteScale);
float clip = 1.f;
// Lowest point of the planet sprite.
double planetBottomY = height / 2. + spriteScale * sprite->Height() / 2.;
// Calculate the correct clip on the bottom of the sprite if necessary.
// It is done by looking at how much space is available,
// and the difference between that and the lowest point of the sprite.
// Of course, the clipping needs to be done relative to the size of the sprite.
if(availableBottomSpace <= planetBottomY)
clip = 1.f + (availableBottomSpace - planetBottomY) / (spriteScale * sprite->Height());
spriteItem.clip = clip;
spriteItem.position[1] -= (sprite->Height() * ((1.f - clip) * .5f)) * spriteScale;
spriteItem.transform[3] *= clip;
SpriteShader::Bind();
SpriteShader::Add(spriteItem);
SpriteShader::Unbind();
// Check if drawing a category would not go out of the panel.
const auto FitsCategory = [availableBottomSpace, categorySize, height]
(double number)
{
return availableBottomSpace >= height - (categorySize * number);
};
// Draw the name of the planet.
if(FitsCategory(categories + hasGovernments))
font.Draw({ planetName, alignLeft }, uiPoint + Point(0, textStart), isSelected ? medium : dim);
// Draw the government name, reputation, shipyard, outfitter and visited.
const double margin = mapInterface->GetValue("text margin");
if(hasGovernments && FitsCategory(categories))
font.Draw(governmentName, uiPoint + Point(margin, textStart + categorySize),
governmentName == "Uninhabited" ? faint : dim);
if(FitsCategory(4.))
font.Draw(reputationLabel, uiPoint + Point(margin, textStart + categorySize * (1. + hasGovernments)),
hasSpaceport ? medium : faint);
if(FitsCategory(3.))
font.Draw("Shipyard", uiPoint + Point(margin, textStart + categorySize * (2. + hasGovernments)),
hasShipyard ? medium : faint);
if(FitsCategory(2.))
font.Draw("Outfitter", uiPoint + Point(margin, textStart + categorySize * (3. + hasGovernments)),
hasOutfitter ? medium : faint);
if(FitsCategory(1.))
font.Draw(hasVisited ? "(has been visited)" : "(not yet visited)",
uiPoint + Point(margin, textStart + categorySize * (4. + hasGovernments)), dim);
// Draw the arrow pointing to the selected category.
if(FitsCategory(categories - (selectedCategory + 1.)))
PointerShader::Draw(uiPoint + Point(margin, textStart + 8. + (selectedCategory + 1) * categorySize),
Point(1., 0.), 10.f, 10.f, 0.f, medium);
if(isSelected)
Highlight(availableBottomSpace);
}
else
yCoordinate = Screen::Bottom();
return isShown;
}
bool MapPlanetCard::IsShown() const
{
return AvailableSpace() > 15.;
}
bool MapPlanetCard::IsSelected() const
{
return isSelected;
}
double MapPlanetCard::AvailableSpace() const
{
return min(AvailableBottomSpace(), AvailableTopSpace());
}
const Planet *MapPlanetCard::GetPlanet() const
{
return planet;
}
void MapPlanetCard::Select(bool select)
{
isSelected = select;
}
double MapPlanetCard::Height()
{
const Interface *planetCardInterface = GameData::Interfaces().Get("map planet card");
return planetCardInterface->GetValue("height padding") +
(planetCardInterface->GetValue("categories") + hasGovernments) *
planetCardInterface->GetValue("category size");
}
void MapPlanetCard::ResetSize()
{
hasGovernments = false;
}
void MapPlanetCard::Highlight(double availableSpace) const
{
const Interface *planetCardInterface = GameData::Interfaces().Get("map planet card");
const double width = planetCardInterface->GetValue("width");
Rectangle highlightRegion = Rectangle::FromCorner(Point(Screen::Left(), yCoordinate), Point(width, availableSpace));
FillShader::Fill(highlightRegion, *GameData::Colors().Get("item selected"));
}
double MapPlanetCard::AvailableTopSpace() const
{
const double height = Height();
return min(height, max(0., (number + 1) * height - parent->GetScroll()));
}
double MapPlanetCard::AvailableBottomSpace() const
{
const Interface *mapInterface = GameData::Interfaces().Get("map detail panel");
double maxPlanetPanelHeight = mapInterface->GetValue("max planet panel height");
return min(Height(), max(0., Screen::Top() +
min(MapDetailPanel::PlanetPanelHeight(), maxPlanetPanelHeight) - yCoordinate));
}
|