Change Widget key support: Move (unused) Widget.OnKeyPress hook onto ButtonWidget; Buttons will respond to keys with modifiers.
This commit is contained in:
@@ -25,12 +25,14 @@ namespace OpenRA.Widgets
|
|||||||
public Func<string> GetText;
|
public Func<string> GetText;
|
||||||
public Func<bool> IsDisabled = () => false;
|
public Func<bool> IsDisabled = () => false;
|
||||||
public Action OnClick = () => {};
|
public Action OnClick = () => {};
|
||||||
|
public Action<KeyInput> OnKeyPress = _ => {};
|
||||||
|
|
||||||
public ButtonWidget()
|
public ButtonWidget()
|
||||||
: base()
|
: base()
|
||||||
{
|
{
|
||||||
GetText = () => { return Text; };
|
GetText = () => { return Text; };
|
||||||
OnMouseUp = mi => { if (!IsDisabled()) OnClick(); return true; };
|
OnMouseUp = mi => { if (!IsDisabled()) OnClick(); return true; };
|
||||||
|
OnKeyPress = _ => OnClick();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ButtonWidget(ButtonWidget widget)
|
protected ButtonWidget(ButtonWidget widget)
|
||||||
@@ -42,6 +44,7 @@ namespace OpenRA.Widgets
|
|||||||
VisualHeight = widget.VisualHeight;
|
VisualHeight = widget.VisualHeight;
|
||||||
GetText = widget.GetText;
|
GetText = widget.GetText;
|
||||||
OnMouseUp = mi => { if (!IsDisabled()) OnClick(); return true; };
|
OnMouseUp = mi => { if (!IsDisabled()) OnClick(); return true; };
|
||||||
|
OnKeyPress = _ => OnClick();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool LoseFocus(MouseInput mi)
|
public override bool LoseFocus(MouseInput mi)
|
||||||
@@ -52,11 +55,12 @@ namespace OpenRA.Widgets
|
|||||||
|
|
||||||
public override bool HandleKeyPressInner(KeyInput e)
|
public override bool HandleKeyPressInner(KeyInput e)
|
||||||
{
|
{
|
||||||
if (e.KeyName != Key || e.Modifiers != Modifiers.None || e.Event != KeyInputEvent.Down)
|
if (e.KeyName != Key || e.Event != KeyInputEvent.Down)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!IsDisabled())
|
if (!IsDisabled())
|
||||||
OnClick();
|
OnKeyPress(e);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ namespace OpenRA.Widgets
|
|||||||
public Func<MouseInput, bool> OnMouseDown = mi => false;
|
public Func<MouseInput, bool> OnMouseDown = mi => false;
|
||||||
public Func<MouseInput, bool> OnMouseUp = mi => false;
|
public Func<MouseInput, bool> OnMouseUp = mi => false;
|
||||||
public Action<MouseInput> OnMouseMove = mi => { };
|
public Action<MouseInput> OnMouseMove = mi => { };
|
||||||
public Func<KeyInput, bool> OnKeyPress = e => false;
|
|
||||||
|
|
||||||
public Func<bool> IsVisible;
|
public Func<bool> IsVisible;
|
||||||
|
|
||||||
@@ -70,7 +69,6 @@ namespace OpenRA.Widgets
|
|||||||
OnMouseDown = widget.OnMouseDown;
|
OnMouseDown = widget.OnMouseDown;
|
||||||
OnMouseUp = widget.OnMouseUp;
|
OnMouseUp = widget.OnMouseUp;
|
||||||
OnMouseMove = widget.OnMouseMove;
|
OnMouseMove = widget.OnMouseMove;
|
||||||
OnKeyPress = widget.OnKeyPress;
|
|
||||||
|
|
||||||
IsVisible = widget.IsVisible;
|
IsVisible = widget.IsVisible;
|
||||||
|
|
||||||
@@ -252,9 +250,6 @@ namespace OpenRA.Widgets
|
|||||||
// Do any widgety behavior (enter text etc)
|
// Do any widgety behavior (enter text etc)
|
||||||
var handled = HandleKeyPressInner(e);
|
var handled = HandleKeyPressInner(e);
|
||||||
|
|
||||||
// Apply any special logic added by event handlers; they return true if they caught the input
|
|
||||||
if (OnKeyPress(e)) return true;
|
|
||||||
|
|
||||||
return handled;
|
return handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user