Codechange: make StoryPageElementType a scoped enum

This commit is contained in:
Peter Nelson
2026-05-30 22:27:24 +01:00
committed by Peter Nelson
parent f9dfbd59a3
commit 06af27d52e
6 changed files with 72 additions and 73 deletions
+10 -10
View File
@@ -25,7 +25,7 @@
static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
{
return type == SPET_TEXT || type == SPET_LOCATION || type == SPET_BUTTON_PUSH || type == SPET_BUTTON_TILE || type == SPET_BUTTON_VEHICLE;
return type == StoryPageElementType::Text || type == StoryPageElementType::Location || type == StoryPageElementType::ButtonPush || type == StoryPageElementType::ButtonTile || type == StoryPageElementType::ButtonVehicle;
}
/* static */ bool ScriptStoryPage::IsValidStoryPage(StoryPageID story_page_id)
@@ -123,23 +123,23 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
encoded_text = text->GetEncodedText();
EnforcePreconditionEncodedText(false, encoded_text);
}
EnforcePrecondition(false, type != ::SPET_LOCATION || ::IsValidTile((::TileIndex)reference));
EnforcePrecondition(false, type != ::SPET_GOAL || ScriptGoal::IsValidGoal(static_cast<::GoalID>(reference)));
EnforcePrecondition(false, type != ::SPET_GOAL || !(p->company == CompanyID::Invalid() && Goal::Get(reference)->company != CompanyID::Invalid()));
EnforcePrecondition(false, type != ::StoryPageElementType::Location || ::IsValidTile((::TileIndex)reference));
EnforcePrecondition(false, type != ::StoryPageElementType::Goal || ScriptGoal::IsValidGoal(static_cast<::GoalID>(reference)));
EnforcePrecondition(false, type != ::StoryPageElementType::Goal || !(p->company == CompanyID::Invalid() && Goal::Get(reference)->company != CompanyID::Invalid()));
uint32_t refid = 0;
TileIndex reftile{};
switch (type) {
case ::SPET_LOCATION:
case ::StoryPageElementType::Location:
reftile = TileIndex(reference);
break;
case ::SPET_GOAL:
case ::SPET_BUTTON_PUSH:
case ::SPET_BUTTON_TILE:
case ::SPET_BUTTON_VEHICLE:
case ::StoryPageElementType::Goal:
case ::StoryPageElementType::ButtonPush:
case ::StoryPageElementType::ButtonTile:
case ::StoryPageElementType::ButtonVehicle:
refid = reference;
break;
case ::SPET_TEXT:
case ::StoryPageElementType::Text:
break;
default:
NOT_REACHED();
+6 -6
View File
@@ -45,12 +45,12 @@ public:
* Story page element types.
*/
enum StoryPageElementType : uint8_t {
SPET_TEXT = ::SPET_TEXT, ///< An element that displays a block of text.
SPET_LOCATION = ::SPET_LOCATION, ///< An element that displays a single line of text along with a button to view the referenced location.
SPET_GOAL = ::SPET_GOAL, ///< An element that displays a goal.
SPET_BUTTON_PUSH = ::SPET_BUTTON_PUSH, ///< A push button that triggers an immediate event.
SPET_BUTTON_TILE = ::SPET_BUTTON_TILE, ///< A button that allows the player to select a tile, and triggers an event with the tile.
SPET_BUTTON_VEHICLE = ::SPET_BUTTON_VEHICLE, ///< A button that allows the player to select a vehicle, and triggers an event with the vehicle.
SPET_TEXT = ::to_underlying(::StoryPageElementType::Text), ///< An element that displays a block of text.
SPET_LOCATION = ::to_underlying(::StoryPageElementType::Location), ///< An element that displays a single line of text along with a button to view the referenced location.
SPET_GOAL = ::to_underlying(::StoryPageElementType::Goal), ///< An element that displays a goal.
SPET_BUTTON_PUSH = ::to_underlying(::StoryPageElementType::ButtonPush), ///< A push button that triggers an immediate event.
SPET_BUTTON_TILE = ::to_underlying(::StoryPageElementType::ButtonTile), ///< A button that allows the player to select a tile, and triggers an event with the tile.
SPET_BUTTON_VEHICLE = ::to_underlying(::StoryPageElementType::ButtonVehicle), ///< A button that allows the player to select a vehicle, and triggers an event with the vehicle.
};
/**
+15 -15
View File
@@ -62,28 +62,28 @@ static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElement
StoryPageButtonData button_data{ reference };
switch (type) {
case SPET_TEXT:
case StoryPageElementType::Text:
if (text.empty()) return false;
break;
case SPET_LOCATION:
case StoryPageElementType::Location:
if (text.empty()) return false;
if (!IsValidTile(tile)) return false;
break;
case SPET_GOAL:
case StoryPageElementType::Goal:
if (!Goal::IsValidID((GoalID)reference)) return false;
/* Reject company specific goals on global pages */
if (StoryPage::Get(page_id)->company == CompanyID::Invalid() && Goal::Get((GoalID)reference)->company != CompanyID::Invalid()) return false;
break;
case SPET_BUTTON_PUSH:
case StoryPageElementType::ButtonPush:
if (!button_data.ValidateColour()) return false;
if (!button_data.ValidateFlags()) return false;
return true;
case SPET_BUTTON_TILE:
case StoryPageElementType::ButtonTile:
if (!button_data.ValidateColour()) return false;
if (!button_data.ValidateFlags()) return false;
if (!button_data.ValidateCursor()) return false;
return true;
case SPET_BUTTON_VEHICLE:
case StoryPageElementType::ButtonVehicle:
if (!button_data.ValidateColour()) return false;
if (!button_data.ValidateFlags()) return false;
if (!button_data.ValidateCursor()) return false;
@@ -107,19 +107,19 @@ static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElement
static void UpdateElement(StoryPageElement &pe, TileIndex tile, uint32_t reference, const EncodedString &text)
{
switch (pe.type) {
case SPET_TEXT:
case StoryPageElementType::Text:
pe.text = text;
break;
case SPET_LOCATION:
case StoryPageElementType::Location:
pe.text = text;
pe.referenced_id = tile.base();
break;
case SPET_GOAL:
case StoryPageElementType::Goal:
pe.referenced_id = reference;
break;
case SPET_BUTTON_PUSH:
case SPET_BUTTON_TILE:
case SPET_BUTTON_VEHICLE:
case StoryPageElementType::ButtonPush:
case StoryPageElementType::ButtonTile:
case StoryPageElementType::ButtonVehicle:
pe.text = text;
pe.referenced_id = reference;
break;
@@ -486,15 +486,15 @@ CommandCost CmdStoryPageButton(DoCommandFlags flags, TileIndex tile, StoryPageEl
if (sp->company != CompanyID::Invalid() && sp->company != _current_company) return CMD_ERROR;
switch (pe->type) {
case SPET_BUTTON_PUSH:
case StoryPageElementType::ButtonPush:
/* No validation required */
if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageButtonClick(_current_company, pe->page, page_element_id));
break;
case SPET_BUTTON_TILE:
case StoryPageElementType::ButtonTile:
if (!IsValidTile(tile)) return CMD_ERROR;
if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageTileSelect(_current_company, pe->page, page_element_id, tile));
break;
case SPET_BUTTON_VEHICLE:
case StoryPageElementType::ButtonVehicle:
if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
if (flags.Test(DoCommandFlag::Execute)) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, reference));
break;
+8 -9
View File
@@ -26,15 +26,14 @@ extern uint32_t _story_page_element_next_sort_value;
extern uint32_t _story_page_next_sort_value;
/** Each story page element is one of these types. */
enum StoryPageElementType : uint8_t {
SPET_TEXT = 0, ///< A text element.
SPET_LOCATION, ///< An element that references a tile along with a one-line text.
SPET_GOAL, ///< An element that references a goal.
SPET_BUTTON_PUSH, ///< A push button that triggers an immediate event.
SPET_BUTTON_TILE, ///< A button that allows the player to select a tile, and triggers an event with the tile.
SPET_BUTTON_VEHICLE, ///< A button that allows the player to select a vehicle, and triggers an event with the vehicle.
SPET_END,
INVALID_SPET = 0xFF,
enum class StoryPageElementType : uint8_t {
Text = 0, ///< A text element.
Location, ///< An element that references a tile along with a one-line text.
Goal, ///< An element that references a goal.
ButtonPush, ///< A push button that triggers an immediate event.
ButtonTile, ///< A button that allows the player to select a tile, and triggers an event with the tile.
ButtonVehicle, ///< A button that allows the player to select a vehicle, and triggers an event with the vehicle.
Invalid = 0xFF, ///< Invalid story page element type.
};
/** Flags available for buttons */
+32 -32
View File
@@ -297,17 +297,17 @@ protected:
* Decides which sprite to display for a given page element.
* @param pe The page element.
* @return The SpriteID of the sprite to display.
* @pre pe.type must be SPET_GOAL or SPET_LOCATION.
* @pre pe.type must be StoryPageElementType::Goal or StoryPageElementType::Location.
*/
SpriteID GetPageElementSprite(const StoryPageElement &pe) const
{
switch (pe.type) {
case SPET_GOAL: {
case StoryPageElementType::Goal: {
Goal *g = Goal::Get((GoalID) pe.referenced_id);
if (g == nullptr) return SPR_IMG_GOAL_BROKEN_REF;
return g->completed ? SPR_IMG_GOAL_COMPLETED : SPR_IMG_GOAL;
}
case SPET_LOCATION:
case StoryPageElementType::Location:
return SPR_IMG_VIEW_LOCATION;
default:
NOT_REACHED();
@@ -323,18 +323,18 @@ protected:
uint GetPageElementHeight(const StoryPageElement &pe, int max_width) const
{
switch (pe.type) {
case SPET_TEXT:
case StoryPageElementType::Text:
return GetStringHeight(pe.text.GetDecodedString(), max_width);
case SPET_GOAL:
case SPET_LOCATION: {
case StoryPageElementType::Goal:
case StoryPageElementType::Location: {
Dimension sprite_dim = GetSpriteSize(GetPageElementSprite(pe));
return sprite_dim.height;
}
case SPET_BUTTON_PUSH:
case SPET_BUTTON_TILE:
case SPET_BUTTON_VEHICLE: {
case StoryPageElementType::ButtonPush:
case StoryPageElementType::ButtonTile:
case StoryPageElementType::ButtonVehicle: {
Dimension dim = GetStringBoundingBox(pe.text.GetDecodedString(), FontSize::Normal);
return dim.height + WidgetDimensions::scaled.framerect.Vertical() + WidgetDimensions::scaled.frametext.Vertical();
}
@@ -353,9 +353,9 @@ protected:
ElementFloat GetPageElementFloat(const StoryPageElement &pe) const
{
switch (pe.type) {
case SPET_BUTTON_PUSH:
case SPET_BUTTON_TILE:
case SPET_BUTTON_VEHICLE: {
case StoryPageElementType::ButtonPush:
case StoryPageElementType::ButtonTile:
case StoryPageElementType::ButtonVehicle: {
StoryPageButtonFlags flags = StoryPageButtonData{ pe.referenced_id }.GetFlags();
if (flags.Test(StoryPageButtonFlag::FloatLeft)) return ElementFloat::Left;
if (flags.Test(StoryPageButtonFlag::FloatRight)) return ElementFloat::Right;
@@ -375,9 +375,9 @@ protected:
int GetPageElementFloatWidth(const StoryPageElement &pe) const
{
switch (pe.type) {
case SPET_BUTTON_PUSH:
case SPET_BUTTON_TILE:
case SPET_BUTTON_VEHICLE: {
case StoryPageElementType::ButtonPush:
case StoryPageElementType::ButtonTile:
case StoryPageElementType::ButtonVehicle: {
Dimension dim = GetStringBoundingBox(pe.text.GetDecodedString(), FontSize::Normal);
return dim.width + WidgetDimensions::scaled.framerect.Vertical() + WidgetDimensions::scaled.frametext.Vertical();
}
@@ -440,9 +440,9 @@ protected:
/* Check for button that needs extra margin */
if (left_offset == 0 && right_offset == 0) {
switch (pe->type) {
case SPET_BUTTON_PUSH:
case SPET_BUTTON_TILE:
case SPET_BUTTON_VEHICLE:
case StoryPageElementType::ButtonPush:
case StoryPageElementType::ButtonTile:
case StoryPageElementType::ButtonVehicle:
left_offset = right_offset = available_width / 5;
break;
default:
@@ -534,11 +534,11 @@ protected:
void OnPageElementClick(const StoryPageElement &pe)
{
switch (pe.type) {
case SPET_TEXT:
case StoryPageElementType::Text:
/* Do nothing. */
break;
case SPET_LOCATION:
case StoryPageElementType::Location:
if (_ctrl_pressed) {
ShowExtraViewportWindow((TileIndex)pe.referenced_id);
} else {
@@ -546,11 +546,11 @@ protected:
}
break;
case SPET_GOAL:
case StoryPageElementType::Goal:
ShowGoalsList(this->window_number);
break;
case SPET_BUTTON_PUSH:
case StoryPageElementType::ButtonPush:
if (this->active_button_id != StoryPageElementID::Invalid()) ResetObjectToPlace();
this->active_button_id = pe.index;
this->SetTimeout();
@@ -559,7 +559,7 @@ protected:
Command<Commands::StoryPageButton>::Post(TileIndex{}, pe.index, VehicleID::Invalid());
break;
case SPET_BUTTON_TILE:
case StoryPageElementType::ButtonTile:
if (this->active_button_id == pe.index) {
ResetObjectToPlace();
this->active_button_id = StoryPageElementID::Invalid();
@@ -571,7 +571,7 @@ protected:
this->SetWidgetDirty(WID_SB_PAGE_PANEL);
break;
case SPET_BUTTON_VEHICLE:
case StoryPageElementType::ButtonVehicle:
if (this->active_button_id == pe.index) {
ResetObjectToPlace();
this->active_button_id = StoryPageElementID::Invalid();
@@ -714,26 +714,26 @@ public:
if (y_offset > fr.bottom) return;
switch (ce.pe->type) {
case SPET_TEXT:
case StoryPageElementType::Text:
DrawStringMultiLineWithClipping(ce.bounds.left, ce.bounds.right, ce.bounds.top - scrollpos, ce.bounds.bottom - scrollpos,
ce.pe->text.GetDecodedString(), TextColour::Black, SA_TOP | SA_LEFT);
break;
case SPET_GOAL: {
case StoryPageElementType::Goal: {
Goal *g = Goal::Get((GoalID) ce.pe->referenced_id);
DrawActionElement(y_offset, ce.bounds.right - ce.bounds.left, line_height, GetPageElementSprite(*ce.pe),
g == nullptr ? GetString(STR_STORY_BOOK_INVALID_GOAL_REF) : g->text.GetDecodedString());
break;
}
case SPET_LOCATION:
case StoryPageElementType::Location:
DrawActionElement(y_offset, ce.bounds.right - ce.bounds.left, line_height, GetPageElementSprite(*ce.pe),
ce.pe->text.GetDecodedString());
break;
case SPET_BUTTON_PUSH:
case SPET_BUTTON_TILE:
case SPET_BUTTON_VEHICLE: {
case StoryPageElementType::ButtonPush:
case StoryPageElementType::ButtonTile:
case StoryPageElementType::ButtonVehicle: {
const int tmargin = WidgetDimensions::scaled.bevel.top + WidgetDimensions::scaled.frametext.top;
const FrameFlags frame = this->active_button_id == ce.pe->index ? FrameFlag::Lowered : FrameFlags{};
const Colours bgcolour = StoryPageButtonData{ ce.pe->referenced_id }.GetColour();
@@ -894,7 +894,7 @@ public:
void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override
{
const StoryPageElement *const pe = StoryPageElement::GetIfValid(this->active_button_id);
if (pe == nullptr || pe->type != SPET_BUTTON_TILE) {
if (pe == nullptr || pe->type != StoryPageElementType::ButtonTile) {
ResetObjectToPlace();
this->active_button_id = StoryPageElementID::Invalid();
this->SetWidgetDirty(WID_SB_PAGE_PANEL);
@@ -908,7 +908,7 @@ public:
bool OnVehicleSelect(const Vehicle *v) override
{
const StoryPageElement *const pe = StoryPageElement::GetIfValid(this->active_button_id);
if (pe == nullptr || pe->type != SPET_BUTTON_VEHICLE) {
if (pe == nullptr || pe->type != StoryPageElementType::ButtonVehicle) {
ResetObjectToPlace();
this->active_button_id = StoryPageElementID::Invalid();
this->SetWidgetDirty(WID_SB_PAGE_PANEL);
+1 -1
View File
@@ -16,7 +16,7 @@ using StoryPageElementID = PoolID<uint16_t, struct StoryPageElementIDTag, 64000,
using StoryPageID = PoolID<uint16_t, struct StoryPageIDTag, 64000, 0xFFFF>; ///< ID of a story page
struct StoryPageElement;
struct StoryPage;
enum StoryPageElementType : uint8_t;
enum class StoryPageElementType : uint8_t;
#endif /* STORY_TYPE_H */