2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file texteff.cpp Handling of text effects. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "strings_type.h"
|
2007-06-21 16:17:47 +00:00
|
|
|
#include "texteff.hpp"
|
2008-08-11 22:45:11 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-11-10 01:17:15 +00:00
|
|
|
#include "transparency.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 09:48:53 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "viewport_func.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-30 00:46:48 +00:00
|
|
|
enum {
|
2008-08-11 22:45:11 +00:00
|
|
|
INIT_NUM_TEXT_EFFECTS = 20,
|
2006-12-30 00:46:48 +00:00
|
|
|
};
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct TextEffect {
|
2004-08-09 17:04:08 +00:00
|
|
|
StringID string_id;
|
2005-01-03 08:50:44 +00:00
|
|
|
int32 x;
|
|
|
|
int32 y;
|
|
|
|
int32 right;
|
|
|
|
int32 bottom;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 duration;
|
2007-06-21 19:08:47 +00:00
|
|
|
uint64 params_1;
|
2007-06-21 16:17:47 +00:00
|
|
|
TextEffectMode mode;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-08-19 09:38:30 +00:00
|
|
|
/* used for text effects */
|
2007-06-21 16:17:47 +00:00
|
|
|
static TextEffect *_text_effect_list = NULL;
|
2008-08-11 22:45:11 +00:00
|
|
|
static uint16 _num_text_effects = INIT_NUM_TEXT_EFFECTS;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2007-09-09 10:13:17 +00: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 17:04:08 +00:00
|
|
|
static void MarkTextEffectAreaDirty(TextEffect *te)
|
|
|
|
{
|
2007-09-07 21:15:32 +00: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 17:04:08 +00: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 16:17:47 +00:00
|
|
|
TextEffectID AddTextEffect(StringID msg, int x, int y, uint16 duration, TextEffectMode mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
TextEffect *te;
|
|
|
|
int w;
|
|
|
|
char buffer[100];
|
2007-06-21 16:17:47 +00:00
|
|
|
TextEffectID i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-21 16:17:47 +00:00
|
|
|
if (_game_mode == GM_MENU) return INVALID_TE_ID;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-21 16:17:47 +00: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 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 16:17:47 +00:00
|
|
|
/* If there is none found, we grow the array */
|
|
|
|
if (i == _num_text_effects) {
|
|
|
|
_num_text_effects += 25;
|
2007-12-08 14:50:41 +00:00
|
|
|
_text_effect_list = ReallocT<TextEffect>(_text_effect_list, _num_text_effects);
|
2007-06-21 16:17:47 +00: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 17:04:08 +00:00
|
|
|
te->string_id = msg;
|
|
|
|
te->duration = duration;
|
|
|
|
te->y = y - 5;
|
|
|
|
te->bottom = y + 5;
|
2004-12-02 22:53:07 +00:00
|
|
|
te->params_1 = GetDParam(0);
|
2007-06-21 16:17:47 +00:00
|
|
|
te->mode = mode;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(buffer, msg, lastof(buffer));
|
2006-09-16 13:20:14 +00:00
|
|
|
w = GetStringBoundingBox(buffer).width;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
te->x = x - (w >> 1);
|
|
|
|
te->right = x + (w >> 1) - 1;
|
|
|
|
MarkTextEffectAreaDirty(te);
|
2007-06-21 16:17:47 +00: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 21:15:32 +00: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 16:17:47 +00: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 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MoveTextEffect(TextEffect *te)
|
|
|
|
{
|
2007-06-21 16:17:47 +00:00
|
|
|
/* Never expire for duration of 0xFFFF */
|
|
|
|
if (te->duration == 0xFFFF) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (te->duration < 8) {
|
2005-09-28 21:49:55 +00:00
|
|
|
te->string_id = INVALID_STRING_ID;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-10-23 13:04:44 +00:00
|
|
|
te->duration -= 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
te->y--;
|
|
|
|
te->bottom--;
|
|
|
|
}
|
|
|
|
MarkTextEffectAreaDirty(te);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void MoveAllTextEffects()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-21 16:17:47 +00: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 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitTextEffects()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-21 16:17:47 +00:00
|
|
|
if (_text_effect_list == NULL) _text_effect_list = MallocT<TextEffect>(_num_text_effects);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-21 16:17:47 +00:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTextEffects(DrawPixelInfo *dpi)
|
|
|
|
{
|
2006-08-31 07:13:36 +00:00
|
|
|
switch (dpi->zoom) {
|
2007-05-15 14:08:39 +00:00
|
|
|
case ZOOM_LVL_NORMAL:
|
2007-06-21 16:17:47 +00:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) {
|
|
|
|
TextEffect *te = &_text_effect_list[i];
|
2006-08-31 07:13:36 +00: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 15:13:28 +00:00
|
|
|
if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
|
2009-06-29 09:32:58 +00:00
|
|
|
AddStringToDraw(te->x, te->y, te->string_id, te->params_1, INVALID_STRING_ID);
|
2007-06-21 16:17:47 +00:00
|
|
|
}
|
2006-08-31 07:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-05-15 14:08:39 +00:00
|
|
|
case ZOOM_LVL_OUT_2X:
|
2007-06-21 16:17:47 +00:00
|
|
|
for (TextEffectID i = 0; i < _num_text_effects; i++) {
|
|
|
|
TextEffect *te = &_text_effect_list[i];
|
2006-08-31 07:13:36 +00: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 15:13:28 +00:00
|
|
|
if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
|
2009-06-29 09:32:58 +00:00
|
|
|
AddStringToDraw(te->x, te->y, (StringID)(te->string_id - 1), te->params_1, INVALID_STRING_ID);
|
2007-06-21 16:17:47 +00:00
|
|
|
}
|
2006-08-31 07:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-05-15 14:08:39 +00:00
|
|
|
|
2007-05-15 16:08:46 +00:00
|
|
|
case ZOOM_LVL_OUT_4X:
|
2007-05-19 22:48:04 +00:00
|
|
|
case ZOOM_LVL_OUT_8X:
|
2007-05-15 14:08:39 +00:00
|
|
|
break;
|
2007-05-15 16:08:46 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|