Merge pull request #9582 from ChaoticMind/ctrl_backspace
Implement some keyboard hotkeys for chat (TextFieldWidget)
This commit is contained in:
@@ -61,6 +61,7 @@ namespace OpenRA
|
|||||||
Bitmap TakeScreenshot();
|
Bitmap TakeScreenshot();
|
||||||
void PumpInput(IInputHandler inputHandler);
|
void PumpInput(IInputHandler inputHandler);
|
||||||
string GetClipboardText();
|
string GetClipboardText();
|
||||||
|
bool SetClipboardText(string text);
|
||||||
void DrawPrimitives(PrimitiveType type, int firstVertex, int numVertices);
|
void DrawPrimitives(PrimitiveType type, int firstVertex, int numVertices);
|
||||||
|
|
||||||
void SetLineWidth(float width);
|
void SetLineWidth(float width);
|
||||||
|
|||||||
@@ -267,5 +267,10 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
return Device.GetClipboardText();
|
return Device.GetClipboardText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool SetClipboardText(string text)
|
||||||
|
{
|
||||||
|
return Device.SetClipboardText(text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,12 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
return base.YieldKeyboardFocus();
|
return base.YieldKeyboardFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void ResetBlinkCycle()
|
||||||
|
{
|
||||||
|
blinkCycle = 10;
|
||||||
|
showCursor = true;
|
||||||
|
}
|
||||||
|
|
||||||
public override bool HandleMouseInput(MouseInput mi)
|
public override bool HandleMouseInput(MouseInput mi)
|
||||||
{
|
{
|
||||||
if (IsDisabled())
|
if (IsDisabled())
|
||||||
@@ -81,15 +87,14 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
if (!RenderBounds.Contains(mi.Location) || !TakeKeyboardFocus())
|
if (!RenderBounds.Contains(mi.Location) || !TakeKeyboardFocus())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
blinkCycle = 10;
|
ResetBlinkCycle();
|
||||||
showCursor = true;
|
|
||||||
CursorPosition = ClosestCursorPosition(mi.Location.X);
|
CursorPosition = ClosestCursorPosition(mi.Location.X);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual string GetApparentText() { return text; }
|
protected virtual string GetApparentText() { return text; }
|
||||||
|
|
||||||
public int ClosestCursorPosition(int x)
|
int ClosestCursorPosition(int x)
|
||||||
{
|
{
|
||||||
var apparentText = GetApparentText();
|
var apparentText = GetApparentText();
|
||||||
var font = Game.Renderer.Fonts[Font];
|
var font = Game.Renderer.Fonts[Font];
|
||||||
@@ -113,6 +118,23 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
return minIndex;
|
return minIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetPrevWhitespaceIndex()
|
||||||
|
{
|
||||||
|
return Text.Substring(0, CursorPosition).TrimEnd().LastIndexOf(' ') + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetNextWhitespaceIndex()
|
||||||
|
{
|
||||||
|
var substr = Text.Substring(CursorPosition);
|
||||||
|
var substrTrimmed = substr.TrimStart();
|
||||||
|
var trimmedSpaces = substr.Length - substrTrimmed.Length;
|
||||||
|
var nextWhitespace = substrTrimmed.IndexOf(' ');
|
||||||
|
if (nextWhitespace == -1)
|
||||||
|
return Text.Length;
|
||||||
|
|
||||||
|
return CursorPosition + trimmedSpaces + nextWhitespace;
|
||||||
|
}
|
||||||
|
|
||||||
public override bool HandleKeyPress(KeyInput e)
|
public override bool HandleKeyPress(KeyInput e)
|
||||||
{
|
{
|
||||||
if (IsDisabled() || e.Event == KeyInputEvent.Up)
|
if (IsDisabled() || e.Event == KeyInputEvent.Up)
|
||||||
@@ -122,68 +144,160 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
if (!HasKeyboardFocus)
|
if (!HasKeyboardFocus)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ((e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER) && OnEnterKey())
|
var isOSX = Platform.CurrentPlatform == PlatformType.OSX;
|
||||||
return true;
|
|
||||||
|
|
||||||
if (e.Key == Keycode.TAB && OnTabKey())
|
switch (e.Key) {
|
||||||
|
case Keycode.RETURN:
|
||||||
|
case Keycode.KP_ENTER:
|
||||||
|
if (OnEnterKey())
|
||||||
return true;
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
if (e.Key == Keycode.ESCAPE && OnEscKey())
|
case Keycode.TAB:
|
||||||
|
if (OnTabKey())
|
||||||
return true;
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
if (e.Key == Keycode.LALT && OnAltKey())
|
case Keycode.ESCAPE:
|
||||||
|
if (OnEscKey())
|
||||||
return true;
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
if (e.Key == Keycode.LEFT)
|
case Keycode.LALT:
|
||||||
{
|
if (OnAltKey())
|
||||||
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.LEFT:
|
||||||
|
ResetBlinkCycle();
|
||||||
if (CursorPosition > 0)
|
if (CursorPosition > 0)
|
||||||
CursorPosition--;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.RIGHT)
|
|
||||||
{
|
|
||||||
if (CursorPosition <= Text.Length - 1)
|
|
||||||
CursorPosition++;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.HOME)
|
|
||||||
{
|
{
|
||||||
|
if ((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
|
CursorPosition = GetPrevWhitespaceIndex();
|
||||||
|
else if (isOSX && e.Modifiers.HasModifier(Modifiers.Meta))
|
||||||
CursorPosition = 0;
|
CursorPosition = 0;
|
||||||
return true;
|
else
|
||||||
|
CursorPosition--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.Key == Keycode.END)
|
break;
|
||||||
|
|
||||||
|
case Keycode.RIGHT:
|
||||||
|
ResetBlinkCycle();
|
||||||
|
if (CursorPosition <= Text.Length - 1)
|
||||||
{
|
{
|
||||||
|
if ((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
|
CursorPosition = GetNextWhitespaceIndex();
|
||||||
|
else if (isOSX && e.Modifiers.HasModifier(Modifiers.Meta))
|
||||||
CursorPosition = Text.Length;
|
CursorPosition = Text.Length;
|
||||||
return true;
|
else
|
||||||
|
CursorPosition++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.Key == Keycode.DELETE)
|
break;
|
||||||
|
|
||||||
|
case Keycode.HOME:
|
||||||
|
ResetBlinkCycle();
|
||||||
|
CursorPosition = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.END:
|
||||||
|
ResetBlinkCycle();
|
||||||
|
CursorPosition = Text.Length;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.D:
|
||||||
|
if (e.Modifiers.HasModifier(Modifiers.Ctrl) && CursorPosition < Text.Length)
|
||||||
{
|
{
|
||||||
|
Text = Text.Remove(CursorPosition, 1);
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.K:
|
||||||
|
// ctrl+k is equivalent to cmd+delete on osx (but also works on osx)
|
||||||
|
ResetBlinkCycle();
|
||||||
|
if (e.Modifiers.HasModifier(Modifiers.Ctrl) && CursorPosition < Text.Length)
|
||||||
|
{
|
||||||
|
Text = Text.Remove(CursorPosition);
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.U:
|
||||||
|
// ctrl+u is equivalent to cmd+backspace on osx
|
||||||
|
ResetBlinkCycle();
|
||||||
|
if (!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl) && CursorPosition > 0)
|
||||||
|
{
|
||||||
|
Text = Text.Substring(CursorPosition);
|
||||||
|
CursorPosition = 0;
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.X:
|
||||||
|
ResetBlinkCycle();
|
||||||
|
if (((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Meta))) &&
|
||||||
|
!string.IsNullOrEmpty(Text))
|
||||||
|
{
|
||||||
|
Game.Renderer.SetClipboardText(Text);
|
||||||
|
Text = Text.Remove(0);
|
||||||
|
CursorPosition = 0;
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.DELETE:
|
||||||
|
// cmd+delete is equivalent to ctrl+k on non-osx
|
||||||
|
ResetBlinkCycle();
|
||||||
if (CursorPosition < Text.Length)
|
if (CursorPosition < Text.Length)
|
||||||
{
|
{
|
||||||
|
if ((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
|
Text = Text.Substring(0, CursorPosition) + Text.Substring(GetNextWhitespaceIndex());
|
||||||
|
else if (isOSX && e.Modifiers.HasModifier(Modifiers.Meta))
|
||||||
|
Text = Text.Remove(CursorPosition);
|
||||||
|
else
|
||||||
Text = Text.Remove(CursorPosition, 1);
|
Text = Text.Remove(CursorPosition, 1);
|
||||||
|
|
||||||
OnTextEdited();
|
OnTextEdited();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.BACKSPACE && CursorPosition > 0)
|
case Keycode.BACKSPACE:
|
||||||
|
// cmd+backspace is equivalent to ctrl+u on non-osx
|
||||||
|
ResetBlinkCycle();
|
||||||
|
if (CursorPosition > 0)
|
||||||
|
{
|
||||||
|
if ((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
|
{
|
||||||
|
var prevWhitespace = GetPrevWhitespaceIndex();
|
||||||
|
Text = Text.Substring(0, prevWhitespace) + Text.Substring(CursorPosition);
|
||||||
|
CursorPosition = prevWhitespace;
|
||||||
|
}
|
||||||
|
else if (isOSX && e.Modifiers.HasModifier(Modifiers.Meta))
|
||||||
|
{
|
||||||
|
Text = Text.Substring(CursorPosition);
|
||||||
|
CursorPosition = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
CursorPosition--;
|
CursorPosition--;
|
||||||
Text = Text.Remove(CursorPosition, 1);
|
Text = Text.Remove(CursorPosition, 1);
|
||||||
OnTextEdited();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.Key == Keycode.V &&
|
OnTextEdited();
|
||||||
((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
}
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Meta))))
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.V:
|
||||||
|
ResetBlinkCycle();
|
||||||
|
if ((!isOSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) || (isOSX && e.Modifiers.HasModifier(Modifiers.Meta)))
|
||||||
{
|
{
|
||||||
var clipboardText = Game.Renderer.GetClipboardText();
|
var clipboardText = Game.Renderer.GetClipboardText();
|
||||||
|
|
||||||
@@ -195,8 +309,12 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
clipboardText = clipboardText.Trim();
|
clipboardText = clipboardText.Trim();
|
||||||
if (clipboardText.Length > 0)
|
if (clipboardText.Length > 0)
|
||||||
HandleTextInput(clipboardText);
|
HandleTextInput(clipboardText);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -382,6 +382,12 @@ namespace OpenRA.Platforms.Default
|
|||||||
return input.GetClipboardText();
|
return input.GetClipboardText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool SetClipboardText(string text)
|
||||||
|
{
|
||||||
|
VerifyThreadAffinity();
|
||||||
|
return input.SetClipboardText(text);
|
||||||
|
}
|
||||||
|
|
||||||
public IVertexBuffer<Vertex> CreateVertexBuffer(int size)
|
public IVertexBuffer<Vertex> CreateVertexBuffer(int size)
|
||||||
{
|
{
|
||||||
VerifyThreadAffinity();
|
VerifyThreadAffinity();
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ namespace OpenRA.Platforms.Default
|
|||||||
MouseButton lastButtonBits = (MouseButton)0;
|
MouseButton lastButtonBits = (MouseButton)0;
|
||||||
|
|
||||||
public string GetClipboardText() { return SDL.SDL_GetClipboardText(); }
|
public string GetClipboardText() { return SDL.SDL_GetClipboardText(); }
|
||||||
|
public bool SetClipboardText(string text) { return SDL.SDL_SetClipboardText(text) == 0; }
|
||||||
|
|
||||||
static MouseButton MakeButton(byte b)
|
static MouseButton MakeButton(byte b)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ namespace OpenRA.Platforms.Null
|
|||||||
public Bitmap TakeScreenshot() { return new Bitmap(1, 1); }
|
public Bitmap TakeScreenshot() { return new Bitmap(1, 1); }
|
||||||
|
|
||||||
public string GetClipboardText() { return ""; }
|
public string GetClipboardText() { return ""; }
|
||||||
|
public bool SetClipboardText(string text) { return false; }
|
||||||
public void PumpInput(IInputHandler ih)
|
public void PumpInput(IInputHandler ih)
|
||||||
{
|
{
|
||||||
Game.HasInputFocus = false;
|
Game.HasInputFocus = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user