Some small helpers to draw strings and sprites dealing with left-to-right/right-to-left.

This commit is contained in:
Juanjo
2012-12-31 11:25:18 +01:00
committed by pelya
parent 9fa95981fc
commit b792189e59
2 changed files with 112 additions and 0 deletions

View File

@@ -1592,3 +1592,104 @@ void SortResolutions(int count)
{
QSortT(_resolutions, count, &compare_res);
}
/**
* Returns the initial value for a margin, after telling where are the left and right margins and where we want to draw/write (begining/end of line)
* @param left is the left margin of the horizontal space we want to draw to
* @param right: right margin
* @param to_end_line: 0 if working at the begining of the line, 1 if working at the end
* @return the margin we asked
*/
int InitTempMargin(int left, int right, bool to_end_line)
{
return to_end_line ^ (_current_text_dir == TD_RTL) ? right :left;
}
/**
* Consumes a space in an horizontal margin
* @param space: amount of space used
* @param here: the margin where to add the space
* @param to_end_line: 0 if working at the begining of the line, 1 if working at the end
*/
void AddSpace(int space, int &here, bool to_end_line)
{
here += to_end_line ^ (_current_text_dir == TD_RTL) ? -space : space;
}
/**
* After drawing something, update a margin
* @param end is where we ended drawing (usually the return value of a DrawString function)
* @param margin is the margin we want to update
* @param to_end_line: 0 if working at the begining of the line, 1 if working at the end
*/
void UpdateMarginEnd(int end, int &margin, bool to_end_line)
{
margin = to_end_line ^ (_current_text_dir == TD_RTL) ? min(end, margin) : max(end, margin);
}
/**
* After drawing something, horizontal margins are updated
* @param end: last position drawn
* @param left is the left margin of the horizontal space drawn
* @param right: right margin
* @param to_end_line: 0 if working at the begining of the line, 1 if working at the end
*/
void UpdateMarginsEnd(int end, int &left, int &right, bool to_end_line)
{
if (to_end_line ^ (_current_text_dir == TD_RTL)) {
right = end;
} else {
left = end;
}
}
/**
* After drawing something of a certain width, update margins
* @param width: used space
* @param initial left margin
* @param initial right margin
* @param to_end_line: 0 if working at the begining of the line, 1 if working at the end
*/
void UpdateMarginsWidth(int width, int &left, int &right, bool to_end_line)
{
if (to_end_line ^ (_current_text_dir == TD_RTL)) {
right -= width;
} else {
left += width;
}
}
/**
* Draws a string in a delimited space; temporal margin gets updated
* @param left is the left margin of the horizontal space we want to draw to
* @param right: right margin of the horizontal space we want to draw to
* @param top: vertical position
* @param margin keeps the most extreme limit used of the line (this should be previously initialized with InitTempLimit)
* @param string to draw
* @param colour for the string
* @param alignment of the string (only left or right alignment)
* @param underline
*/
void DrawString2(int left, int right, int top, int &margin, StringID str, TextColour colour, StringAlignment align, bool underline)
{
int end = DrawString(left, right, top, str, colour, align, underline);
UpdateMarginEnd(end, margin, align == SA_RIGHT);
}
/**
* Draws a sprite in a delimited space; temporal margin gets updated
* @param width of the sprite
* @param left is the left margin of the horizontal space we want to draw to
* @param right: right margin of the horizontal space
* @param top: vertical position
* @param margin keeps the most extreme limit used of the line (this should be previously initialized with InitTempLimit)
* @param sprite
* @param palette
* @param to_end_line: 0 if working at the begining of the line, 1 if working at the end
*/
void DrawSprite2(int width, int left, int right, int top, int &margin, SpriteID img, PaletteID pal, bool to_end_line, SubSprite *sub)
{
DrawSprite(img, pal, to_end_line ^ (_current_text_dir == TD_RTL) ? right - width : left, top, sub);
margin = to_end_line ^ (_current_text_dir == TD_RTL) ? min(right - width, margin): max(margin, left + width);
}