mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: simplify news string drawing by using StrMakeValid to replaces newlines with spaces
This commit is contained in:
parent
9610705f46
commit
00695c29de
|
@ -1099,37 +1099,13 @@ void ShowLastNewsMessage()
|
||||||
*/
|
*/
|
||||||
static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
|
static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
|
||||||
{
|
{
|
||||||
char buffer[512], buffer2[512];
|
|
||||||
StringID str;
|
|
||||||
|
|
||||||
CopyInDParam(0, ni->params, lengthof(ni->params));
|
CopyInDParam(0, ni->params, lengthof(ni->params));
|
||||||
str = ni->string_id;
|
|
||||||
|
|
||||||
GetString(buffer, str, lastof(buffer));
|
/* Get the string, replaces newlines with spaces and remove control codes from the string. */
|
||||||
/* Copy the just gotten string to another buffer to remove any formatting
|
std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
|
||||||
* from it such as big fonts, etc. */
|
|
||||||
const char *ptr = buffer;
|
|
||||||
char *dest = buffer2;
|
|
||||||
WChar c_last = '\0';
|
|
||||||
for (;;) {
|
|
||||||
WChar c = Utf8Consume(&ptr);
|
|
||||||
if (c == 0) break;
|
|
||||||
/* Make a space from a newline, but ignore multiple newlines */
|
|
||||||
if (c == '\n' && c_last != '\n') {
|
|
||||||
dest[0] = ' ';
|
|
||||||
dest++;
|
|
||||||
} else if (c == '\r') {
|
|
||||||
dest[0] = dest[1] = dest[2] = dest[3] = ' ';
|
|
||||||
dest += 4;
|
|
||||||
} else if (IsPrintable(c)) {
|
|
||||||
dest += Utf8Encode(dest, c);
|
|
||||||
}
|
|
||||||
c_last = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
*dest = '\0';
|
|
||||||
/* Truncate and show string; postfixed by '...' if necessary */
|
/* Truncate and show string; postfixed by '...' if necessary */
|
||||||
DrawString(left, right, y, buffer2, colour);
|
DrawString(left, right, y, message, colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MessageHistoryWindow : Window {
|
struct MessageHistoryWindow : Window {
|
||||||
|
|
|
@ -40,39 +40,18 @@
|
||||||
static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
|
static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
|
||||||
{
|
{
|
||||||
CopyInDParam(0, ni->params, lengthof(ni->params));
|
CopyInDParam(0, ni->params, lengthof(ni->params));
|
||||||
StringID str = ni->string_id;
|
|
||||||
|
|
||||||
char buf[512];
|
/* Replace newlines and the likes with spaces. */
|
||||||
GetString(buf, str, lastof(buf));
|
std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
|
||||||
const char *s = buf;
|
|
||||||
|
|
||||||
char buffer[256];
|
|
||||||
char *d = buffer;
|
|
||||||
const char *last = lastof(buffer);
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
WChar c = Utf8Consume(&s);
|
|
||||||
if (c == 0) {
|
|
||||||
break;
|
|
||||||
} else if (c == '\n') {
|
|
||||||
if (d + 4 >= last) break;
|
|
||||||
d[0] = d[1] = d[2] = d[3] = ' ';
|
|
||||||
d += 4;
|
|
||||||
} else if (IsPrintable(c)) {
|
|
||||||
if (d + Utf8CharLen(c) >= last) break;
|
|
||||||
d += Utf8Encode(d, c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*d = '\0';
|
|
||||||
|
|
||||||
DrawPixelInfo tmp_dpi;
|
DrawPixelInfo tmp_dpi;
|
||||||
if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
|
if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
|
||||||
|
|
||||||
int width = GetStringBoundingBox(buffer).width;
|
int width = GetStringBoundingBox(message).width;
|
||||||
int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
|
int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
|
||||||
|
|
||||||
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
|
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
|
||||||
DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
|
DrawString(pos, INT16_MAX, 0, message, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
|
||||||
|
|
||||||
return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
|
return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue