Beginnings of textfield widget

This commit is contained in:
Paul Chote
2010-07-10 16:47:00 +12:00
parent 19f74e1d3e
commit faf5e7bbf1
6 changed files with 146 additions and 3 deletions

View File

@@ -61,6 +61,7 @@ namespace OpenRA
public static Widget rootWidget = null;
public static Widget selectedWidget;
public static Widget keyboardFocusWidget;
public void Tick(World world)
{
@@ -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)

View File

@@ -494,6 +494,9 @@ namespace OpenRA
{
int sync = world.SyncHash();
if (chrome.HandleKeyPress(e, modifiers))
return;
if (e.KeyChar == '\r')
{
chat.Toggle();

View File

@@ -225,6 +225,7 @@
<Compile Include="Traits\SharesCell.cs" />
<Compile Include="Traits\World\AircraftInfluence.cs" />
<Compile Include="Traits\World\HazardLayer.cs" />
<Compile Include="Widgets\TextFieldWidget.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
}
}

View File

@@ -51,6 +51,8 @@ namespace OpenRA.Widgets
public Func<MouseInput,bool> OnMouseDown = mi => {return false;};
public Func<MouseInput,bool> OnMouseUp = mi => {return false;};
public Func<MouseInput,bool> OnMouseMove = mi => {return false;};
public Func<System.Windows.Forms.KeyPressEventArgs, Modifiers,bool> OnKeyPress = (e,modifiers) => {return false;};
public Func<bool> 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)

View File

@@ -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