Changeset - r27411:18fb446ea1a8
[Not reviewed]
master
0 2 0
Rubidium - 13 months ago 2023-05-19 12:22:50
rubidium@openttd.org
Codechange: simplify news string drawing by using StrMakeValid to replaces newlines with spaces
2 files changed with 7 insertions and 52 deletions:
0 comments (0 inline, 0 general)
src/news_gui.cpp
Show inline comments
 
@@ -1099,37 +1099,13 @@ void ShowLastNewsMessage()
 
 */
 
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));
 
	str = ni->string_id;
 

	
 
	GetString(buffer, str, lastof(buffer));
 
	/* Copy the just gotten string to another buffer to remove any formatting
 
	 * 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;
 
	}
 
	/* Get the string, replaces newlines with spaces and remove control codes from the string. */
 
	std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
 

	
 
	*dest = '\0';
 
	/* Truncate and show string; postfixed by '...' if necessary */
 
	DrawString(left, right, y, buffer2, colour);
 
	DrawString(left, right, y, message, colour);
 
}
 

	
 
struct MessageHistoryWindow : Window {
src/statusbar_gui.cpp
Show inline comments
 
@@ -40,39 +40,18 @@
 
static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
 
{
 
	CopyInDParam(0, ni->params, lengthof(ni->params));
 
	StringID str = ni->string_id;
 

	
 
	char buf[512];
 
	GetString(buf, str, lastof(buf));
 
	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';
 
	/* Replace newlines and the likes with spaces. */
 
	std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
 

	
 
	DrawPixelInfo tmp_dpi;
 
	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);
 

	
 
	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);
 
}
0 comments (0 inline, 0 general)