tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file texteff.cpp Handling of text effects. */ belugas@6422: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" maedhros@6453: #include "landscape.h" rubidium@8225: #include "gfx_func.h" rubidium@9336: #include "console_func.h" tron@2153: #include "variables.h" truelight@6937: #include "blitter/factory.hpp" truelight@6998: #include "texteff.hpp" peter1138@7170: #include "video/video_driver.hpp" belugas@7849: #include "transparency.h" rubidium@8114: #include "strings_func.h" rubidium@8130: #include "core/alloc_func.hpp" rubidium@8140: #include "date_func.h" rubidium@8131: #include "functions.h" rubidium@8225: #include "viewport_func.h" rubidium@8270: #include "settings_type.h" truelight@0: rubidium@8264: #include "table/sprites.h" rubidium@8264: rubidium@8264: #include /* va_list */ rubidium@8264: rubidium@5428: enum { Darkvater@6215: MAX_TEXTMESSAGE_LENGTH = 200, truelight@6998: INIT_NUM_TEXT_MESSAGES = 20, rubidium@5428: MAX_CHAT_MESSAGES = 10, rubidium@5428: }; rubidium@5428: rubidium@6248: struct TextEffect { truelight@0: StringID string_id; tron@849: int32 x; tron@849: int32 y; tron@849: int32 right; tron@849: int32 bottom; truelight@0: uint16 duration; rubidium@7006: uint64 params_1; rubidium@7006: uint64 params_2; truelight@6998: TextEffectMode mode; rubidium@6248: }; truelight@0: truelight@543: rubidium@7454: struct ChatMessage { truelight@543: char message[MAX_TEXTMESSAGE_LENGTH]; truelight@543: uint16 color; truelight@4363: Date end_date; rubidium@6248: }; truelight@543: rubidium@7454: /* used for text effects */ truelight@6998: static TextEffect *_text_effect_list = NULL; rubidium@7454: static uint16 _num_text_effects = INIT_NUM_TEXT_MESSAGES; truelight@0: rubidium@7454: /* used for chat window */ rubidium@7454: static ChatMessage _chatmsg_list[MAX_CHAT_MESSAGES]; rubidium@7454: static bool _chatmessage_dirty = false; rubidium@7454: static bool _chatmessage_visible = false; truelight@543: Darkvater@4956: /* The chatbox grows from the bottom so the coordinates are pixels from Darkvater@4956: * the left and pixels from the bottom. The height is the maximum height */ rubidium@8121: static const PointDimension _chatmsg_box = {10, 30, 500, 150}; rubidium@7454: static uint8 _chatmessage_backup[150 * 500 * 6]; // (height * width) truelight@543: rubidium@7454: static inline uint GetChatMessageCount() Darkvater@4957: { Darkvater@4959: uint i; Darkvater@4957: Darkvater@4959: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { rubidium@7454: if (_chatmsg_list[i].message[0] == '\0') break; Darkvater@4957: } Darkvater@4957: Darkvater@4957: return i; Darkvater@4957: } Darkvater@4957: Darkvater@4957: /* Add a text message to the 'chat window' to be shown Darkvater@4957: * @param color The colour this message is to be shown in Darkvater@4957: * @param duration The duration of the chat message in game-days Darkvater@4957: * @param message message itself in printf() style */ rubidium@7454: void CDECL AddChatMessage(uint16 color, uint8 duration, const char *message, ...) truelight@543: { tron@2455: char buf[MAX_TEXTMESSAGE_LENGTH]; Darkvater@4957: const char *bufp; truelight@543: va_list va; Darkvater@4957: uint msg_count; Darkvater@4957: uint16 lines; truelight@543: truelight@543: va_start(va, message); tron@2373: vsnprintf(buf, lengthof(buf), message, va); truelight@543: va_end(va); truelight@543: Darkvater@6215: Darkvater@6215: Utf8TrimString(buf, MAX_TEXTMESSAGE_LENGTH); Darkvater@6215: Darkvater@4957: /* Force linebreaks for strings that are too long */ rubidium@7454: lines = GB(FormatStringLinebreaks(buf, _chatmsg_box.width - 8), 0, 16) + 1; Darkvater@4957: if (lines >= MAX_CHAT_MESSAGES) return; truelight@543: rubidium@7454: msg_count = GetChatMessageCount(); Darkvater@4957: /* We want to add more chat messages than there is free space for, remove 'old' */ Darkvater@4957: if (lines > MAX_CHAT_MESSAGES - msg_count) { Darkvater@4957: int i = lines - (MAX_CHAT_MESSAGES - msg_count); rubidium@7454: memmove(&_chatmsg_list[0], &_chatmsg_list[i], sizeof(_chatmsg_list[0]) * (msg_count - i)); Darkvater@4957: msg_count = MAX_CHAT_MESSAGES - lines; truelight@543: } truelight@543: Darkvater@4957: for (bufp = buf; lines != 0; lines--) { rubidium@7454: ChatMessage *cmsg = &_chatmsg_list[msg_count++]; rubidium@7454: ttd_strlcpy(cmsg->message, bufp, sizeof(cmsg->message)); Darkvater@4957: Darkvater@4957: /* The default colour for a message is player colour. Replace this with Darkvater@4957: * white for any additional lines */ rubidium@7454: cmsg->color = (bufp == buf && color & IS_PALETTE_COLOR) ? color : (0x1D - 15) | IS_PALETTE_COLOR; rubidium@7454: cmsg->end_date = _date + duration; Darkvater@4957: Darkvater@4957: bufp += strlen(bufp) + 1; // jump to 'next line' in the formatted string Darkvater@4957: } truelight@543: rubidium@7454: _chatmessage_dirty = true; truelight@543: } truelight@543: rubidium@7454: void InitChatMessage() truelight@543: { tron@2639: uint i; tron@2639: tron@2639: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { rubidium@7454: _chatmsg_list[i].message[0] = '\0'; tron@2639: } truelight@543: } truelight@543: rubidium@7454: /** Hide the chatbox */ rubidium@7454: void UndrawChatMessage() truelight@543: { rubidium@7454: if (_chatmessage_visible) { truelight@6937: Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); rubidium@5428: /* Sometimes we also need to hide the cursor rubidium@5428: * This is because both textmessage and the cursor take a shot of the rubidium@5428: * screen before drawing. rubidium@5428: * Now the textmessage takes his shot and paints his data before the cursor rubidium@5428: * does, so in the shot of the cursor is the screen-data of the textmessage rubidium@5428: * included when the cursor hangs somewhere over the textmessage. To rubidium@5428: * avoid wrong repaints, we undraw the cursor in that case, and everything rubidium@5428: * looks nicely ;) rubidium@5428: * (and now hope this story above makes sense to you ;)) rubidium@5428: */ truelight@543: truelight@543: if (_cursor.visible) { rubidium@7454: if (_cursor.draw_pos.x + _cursor.draw_size.x >= _chatmsg_box.x && rubidium@7454: _cursor.draw_pos.x <= _chatmsg_box.x + _chatmsg_box.width && rubidium@7454: _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _chatmsg_box.y - _chatmsg_box.height && rubidium@7454: _cursor.draw_pos.y <= _screen.height - _chatmsg_box.y) { truelight@543: UndrawMouseCursor(); truelight@543: } truelight@543: } truelight@543: rubidium@7454: int x = _chatmsg_box.x; rubidium@7454: int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height; rubidium@7454: int width = _chatmsg_box.width; rubidium@7454: int height = _chatmsg_box.height; rubidium@6366: if (y < 0) { rubidium@7454: height = max(height + y, min(_chatmsg_box.height, _screen.height)); rubidium@6366: y = 0; rubidium@6366: } rubidium@6366: if (x + width >= _screen.width) { rubidium@6366: width = _screen.width - x; rubidium@6366: } rubidium@6374: if (width <= 0 || height <= 0) return; rubidium@6366: rubidium@7454: _chatmessage_visible = false; rubidium@5428: /* Put our 'shot' back to the screen */ rubidium@7454: blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); rubidium@5428: /* And make sure it is updated next time */ peter1138@7170: _video_driver->MakeDirty(x, y, width, height); truelight@543: rubidium@7454: _chatmessage_dirty = true; truelight@543: } truelight@543: } truelight@543: belugas@6422: /** Check if a message is expired every day */ rubidium@7454: void ChatMessageDailyLoop() truelight@543: { tron@2639: uint i; tron@2639: truelight@1595: for (i = 0; i < MAX_CHAT_MESSAGES; i++) { rubidium@7454: ChatMessage *cmsg = &_chatmsg_list[i]; rubidium@7454: if (cmsg->message[0] == '\0') continue; truelight@1595: Darkvater@4957: /* Message has expired, remove from the list */ rubidium@7454: if (cmsg->end_date < _date) { truelight@1595: /* Move the remaining messages over the current message */ rubidium@7454: if (i != MAX_CHAT_MESSAGES - 1) memmove(cmsg, cmsg + 1, sizeof(*cmsg) * (MAX_CHAT_MESSAGES - i - 1)); truelight@1595: truelight@1595: /* Mark the last item as empty */ rubidium@7454: _chatmsg_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0'; rubidium@7454: _chatmessage_dirty = true; truelight@1595: truelight@1595: /* Go one item back, because we moved the array 1 to the left */ truelight@543: i--; truelight@543: } truelight@543: } truelight@543: } truelight@543: rubidium@7454: /** Draw the chat message-box */ rubidium@7454: void DrawChatMessage() truelight@543: { truelight@6937: Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); rubidium@7454: if (!_chatmessage_dirty) return; truelight@543: rubidium@5428: /* First undraw if needed */ rubidium@7454: UndrawChatMessage(); truelight@543: Darkvater@4959: if (_iconsole_mode == ICONSOLE_FULL) return; truelight@543: truelight@1595: /* Check if we have anything to draw at all */ rubidium@7454: uint count = GetChatMessageCount(); Darkvater@4960: if (count == 0) return; truelight@543: rubidium@7454: int x = _chatmsg_box.x; rubidium@7454: int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height; rubidium@7454: int width = _chatmsg_box.width; rubidium@7454: int height = _chatmsg_box.height; rubidium@6366: if (y < 0) { rubidium@7454: height = max(height + y, min(_chatmsg_box.height, _screen.height)); rubidium@6366: y = 0; rubidium@6366: } rubidium@6366: if (x + width >= _screen.width) { rubidium@6366: width = _screen.width - x; rubidium@6366: } rubidium@6374: if (width <= 0 || height <= 0) return; rubidium@6374: rubidium@7454: assert(blitter->BufferSize(width, height) < (int)sizeof(_chatmessage_backup)); truelight@6985: rubidium@5428: /* Make a copy of the screen as it is before painting (for undraw) */ rubidium@7454: blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); truelight@543: Darkvater@4960: _cur_dpi = &_screen; // switch to _screen painting truelight@543: rubidium@7454: /* Paint a half-transparent box behind the chat messages */ Darkvater@4960: GfxFillRect( rubidium@7454: _chatmsg_box.x, rubidium@7454: _screen.height - _chatmsg_box.y - count * 13 - 2, rubidium@7454: _chatmsg_box.x + _chatmsg_box.width - 1, rubidium@7454: _screen.height - _chatmsg_box.y - 2, peter1138@5668: PALETTE_TO_TRANSPARENT | (1 << USE_COLORTABLE) // black, but with some alpha for background Darkvater@4960: ); truelight@1595: rubidium@7454: /* Paint the chat messages starting with the lowest at the bottom */ rubidium@6366: for (uint y = 13; count-- != 0; y += 13) { rubidium@7454: DoDrawString(_chatmsg_list[count].message, _chatmsg_box.x + 3, _screen.height - _chatmsg_box.y - y + 1, _chatmsg_list[count].color); rubidium@6492: } truelight@543: rubidium@5428: /* Make sure the data is updated next flush */ peter1138@7170: _video_driver->MakeDirty(x, y, width, height); truelight@543: rubidium@7454: _chatmessage_visible = true; rubidium@7454: _chatmessage_dirty = false; truelight@543: } truelight@543: rubidium@7545: /* Text Effects */ rubidium@7545: /** rubidium@7545: * Mark the area of the text effect as dirty. rubidium@7545: * rubidium@7545: * This function marks the area of a text effect as dirty for repaint. rubidium@7545: * rubidium@7545: * @param te The TextEffect to mark the area dirty rubidium@7545: * @ingroup dirty rubidium@7545: */ truelight@0: static void MarkTextEffectAreaDirty(TextEffect *te) truelight@0: { rubidium@7535: /* Width and height of the text effect are doubled, so they are correct in both zoom out levels 1x and 2x. */ truelight@0: MarkAllViewportsDirty( truelight@0: te->x, truelight@0: te->y - 1, truelight@0: (te->right - te->x)*2 + te->x + 1, truelight@0: (te->bottom - (te->y - 1)) * 2 + (te->y - 1) + 1 truelight@0: ); truelight@0: } truelight@0: truelight@6998: TextEffectID AddTextEffect(StringID msg, int x, int y, uint16 duration, TextEffectMode mode) truelight@0: { truelight@0: TextEffect *te; truelight@0: int w; truelight@0: char buffer[100]; truelight@6998: TextEffectID i; truelight@0: truelight@6998: if (_game_mode == GM_MENU) return INVALID_TE_ID; truelight@193: truelight@6998: /* Look for a free spot in the text effect array */ truelight@6998: for (i = 0; i < _num_text_effects; i++) { truelight@6998: if (_text_effect_list[i].string_id == INVALID_STRING_ID) break; truelight@0: } truelight@0: truelight@6998: /* If there is none found, we grow the array */ truelight@6998: if (i == _num_text_effects) { truelight@6998: _num_text_effects += 25; rubidium@8037: _text_effect_list = ReallocT(_text_effect_list, _num_text_effects); truelight@6998: for (; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID; truelight@6998: i = _num_text_effects - 1; truelight@6998: } truelight@6998: truelight@6998: te = &_text_effect_list[i]; truelight@6998: truelight@6998: /* Start defining this object */ truelight@0: te->string_id = msg; truelight@0: te->duration = duration; truelight@0: te->y = y - 5; truelight@0: te->bottom = y + 5; tron@534: te->params_1 = GetDParam(0); tron@534: te->params_2 = GetDParam(4); truelight@6998: te->mode = mode; truelight@0: Darkvater@4912: GetString(buffer, msg, lastof(buffer)); Darkvater@4609: w = GetStringBoundingBox(buffer).width; truelight@0: truelight@0: te->x = x - (w >> 1); truelight@0: te->right = x + (w >> 1) - 1; truelight@0: MarkTextEffectAreaDirty(te); truelight@6998: truelight@6998: return i; truelight@6998: } truelight@6998: truelight@6998: void UpdateTextEffect(TextEffectID te_id, StringID msg) truelight@6998: { truelight@6998: assert(te_id < _num_text_effects); truelight@6998: TextEffect *te; truelight@6998: truelight@6998: /* Update details */ truelight@6998: te = &_text_effect_list[te_id]; truelight@6998: te->string_id = msg; truelight@6998: te->params_1 = GetDParam(0); truelight@6998: te->params_2 = GetDParam(4); truelight@6998: rubidium@7535: /* Update width of text effect */ rubidium@7535: char buffer[100]; rubidium@7535: GetString(buffer, msg, lastof(buffer)); rubidium@7535: int w = GetStringBoundingBox(buffer).width; rubidium@7535: rubidium@7535: /* Only allow to make it broader, so it completely covers the old text. That avoids remnants of the old text. */ rubidium@7535: int right_new = te->x + w; rubidium@7535: if (te->right < right_new) te->right = right_new; rubidium@7535: truelight@6998: MarkTextEffectAreaDirty(te); truelight@6998: } truelight@6998: truelight@6998: void RemoveTextEffect(TextEffectID te_id) truelight@6998: { truelight@6998: assert(te_id < _num_text_effects); truelight@6998: TextEffect *te; truelight@6998: truelight@6998: te = &_text_effect_list[te_id]; truelight@6998: MarkTextEffectAreaDirty(te); truelight@6998: te->string_id = INVALID_STRING_ID; truelight@0: } truelight@0: truelight@0: static void MoveTextEffect(TextEffect *te) truelight@0: { truelight@6998: /* Never expire for duration of 0xFFFF */ truelight@6998: if (te->duration == 0xFFFF) return; truelight@0: if (te->duration < 8) { tron@2470: te->string_id = INVALID_STRING_ID; truelight@0: } else { tron@2549: te->duration -= 8; truelight@0: te->y--; truelight@0: te->bottom--; truelight@0: } truelight@0: MarkTextEffectAreaDirty(te); truelight@0: } truelight@0: rubidium@6247: void MoveAllTextEffects() truelight@0: { truelight@6998: for (TextEffectID i = 0; i < _num_text_effects; i++) { truelight@6998: TextEffect *te = &_text_effect_list[i]; truelight@6998: if (te->string_id != INVALID_STRING_ID && te->mode == TE_RISING) MoveTextEffect(te); truelight@0: } truelight@0: } truelight@0: rubidium@6247: void InitTextEffects() truelight@0: { truelight@6998: if (_text_effect_list == NULL) _text_effect_list = MallocT(_num_text_effects); truelight@0: truelight@6998: for (TextEffectID i = 0; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID; truelight@0: } truelight@0: truelight@0: void DrawTextEffects(DrawPixelInfo *dpi) truelight@0: { tron@4469: switch (dpi->zoom) { truelight@6624: case ZOOM_LVL_NORMAL: truelight@6998: for (TextEffectID i = 0; i < _num_text_effects; i++) { truelight@6998: TextEffect *te = &_text_effect_list[i]; tron@4469: if (te->string_id != INVALID_STRING_ID && tron@4469: dpi->left <= te->right && tron@4469: dpi->top <= te->bottom && tron@4469: dpi->left + dpi->width > te->x && tron@4469: dpi->top + dpi->height > te->y) { rubidium@9354: if (te->mode == TE_RISING || (_settings.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) { truelight@6998: AddStringToDraw(te->x, te->y, te->string_id, te->params_1, te->params_2); truelight@6998: } tron@4469: } tron@4469: } tron@4469: break; truelight@0: truelight@6624: case ZOOM_LVL_OUT_2X: truelight@6998: for (TextEffectID i = 0; i < _num_text_effects; i++) { truelight@6998: TextEffect *te = &_text_effect_list[i]; tron@4469: if (te->string_id != INVALID_STRING_ID && tron@4469: dpi->left <= te->right * 2 - te->x && tron@4469: dpi->top <= te->bottom * 2 - te->y && tron@4469: dpi->left + dpi->width > te->x && tron@4469: dpi->top + dpi->height > te->y) { rubidium@9354: if (te->mode == TE_RISING || (_settings.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) { truelight@6998: AddStringToDraw(te->x, te->y, (StringID)(te->string_id - 1), te->params_1, te->params_2); truelight@6998: } tron@4469: } tron@4469: } tron@4469: break; truelight@6624: truelight@6626: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6624: break; truelight@6626: truelight@6626: default: NOT_REACHED(); truelight@0: } truelight@0: }