OpenTTD Source 20261112-master-gbcbec5e63f
texteff.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "texteff.hpp"
12#include "transparency.h"
13#include "viewport_func.h"
14#include "settings_type.h"
15#include "command_type.h"
16#include "timer/timer.h"
17#include "timer/timer_window.h"
18
19#include "safeguards.h"
20
22struct TextEffect : public ViewportSign {
24 uint8_t duration;
26
28 void Reset()
29 {
30 this->MarkDirty();
31 this->width_normal = 0;
32 this->mode = TextEffectMode::Invalid;
33 }
34
35 inline bool IsValid() const { return this->mode != TextEffectMode::Invalid; }
36};
37
38static std::vector<TextEffect> _text_effects;
39
40/* Text Effects */
41TextEffectID AddTextEffect(EncodedString &&msg, int center, int y, uint8_t duration, TextEffectMode mode)
42{
43 if (_game_mode == GameMode::Menu) return INVALID_TE_ID;
44
45 auto it = std::ranges::find_if(_text_effects, [](const TextEffect &te) { return !te.IsValid(); });
46 if (it == std::end(_text_effects)) {
47 /* _text_effects.size() is the maximum ID + 1 that has been allocated. We should not allocate INVALID_TE_ID or beyond. */
48 if (_text_effects.size() >= INVALID_TE_ID) return INVALID_TE_ID;
49 it = _text_effects.emplace(std::end(_text_effects));
50 }
51
52 TextEffect &te = *it;
53
54 /* Start defining this object */
55 te.msg = std::move(msg);
56 te.duration = duration;
57 te.mode = mode;
58
59 /* Make sure we only dirty the new area */
60 te.width_normal = 0;
61 te.UpdatePosition(center, y, te.msg.GetDecodedString());
62
63 return static_cast<TextEffectID>(it - std::begin(_text_effects));
64}
65
66void UpdateTextEffect(TextEffectID te_id, EncodedString &&msg)
67{
68 /* Update details */
69 TextEffect &te = _text_effects[te_id];
70 if (msg == te.msg) return;
71 te.msg = std::move(msg);
72
74}
75
76void UpdateAllTextEffectVirtCoords()
77{
78 for (auto &te : _text_effects) {
79 if (!te.IsValid()) continue;
80
82 }
83}
84
85void RemoveTextEffect(TextEffectID te_id)
86{
87 _text_effects[te_id].Reset();
88}
89
91const IntervalTimer<TimerWindow> move_all_text_effects_interval = {std::chrono::milliseconds(30), [](uint count) {
92 if (_pause_mode.Any() && _game_mode != GameMode::Editor && _settings_game.construction.command_pause_level <= CommandPauseLevel::NoConstruction) return;
93
94 for (TextEffect &te : _text_effects) {
95 if (!te.IsValid()) continue;
96 if (te.mode != TextEffectMode::Rising) continue;
97
98 if (te.duration < count) {
99 te.Reset();
100 continue;
101 }
102
104 te.duration -= count;
105 te.top -= count * ZOOM_BASE;
107 }
108}};
109
110void InitTextEffects()
111{
112 _text_effects.clear();
113 _text_effects.shrink_to_fit();
114}
115
116void DrawTextEffects(DrawPixelInfo *dpi)
117{
118 /* Don't draw the text effects when zoomed out a lot */
119 if (dpi->zoom > ZoomLevel::TextEffect) return;
121
122 ViewportStringFlags flags{};
123 if (dpi->zoom >= ZoomLevel::TextEffect) flags.Set(ViewportStringFlag::Small);
124
125 for (const TextEffect &te : _text_effects) {
126 if (!te.IsValid()) continue;
127
128 if (te.mode == TextEffectMode::Rising || _settings_client.gui.loading_indicators) {
129 std::string *str = ViewportAddString(dpi, &te, flags, Colours::Invalid);
130 if (str == nullptr) continue;
131
132 *str = te.msg.GetDecodedString();
133 }
134 }
135}
constexpr Timpl & Set()
Set all bits.
Container for an encoded string, created by GetEncodedString.
std::string GetDecodedString() const
Decode the encoded string.
Definition strings.cpp:206
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
Types related to commands.
@ NoConstruction
No construction actions may be executed.
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:46
@ Invalid
Invalid marker.
Definition gfx_type.h:302
void MarkDirty(ZoomLevel maxzoom=ZoomLevel::Max) const
Mark the sign dirty in all viewports.
@ Editor
In the scenario editor.
Definition openttd.h:21
@ Menu
In the main menu.
Definition openttd.h:19
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Data about how and where to blit pixels.
Definition gfx_type.h:157
Container for all information about a text effect.
Definition texteff.cpp:22
TextEffectMode mode
Type of text effect.
Definition texteff.cpp:23
uint8_t duration
How long the text effect should stay, in ticks (applies only when mode == TextEffectMode::Rising).
Definition texteff.cpp:24
void Reset()
Reset the text effect.
Definition texteff.cpp:28
EncodedString msg
Encoded message for text effect.
Definition texteff.cpp:25
Location information about a sign as seen on the viewport.
int32_t center
The center position of the sign.
uint16_t width_normal
The width when not zoomed out (normal font).
void UpdatePosition(int center, int top, std::string_view str, std::string_view str_small={})
Update the position of the viewport sign.
int32_t top
The top of the sign.
const IntervalTimer< TimerWindow > move_all_text_effects_interval
Slowly move text effects upwards.
Definition texteff.cpp:91
static std::vector< TextEffect > _text_effects
Text effects are stored there.
Definition texteff.cpp:38
Functions related to text effects.
TextEffectMode
Text effect modes.
Definition texteff.hpp:20
@ Rising
Make the text effect slowly go upwards.
Definition texteff.hpp:22
@ Invalid
Text effect is invalid.
Definition texteff.hpp:21
Definition of Interval and OneShot timers.
Definition of the Window system.
Functions related to transparency.
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
@ Text
loading and cost/income text
std::string * ViewportAddString(const DrawPixelInfo *dpi, const ViewportSign *sign, ViewportStringFlags flags, Colours colour)
Add a string to draw in the current viewport.
Functions related to (drawing on) viewports.
@ Small
Draw using the small font.
EnumBitSet< ViewportStringFlag, uint8_t > ViewportStringFlags
Bitset of ViewportStringFlag elements.
@ TextEffect
All zoom levels above this will not show text effects.
Definition zoom_type.h:45