Refactor TextFieldWidget into much clearer and extensible code
This commit is contained in:
@@ -115,18 +115,6 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
if (!HasKeyboardFocus)
|
if (!HasKeyboardFocus)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ((e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER) && OnEnterKey())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (e.Key == Keycode.TAB && OnTabKey())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (e.Key == Keycode.ESCAPE && OnEscKey())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (e.Key == Keycode.LALT && OnAltKey())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
Func<int> getPrevWhitespaceIndex = () =>
|
Func<int> getPrevWhitespaceIndex = () =>
|
||||||
Text.Substring(0, CursorPosition).TrimEnd().LastIndexOf(' ') + 1;
|
Text.Substring(0, CursorPosition).TrimEnd().LastIndexOf(' ') + 1;
|
||||||
|
|
||||||
@@ -141,157 +129,156 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
return CursorPosition + trimmed_spaces + next_whitespace;
|
return CursorPosition + trimmed_spaces + next_whitespace;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (e.Key == Keycode.LEFT)
|
Func<bool> isOSX = () => Platform.CurrentPlatform == PlatformType.OSX;
|
||||||
{
|
|
||||||
if (CursorPosition > 0)
|
|
||||||
{
|
|
||||||
if ((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
|
||||||
{
|
|
||||||
CursorPosition = getPrevWhitespaceIndex();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CursorPosition--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
switch (e.Key) {
|
||||||
}
|
case Keycode.RETURN:
|
||||||
|
case Keycode.KP_ENTER:
|
||||||
|
if (OnEnterKey())
|
||||||
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
if (e.Key == Keycode.RIGHT)
|
case Keycode.TAB:
|
||||||
{
|
if (OnTabKey())
|
||||||
if (CursorPosition <= Text.Length - 1) {
|
return true;
|
||||||
if ((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
break;
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
|
||||||
{
|
|
||||||
CursorPosition = getNextWhitespaceIndex();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CursorPosition++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
case Keycode.ESCAPE:
|
||||||
}
|
if (OnEscKey())
|
||||||
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
if (e.Key == Keycode.HOME)
|
case Keycode.LALT:
|
||||||
{
|
if (OnAltKey())
|
||||||
CursorPosition = 0;
|
return true;
|
||||||
return true;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.END)
|
case Keycode.LEFT:
|
||||||
{
|
if (CursorPosition > 0)
|
||||||
CursorPosition = Text.Length;
|
if ((!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
||||||
return true;
|
(isOSX() && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
}
|
CursorPosition = getPrevWhitespaceIndex();
|
||||||
|
else
|
||||||
|
CursorPosition--;
|
||||||
|
break;
|
||||||
|
|
||||||
if (e.Key == Keycode.K && e.Modifiers.HasModifier(Modifiers.Ctrl) &&
|
case Keycode.RIGHT:
|
||||||
(Platform.CurrentPlatform != PlatformType.OSX))
|
if (CursorPosition <= Text.Length - 1)
|
||||||
{
|
if ((!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
||||||
// equivalent to cmd+delete on osx
|
(isOSX() && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
if (CursorPosition < Text.Length)
|
CursorPosition = getNextWhitespaceIndex();
|
||||||
{
|
else
|
||||||
Text = Text.Remove(CursorPosition);
|
CursorPosition++;
|
||||||
OnTextEdited();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.U && e.Modifiers.HasModifier(Modifiers.Ctrl) &&
|
case Keycode.HOME:
|
||||||
(Platform.CurrentPlatform != PlatformType.OSX))
|
|
||||||
{
|
|
||||||
// equivalent to cmd+backspace on osx
|
|
||||||
Text = Text.Substring(CursorPosition);
|
|
||||||
CursorPosition = 0;
|
|
||||||
OnTextEdited();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.X &&
|
|
||||||
((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Meta))))
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(Text))
|
|
||||||
{
|
|
||||||
Text = Text.Remove(0);
|
|
||||||
CursorPosition = 0;
|
CursorPosition = 0;
|
||||||
OnTextEdited();
|
break;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.DELETE)
|
case Keycode.END:
|
||||||
{
|
CursorPosition = Text.Length;
|
||||||
if (CursorPosition < Text.Length)
|
break;
|
||||||
{
|
|
||||||
if ((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
case Keycode.K:
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
// ctrl+k is equivalent to cmd+delete on osx
|
||||||
|
if (!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl) && CursorPosition < Text.Length)
|
||||||
{
|
{
|
||||||
Text = Text.Substring(0, CursorPosition) + Text.Substring(getNextWhitespaceIndex());
|
|
||||||
}
|
|
||||||
else if (Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Meta))
|
|
||||||
{
|
|
||||||
// equivalent to ctrl+k on non-osx
|
|
||||||
Text = Text.Remove(CursorPosition);
|
Text = Text.Remove(CursorPosition);
|
||||||
|
OnTextEdited();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.U:
|
||||||
|
// ctrl+u is equivalent to cmd+backspace on osx
|
||||||
|
if (!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl) && CursorPosition > 0)
|
||||||
{
|
{
|
||||||
Text = Text.Remove(CursorPosition, 1);
|
Text = Text.Substring(CursorPosition);
|
||||||
|
CursorPosition = 0;
|
||||||
|
OnTextEdited();
|
||||||
}
|
}
|
||||||
|
|
||||||
OnTextEdited();
|
break;
|
||||||
|
|
||||||
|
case Keycode.X:
|
||||||
|
if (((!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
||||||
|
(isOSX() && e.Modifiers.HasModifier(Modifiers.Meta))) &&
|
||||||
|
(!string.IsNullOrEmpty(Text)))
|
||||||
|
{
|
||||||
|
Text = Text.Remove(0);
|
||||||
|
CursorPosition = 0;
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.DELETE:
|
||||||
|
// cmd+delete is equivalent to ctrl+k on non-osx
|
||||||
|
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);
|
||||||
|
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.BACKSPACE:
|
||||||
|
// cmd+backspace is equivalent to ctrl+u on non-osx
|
||||||
|
if (CursorPosition > 0)
|
||||||
|
{
|
||||||
|
if ((!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
||||||
|
(isOSX() && e.Modifiers.HasModifier(Modifiers.Alt)))
|
||||||
|
{
|
||||||
|
var prev_whitespace = getPrevWhitespaceIndex();
|
||||||
|
Text = Text.Substring(0, prev_whitespace) + Text.Substring(CursorPosition);
|
||||||
|
CursorPosition = prev_whitespace;
|
||||||
|
}
|
||||||
|
else if (isOSX() && e.Modifiers.HasModifier(Modifiers.Meta))
|
||||||
|
{
|
||||||
|
Text = Text.Substring(CursorPosition);
|
||||||
|
CursorPosition = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CursorPosition--;
|
||||||
|
Text = Text.Remove(CursorPosition, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
OnTextEdited();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Keycode.V:
|
||||||
|
if ((!isOSX() && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
||||||
|
(isOSX() && e.Modifiers.HasModifier(Modifiers.Meta)))
|
||||||
|
{
|
||||||
|
var clipboardText = Game.Renderer.GetClipboardText();
|
||||||
|
|
||||||
|
// Take only the first line of the clipboard contents
|
||||||
|
var nl = clipboardText.IndexOf('\n');
|
||||||
|
if (nl > 0)
|
||||||
|
clipboardText = clipboardText.Substring(0, nl);
|
||||||
|
|
||||||
|
clipboardText = clipboardText.Trim();
|
||||||
|
if (clipboardText.Length > 0)
|
||||||
|
HandleTextInput(clipboardText);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.BACKSPACE && CursorPosition > 0)
|
|
||||||
{
|
|
||||||
if ((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Alt)))
|
|
||||||
{
|
|
||||||
var prev_whitespace = getPrevWhitespaceIndex();
|
|
||||||
Text = Text.Substring(0, prev_whitespace) + Text.Substring(CursorPosition);
|
|
||||||
CursorPosition = prev_whitespace;
|
|
||||||
}
|
|
||||||
else if (Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Meta))
|
|
||||||
{
|
|
||||||
// equivalent to ctrl+u on non-osx
|
|
||||||
Text = Text.Substring(CursorPosition);
|
|
||||||
CursorPosition = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CursorPosition--;
|
|
||||||
Text = Text.Remove(CursorPosition, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
OnTextEdited();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Key == Keycode.V &&
|
|
||||||
((Platform.CurrentPlatform != PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Ctrl)) ||
|
|
||||||
(Platform.CurrentPlatform == PlatformType.OSX && e.Modifiers.HasModifier(Modifiers.Meta))))
|
|
||||||
{
|
|
||||||
var clipboardText = Game.Renderer.GetClipboardText();
|
|
||||||
|
|
||||||
// Take only the first line of the clipboard contents
|
|
||||||
var nl = clipboardText.IndexOf('\n');
|
|
||||||
if (nl > 0)
|
|
||||||
clipboardText = clipboardText.Substring(0, nl);
|
|
||||||
|
|
||||||
clipboardText = clipboardText.Trim();
|
|
||||||
if (clipboardText.Length > 0)
|
|
||||||
HandleTextInput(clipboardText);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user