Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DebugConsole to work with all keyboards. #640

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 60 additions & 146 deletions Nez.Portable/Debug/Console/DebugConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public DebugConsole()
_sorted = new List<string>();
_functionKeyActions = new Action[12];

#if FNA
TextInputEXT.TextInput += OnKeyTyped;
#else
Core.Instance.Window.TextInput += OnKeyTyped;
#endif

BuildCommandsList();
}

Expand Down Expand Up @@ -158,6 +164,22 @@ public void Log(string str)

#region Updating and Rendering

#if FNA
private void OnKeyTyped(char c)
{
if (IsOpen)
this.HandleKey(Keys.None, c);
}
#else
private void OnKeyTyped(object sender, TextInputEventArgs e)
{
if (IsOpen)
this.HandleKey(e.Key, e.Character);
else if (e.Key == ConsoleKey)
IsOpen = true;
}
#endif

internal void Update()
{
if (IsOpen)
Expand All @@ -169,6 +191,7 @@ internal void Update()

void UpdateClosed()
{
#if FNA
if (!_canOpen)
{
_canOpen = true;
Expand All @@ -177,7 +200,7 @@ void UpdateClosed()
{
IsOpen = true;
}

#endif
for (var i = 0; i < _functionKeyActions.Length; i++)
if (Input.IsKeyPressed((Keys) (Keys.F1 + i)))
ExecuteFunctionKeyAction(i);
Expand All @@ -201,30 +224,46 @@ void UpdateOpen()

while (_repeatCounter >= REPEAT_DELAY)
{
HandleKey(_repeatKey.Value);
HandleKey(_repeatKey.Value, (char)0);
_repeatCounter -= REPEAT_EVERY;
}
}
else
_repeatKey = null;
}

#if FNA
foreach (var key in Input.CurrentKeyboardState.GetPressedKeys())
{
if (Input.IsKeyPressed(key))
{
HandleKey(key);
HandleKey(key, (char)0);
break;
}
}
#else
if (Input.IsKeyPressed(Keys.Up))
HandleKey(Keys.Up, (char) 0);
if (Input.IsKeyPressed(Keys.Down))
HandleKey(Keys.Down, (char)0);
#endif
}


void HandleKey(Keys key)
void HandleKey(Keys key, char typed)
{
if (typed == '\t')
{
#if !FNA
HandleKey(Keys.Tab, (char) 0);
#endif
return;
}

if (key != Keys.Tab && key != Keys.LeftShift && key != Keys.RightShift && key != Keys.RightAlt &&
key != Keys.LeftAlt && key != Keys.RightControl && key != Keys.LeftControl)
_tabIndex = -1;


if (key != ConsoleKey && key != Keys.Oem8 && key != Keys.Enter && _repeatKey != key)
{
Expand All @@ -235,144 +274,15 @@ void HandleKey(Keys key)
switch (key)
{
default:
if (key.ToString().Length == 1)
{
if (InputUtils.IsShiftDown())
_currentText += key.ToString();
else
_currentText += key.ToString().ToLower();
}

break;
if (typed == (char)0)
break;
if((!char.IsWhiteSpace(typed) || typed == ' ') && !char.IsControl(typed))
_currentText += typed;
if (typed == 8 && _currentText?.Length > 0) // Backspace
_currentText = _currentText.Substring(0, _currentText.Length - 1);

case (Keys.D1):
if (InputUtils.IsShiftDown())
_currentText += '!';
else
_currentText += '1';
break;
case (Keys.D2):
if (InputUtils.IsShiftDown())
_currentText += '@';
else
_currentText += '2';
break;
case (Keys.D3):
if (InputUtils.IsShiftDown())
_currentText += '#';
else
_currentText += '3';
break;
case (Keys.D4):
if (InputUtils.IsShiftDown())
_currentText += '$';
else
_currentText += '4';
break;
case (Keys.D5):
if (InputUtils.IsShiftDown())
_currentText += '%';
else
_currentText += '5';
break;
case (Keys.D6):
if (InputUtils.IsShiftDown())
_currentText += '^';
else
_currentText += '6';
break;
case (Keys.D7):
if (InputUtils.IsShiftDown())
_currentText += '&';
else
_currentText += '7';
break;
case (Keys.D8):
if (InputUtils.IsShiftDown())
_currentText += '*';
else
_currentText += '8';
break;
case (Keys.D9):
if (InputUtils.IsShiftDown())
_currentText += '(';
else
_currentText += '9';
break;
case (Keys.D0):
if (InputUtils.IsShiftDown())
_currentText += ')';
else
_currentText += '0';
break;
case (Keys.OemComma):
if (InputUtils.IsShiftDown())
_currentText += '<';
else
_currentText += ',';
break;
case Keys.OemPeriod:
if (InputUtils.IsShiftDown())
_currentText += '>';
else
_currentText += '.';
break;
case Keys.OemQuestion:
if (InputUtils.IsShiftDown())
_currentText += '?';
else
_currentText += '/';
break;
case Keys.OemSemicolon:
if (InputUtils.IsShiftDown())
_currentText += ':';
else
_currentText += ';';
break;
case Keys.OemQuotes:
if (InputUtils.IsShiftDown())
_currentText += '"';
else
_currentText += '\'';
break;
case Keys.OemBackslash:
if (InputUtils.IsShiftDown())
_currentText += '|';
else
_currentText += '\\';
break;
case Keys.OemOpenBrackets:
if (InputUtils.IsShiftDown())
_currentText += '{';
else
_currentText += '[';
break;
case Keys.OemCloseBrackets:
if (InputUtils.IsShiftDown())
_currentText += '}';
else
_currentText += ']';
break;
case Keys.OemMinus:
if (InputUtils.IsShiftDown())
_currentText += '_';
else
_currentText += '-';
break;
case Keys.OemPlus:
if (InputUtils.IsShiftDown())
_currentText += '+';
else
_currentText += '=';
break;

case Keys.Space:
_currentText += " ";
break;
case Keys.Back:
if (_currentText.Length > 0)
_currentText = _currentText.Substring(0, _currentText.Length - 1);
break;
case Keys.Delete:
_currentText = "";
break;
Expand Down Expand Up @@ -459,7 +369,11 @@ void HandleKey(Keys key)

if (key == ConsoleKey)
{
#if FNA
IsOpen = _canOpen = false;
#else
IsOpen = false;
#endif
}
}

Expand Down Expand Up @@ -563,10 +477,10 @@ internal void Render()
Graphics.Instance.Batcher.End();
}

#endregion
#endregion


#region Execute
#region Execute

void ExecuteCommand(string command, string[] args)
{
Expand Down Expand Up @@ -600,10 +514,10 @@ public static void BindCommandToFunctionKey(int functionKey, string command, par
Instance._functionKeyActions[functionKey - 1] = () => Instance.ExecuteCommand(command, args);
}

#endregion
#endregion


#region Parse Commands
#region Parse Commands

void BuildCommandsList()
{
Expand Down Expand Up @@ -777,7 +691,7 @@ struct CommandInfo
}


#region Parsing Arguments
#region Parsing Arguments

static string ArgString(string arg)
{
Expand Down Expand Up @@ -822,8 +736,8 @@ static float ArgFloat(string arg)
}
}

#endregion
#endregion

#endregion
#endregion
}
}