Normalize TextFields

This commit is contained in:
Paul Chote
2011-05-16 22:23:44 +12:00
parent aa772db9a7
commit e09ccef48b
10 changed files with 147 additions and 98 deletions

View File

@@ -121,79 +121,6 @@ namespace OpenRA.Mods.Cnc.Widgets
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.Fonts["Bold"] : Game.Renderer.Fonts["Regular"];
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); }
}
public class CncSliderWidget : SliderWidget
{