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); 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); }
} }
} }

View File

@@ -50,7 +50,10 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget<TextFieldWidget>("SERVER_NAME").Text = settings.Server.Name ?? ""; panel.GetWidget<TextFieldWidget>("SERVER_NAME").Text = settings.Server.Name ?? "";
panel.GetWidget<TextFieldWidget>("LISTEN_PORT").Text = settings.Server.ListenPort.ToString(); panel.GetWidget<TextFieldWidget>("LISTEN_PORT").Text = settings.Server.ListenPort.ToString();
panel.GetWidget<TextFieldWidget>("EXTERNAL_PORT").Text = settings.Server.ExternalPort.ToString();
var externalPort = panel.GetWidget<CncTextFieldWidget>("EXTERNAL_PORT");
externalPort.Text = settings.Server.ExternalPort.ToString();
externalPort.IsDisabled = () => !advertiseOnline;
var advertiseCheckbox = panel.GetWidget<CncCheckboxWidget>("ADVERTISE_CHECKBOX"); var advertiseCheckbox = panel.GetWidget<CncCheckboxWidget>("ADVERTISE_CHECKBOX");
advertiseCheckbox.IsChecked = () => advertiseOnline; advertiseCheckbox.IsChecked = () => advertiseOnline;

View File

@@ -49,7 +49,7 @@ Container@CREATESERVER_PANEL:
Height:25 Height:25
Align:Right Align:Right
Text:Server Name: Text:Server Name:
TextField@SERVER_NAME: CncTextField@SERVER_NAME:
Id:SERVER_NAME Id:SERVER_NAME
X:110 X:110
Y:15 Y:15
@@ -64,7 +64,7 @@ Container@CREATESERVER_PANEL:
Height:25 Height:25
Align:Right Align:Right
Text:Description: Text:Description:
TextField@SERVER_DESC: CncTextField@SERVER_DESC:
Id:SERVER_DESC Id:SERVER_DESC
X:110 X:110
Y:50 Y:50
@@ -79,7 +79,7 @@ Container@CREATESERVER_PANEL:
Height:25 Height:25
Align:Right Align:Right
Text:Password: Text:Password:
TextField@SERVER_PASSWORD: CncTextField@SERVER_PASSWORD:
Id:SERVER_PASSWORD Id:SERVER_PASSWORD
X:110 X:110
Y:85 Y:85
@@ -93,7 +93,7 @@ Container@CREATESERVER_PANEL:
Height:25 Height:25
Align: Right Align: Right
Text:Port: Text:Port:
TextField@LISTEN_PORT: CncTextField@LISTEN_PORT:
Id:LISTEN_PORT Id:LISTEN_PORT
X:110 X:110
Y:120 Y:120
@@ -122,7 +122,7 @@ Container@CREATESERVER_PANEL:
Height:25 Height:25
Align:Right Align:Right
Text:External Port: Text:External Port:
TextField@EXTERNAL_PORT: CncTextField@EXTERNAL_PORT:
Id:EXTERNAL_PORT Id:EXTERNAL_PORT
X:110 X:110
Y:220 Y:220

View File

@@ -25,7 +25,7 @@ Container@DIRECTCONNECT_PANEL:
Height:25 Height:25
Align:Right Align:Right
Text:Address: Text:Address:
TextField@SERVER_ADDRESS: CncTextField@SERVER_ADDRESS:
Id:IP Id:IP
X:150 X:150
Y:15 Y:15
@@ -38,7 +38,7 @@ Container@DIRECTCONNECT_PANEL:
Height:25 Height:25
Align:Right Align:Right
Text:Port: Text:Port:
TextField@PORT: CncTextField@PORT:
Id:PORT Id:PORT
X:150 X:150
Y:50 Y:50

View File

@@ -61,7 +61,7 @@ Container@SERVER_LOBBY:
Height:25 Height:25
Visible:false Visible:false
Children: Children:
TextField@NAME: CncTextField@NAME:
Id:NAME Id:NAME
Text:Name Text:Name
Width:150 Width:150
@@ -309,7 +309,7 @@ Container@SERVER_LOBBY:
Text:Ready Text:Ready
Align:Left Align:Left
Bold:True Bold:True
TextField@CHAT_TEXTFIELD: CncTextField@CHAT_TEXTFIELD:
Id:CHAT_TEXTFIELD Id:CHAT_TEXTFIELD
X:15 X:15
Y:PARENT_BOTTOM - HEIGHT - 15 Y:PARENT_BOTTOM - HEIGHT - 15