Changeset - r15586:ce27c3bf8845
[Not reviewed]
master
0 2 0
yexo - 14 years ago 2010-07-31 11:47:08
yexo@openttd.org
(svn r20256) -Codechange: add a DrawStringMultiline variant that accepts const char* instead of StringID
2 files changed with 47 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/gfx.cpp
Show inline comments
 
@@ -819,31 +819,29 @@ Dimension GetStringMultiLineBoundingBox(
 
 *
 
 * @param left   The left most position to draw on.
 
 * @param right  The right most position to draw on.
 
 * @param top    The top most position to draw on.
 
 * @param bottom The bottom most position to draw on.
 
 * @param str    String to draw.
 
 * @param last   The end of the string buffer to draw.
 
 * @param colour Colour used for drawing the string, see DoDrawString() for details
 
 * @param align  The horizontal and vertical alignment of the string.
 
 * @param underline Whether to underline all strings
 
 *
 
 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
 
 */
 
int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline)
 
static int DrawStringMultiLine(int left, int right, int top, int bottom, char *str, const char *last, TextColour colour, StringAlignment align, bool underline)
 
{
 
	int maxw = right - left + 1;
 
	int maxh = bottom - top + 1;
 

	
 
	/* It makes no sense to even try if it can't be drawn anyway, or
 
	 * do we really want to support fonts of 0 or less pixels high? */
 
	if (maxh <= 0) return top;
 

	
 
	char buffer[DRAW_STRING_BUFFER];
 
	GetString(buffer, str, lastof(buffer));
 

	
 
	uint32 tmp = FormatStringLinebreaks(buffer, lastof(buffer), maxw);
 
	uint32 tmp = FormatStringLinebreaks(str, last, maxw);
 
	int num = GB(tmp, 0, 16) + 1;
 

	
 
	int mt = GetCharacterHeight((FontSize)GB(tmp, 16, 16));
 
	int total_height = num * mt;
 

	
 
	int skip_lines = 0;
 
@@ -873,13 +871,13 @@ int DrawStringMultiLine(int left, int ri
 
			y = bottom - total_height;
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 

	
 
	const char *src = buffer;
 
	const char *src = str;
 
	DrawStringParams params(colour);
 
	int written_top = bottom; // Uppermost position of rendering a line of text
 
	for (;;) {
 
		if (skip_lines == 0) {
 
			char buf2[DRAW_STRING_BUFFER];
 
			strecpy(buf2, src, lastof(buf2));
 
@@ -913,12 +911,54 @@ int DrawStringMultiLine(int left, int ri
 
		}
 
		if (skip_lines > 0) skip_lines--;
 
		if (num == 0) return ((align & SA_VERT_MASK) == SA_BOTTOM) ? written_top : y;
 
	}
 
}
 

	
 
/**
 
 * Draw string, possibly over multiple lines.
 
 *
 
 * @param left   The left most position to draw on.
 
 * @param right  The right most position to draw on.
 
 * @param top    The top most position to draw on.
 
 * @param bottom The bottom most position to draw on.
 
 * @param str    String to draw.
 
 * @param colour Colour used for drawing the string, see DoDrawString() for details
 
 * @param align  The horizontal and vertical alignment of the string.
 
 * @param underline Whether to underline all strings
 
 *
 
 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
 
 */
 
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline)
 
{
 
	char buffer[DRAW_STRING_BUFFER];
 
	strecpy(buffer, str, lastof(buffer));
 
	return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline);
 
}
 

	
 
/**
 
 * Draw string, possibly over multiple lines.
 
 *
 
 * @param left   The left most position to draw on.
 
 * @param right  The right most position to draw on.
 
 * @param top    The top most position to draw on.
 
 * @param bottom The bottom most position to draw on.
 
 * @param str    String to draw.
 
 * @param colour Colour used for drawing the string, see DoDrawString() for details
 
 * @param align  The horizontal and vertical alignment of the string.
 
 * @param underline Whether to underline all strings
 
 *
 
 * @return If \a align is #SA_BOTTOM, the top to where we have written, else the bottom to where we have written.
 
 */
 
int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour, StringAlignment align, bool underline)
 
{
 
	char buffer[DRAW_STRING_BUFFER];
 
	GetString(buffer, str, lastof(buffer));
 
	return DrawStringMultiLine(left, right, top, bottom, buffer, lastof(buffer), colour, align, underline);
 
}
 

	
 
/** Return the string dimension in pixels. The height and width are returned
 
 * in a single Dimension value. TINYFONT, BIGFONT modifiers are only
 
 * supported as the first character of the string. The returned dimensions
 
 * are therefore a rough estimation correct for all the current strings
 
 * but not every possible combination
 
 * @param str string to calculate pixel-width
src/gfx_func.h
Show inline comments
 
@@ -109,12 +109,13 @@ enum StringAlignment {
 
	SA_STRIP       = 1 << 5, ///< Strip the SETX/SETXY commands from the string
 
};
 
DECLARE_ENUM_AS_BIT_SET(StringAlignment)
 

	
 
int DrawString(int left, int right, int top, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false);
 
int DrawString(int left, int right, int top, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false);
 
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false);
 
int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false);
 

	
 
void DrawCharCentered(uint32 c, int x, int y, TextColour colour);
 

	
 
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode = FILLRECT_OPAQUE);
 
void GfxDrawLine(int left, int top, int right, int bottom, int colour);
0 comments (0 inline, 0 general)