mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Add internal widget alignment property, along with widget part.
parent
2a0365b3d9
commit
636e37d183
|
@ -91,24 +91,6 @@ Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom
|
||||||
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
|
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
|
||||||
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
|
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
|
||||||
|
|
||||||
/** How to align the to-be drawn text. */
|
|
||||||
enum StringAlignment {
|
|
||||||
SA_LEFT = 0 << 0, ///< Left align the text.
|
|
||||||
SA_HOR_CENTER = 1 << 0, ///< Horizontally center the text.
|
|
||||||
SA_RIGHT = 2 << 0, ///< Right align the text (must be a single bit).
|
|
||||||
SA_HOR_MASK = 3 << 0, ///< Mask for horizontal alignment.
|
|
||||||
|
|
||||||
SA_TOP = 0 << 2, ///< Top align the text.
|
|
||||||
SA_VERT_CENTER = 1 << 2, ///< Vertically center the text.
|
|
||||||
SA_BOTTOM = 2 << 2, ///< Bottom align the text.
|
|
||||||
SA_VERT_MASK = 3 << 2, ///< Mask for vertical alignment.
|
|
||||||
|
|
||||||
SA_CENTER = SA_HOR_CENTER | SA_VERT_CENTER, ///< Center both horizontally and vertically.
|
|
||||||
|
|
||||||
SA_FORCE = 1 << 4, ///< Force the alignment, i.e. don't swap for RTL languages.
|
|
||||||
};
|
|
||||||
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, FontSize fontsize = FS_NORMAL);
|
int DrawString(int left, int right, int top, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
|
||||||
int DrawString(int left, int right, int top, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
|
int DrawString(int left, int right, int top, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
|
||||||
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, FontSize fontsize = FS_NORMAL);
|
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, FontSize fontsize = FS_NORMAL);
|
||||||
|
|
|
@ -323,4 +323,22 @@ enum Support8bpp {
|
||||||
S8BPP_HARDWARE, ///< Full 8bpp support by OS and hardware.
|
S8BPP_HARDWARE, ///< Full 8bpp support by OS and hardware.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** How to align the to-be drawn text. */
|
||||||
|
enum StringAlignment {
|
||||||
|
SA_LEFT = 0 << 0, ///< Left align the text.
|
||||||
|
SA_HOR_CENTER = 1 << 0, ///< Horizontally center the text.
|
||||||
|
SA_RIGHT = 2 << 0, ///< Right align the text (must be a single bit).
|
||||||
|
SA_HOR_MASK = 3 << 0, ///< Mask for horizontal alignment.
|
||||||
|
|
||||||
|
SA_TOP = 0 << 2, ///< Top align the text.
|
||||||
|
SA_VERT_CENTER = 1 << 2, ///< Vertically center the text.
|
||||||
|
SA_BOTTOM = 2 << 2, ///< Bottom align the text.
|
||||||
|
SA_VERT_MASK = 3 << 2, ///< Mask for vertical alignment.
|
||||||
|
|
||||||
|
SA_CENTER = SA_HOR_CENTER | SA_VERT_CENTER, ///< Center both horizontally and vertically.
|
||||||
|
|
||||||
|
SA_FORCE = 1 << 4, ///< Force the alignment, i.e. don't swap for RTL languages.
|
||||||
|
};
|
||||||
|
DECLARE_ENUM_AS_BIT_SET(StringAlignment)
|
||||||
|
|
||||||
#endif /* GFX_TYPE_H */
|
#endif /* GFX_TYPE_H */
|
||||||
|
|
|
@ -422,7 +422,7 @@ struct NewsWindow : Window {
|
||||||
{
|
{
|
||||||
switch (widget) {
|
switch (widget) {
|
||||||
case WID_N_CAPTION:
|
case WID_N_CAPTION:
|
||||||
DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION);
|
DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION, SA_HOR_CENTER);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WID_N_PANEL:
|
case WID_N_PANEL:
|
||||||
|
|
130
src/widget.cpp
130
src/widget.cpp
|
@ -24,6 +24,33 @@
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate x and y coordinates for an aligned object within a window.
|
||||||
|
* @param r Rectangle of the widget to be drawn in.
|
||||||
|
* @param d Dimension of the object to be drawn.
|
||||||
|
* @param align Alignment of the object.
|
||||||
|
* @return A point containing the position at which to draw.
|
||||||
|
*/
|
||||||
|
static inline Point GetAlignedPosition(const Rect &r, const Dimension &d, StringAlignment align)
|
||||||
|
{
|
||||||
|
Point p;
|
||||||
|
/* In case we have a RTL language we swap the alignment. */
|
||||||
|
if (!(align & SA_FORCE) && _current_text_dir == TD_RTL && (align & SA_HOR_MASK) != SA_HOR_CENTER) align ^= SA_RIGHT;
|
||||||
|
switch (align & SA_HOR_MASK) {
|
||||||
|
case SA_LEFT: p.x = r.left; break;
|
||||||
|
case SA_HOR_CENTER: p.x = CenterBounds(r.left, r.right, d.width); break;
|
||||||
|
case SA_RIGHT: p.x = r.right - d.width; break;
|
||||||
|
default: NOT_REACHED();
|
||||||
|
}
|
||||||
|
switch (align & SA_VERT_MASK) {
|
||||||
|
case SA_TOP: p.y = r.top; break;
|
||||||
|
case SA_VERT_CENTER: p.y = CenterBounds(r.top, r.bottom, d.height); break;
|
||||||
|
case SA_BOTTOM: p.y = r.bottom - d.height; break;
|
||||||
|
default: NOT_REACHED();
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the vertical position of the draggable part of scrollbar
|
* Compute the vertical position of the draggable part of scrollbar
|
||||||
* @param sb Scrollbar list data
|
* @param sb Scrollbar list data
|
||||||
|
@ -212,15 +239,17 @@ void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, Fra
|
||||||
* @param colour Colour of the button.
|
* @param colour Colour of the button.
|
||||||
* @param clicked Button is lowered.
|
* @param clicked Button is lowered.
|
||||||
* @param img Sprite to draw.
|
* @param img Sprite to draw.
|
||||||
|
* @param align Alignment of the sprite.
|
||||||
*/
|
*/
|
||||||
static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img)
|
static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colour, bool clicked, SpriteID img, StringAlignment align)
|
||||||
{
|
{
|
||||||
assert(img != 0);
|
assert(img != 0);
|
||||||
DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE);
|
||||||
|
|
||||||
if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
|
if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2.
|
||||||
Dimension d = GetSpriteSize(img);
|
Dimension d = GetSpriteSize(img);
|
||||||
DrawSprite(img, PAL_NONE, CenterBounds(r.left, r.right, d.width) + clicked, CenterBounds(r.top, r.bottom, d.height) + clicked);
|
Point p = GetAlignedPosition(r, d, align);
|
||||||
|
DrawSprite(img, PAL_NONE, p.x + clicked, p.y + clicked);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -229,14 +258,15 @@ static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colo
|
||||||
* @param type Widget type (#WWT_TEXTBTN, #WWT_TEXTBTN_2, or #WWT_LABEL).
|
* @param type Widget type (#WWT_TEXTBTN, #WWT_TEXTBTN_2, or #WWT_LABEL).
|
||||||
* @param clicked Label is rendered lowered.
|
* @param clicked Label is rendered lowered.
|
||||||
* @param str Text to draw.
|
* @param str Text to draw.
|
||||||
|
* @param align Alignment of the text.
|
||||||
*/
|
*/
|
||||||
static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str)
|
static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
if (str == STR_NULL) return;
|
if (str == STR_NULL) return;
|
||||||
if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
|
if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++;
|
||||||
Dimension d = GetStringBoundingBox(str);
|
Dimension d = GetStringBoundingBox(str);
|
||||||
int offset = std::max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
|
Point p = GetAlignedPosition(r, d, align);
|
||||||
DrawString(r.left + clicked, r.right + clicked, r.top + offset + clicked, str, TC_FROMSTRING, SA_HOR_CENTER);
|
DrawString(r.left + clicked, r.right + clicked, p.y + clicked, str, TC_FROMSTRING, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -244,12 +274,13 @@ static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, Strin
|
||||||
* @param r Rectangle of the background.
|
* @param r Rectangle of the background.
|
||||||
* @param colour Colour of the text.
|
* @param colour Colour of the text.
|
||||||
* @param str Text to draw.
|
* @param str Text to draw.
|
||||||
|
* @param align Alignment of the text.
|
||||||
*/
|
*/
|
||||||
static inline void DrawText(const Rect &r, TextColour colour, StringID str)
|
static inline void DrawText(const Rect &r, TextColour colour, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
Dimension d = GetStringBoundingBox(str);
|
Dimension d = GetStringBoundingBox(str);
|
||||||
int offset = std::max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
|
Point p = GetAlignedPosition(r, d, align);
|
||||||
if (str != STR_NULL) DrawString(r.left, r.right, r.top + offset, str, colour);
|
if (str != STR_NULL) DrawString(r.left, r.right, p.y, str, colour, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -257,11 +288,12 @@ static inline void DrawText(const Rect &r, TextColour colour, StringID str)
|
||||||
* @param r Rectangle of the background.
|
* @param r Rectangle of the background.
|
||||||
* @param colour Colour of the inset.
|
* @param colour Colour of the inset.
|
||||||
* @param str Text to draw.
|
* @param str Text to draw.
|
||||||
|
* @param align Alignment of the text.
|
||||||
*/
|
*/
|
||||||
static inline void DrawInset(const Rect &r, Colours colour, StringID str)
|
static inline void DrawInset(const Rect &r, Colours colour, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED);
|
||||||
if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str);
|
if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str, TC_FROMSTRING, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -405,12 +437,13 @@ static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool l
|
||||||
* @param r Rectangle of the frame.
|
* @param r Rectangle of the frame.
|
||||||
* @param colour Colour of the frame.
|
* @param colour Colour of the frame.
|
||||||
* @param str Text of the frame.
|
* @param str Text of the frame.
|
||||||
|
* @param align Alignment of the text in the frame.
|
||||||
*/
|
*/
|
||||||
static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
|
static inline void DrawFrame(const Rect &r, Colours colour, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
int x2 = r.left; // by default the left side is the left side of the widget
|
int x2 = r.left; // by default the left side is the left side of the widget
|
||||||
|
|
||||||
if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str);
|
if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str, TC_FROMSTRING, align);
|
||||||
|
|
||||||
int c1 = _colour_gradient[colour][3];
|
int c1 = _colour_gradient[colour][3];
|
||||||
int c2 = _colour_gradient[colour][7];
|
int c2 = _colour_gradient[colour][7];
|
||||||
|
@ -458,7 +491,7 @@ static inline void DrawFrame(const Rect &r, Colours colour, StringID str)
|
||||||
*/
|
*/
|
||||||
static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
|
static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
|
||||||
{
|
{
|
||||||
DrawImageButtons(r, WWT_SHADEBOX, colour, clicked, clicked ? SPR_WINDOW_SHADE: SPR_WINDOW_UNSHADE);
|
DrawImageButtons(r, WWT_SHADEBOX, colour, clicked, clicked ? SPR_WINDOW_SHADE: SPR_WINDOW_UNSHADE, SA_CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -469,7 +502,7 @@ static inline void DrawShadeBox(const Rect &r, Colours colour, bool clicked)
|
||||||
*/
|
*/
|
||||||
static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
|
static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
|
||||||
{
|
{
|
||||||
DrawImageButtons(r, WWT_STICKYBOX, colour, clicked, clicked ? SPR_PIN_UP : SPR_PIN_DOWN);
|
DrawImageButtons(r, WWT_STICKYBOX, colour, clicked, clicked ? SPR_PIN_UP : SPR_PIN_DOWN, SA_CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -480,7 +513,7 @@ static inline void DrawStickyBox(const Rect &r, Colours colour, bool clicked)
|
||||||
*/
|
*/
|
||||||
static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
|
static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
|
||||||
{
|
{
|
||||||
DrawImageButtons(r, WWT_DEFSIZEBOX, colour, clicked, SPR_WINDOW_DEFSIZE);
|
DrawImageButtons(r, WWT_DEFSIZEBOX, colour, clicked, SPR_WINDOW_DEFSIZE, SA_CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -491,7 +524,7 @@ static inline void DrawDefSizeBox(const Rect &r, Colours colour, bool clicked)
|
||||||
*/
|
*/
|
||||||
static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
|
static inline void DrawDebugBox(const Rect &r, Colours colour, bool clicked)
|
||||||
{
|
{
|
||||||
DrawImageButtons(r, WWT_DEBUGBOX, colour, clicked, SPR_WINDOW_DEBUG);
|
DrawImageButtons(r, WWT_DEBUGBOX, colour, clicked, SPR_WINDOW_DEBUG, SA_CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -534,8 +567,9 @@ static inline void DrawCloseBox(const Rect &r, Colours colour)
|
||||||
* @param colour Colour of the window.
|
* @param colour Colour of the window.
|
||||||
* @param owner 'Owner' of the window.
|
* @param owner 'Owner' of the window.
|
||||||
* @param str Text to draw in the bar.
|
* @param str Text to draw in the bar.
|
||||||
|
* @param align Alignment of the text.
|
||||||
*/
|
*/
|
||||||
void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
|
void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
bool company_owned = owner < MAX_COMPANIES;
|
bool company_owned = owner < MAX_COMPANIES;
|
||||||
|
|
||||||
|
@ -548,8 +582,8 @@ void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
|
||||||
|
|
||||||
if (str != STR_NULL) {
|
if (str != STR_NULL) {
|
||||||
Dimension d = GetStringBoundingBox(str);
|
Dimension d = GetStringBoundingBox(str);
|
||||||
int offset = std::max(0, ((int)(r.bottom - r.top + 1) - (int)d.height) / 2); // Offset for rendering the text vertically centered
|
Point p = GetAlignedPosition(r, d, align);
|
||||||
DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + offset, str, TC_FROMSTRING, SA_HOR_CENTER);
|
DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, p.y, str, TC_FROMSTRING, align);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -560,10 +594,11 @@ void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str)
|
||||||
* @param clicked_button The button-part is lowered.
|
* @param clicked_button The button-part is lowered.
|
||||||
* @param clicked_dropdown The drop-down part is lowered.
|
* @param clicked_dropdown The drop-down part is lowered.
|
||||||
* @param str Text of the button.
|
* @param str Text of the button.
|
||||||
|
* @param align Alignment of the text within the dropdown.
|
||||||
*
|
*
|
||||||
* @note Magic constants are also used in #NWidgetLeaf::ButtonHit.
|
* @note Magic constants are also used in #NWidgetLeaf::ButtonHit.
|
||||||
*/
|
*/
|
||||||
static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str)
|
static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
int text_offset = std::max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
|
int text_offset = std::max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered
|
||||||
|
|
||||||
|
@ -575,12 +610,12 @@ static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicke
|
||||||
DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
|
DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
|
||||||
DrawFrameRect(r.right + 1 - dd_width, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
|
DrawFrameRect(r.right + 1 - dd_width, r.top, r.right, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
|
||||||
DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.right - (dd_width - 2) + clicked_dropdown, r.top + image_offset + clicked_dropdown);
|
DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.right - (dd_width - 2) + clicked_dropdown, r.top + image_offset + clicked_dropdown);
|
||||||
if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - dd_width - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
|
if (str != STR_NULL) DrawString(r.left + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - dd_width - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK, align);
|
||||||
} else {
|
} else {
|
||||||
DrawFrameRect(r.left + dd_width, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
|
DrawFrameRect(r.left + dd_width, r.top, r.right, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
|
||||||
DrawFrameRect(r.left, r.top, r.left + dd_width - 1, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
|
DrawFrameRect(r.left, r.top, r.left + dd_width - 1, r.bottom, colour, clicked_dropdown ? FR_LOWERED : FR_NONE);
|
||||||
DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.left + 1 + clicked_dropdown, r.top + image_offset + clicked_dropdown);
|
DrawSprite(SPR_ARROW_DOWN, PAL_NONE, r.left + 1 + clicked_dropdown, r.top + image_offset + clicked_dropdown);
|
||||||
if (str != STR_NULL) DrawString(r.left + dd_width + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK);
|
if (str != STR_NULL) DrawString(r.left + dd_width + WD_DROPDOWNTEXT_LEFT + clicked_button, r.right - WD_DROPDOWNTEXT_RIGHT + clicked_button, r.top + text_offset + clicked_button, str, TC_BLACK, align);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -590,10 +625,11 @@ static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicke
|
||||||
* @param colour Background colour of the widget.
|
* @param colour Background colour of the widget.
|
||||||
* @param clicked The widget is lowered.
|
* @param clicked The widget is lowered.
|
||||||
* @param str Text of the button.
|
* @param str Text of the button.
|
||||||
|
* @param align Alignment of the text.
|
||||||
*/
|
*/
|
||||||
static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str)
|
static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str, StringAlignment align)
|
||||||
{
|
{
|
||||||
DrawButtonDropdown(r, colour, false, clicked, str);
|
DrawButtonDropdown(r, colour, false, clicked, str, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -872,6 +908,7 @@ NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y
|
||||||
this->widget_data = widget_data;
|
this->widget_data = widget_data;
|
||||||
this->tool_tip = tool_tip;
|
this->tool_tip = tool_tip;
|
||||||
this->scrollbar_index = -1;
|
this->scrollbar_index = -1;
|
||||||
|
this->align = SA_CENTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -904,6 +941,15 @@ void NWidgetCore::SetToolTip(StringID tool_tip)
|
||||||
this->tool_tip = tool_tip;
|
this->tool_tip = tool_tip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the text/image alignment of the nested widget.
|
||||||
|
* @param align Alignment to use.
|
||||||
|
*/
|
||||||
|
void NWidgetCore::SetAlignment(StringAlignment align)
|
||||||
|
{
|
||||||
|
this->align = align;
|
||||||
|
}
|
||||||
|
|
||||||
void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
|
void NWidgetCore::FillNestedArray(NWidgetBase **array, uint length)
|
||||||
{
|
{
|
||||||
if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
|
if (this->index >= 0 && (uint)(this->index) < length) array[this->index] = this;
|
||||||
|
@ -1725,6 +1771,7 @@ NWidgetBackground::NWidgetBackground(WidgetType tp, Colours colour, int index, N
|
||||||
assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
|
assert(tp == WWT_PANEL || tp == WWT_INSET || tp == WWT_FRAME);
|
||||||
if (index >= 0) this->SetIndex(index);
|
if (index >= 0) this->SetIndex(index);
|
||||||
this->child = child;
|
this->child = child;
|
||||||
|
this->SetAlignment(SA_TOP | SA_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
NWidgetBackground::~NWidgetBackground()
|
NWidgetBackground::~NWidgetBackground()
|
||||||
|
@ -1858,12 +1905,12 @@ void NWidgetBackground::Draw(const Window *w)
|
||||||
|
|
||||||
case WWT_FRAME:
|
case WWT_FRAME:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawFrame(r, this->colour, this->widget_data);
|
DrawFrame(r, this->colour, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WWT_INSET:
|
case WWT_INSET:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawInset(r, this->colour, this->widget_data);
|
DrawInset(r, this->colour, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -2192,6 +2239,11 @@ NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data,
|
||||||
case WWT_EMPTY:
|
case WWT_EMPTY:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case WWT_TEXT:
|
||||||
|
this->SetFill(0, 0);
|
||||||
|
this->SetAlignment(SA_LEFT | SA_VERT_CENTER);
|
||||||
|
break;
|
||||||
|
|
||||||
case WWT_PUSHBTN:
|
case WWT_PUSHBTN:
|
||||||
case WWT_IMGBTN:
|
case WWT_IMGBTN:
|
||||||
case WWT_PUSHIMGBTN:
|
case WWT_PUSHIMGBTN:
|
||||||
|
@ -2200,7 +2252,6 @@ NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data,
|
||||||
case WWT_PUSHTXTBTN:
|
case WWT_PUSHTXTBTN:
|
||||||
case WWT_TEXTBTN_2:
|
case WWT_TEXTBTN_2:
|
||||||
case WWT_LABEL:
|
case WWT_LABEL:
|
||||||
case WWT_TEXT:
|
|
||||||
case WWT_MATRIX:
|
case WWT_MATRIX:
|
||||||
case NWID_BUTTON_DROPDOWN:
|
case NWID_BUTTON_DROPDOWN:
|
||||||
case NWID_PUSHBUTTON_DROPDOWN:
|
case NWID_PUSHBUTTON_DROPDOWN:
|
||||||
|
@ -2259,6 +2310,7 @@ NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data,
|
||||||
case WWT_DROPDOWN:
|
case WWT_DROPDOWN:
|
||||||
this->SetFill(0, 0);
|
this->SetFill(0, 0);
|
||||||
this->min_y = WD_DROPDOWN_HEIGHT;
|
this->min_y = WD_DROPDOWN_HEIGHT;
|
||||||
|
this->SetAlignment(SA_TOP | SA_LEFT);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -2493,7 +2545,7 @@ void NWidgetLeaf::Draw(const Window *w)
|
||||||
case WWT_IMGBTN:
|
case WWT_IMGBTN:
|
||||||
case WWT_PUSHIMGBTN:
|
case WWT_PUSHIMGBTN:
|
||||||
case WWT_IMGBTN_2:
|
case WWT_IMGBTN_2:
|
||||||
DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data);
|
DrawImageButtons(r, this->type, this->colour, clicked, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WWT_TEXTBTN:
|
case WWT_TEXTBTN:
|
||||||
|
@ -2501,7 +2553,7 @@ void NWidgetLeaf::Draw(const Window *w)
|
||||||
case WWT_TEXTBTN_2:
|
case WWT_TEXTBTN_2:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
|
DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE);
|
||||||
DrawLabel(r, this->type, clicked, this->widget_data);
|
DrawLabel(r, this->type, clicked, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WWT_ARROWBTN:
|
case WWT_ARROWBTN:
|
||||||
|
@ -2514,18 +2566,18 @@ void NWidgetLeaf::Draw(const Window *w)
|
||||||
case AWV_RIGHT: sprite = SPR_ARROW_RIGHT; break;
|
case AWV_RIGHT: sprite = SPR_ARROW_RIGHT; break;
|
||||||
default: NOT_REACHED();
|
default: NOT_REACHED();
|
||||||
}
|
}
|
||||||
DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite);
|
DrawImageButtons(r, WWT_PUSHIMGBTN, this->colour, clicked, sprite, this->align);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case WWT_LABEL:
|
case WWT_LABEL:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawLabel(r, this->type, clicked, this->widget_data);
|
DrawLabel(r, this->type, clicked, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WWT_TEXT:
|
case WWT_TEXT:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawText(r, (TextColour)this->colour, this->widget_data);
|
DrawText(r, (TextColour)this->colour, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WWT_MATRIX:
|
case WWT_MATRIX:
|
||||||
|
@ -2540,7 +2592,7 @@ void NWidgetLeaf::Draw(const Window *w)
|
||||||
|
|
||||||
case WWT_CAPTION:
|
case WWT_CAPTION:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawCaption(r, this->colour, w->owner, this->widget_data);
|
DrawCaption(r, this->colour, w->owner, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WWT_SHADEBOX:
|
case WWT_SHADEBOX:
|
||||||
|
@ -2573,13 +2625,13 @@ void NWidgetLeaf::Draw(const Window *w)
|
||||||
|
|
||||||
case WWT_DROPDOWN:
|
case WWT_DROPDOWN:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawDropdown(r, this->colour, clicked, this->widget_data);
|
DrawDropdown(r, this->colour, clicked, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NWID_BUTTON_DROPDOWN:
|
case NWID_BUTTON_DROPDOWN:
|
||||||
case NWID_PUSHBUTTON_DROPDOWN:
|
case NWID_PUSHBUTTON_DROPDOWN:
|
||||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||||
DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data);
|
DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->widget_data, this->align);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -2718,6 +2770,14 @@ static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case WPT_ALIGNMENT: {
|
||||||
|
NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
|
||||||
|
if (nwc != nullptr) {
|
||||||
|
nwc->SetAlignment(parts->u.align.align);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case WPT_FILL: {
|
case WPT_FILL: {
|
||||||
NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
|
NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
|
||||||
if (nwrb != nullptr) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
|
if (nwrb != nullptr) nwrb->SetFill(parts->u.xy.x, parts->u.xy.y);
|
||||||
|
|
|
@ -89,6 +89,7 @@ enum WidgetType {
|
||||||
WPT_DATATIP, ///< Widget part for specifying data and tooltip.
|
WPT_DATATIP, ///< Widget part for specifying data and tooltip.
|
||||||
WPT_PADDING, ///< Widget part for specifying a padding.
|
WPT_PADDING, ///< Widget part for specifying a padding.
|
||||||
WPT_PIPSPACE, ///< Widget part for specifying pre/inter/post space for containers.
|
WPT_PIPSPACE, ///< Widget part for specifying pre/inter/post space for containers.
|
||||||
|
WPT_ALIGNMENT, ///< Widget part for specifying text/image alignment.
|
||||||
WPT_ENDCONTAINER, ///< Widget part to denote end of a container.
|
WPT_ENDCONTAINER, ///< Widget part to denote end of a container.
|
||||||
WPT_FUNCTION, ///< Widget part for calling a user function.
|
WPT_FUNCTION, ///< Widget part for calling a user function.
|
||||||
WPT_SCROLLBAR, ///< Widget part for attaching a scrollbar.
|
WPT_SCROLLBAR, ///< Widget part for attaching a scrollbar.
|
||||||
|
@ -296,6 +297,7 @@ public:
|
||||||
void SetIndex(int index);
|
void SetIndex(int index);
|
||||||
void SetDataTip(uint32 widget_data, StringID tool_tip);
|
void SetDataTip(uint32 widget_data, StringID tool_tip);
|
||||||
void SetToolTip(StringID tool_tip);
|
void SetToolTip(StringID tool_tip);
|
||||||
|
void SetAlignment(StringAlignment align);
|
||||||
|
|
||||||
inline void SetLowered(bool lowered);
|
inline void SetLowered(bool lowered);
|
||||||
inline bool IsLowered() const;
|
inline bool IsLowered() const;
|
||||||
|
@ -315,6 +317,7 @@ public:
|
||||||
StringID tool_tip; ///< Tooltip of the widget. @see Widget::tootips
|
StringID tool_tip; ///< Tooltip of the widget. @see Widget::tootips
|
||||||
int scrollbar_index; ///< Index of an attached scrollbar.
|
int scrollbar_index; ///< Index of an attached scrollbar.
|
||||||
TextColour highlight_colour; ///< Colour of highlight.
|
TextColour highlight_colour; ///< Colour of highlight.
|
||||||
|
StringAlignment align; ///< Alignment of text/image within widget.
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -905,6 +908,14 @@ struct NWidgetPartTextLines {
|
||||||
FontSize size; ///< Font size of text lines.
|
FontSize size; ///< Font size of text lines.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Widget part for setting text/image alignment within a widget.
|
||||||
|
* @ingroup NestedWidgetParts
|
||||||
|
*/
|
||||||
|
struct NWidgetPartAlignment {
|
||||||
|
StringAlignment align; ///< Alignment of text/image.
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pointer to function returning a nested widget.
|
* Pointer to function returning a nested widget.
|
||||||
* @param biggest_index Pointer to storage for collecting the biggest index used in the nested widget.
|
* @param biggest_index Pointer to storage for collecting the biggest index used in the nested widget.
|
||||||
|
@ -926,6 +937,7 @@ struct NWidgetPart {
|
||||||
NWidgetPartPaddings padding; ///< Part with paddings.
|
NWidgetPartPaddings padding; ///< Part with paddings.
|
||||||
NWidgetPartPIP pip; ///< Part with pre/inter/post spaces.
|
NWidgetPartPIP pip; ///< Part with pre/inter/post spaces.
|
||||||
NWidgetPartTextLines text_lines; ///< Part with text line data.
|
NWidgetPartTextLines text_lines; ///< Part with text line data.
|
||||||
|
NWidgetPartAlignment align; ///< Part with internal alignment.
|
||||||
NWidgetFunctionType *func_ptr; ///< Part with a function call.
|
NWidgetFunctionType *func_ptr; ///< Part with a function call.
|
||||||
NWidContainerFlags cont_flags; ///< Part with container flags.
|
NWidContainerFlags cont_flags; ///< Part with container flags.
|
||||||
} u;
|
} u;
|
||||||
|
@ -984,6 +996,21 @@ static inline NWidgetPart SetMinimalTextLines(uint8 lines, uint8 spacing, FontSi
|
||||||
return part;
|
return part;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Widget part function for setting the alignment of text/images.
|
||||||
|
* @param align Alignment of text/image within widget.
|
||||||
|
* @ingroup NestedWidgetParts
|
||||||
|
*/
|
||||||
|
static inline NWidgetPart SetAlignment(StringAlignment align)
|
||||||
|
{
|
||||||
|
NWidgetPart part;
|
||||||
|
|
||||||
|
part.type = WPT_ALIGNMENT;
|
||||||
|
part.u.align.align = align;
|
||||||
|
|
||||||
|
return part;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Widget part function for setting filling.
|
* Widget part function for setting filling.
|
||||||
* @param fill_x Horizontal filling step from minimal size.
|
* @param fill_x Horizontal filling step from minimal size.
|
||||||
|
|
|
@ -140,7 +140,7 @@ enum WidgetDrawDistances {
|
||||||
|
|
||||||
/* widget.cpp */
|
/* widget.cpp */
|
||||||
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
|
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags);
|
||||||
void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str);
|
void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str, StringAlignment align);
|
||||||
|
|
||||||
/* window.cpp */
|
/* window.cpp */
|
||||||
extern Window *_z_front_window;
|
extern Window *_z_front_window;
|
||||||
|
|
Loading…
Reference in New Issue