2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2005-06-02 21:30:21 +02:00
|
|
|
#include "openttd.h"
|
2009-01-31 21:16:06 +01:00
|
|
|
#include "strings_type.h"
|
2007-06-21 18:17:47 +02:00
|
|
|
#include "texteff.hpp"
|
2008-08-12 00:45:11 +02:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-11-10 02:17:15 +01:00
|
|
|
#include "transparency.h"
|
2007-12-21 20:49:27 +01:00
|
|
|
#include "strings_func.h"
|
2007-12-25 10:48:53 +01:00
|
|
|
#include "core/alloc_func.hpp"
|
2008-01-09 10:57:48 +01:00
|
|
|
#include "viewport_func.h"
|
2008-01-13 15:37:30 +01:00
|
|
|
#include "settings_type.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-12-30 01:46:48 +01:00
|
|
|
enum {
|
2008-08-12 00:45:11 +02:00
|
|
|
INIT_NUM_TEXT_EFFECTS = 20,
|
2006-12-30 01:46:48 +01:00
|
|
|
};
|
|
|
|
|
2007-03-07 13:11:48 +01:00
|
|
|
struct TextEffect {
|
2004-08-09 19:04:08 +02:00
|
|
|
StringID string_id;
|
2005-01-03 09:50:44 +01:00
|
|
|
int32 x;
|
|
|
|
int32 y;
|
|
|
|
int32 right;
|
|
|
|
int32 bottom;
|
2004-08-09 19:04:08 +02:00
|
|
|
uint16 duration;
|
2007-06-21 21:08:47 +02:00
|
|
|
uint64 params_1;
|
2007-06-21 18:17:47 +02:00
|
|
|
TextEffectMode mode;
|
2007-03-07 13:11:48 +01:00
|
|
|
};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-08-19 11:38:30 +02:00
|
|
|
/* used for text effects */
|
2007-06-21 18:17:47 +02:00
|
|
|
static TextEffect *_text_effect_list = NULL;
|
2008-08-12 00:45:11 +02:00
|
|
|
static uint16 _num_text_effects = INIT_NUM_TEXT_EFFECTS;
|
2004-12-04 18:54:56 +01:00
|
|
|
|
2007-09-09 12:13:17 +02:00
|
|
|
/* Text Effects */
|
|
|
|
/**
|
|
|
|
* Mark the area of the text effect as dirty.
|
|
|
|
*
|
|
|
|
* This function marks the area of a text effect as dirty for repaint.
|
|
|
|
*
|
|
|
|
* @param te The TextEffect to mark the area dirty
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
2004-08-09 19:04:08 +02:00
|
|
|
static void MarkTextEffectAreaDirty(TextEffect *te)
|
|
|
|
{
|
2007-09-07 23:15:32 +02:00
|
|
|
/* Width and height of the text effect are doubled, so they are correct in both zoom out levels 1x and 2x. */
|
2004-08-09 19:04:08 +02:00
|
|
|
MarkAllViewportsDirty(
|
|
|
|
te->x,
|
|
|
|
te->y - 1,
|
|
|
|
(te->right - te->x)*2 + te->x + 1,
|
|
|
|
(te->bottom - (te->y - 1)) * 2 + (te->y - 1) + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-06-21 18:17:47 +02:00
|
|
|
TextEffectID AddTextEffect(StringID msg, int x, int y, uint16 duration, TextEffectMode mode)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
TextEffect *te;
|
|
|
|
int w;
|
|
|
|
char buffer[100];
|
2007-06-21 18:17:47 +02:00
|
|
|
TextEffectID i;
|
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
|
|
|
|
2007-06-21 18:17:47 +02:00
|
|
|
/* Look for a free spot in the text effect array */
|
|
|
|
for (i = 0; i < _num_text_effects; i++) {
|
|
|
|
if (_text_effect_list[i].string_id == INVALID_STRING_ID) break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-06-21 18:17:47 +02:00
|
|
|
/* If there is none found, we grow the array */
|
|
|
|
if (i == _num_text_effects) {
|
|
|
|
_num_text_effects += 25;
|
2007-12-08 15:50:41 +01:00
|
|
|
_text_effect_list = ReallocT<TextEffect>(_text_effect_list, _num_text_effects);
|
2007-06-21 18:17:47 +02:00
|
|
|
for (; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
|
|
|
|
i = _num_text_effects - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
te = &_text_effect_list[i];
|
|
|
|
|
|
|
|
/* Start defining this object */
|
2004-08-09 19:04:08 +02:00
|
|
|
te->string_id = msg;
|
|
|
|
te->duration = duration;
|
|
|
|
te->y = y - 5;
|
|
|
|
te->bottom = y + 5;
|
2004-12-02 23:53:07 +01:00
|
|
|
te->params_1 = GetDParam(0);
|
2007-06-21 18:17:47 +02:00
|
|
|
te->mode = mode;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-10-22 01:31:34 +02:00
|
|
|
GetString(buffer, msg, lastof(buffer));
|
2006-09-16 15:20:14 +02:00
|
|
|
w = GetStringBoundingBox(buffer).width;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
te->x = x - (w >> 1);
|
|
|
|
te->right = x + (w >> 1) - 1;
|
|
|
|
MarkTextEffectAreaDirty(te);
|
2007-06-21 18:17:47 +02:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateTextEffect(TextEffectID te_id, StringID msg)
|
|
|
|
{
|
|
|
|
assert(te_id < _num_text_effects);
|
|
|
|
TextEffect *te;
|
|
|
|
|
|
|
|
/* Update details */
|
|
|
|
te = &_text_effect_list[te_id];
|
|
|
|
te->string_id = msg;
|
|
|
|
te->params_1 = GetDParam(0);
|
|
|
|
|
2007-09-07 23:15:32 +02:00
|
|
|
/* Update width of text effect */
|
|
|
|
char buffer[100];
|
|
|
|
GetString(buffer, msg, lastof(buffer));
|
|
|
|
int w = GetStringBoundingBox(buffer).width;
|
|
|
|
|
|
|
|
/* Only allow to make it broader, so it completely covers the old text. That avoids remnants of the old text. */
|
|
|
|
int right_new = te->x + w;
|
|
|
|
if (te->right < right_new) te->right = right_new;
|
|
|
|
|
2007-06-21 18:17:47 +02:00
|
|
|
MarkTextEffectAreaDirty(te);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveTextEffect(TextEffectID te_id)
|
|
|
|
{
|
|
|
|
assert(te_id < _num_text_effects);
|
|
|
|
TextEffect *te;
|
|
|
|
|
|
|
|
te = &_text_effect_list[te_id];
|
|
|
|
MarkTextEffectAreaDirty(te);
|
|
|
|
te->string_id = INVALID_STRING_ID;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MoveTextEffect(TextEffect *te)
|
|
|
|
{
|
2007-06-21 18:17:47 +02:00
|
|
|
/* Never expire for duration of 0xFFFF */
|
|
|
|
if (te->duration == 0xFFFF) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
if (te->duration < 8) {
|
2005-09-28 23:49:55 +02:00
|
|
|
te->string_id = INVALID_STRING_ID;
|
2004-08-09 19:04:08 +02:00
|
|
|
} else {
|
2005-10-23 15:04:44 +02:00
|
|
|
te->duration -= 8;
|
2004-08-09 19:04:08 +02:00
|
|
|
te->y--;
|
|
|
|
te->bottom--;
|
|
|
|
}
|
|
|
|
MarkTextEffectAreaDirty(te);
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
void MoveAllTextEffects()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-06-21 18:17:47 +02:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) {
|
|
|
|
TextEffect *te = &_text_effect_list[i];
|
|
|
|
if (te->string_id != INVALID_STRING_ID && te->mode == TE_RISING) MoveTextEffect(te);
|
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
|
|
|
{
|
2007-06-21 18:17:47 +02:00
|
|
|
if (_text_effect_list == NULL) _text_effect_list = MallocT<TextEffect>(_num_text_effects);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-06-21 18:17:47 +02:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTextEffects(DrawPixelInfo *dpi)
|
|
|
|
{
|
2006-08-31 09:13:36 +02:00
|
|
|
switch (dpi->zoom) {
|
2007-05-15 16:08:39 +02:00
|
|
|
case ZOOM_LVL_NORMAL:
|
2007-06-21 18:17:47 +02:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) {
|
|
|
|
TextEffect *te = &_text_effect_list[i];
|
2006-08-31 09:13:36 +02:00
|
|
|
if (te->string_id != INVALID_STRING_ID &&
|
|
|
|
dpi->left <= te->right &&
|
|
|
|
dpi->top <= te->bottom &&
|
|
|
|
dpi->left + dpi->width > te->x &&
|
|
|
|
dpi->top + dpi->height > te->y) {
|
2008-05-29 17:13:28 +02:00
|
|
|
if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
|
2009-06-29 11:32:58 +02:00
|
|
|
AddStringToDraw(te->x, te->y, te->string_id, te->params_1, INVALID_STRING_ID);
|
2007-06-21 18:17:47 +02:00
|
|
|
}
|
2006-08-31 09:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-05-15 16:08:39 +02:00
|
|
|
case ZOOM_LVL_OUT_2X:
|
2007-06-21 18:17:47 +02:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) {
|
|
|
|
TextEffect *te = &_text_effect_list[i];
|
2006-08-31 09:13:36 +02:00
|
|
|
if (te->string_id != INVALID_STRING_ID &&
|
|
|
|
dpi->left <= te->right * 2 - te->x &&
|
|
|
|
dpi->top <= te->bottom * 2 - te->y &&
|
|
|
|
dpi->left + dpi->width > te->x &&
|
|
|
|
dpi->top + dpi->height > te->y) {
|
2008-05-29 17:13:28 +02:00
|
|
|
if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
|
2009-06-29 11:32:58 +02:00
|
|
|
AddStringToDraw(te->x, te->y, (StringID)(te->string_id - 1), te->params_1, INVALID_STRING_ID);
|
2007-06-21 18:17:47 +02:00
|
|
|
}
|
2006-08-31 09:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-05-15 16:08:39 +02:00
|
|
|
|
2007-05-15 18:08:46 +02:00
|
|
|
case ZOOM_LVL_OUT_4X:
|
2007-05-20 00:48:04 +02:00
|
|
|
case ZOOM_LVL_OUT_8X:
|
2007-05-15 16:08:39 +02:00
|
|
|
break;
|
2007-05-15 18:08:46 +02:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|