diff --git a/OpenRA.Mods.Cnc/Widgets/CncMenuButton.cs b/OpenRA.Mods.Cnc/Widgets/CncMenuButton.cs index f656c08cf1..91829bb3f4 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncMenuButton.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncMenuButton.cs @@ -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 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); } } } diff --git a/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs index 30ec8fbc1d..c05051802d 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs @@ -50,7 +50,10 @@ namespace OpenRA.Mods.Cnc.Widgets panel.GetWidget("SERVER_NAME").Text = settings.Server.Name ?? ""; panel.GetWidget("LISTEN_PORT").Text = settings.Server.ListenPort.ToString(); - panel.GetWidget("EXTERNAL_PORT").Text = settings.Server.ExternalPort.ToString(); + + var externalPort = panel.GetWidget("EXTERNAL_PORT"); + externalPort.Text = settings.Server.ExternalPort.ToString(); + externalPort.IsDisabled = () => !advertiseOnline; var advertiseCheckbox = panel.GetWidget("ADVERTISE_CHECKBOX"); advertiseCheckbox.IsChecked = () => advertiseOnline; diff --git a/mods/cnc/chrome/createserver.yaml b/mods/cnc/chrome/createserver.yaml index 847b57f53b..61589cfed5 100644 --- a/mods/cnc/chrome/createserver.yaml +++ b/mods/cnc/chrome/createserver.yaml @@ -49,7 +49,7 @@ Container@CREATESERVER_PANEL: Height:25 Align:Right Text:Server Name: - TextField@SERVER_NAME: + CncTextField@SERVER_NAME: Id:SERVER_NAME X:110 Y:15 @@ -64,7 +64,7 @@ Container@CREATESERVER_PANEL: Height:25 Align:Right Text:Description: - TextField@SERVER_DESC: + CncTextField@SERVER_DESC: Id:SERVER_DESC X:110 Y:50 @@ -79,7 +79,7 @@ Container@CREATESERVER_PANEL: Height:25 Align:Right Text:Password: - TextField@SERVER_PASSWORD: + CncTextField@SERVER_PASSWORD: Id:SERVER_PASSWORD X:110 Y:85 @@ -93,7 +93,7 @@ Container@CREATESERVER_PANEL: Height:25 Align: Right Text:Port: - TextField@LISTEN_PORT: + CncTextField@LISTEN_PORT: Id:LISTEN_PORT X:110 Y:120 @@ -122,7 +122,7 @@ Container@CREATESERVER_PANEL: Height:25 Align:Right Text:External Port: - TextField@EXTERNAL_PORT: + CncTextField@EXTERNAL_PORT: Id:EXTERNAL_PORT X:110 Y:220 diff --git a/mods/cnc/chrome/directconnect.yaml b/mods/cnc/chrome/directconnect.yaml index 7b61739f1d..088a57f401 100644 --- a/mods/cnc/chrome/directconnect.yaml +++ b/mods/cnc/chrome/directconnect.yaml @@ -25,7 +25,7 @@ Container@DIRECTCONNECT_PANEL: Height:25 Align:Right Text:Address: - TextField@SERVER_ADDRESS: + CncTextField@SERVER_ADDRESS: Id:IP X:150 Y:15 @@ -38,7 +38,7 @@ Container@DIRECTCONNECT_PANEL: Height:25 Align:Right Text:Port: - TextField@PORT: + CncTextField@PORT: Id:PORT X:150 Y:50 diff --git a/mods/cnc/chrome/lobby.yaml b/mods/cnc/chrome/lobby.yaml index 89aaddd8f1..1cd291e810 100644 --- a/mods/cnc/chrome/lobby.yaml +++ b/mods/cnc/chrome/lobby.yaml @@ -61,7 +61,7 @@ Container@SERVER_LOBBY: Height:25 Visible:false Children: - TextField@NAME: + CncTextField@NAME: Id:NAME Text:Name Width:150 @@ -309,7 +309,7 @@ Container@SERVER_LOBBY: Text:Ready Align:Left Bold:True - TextField@CHAT_TEXTFIELD: + CncTextField@CHAT_TEXTFIELD: Id:CHAT_TEXTFIELD X:15 Y:PARENT_BOTTOM - HEIGHT - 15