public bool AcceptInput(ConsoleKeyInfo keyPressed, GraphicsContext g) { var inputs = InputMap.Compile(); if (Utils.KeyCorresponds(inputs, keyPressed, StandardActionNames.BaseAction)) { ActionOnComponent?.Invoke(this, new ActionEventArgs(this, keyPressed, g)); return(true); } return(false); }
public bool AcceptInput(ConsoleKeyInfo keyPressed, GraphicsContext g) { g.SetCursorVisible(false); var map = InputMap.Compile(); if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.MoveLeftWord)) { // DOING // Move one word to the left, i.e. move to the left until you meet a space (or the beginning of the line) var i = CaretPosition - 1; var skipSpaces = true; // this is so that we jump to the previous word if we are at the beginning of a // word when ctrl <- is invoked while (CaretPosition >= 0 && CaretPosition <= Text.Length) { if (CaretPosition == 0) { UpdateCursorState(g); return(true); } switch (Text[i]) { case ' ': if (!skipSpaces) { UpdateCursorState(g); return(true); } goto default; // Jump to case default default: CaretPosition--; i--; skipSpaces = false; break; } } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.MoveRightWord)) { var skipSpaces = true; while (CaretPosition <= Text.Length) { if (CaretPosition == Text.Length) { UpdateCursorState(g); return(true); } switch (Text[CaretPosition]) { case ' ': if (!skipSpaces) { UpdateCursorState(g); return(true); } CaretPosition++; break; default: CaretPosition++; skipSpaces = false; break; } } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.BaseAction)) { ActionOnComponent?.Invoke(this, new ActionEventArgs(this, keyPressed, g)); UpdateCursorState(g); return(true); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.MoveLeft)) { if (CaretPosition > 0) { CaretPosition -= 1; UpdateCursorState(g); return(true); } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.MoveRight)) { if (CaretPosition < Text.Length) { CaretPosition += 1; UpdateCursorState(g); return(true); } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.DeleteToTheLeftAction)) { if (CaretPosition > 0) { Text = Text.Remove(CaretPosition - 1, 1); CaretPosition -= 1; Print(1, g); OnTextChanged?.Invoke(this, new ActionEventArgs(this, keyPressed, g)); UpdateCursorState(g); return(true); } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.DeleteToTheRightAction)) { if (CaretPosition > 0 && CaretPosition < Text.Length) { Text = Text.Remove(CaretPosition, 1); Print(1, g); OnTextChanged?.Invoke(this, new ActionEventArgs(this, keyPressed, g)); UpdateCursorState(g); return(true); } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.MoveLineStart)) { // TODO // Move the caret to the beginning of the line if (CaretPosition > 0) { CaretPosition = 0; UpdateCursorState(g); return(true); } UpdateCursorState(g); return(false); } if (Utils.KeyCorresponds(map, keyPressed, StandardActionNames.MoveLineEnd)) { // TODO // Move the caret to the end of the line if (CaretPosition < Text.Length) { CaretPosition = Text.Length; UpdateCursorState(g); return(true); } UpdateCursorState(g); return(false); } // Letters: add the letters to Text and write them with the following line. Also increase CaretPosition. // g.Write(X + CaretPosition, Y, <the letter>); // Backspace: delete the letter, careposition - 1, make sure to rewrite the string correctly (or call Print // if you're lazy) if (keyPressed.KeyChar > (char)31 && keyPressed.Key != ConsoleKey.Enter && keyPressed.Key != ConsoleKey.Tab && keyPressed.Key != ConsoleKey.Escape && // NOT(control pressed XOR alt pressed) (keyPressed.Modifiers & ConsoleModifiers.Control) == 0 == ((keyPressed.Modifiers & ConsoleModifiers.Alt) == 0)) { Text = Text.Insert(CaretPosition, "" + keyPressed.KeyChar); CaretPosition += 1; Print(PlaceholderText.Length - 1, g); OnTextChanged?.Invoke(this, new ActionEventArgs(this, keyPressed, g)); UpdateCursorState(g); return(true); } // USE .KeyChar AND NOT .Key.ToString() !!! // If it matched one of the cases above, return true // Anything else: return false UpdateCursorState(g); return(false); }
public virtual bool AcceptInput(ConsoleKeyInfo keyPressed, GraphicsContext g) { foreach (var c in Components) { if (c is IFocusable foc && foc.IsFocusable() && foc.IsFocused()) { if (foc.AcceptInput(keyPressed, g)) { return(true); } var ins = c.InputMap.Compile(); var str = Utils.GetActionNameForKey(ins, keyPressed); if (str != null) { var actMap = c.ActionMap.Compile(); if (actMap.ContainsKey(str)) { actMap[str](c, new ActionEventArgs(c, keyPressed, g)); return(true); } } } } // If we are still here it means that none of the focusable components have accepted any input // Use our own shortcuts to switch to the next focusable component var prev = GetFocusedElement(false); var inputs = InputMap.Compile(); IFocusable next = null; if (Utils.KeyCorresponds(inputs, keyPressed, StandardActionNames.MoveUp)) { next = Strategy.GetUpFocusableElement(this, prev); } else if (Utils.KeyCorresponds(inputs, keyPressed, StandardActionNames.MoveLeft)) { next = Strategy.GetPreviousFocusableElement(this, prev); } else if (Utils.KeyCorresponds(inputs, keyPressed, StandardActionNames.MoveRight)) { next = Strategy.GetNextFocusableElement(this, prev); } else if (Utils.KeyCorresponds(inputs, keyPressed, StandardActionNames.MoveDown)) { next = Strategy.GetDownFocusableElement(this, prev); } if (next != null) { if (prev != null) { var f = prev as IFocusable; Debug.Assert(f != null, nameof(f) + " != null"); f.SetFocused(false, g); } next.SetFocused(true, g); return(true); } return(false); }