New textfield with support for hover and disabled states.

This commit is contained in:
Paul Chote
2011-05-08 14:30:08 +12:00
parent 1881a6b713
commit a607a60b8f
5 changed files with 87 additions and 11 deletions

View File

@@ -153,8 +153,81 @@ namespace OpenRA.Mods.Cnc.Widgets
{
ListOffset = Math.Min(0,Bounds.Height - ContentHeight);
}
public override Widget Clone() { return new CncScrollPanelWidget(this); }
public override Widget Clone() { return new CncScrollPanelWidget(this); }
}
public class CncTextFieldWidget : TextFieldWidget
{
public CncTextFieldWidget()
: base() { }
protected CncTextFieldWidget(CncTextFieldWidget widget)
: base(widget) { }
public Func<bool> IsDisabled = () => false;
public Color TextColor = Color.White;
public Color DisabledColor = Color.Gray;
public override bool HandleMouseInput(MouseInput mi)
{
if (IsDisabled())
return false;
return base.HandleMouseInput(mi);
}
public override bool HandleKeyPressInner(KeyInput e)
{
if (IsDisabled())
return false;
return base.HandleKeyPressInner(e);
}
public override void DrawWithString(string text)
{
if (text == null) text = "";
var font = (Bold) ? Game.Renderer.BoldFont : Game.Renderer.RegularFont;
var pos = RenderOrigin;
if (CursorPosition > text.Length)
CursorPosition = text.Length;
var textSize = font.Measure(text);
var cursorPosition = font.Measure(text.Substring(0,CursorPosition));
var disabled = IsDisabled();
var state = disabled ? "button-disabled" :
Focused ? "button-pressed" :
RenderBounds.Contains(Viewport.LastMousePos) ? "button-hover" :
"button";
WidgetUtils.DrawPanel(state,
new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height));
// Inset text by the margin and center vertically
var textPos = pos + new int2(LeftMargin, (Bounds.Height - textSize.Y) / 2 - VisualHeight);
// Right align when editing and scissor when the text overflows
if (textSize.X > Bounds.Width - LeftMargin - RightMargin)
{
if (Focused)
textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0);
Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y, Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom);
}
var color = disabled ? DisabledColor : TextColor;
font.DrawText(text, textPos, color);
if (showCursor && Focused)
font.DrawText("|", new float2(textPos.X + cursorPosition.X - 2, textPos.Y), color);
if (textSize.X > Bounds.Width - LeftMargin - RightMargin)
Game.Renderer.DisableScissor();
}
public override Widget Clone() { return new CncTextFieldWidget(this); }
}
}