4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 <http://www.gnu.org/licenses/>.
10 /** @file texteff.cpp Handling of text effects. */
13 #include "texteff.hpp"
14 #include "transparency.h"
15 #include "strings_func.h"
16 #include "core/smallvec_type.hpp"
17 #include "viewport_func.h"
18 #include "settings_type.h"
20 #include "safeguards.h"
22 /** Container for all information about a text effect */
23 struct TextEffect
: public ViewportSign
{
24 uint64 params_1
; ///< DParam parameter
25 uint64 params_2
; ///< second DParam parameter
26 StringID string_id
; ///< String to draw for the text effect, if INVALID_STRING_ID then it's not valid
27 uint8 duration
; ///< How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
28 TextEffectMode mode
; ///< Type of text effect
30 /** Reset the text effect */
34 this->width_normal
= 0;
35 this->string_id
= INVALID_STRING_ID
;
39 static SmallVector
<struct TextEffect
, 32> _text_effects
; ///< Text effects are stored there
42 TextEffectID
AddTextEffect(StringID msg
, int center
, int y
, uint8 duration
, TextEffectMode mode
)
44 if (_game_mode
== GM_MENU
) return INVALID_TE_ID
;
47 for (i
= 0; i
< _text_effects
.Length(); i
++) {
48 if (_text_effects
[i
].string_id
== INVALID_STRING_ID
) break;
50 if (i
== _text_effects
.Length()) _text_effects
.Append();
52 TextEffect
*te
= _text_effects
.Get(i
);
54 /* Start defining this object */
56 te
->duration
= duration
;
57 te
->params_1
= GetDParam(0);
58 te
->params_2
= GetDParam(1);
61 /* Make sure we only dirty the new area */
63 te
->UpdatePosition(center
, y
, msg
);
68 void UpdateTextEffect(TextEffectID te_id
, StringID msg
)
71 TextEffect
*te
= _text_effects
.Get(te_id
);
72 if (msg
== te
->string_id
&& GetDParam(0) == te
->params_1
) return;
74 te
->params_1
= GetDParam(0);
75 te
->params_2
= GetDParam(1);
77 te
->UpdatePosition(te
->center
, te
->top
, msg
);
80 void RemoveTextEffect(TextEffectID te_id
)
82 _text_effects
[te_id
].Reset();
85 void MoveAllTextEffects()
87 const TextEffect
*end
= _text_effects
.End();
88 for (TextEffect
*te
= _text_effects
.Begin(); te
!= end
; te
++) {
89 if (te
->string_id
== INVALID_STRING_ID
) continue;
90 if (te
->mode
!= TE_RISING
) continue;
92 if (te
->duration
-- == 0) {
97 te
->MarkDirty(ZOOM_LVL_OUT_8X
);
98 te
->top
-= ZOOM_LVL_BASE
;
99 te
->MarkDirty(ZOOM_LVL_OUT_8X
);
103 void InitTextEffects()
105 _text_effects
.Reset();
108 void DrawTextEffects(DrawPixelInfo
*dpi
)
110 /* Don't draw the text effects when zoomed out a lot */
111 if (dpi
->zoom
> ZOOM_LVL_OUT_8X
) return;
113 const TextEffect
*end
= _text_effects
.End();
114 for (TextEffect
*te
= _text_effects
.Begin(); te
!= end
; te
++) {
115 if (te
->string_id
== INVALID_STRING_ID
) continue;
116 if (te
->mode
== TE_RISING
|| (_settings_client
.gui
.loading_indicators
&& !IsTransparencySet(TO_LOADING
))) {
117 ViewportAddString(dpi
, ZOOM_LVL_OUT_8X
, te
, te
->string_id
, te
->string_id
- 1, STR_NULL
, te
->params_1
, te
->params_2
);