1
0
Fork 0

Codechange: Store EncodedString for text effects.

This replaces capturing and storing string parameters.
pull/13159/head
Peter Nelson 2024-12-07 12:21:36 +00:00
parent 0ca00721b5
commit a2afaaeda4
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
4 changed files with 30 additions and 36 deletions

View File

@ -565,8 +565,7 @@ void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
cost = -cost;
msg = STR_INCOME_FLOAT_INCOME;
}
SetDParam(0, cost);
AddTextEffect(msg, pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
AddTextEffect(GetEncodedString(msg, cost), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
}
/**
@ -581,17 +580,15 @@ void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income
{
Point pt = RemapCoords(x, y, z);
SetDParam(0, transfer);
if (income == 0) {
AddTextEffect(STR_FEEDER, pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
AddTextEffect(GetEncodedString(STR_FEEDER, transfer), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
} else {
StringID msg = STR_FEEDER_COST;
if (income < 0) {
income = -income;
msg = STR_FEEDER_INCOME;
}
SetDParam(1, income);
AddTextEffect(msg, pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
AddTextEffect(GetEncodedString(msg, transfer, income), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
}
}
@ -610,8 +607,7 @@ TextEffectID ShowFillingPercent(int x, int y, int z, uint8_t percent, StringID s
assert(string != STR_NULL);
SetDParam(0, percent);
return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
return AddTextEffect(GetEncodedString(string, percent), pt.x, pt.y, 0, TE_STATIC);
}
/**
@ -623,8 +619,7 @@ void UpdateFillingPercent(TextEffectID te_id, uint8_t percent, StringID string)
{
assert(string != STR_NULL);
SetDParam(0, percent);
UpdateTextEffect(te_id, string);
UpdateTextEffect(te_id, GetEncodedString(string, percent));
}
/**

View File

@ -21,28 +21,29 @@
/** Container for all information about a text effect */
struct TextEffect : public ViewportSign {
std::vector<StringParameterData> params; ///< Backup of string parameters
StringID string_id; ///< String to draw for the text effect, if INVALID_STRING_ID then it's not valid
uint8_t duration; ///< How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
TextEffectMode mode; ///< Type of text effect
TextEffectMode mode; ///< Type of text effect.
uint8_t duration; ///< How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
EncodedString msg; ///< Encoded message for text effect.
/** Reset the text effect */
void Reset()
{
this->MarkDirty();
this->width_normal = 0;
this->string_id = INVALID_STRING_ID;
this->mode = TE_INVALID;
}
inline bool IsValid() const { return this->mode != TE_INVALID; }
};
static std::vector<struct TextEffect> _text_effects; ///< Text effects are stored there
static std::vector<TextEffect> _text_effects; ///< Text effects are stored there
/* Text Effects */
TextEffectID AddTextEffect(StringID msg, int center, int y, uint8_t duration, TextEffectMode mode)
TextEffectID AddTextEffect(EncodedString &&msg, int center, int y, uint8_t duration, TextEffectMode mode)
{
if (_game_mode == GM_MENU) return INVALID_TE_ID;
auto it = std::ranges::find(_text_effects, INVALID_STRING_ID, &TextEffect::string_id);
auto it = std::ranges::find_if(_text_effects, [](const TextEffect &te) { return !te.IsValid(); });
if (it == std::end(_text_effects)) {
/* _text_effects.size() is the maximum ID + 1 that has been allocated. We should not allocate INVALID_TE_ID or beyond. */
if (_text_effects.size() >= INVALID_TE_ID) return INVALID_TE_ID;
@ -52,35 +53,33 @@ TextEffectID AddTextEffect(StringID msg, int center, int y, uint8_t duration, Te
TextEffect &te = *it;
/* Start defining this object */
te.string_id = msg;
te.msg = std::move(msg);
te.duration = duration;
CopyOutDParam(te.params, 2);
te.mode = mode;
/* Make sure we only dirty the new area */
te.width_normal = 0;
te.UpdatePosition(center, y, GetString(te.string_id));
te.UpdatePosition(center, y, te.msg.GetDecodedString());
return static_cast<TextEffectID>(it - std::begin(_text_effects));
}
void UpdateTextEffect(TextEffectID te_id, StringID msg)
void UpdateTextEffect(TextEffectID te_id, EncodedString &&msg)
{
/* Update details */
TextEffect &te = _text_effects[te_id];
if (msg == te.string_id && !HaveDParamChanged(te.params)) return;
te.string_id = msg;
CopyOutDParam(te.params, 2);
if (msg == te.msg) return;
te.msg = std::move(msg);
te.UpdatePosition(te.center, te.top, GetString(te.string_id));
te.UpdatePosition(te.center, te.top, te.msg.GetDecodedString());
}
void UpdateAllTextEffectVirtCoords()
{
for (auto &te : _text_effects) {
if (te.string_id == INVALID_STRING_ID) continue;
CopyInDParam(te.params);
te.UpdatePosition(te.center, te.top, GetString(te.string_id));
if (!te.IsValid()) continue;
te.UpdatePosition(te.center, te.top, te.msg.GetDecodedString());
}
}
@ -94,7 +93,7 @@ IntervalTimer<TimerWindow> move_all_text_effects_interval = {std::chrono::millis
if (_pause_mode && _game_mode != GM_EDITOR && _settings_game.construction.command_pause_level <= CMDPL_NO_CONSTRUCTION) return;
for (TextEffect &te : _text_effects) {
if (te.string_id == INVALID_STRING_ID) continue;
if (!te.IsValid()) continue;
if (te.mode != TE_RISING) continue;
if (te.duration < count) {
@ -125,14 +124,13 @@ void DrawTextEffects(DrawPixelInfo *dpi)
if (dpi->zoom >= ZOOM_LVL_TEXT_EFFECT) flags.Set(ViewportStringFlag::Small);
for (const TextEffect &te : _text_effects) {
if (te.string_id == INVALID_STRING_ID) continue;
if (!te.IsValid()) continue;
if (te.mode == TE_RISING || _settings_client.gui.loading_indicators) {
std::string *str = ViewportAddString(dpi, &te, flags, INVALID_COLOUR);
if (str == nullptr) continue;
CopyInDParam(te.params);
*str = GetString(te.string_id);
*str = te.msg.GetDecodedString();
}
}
}

View File

@ -18,6 +18,7 @@
* Text effect modes.
*/
enum TextEffectMode : uint8_t {
TE_INVALID, ///< Text effect is invalid.
TE_RISING, ///< Make the text effect slowly go upwards
TE_STATIC, ///< Keep the text effect static
};
@ -26,10 +27,10 @@ using TextEffectID = uint16_t;
static const TextEffectID INVALID_TE_ID = UINT16_MAX;
TextEffectID AddTextEffect(StringID msg, int x, int y, uint8_t duration, TextEffectMode mode);
TextEffectID AddTextEffect(EncodedString &&msg, int x, int y, uint8_t duration, TextEffectMode mode);
void InitTextEffects();
void DrawTextEffects(DrawPixelInfo *dpi);
void UpdateTextEffect(TextEffectID effect_id, StringID msg);
void UpdateTextEffect(TextEffectID effect_id, EncodedString &&msg);
void RemoveTextEffect(TextEffectID effect_id);
void UpdateAllTextEffectVirtCoords();

View File

@ -2981,7 +2981,7 @@ void CcStartStopVehicle(Commands, const CommandCost &result, VehicleID veh_id, b
StringID msg = (v->vehstatus & VS_STOPPED) ? STR_VEHICLE_COMMAND_STOPPED : STR_VEHICLE_COMMAND_STARTED;
Point pt = RemapCoords(v->x_pos, v->y_pos, v->z_pos);
AddTextEffect(msg, pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
AddTextEffect(GetEncodedString(msg), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
}
/**