2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file texteff.cpp Handling of text effects. */
|
2007-04-04 05:21:14 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
2007-06-21 18:17:47 +02:00
|
|
|
#include "texteff.hpp"
|
2007-11-10 02:17:15 +01:00
|
|
|
#include "transparency.h"
|
2007-12-21 20:49:27 +01:00
|
|
|
#include "strings_func.h"
|
2008-01-09 10:57:48 +01:00
|
|
|
#include "viewport_func.h"
|
2008-01-13 15:37:30 +01:00
|
|
|
#include "settings_type.h"
|
2023-04-13 17:18:27 +02:00
|
|
|
#include "command_type.h"
|
|
|
|
#include "timer/timer.h"
|
|
|
|
#include "timer/timer_window.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-12-22 13:50:41 +01:00
|
|
|
/** Container for all information about a text effect */
|
2010-02-10 18:37:47 +01:00
|
|
|
struct TextEffect : public ViewportSign {
|
2023-06-25 06:27:33 +02:00
|
|
|
std::vector<StringParameterBackup> params; ///< Backup of string parameters
|
2009-12-26 00:22:41 +01:00
|
|
|
StringID string_id; ///< String to draw for the text effect, if INVALID_STRING_ID then it's not valid
|
2023-05-08 19:01:06 +02:00
|
|
|
uint8_t duration; ///< How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
|
2009-12-22 13:50:41 +01:00
|
|
|
TextEffectMode mode; ///< Type of text effect
|
|
|
|
|
|
|
|
/** Reset the text effect */
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
this->MarkDirty();
|
|
|
|
this->width_normal = 0;
|
|
|
|
this->string_id = INVALID_STRING_ID;
|
|
|
|
}
|
2007-03-07 13:11:48 +01:00
|
|
|
};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2019-03-03 18:30:09 +01:00
|
|
|
static std::vector<struct TextEffect> _text_effects; ///< Text effects are stored there
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2007-09-09 12:13:17 +02:00
|
|
|
/* Text Effects */
|
2023-05-08 19:01:06 +02:00
|
|
|
TextEffectID AddTextEffect(StringID msg, int center, int y, uint8_t duration, TextEffectMode mode)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-06-21 18:17:47 +02:00
|
|
|
if (_game_mode == GM_MENU) return INVALID_TE_ID;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2023-09-09 17:36:07 +02:00
|
|
|
auto it = std::find_if(std::begin(_text_effects), std::end(_text_effects), [](const TextEffect &te) { return te.string_id == INVALID_STRING_ID; });
|
2023-09-09 17:38:12 +02:00
|
|
|
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;
|
|
|
|
it = _text_effects.emplace(std::end(_text_effects));
|
|
|
|
}
|
2007-06-21 18:17:47 +02:00
|
|
|
|
2023-09-09 17:36:07 +02:00
|
|
|
TextEffect &te = *it;
|
2007-06-21 18:17:47 +02:00
|
|
|
|
|
|
|
/* Start defining this object */
|
2019-02-18 23:39:06 +01:00
|
|
|
te.string_id = msg;
|
|
|
|
te.duration = duration;
|
2023-06-25 06:27:33 +02:00
|
|
|
CopyOutDParam(te.params, 2);
|
2019-02-18 23:39:06 +01:00
|
|
|
te.mode = mode;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2009-12-22 13:50:41 +01:00
|
|
|
/* Make sure we only dirty the new area */
|
2019-02-18 23:39:06 +01:00
|
|
|
te.width_normal = 0;
|
|
|
|
te.UpdatePosition(center, y, msg);
|
2007-06-21 18:17:47 +02:00
|
|
|
|
2023-09-09 17:36:07 +02:00
|
|
|
return static_cast<TextEffectID>(it - std::begin(_text_effects));
|
2007-06-21 18:17:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateTextEffect(TextEffectID te_id, StringID msg)
|
|
|
|
{
|
|
|
|
/* Update details */
|
2023-09-09 17:36:46 +02:00
|
|
|
TextEffect &te = _text_effects[te_id];
|
|
|
|
if (msg == te.string_id && !HaveDParamChanged(te.params)) return;
|
|
|
|
te.string_id = msg;
|
|
|
|
CopyOutDParam(te.params, 2);
|
2007-06-21 18:17:47 +02:00
|
|
|
|
2023-09-09 17:36:46 +02:00
|
|
|
te.UpdatePosition(te.center, te.top, te.string_id, te.string_id - 1);
|
2021-05-02 11:43:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateAllTextEffectVirtCoords()
|
|
|
|
{
|
|
|
|
for (auto &te : _text_effects) {
|
2021-05-03 16:12:47 +02:00
|
|
|
if (te.string_id == INVALID_STRING_ID) continue;
|
2023-06-25 06:27:33 +02:00
|
|
|
CopyInDParam(te.params);
|
2021-05-02 11:43:14 +02:00
|
|
|
te.UpdatePosition(te.center, te.top, te.string_id, te.string_id - 1);
|
|
|
|
}
|
2007-06-21 18:17:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveTextEffect(TextEffectID te_id)
|
|
|
|
{
|
2009-12-26 00:15:08 +01:00
|
|
|
_text_effects[te_id].Reset();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:18:27 +02:00
|
|
|
/** Slowly move text effects upwards. */
|
|
|
|
IntervalTimer<TimerWindow> move_all_text_effects_interval = {std::chrono::milliseconds(30), [](uint count) {
|
|
|
|
if (_pause_mode && _game_mode != GM_EDITOR && _settings_game.construction.command_pause_level <= CMDPL_NO_CONSTRUCTION) return;
|
2018-05-11 18:52:06 +02:00
|
|
|
|
2019-02-17 12:20:52 +01:00
|
|
|
for (TextEffect &te : _text_effects) {
|
|
|
|
if (te.string_id == INVALID_STRING_ID) continue;
|
|
|
|
if (te.mode != TE_RISING) continue;
|
2009-12-26 00:22:41 +01:00
|
|
|
|
2019-02-17 12:20:52 +01:00
|
|
|
if (te.duration < count) {
|
|
|
|
te.Reset();
|
2009-12-26 00:22:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-04-04 21:27:11 +02:00
|
|
|
te.MarkDirty(ZOOM_LVL_TEXT_EFFECT);
|
2019-02-17 12:20:52 +01:00
|
|
|
te.duration -= count;
|
2024-04-04 19:27:34 +02:00
|
|
|
te.top -= count * ZOOM_BASE;
|
2024-04-04 21:27:11 +02:00
|
|
|
te.MarkDirty(ZOOM_LVL_TEXT_EFFECT);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2023-04-13 17:18:27 +02:00
|
|
|
}};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
void InitTextEffects()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2018-09-23 18:16:49 +02:00
|
|
|
_text_effects.clear();
|
|
|
|
_text_effects.shrink_to_fit();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTextEffects(DrawPixelInfo *dpi)
|
|
|
|
{
|
2009-12-22 13:50:41 +01:00
|
|
|
/* Don't draw the text effects when zoomed out a lot */
|
2024-04-04 21:27:11 +02:00
|
|
|
if (dpi->zoom > ZOOM_LVL_TEXT_EFFECT) return;
|
2023-08-25 16:04:40 +02:00
|
|
|
if (IsTransparencySet(TO_TEXT)) return;
|
2019-02-17 12:20:52 +01:00
|
|
|
for (TextEffect &te : _text_effects) {
|
|
|
|
if (te.string_id == INVALID_STRING_ID) continue;
|
2023-08-25 16:04:40 +02:00
|
|
|
if (te.mode == TE_RISING || _settings_client.gui.loading_indicators) {
|
2023-06-25 06:27:33 +02:00
|
|
|
CopyInDParam(te.params);
|
2024-04-04 21:27:11 +02:00
|
|
|
ViewportAddString(dpi, ZOOM_LVL_TEXT_EFFECT, &te, te.string_id, te.string_id - 1, STR_NULL);
|
2009-12-22 13:50:41 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|