mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Store EncodedString for text effects.
This replaces capturing and storing string parameters.pull/13159/head
parent
0ca00721b5
commit
a2afaaeda4
|
@ -565,8 +565,7 @@ void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
|
||||||
cost = -cost;
|
cost = -cost;
|
||||||
msg = STR_INCOME_FLOAT_INCOME;
|
msg = STR_INCOME_FLOAT_INCOME;
|
||||||
}
|
}
|
||||||
SetDParam(0, cost);
|
AddTextEffect(GetEncodedString(msg, cost), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
|
||||||
AddTextEffect(msg, 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);
|
Point pt = RemapCoords(x, y, z);
|
||||||
|
|
||||||
SetDParam(0, transfer);
|
|
||||||
if (income == 0) {
|
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 {
|
} else {
|
||||||
StringID msg = STR_FEEDER_COST;
|
StringID msg = STR_FEEDER_COST;
|
||||||
if (income < 0) {
|
if (income < 0) {
|
||||||
income = -income;
|
income = -income;
|
||||||
msg = STR_FEEDER_INCOME;
|
msg = STR_FEEDER_INCOME;
|
||||||
}
|
}
|
||||||
SetDParam(1, income);
|
AddTextEffect(GetEncodedString(msg, transfer, income), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
|
||||||
AddTextEffect(msg, 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);
|
assert(string != STR_NULL);
|
||||||
|
|
||||||
SetDParam(0, percent);
|
return AddTextEffect(GetEncodedString(string, percent), pt.x, pt.y, 0, TE_STATIC);
|
||||||
return AddTextEffect(string, 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);
|
assert(string != STR_NULL);
|
||||||
|
|
||||||
SetDParam(0, percent);
|
UpdateTextEffect(te_id, GetEncodedString(string, percent));
|
||||||
UpdateTextEffect(te_id, string);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,28 +21,29 @@
|
||||||
|
|
||||||
/** Container for all information about a text effect */
|
/** Container for all information about a text effect */
|
||||||
struct TextEffect : public ViewportSign {
|
struct TextEffect : public ViewportSign {
|
||||||
std::vector<StringParameterData> params; ///< Backup of string parameters
|
TextEffectMode mode; ///< Type of text effect.
|
||||||
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)
|
uint8_t duration; ///< How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
|
||||||
TextEffectMode mode; ///< Type of text effect
|
EncodedString msg; ///< Encoded message for text effect.
|
||||||
|
|
||||||
/** Reset the text effect */
|
/** Reset the text effect */
|
||||||
void Reset()
|
void Reset()
|
||||||
{
|
{
|
||||||
this->MarkDirty();
|
this->MarkDirty();
|
||||||
this->width_normal = 0;
|
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 */
|
/* 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;
|
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)) {
|
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. */
|
/* _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;
|
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;
|
TextEffect &te = *it;
|
||||||
|
|
||||||
/* Start defining this object */
|
/* Start defining this object */
|
||||||
te.string_id = msg;
|
te.msg = std::move(msg);
|
||||||
te.duration = duration;
|
te.duration = duration;
|
||||||
CopyOutDParam(te.params, 2);
|
|
||||||
te.mode = mode;
|
te.mode = mode;
|
||||||
|
|
||||||
/* Make sure we only dirty the new area */
|
/* Make sure we only dirty the new area */
|
||||||
te.width_normal = 0;
|
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));
|
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 */
|
/* Update details */
|
||||||
TextEffect &te = _text_effects[te_id];
|
TextEffect &te = _text_effects[te_id];
|
||||||
if (msg == te.string_id && !HaveDParamChanged(te.params)) return;
|
if (msg == te.msg) return;
|
||||||
te.string_id = msg;
|
te.msg = std::move(msg);
|
||||||
CopyOutDParam(te.params, 2);
|
|
||||||
|
|
||||||
te.UpdatePosition(te.center, te.top, GetString(te.string_id));
|
te.UpdatePosition(te.center, te.top, te.msg.GetDecodedString());
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateAllTextEffectVirtCoords()
|
void UpdateAllTextEffectVirtCoords()
|
||||||
{
|
{
|
||||||
for (auto &te : _text_effects) {
|
for (auto &te : _text_effects) {
|
||||||
if (te.string_id == INVALID_STRING_ID) continue;
|
if (!te.IsValid()) continue;
|
||||||
CopyInDParam(te.params);
|
|
||||||
te.UpdatePosition(te.center, te.top, GetString(te.string_id));
|
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;
|
if (_pause_mode && _game_mode != GM_EDITOR && _settings_game.construction.command_pause_level <= CMDPL_NO_CONSTRUCTION) return;
|
||||||
|
|
||||||
for (TextEffect &te : _text_effects) {
|
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.mode != TE_RISING) continue;
|
||||||
|
|
||||||
if (te.duration < count) {
|
if (te.duration < count) {
|
||||||
|
@ -125,14 +124,13 @@ void DrawTextEffects(DrawPixelInfo *dpi)
|
||||||
if (dpi->zoom >= ZOOM_LVL_TEXT_EFFECT) flags.Set(ViewportStringFlag::Small);
|
if (dpi->zoom >= ZOOM_LVL_TEXT_EFFECT) flags.Set(ViewportStringFlag::Small);
|
||||||
|
|
||||||
for (const TextEffect &te : _text_effects) {
|
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) {
|
if (te.mode == TE_RISING || _settings_client.gui.loading_indicators) {
|
||||||
std::string *str = ViewportAddString(dpi, &te, flags, INVALID_COLOUR);
|
std::string *str = ViewportAddString(dpi, &te, flags, INVALID_COLOUR);
|
||||||
if (str == nullptr) continue;
|
if (str == nullptr) continue;
|
||||||
|
|
||||||
CopyInDParam(te.params);
|
*str = te.msg.GetDecodedString();
|
||||||
*str = GetString(te.string_id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
* Text effect modes.
|
* Text effect modes.
|
||||||
*/
|
*/
|
||||||
enum TextEffectMode : uint8_t {
|
enum TextEffectMode : uint8_t {
|
||||||
|
TE_INVALID, ///< Text effect is invalid.
|
||||||
TE_RISING, ///< Make the text effect slowly go upwards
|
TE_RISING, ///< Make the text effect slowly go upwards
|
||||||
TE_STATIC, ///< Keep the text effect static
|
TE_STATIC, ///< Keep the text effect static
|
||||||
};
|
};
|
||||||
|
@ -26,10 +27,10 @@ using TextEffectID = uint16_t;
|
||||||
|
|
||||||
static const TextEffectID INVALID_TE_ID = UINT16_MAX;
|
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 InitTextEffects();
|
||||||
void DrawTextEffects(DrawPixelInfo *dpi);
|
void DrawTextEffects(DrawPixelInfo *dpi);
|
||||||
void UpdateTextEffect(TextEffectID effect_id, StringID msg);
|
void UpdateTextEffect(TextEffectID effect_id, EncodedString &&msg);
|
||||||
void RemoveTextEffect(TextEffectID effect_id);
|
void RemoveTextEffect(TextEffectID effect_id);
|
||||||
void UpdateAllTextEffectVirtCoords();
|
void UpdateAllTextEffectVirtCoords();
|
||||||
|
|
||||||
|
|
|
@ -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;
|
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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue