diff --git a/OpenRA.Game/Chrome.cs b/OpenRA.Game/Chrome.cs index b6a5f370e8..f9e9517ab4 100644 --- a/OpenRA.Game/Chrome.cs +++ b/OpenRA.Game/Chrome.cs @@ -61,7 +61,8 @@ namespace OpenRA public static Widget rootWidget = null; public static Widget selectedWidget; - + public static Widget keyboardFocusWidget; + public void Tick(World world) { if (!world.GameHasStarted) return; @@ -225,7 +226,7 @@ namespace OpenRA { if (selectedWidget != null) return selectedWidget.HandleInput(mi); - + if (rootWidget.HandleInput(mi)) return true; @@ -247,6 +248,17 @@ namespace OpenRA return true; } + + public bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) + { + if (keyboardFocusWidget != null) + return keyboardFocusWidget.HandleKeyPress(e, modifiers); + + if (rootWidget.HandleKeyPress(e, modifiers)) + return true; + return false; + } + public bool HitTest(int2 mousePos) { if (selectedWidget != null) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 741c5dbe20..4cbd868230 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -492,7 +492,10 @@ namespace OpenRA public static void HandleKeyPress(KeyPressEventArgs e, Modifiers modifiers) { - int sync = world.SyncHash(); + int sync = world.SyncHash(); + + if (chrome.HandleKeyPress(e, modifiers)) + return; if (e.KeyChar == '\r') { diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 1b7466b8f1..6568d9cb6f 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -225,6 +225,7 @@ + diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs new file mode 100644 index 0000000000..01dc2cd14c --- /dev/null +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -0,0 +1,98 @@ +#region Copyright & License Information +/* + * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. + * This file is part of OpenRA. + * + * OpenRA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * OpenRA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OpenRA. If not, see . + */ +#endregion + +using System.Drawing; +using System; + +namespace OpenRA.Widgets +{ + class TextFieldWidget : Widget + { + string TextBuffer = "zomg text"; + + public TextFieldWidget() + : base() + { + } + + public TextFieldWidget(Widget widget) + :base(widget) + { + TextBuffer = (widget as TextFieldWidget).TextBuffer; + } + + public override bool HandleInput(MouseInput mi) + { + // Are we able to handle this event? + if (!Visible || !GetEventBounds().Contains(mi.Location.X,mi.Location.Y)) + return base.HandleInput(mi); + + if (base.HandleInput(mi)) + return true; + + if (mi.Event == MouseInputEvent.Down) + { + Chrome.keyboardFocusWidget = this; + return true; + } + + return false; + } + + public override bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) + { + if (base.HandleKeyPress(e,modifiers)) + return true; + + TypeChar(e.KeyChar); + return true; + } + + public void TypeChar(char c) + { + if (c == '\b' || c == 0x7f) + { + if (TextBuffer.Length > 0) + TextBuffer = TextBuffer.Remove(TextBuffer.Length - 1); + } + else if (!char.IsControl(c)) + TextBuffer += c; + } + + public override void DrawInner(World world) + { + var pos = DrawPosition(); + int margin = 10; + WidgetUtils.DrawPanel("dialog3", + new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height ) ); + + var text = TextBuffer; + Game.chrome.renderer.BoldFont.DrawText(text, + new int2( pos.X + margin, pos.Y + Bounds.Height / 2) + - new int2(0, Game.chrome.renderer.BoldFont.Measure(text).Y / 2), + Color.White); + } + + public override Widget Clone() + { + return new TextFieldWidget(this); + } + } +} \ No newline at end of file diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index 0d36e74cfa..121ad3f98e 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -51,6 +51,8 @@ namespace OpenRA.Widgets public Func OnMouseDown = mi => {return false;}; public Func OnMouseUp = mi => {return false;}; public Func OnMouseMove = mi => {return false;}; + public Func OnKeyPress = (e,modifiers) => {return false;}; + public Func IsVisible; public Widget() { IsVisible = () => Visible; } @@ -72,6 +74,8 @@ namespace OpenRA.Widgets OnMouseDown = widget.OnMouseDown; OnMouseUp = widget.OnMouseUp; OnMouseMove = widget.OnMouseMove; + OnKeyPress = widget.OnKeyPress; + IsVisible = widget.IsVisible; foreach(var child in widget.Children) @@ -169,6 +173,17 @@ namespace OpenRA.Widgets throw new InvalidOperationException("Impossible"); } + public virtual bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) + { + // Can any of our children handle this? + foreach (var child in Children) + if (child.HandleKeyPress(e, modifiers)) + return true; + + // Have we been assigned an action? + return OnKeyPress(e,modifiers); + } + public abstract void DrawInner( World world ); public void Draw(World world) diff --git a/mods/cnc/menus.yaml b/mods/cnc/menus.yaml index 6627ae0ba4..0db4c7ccea 100644 --- a/mods/cnc/menus.yaml +++ b/mods/cnc/menus.yaml @@ -491,6 +491,20 @@ Container: Width:120 Height:25 Text:Change Map + TextField@CHAT_TEXTFIELD: + Id:CHAT_TEXTFIELD + Visible:true + X:PARENT_RIGHT-550 + Y:300 + Width:300 + Height:25 + TextField@CHAT_TEXTFIELD2: + Id:CHAT_TEXTFIELD2 + Visible:true + X:PARENT_RIGHT-550 + Y:250 + Width:300 + Height:25 Button@DISCONNECT_BUTTON: Id:DISCONNECT_BUTTON Visible:true