Scissor the text of a ButtonWidget if it overflows and display a tooltip
Also: * add a variable to a comomn pattern used for truncating text in HotkeyEntryWidget and TextFieldWidget
This commit is contained in:
committed by
abcdefg30
parent
f325a4d190
commit
76a6e7ec92
@@ -243,12 +243,27 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
var bgDark = GetContrastColorDark();
|
var bgDark = GetContrastColorDark();
|
||||||
var bgLight = GetContrastColorLight();
|
var bgLight = GetContrastColorLight();
|
||||||
|
|
||||||
|
var contentWidth = Bounds.Width - LeftMargin - RightMargin;
|
||||||
var stateOffset = Depressed ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
|
var stateOffset = Depressed ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
|
||||||
|
|
||||||
var position = GetTextPosition(text, font, rb);
|
var position = GetTextPosition(text, font, rb);
|
||||||
|
|
||||||
var hover = Ui.MouseOverWidget == this || Children.Any(c => c == Ui.MouseOverWidget);
|
var hover = Ui.MouseOverWidget == this || Children.Any(c => c == Ui.MouseOverWidget);
|
||||||
DrawBackground(rb, disabled, Depressed, hover, highlighted);
|
DrawBackground(rb, disabled, Depressed, hover, highlighted);
|
||||||
|
|
||||||
|
// Scissor when the text overflows
|
||||||
|
var isTextOverflowing = font.Measure(text).X > contentWidth;
|
||||||
|
if (isTextOverflowing && TooltipContainer != null)
|
||||||
|
{
|
||||||
|
Game.Renderer.EnableScissor(new Rectangle(RenderOrigin.X + LeftMargin, RenderOrigin.Y,
|
||||||
|
contentWidth, Bounds.Bottom));
|
||||||
|
|
||||||
|
if (GetTooltipText() == null)
|
||||||
|
GetTooltipText = () => GetText();
|
||||||
|
|
||||||
|
text = WidgetUtils.TruncateText(text, contentWidth, font);
|
||||||
|
}
|
||||||
|
|
||||||
if (Contrast)
|
if (Contrast)
|
||||||
font.DrawTextWithContrast(text, position + stateOffset,
|
font.DrawTextWithContrast(text, position + stateOffset,
|
||||||
disabled ? colordisabled : color, bgDark, bgLight, 2);
|
disabled ? colordisabled : color, bgDark, bgLight, 2);
|
||||||
@@ -257,6 +272,9 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
else
|
else
|
||||||
font.DrawText(text, position + stateOffset,
|
font.DrawText(text, position + stateOffset,
|
||||||
disabled ? colordisabled : color);
|
disabled ? colordisabled : color);
|
||||||
|
|
||||||
|
if (isTextOverflowing && TooltipContainer != null)
|
||||||
|
Game.Renderer.DisableScissor();
|
||||||
}
|
}
|
||||||
|
|
||||||
int2 GetTextPosition(string text, SpriteFont font, Rectangle rb)
|
int2 GetTextPosition(string text, SpriteFont font, Rectangle rb)
|
||||||
|
|||||||
@@ -124,7 +124,8 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
var textPos = pos + new int2(LeftMargin, (Bounds.Height - textSize.Y) / 2 - VisualHeight);
|
var textPos = pos + new int2(LeftMargin, (Bounds.Height - textSize.Y) / 2 - VisualHeight);
|
||||||
|
|
||||||
// Scissor when the text overflows
|
// Scissor when the text overflows
|
||||||
if (textSize.X > Bounds.Width - LeftMargin - RightMargin)
|
var isTextOverflowing = textSize.X > Bounds.Width - LeftMargin - RightMargin;
|
||||||
|
if (isTextOverflowing)
|
||||||
{
|
{
|
||||||
Game.Renderer.EnableScissor(new Rectangle(pos.X + LeftMargin, pos.Y,
|
Game.Renderer.EnableScissor(new Rectangle(pos.X + LeftMargin, pos.Y,
|
||||||
Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom));
|
Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom));
|
||||||
@@ -133,7 +134,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
var color = disabled ? TextColorDisabled : TextColor;
|
var color = disabled ? TextColorDisabled : TextColor;
|
||||||
font.DrawText(apparentText, textPos, color);
|
font.DrawText(apparentText, textPos, color);
|
||||||
|
|
||||||
if (textSize.X > Bounds.Width - LeftMargin - RightMargin)
|
if (isTextOverflowing)
|
||||||
Game.Renderer.DisableScissor();
|
Game.Renderer.DisableScissor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -579,7 +579,8 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
var textPos = pos + new int2(LeftMargin, verticalMargin);
|
var textPos = pos + new int2(LeftMargin, verticalMargin);
|
||||||
|
|
||||||
// Right align when editing and scissor when the text overflows
|
// Right align when editing and scissor when the text overflows
|
||||||
if (textSize.X > Bounds.Width - LeftMargin - RightMargin)
|
var isTextOverflowing = textSize.X > Bounds.Width - LeftMargin - RightMargin;
|
||||||
|
if (isTextOverflowing)
|
||||||
{
|
{
|
||||||
if (HasKeyboardFocus)
|
if (HasKeyboardFocus)
|
||||||
textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0);
|
textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0);
|
||||||
@@ -609,7 +610,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
if (showCursor && HasKeyboardFocus)
|
if (showCursor && HasKeyboardFocus)
|
||||||
font.DrawText("|", new float2(textPos.X + cursorPosition.X - 2, textPos.Y), TextColor);
|
font.DrawText("|", new float2(textPos.X + cursorPosition.X - 2, textPos.Y), TextColor);
|
||||||
|
|
||||||
if (textSize.X > Bounds.Width - LeftMargin - RightMargin)
|
if (isTextOverflowing)
|
||||||
Game.Renderer.DisableScissor();
|
Game.Renderer.DisableScissor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user