public override void OnInputKey(ConsoleKeyInfo key) { if (key.Key == ConsoleKey.F1) { ConsoleManager.CreateWindow <IAnimalList>(); } else if (key.Key == ConsoleKey.F2) { var killedAnimalResult = _zooManager.AdvanceWeek(); var message = killedAnimalResult != null ? $"Week advanced, a poor {killedAnimalResult.Target.Species} named {killedAnimalResult.Target.Name} was killed by {killedAnimalResult.Killer.Name}!" : "Week advanced, no animals were harmed!"; ConsoleManager.CreateWindow <IMessage>() .SetText(message); } else if (key.Key == ConsoleKey.F3) { ConsoleManager.CreateWindow <IShowAnimalRequirements>(); } else if (key.Key == ConsoleKey.F4) { ConsoleManager.CreateWindow <IWipeDatabase>(); } }
public override void OnInputKey(ConsoleKeyInfo key) { if (key.Key == ConsoleKey.Escape) { Close(); return; } if (key.Key == ConsoleKey.UpArrow) { if (_selectedIndex > 0) { _selectedIndex--; Invalidate(); } } else if (key.Key == ConsoleKey.DownArrow) { if (_selectedIndex + 1 < _database.Animals.Count()) { _selectedIndex++; Invalidate(); } } else if (key.Key == ConsoleKey.A) { ConsoleManager.CreateWindow <IAddAnimal>(); } }
public override void OnInputKey(ConsoleKeyInfo key) { if (key.Key == ConsoleKey.Escape) { Close(); return; } if (key.Key == ConsoleKey.Tab) { // select next "component" ++_focusedComponentIndex; if (_focusedComponentIndex >= _components.Count) { _focusedComponentIndex = 0; } Invalidate(); return; } if (key.Key == ConsoleKey.Backspace) { var component = _components[_focusedComponentIndex]; if (component.Value.Length > 0) { component.Value.Remove(component.Value.Length - 1, 1); Invalidate(); } return; } if (key.Key == ConsoleKey.Enter) { if (_speciesComponent.Value.Length == 0) { ConsoleManager.CreateWindow <IMessage>().SetText("Species must be set."); return; } if (_nameComponent.Value.Length == 0) { ConsoleManager.CreateWindow <IMessage>().SetText("Name must be set."); return; } TimeSpan?age = null; if (_ageComponent.Value.Length != 0) { if (!TryParseAge(_ageComponent.Value.ToString(), out TimeSpan tsAge)) { ConsoleManager.CreateWindow <IMessage>().SetText("Age is not valid."); return; } age = tsAge; } if (age == null) { age = TimeSpan.FromDays(_randomizer.Next(1, 365 * 10)); // up to 10 years old random age } try { _database.Create(_speciesComponent.Value.ToString(), _nameComponent.Value.ToString(), age); } catch (AnimalTypeNotFoundException) { ConsoleManager.CreateWindow <IMessage>().SetText("Species not found."); return; } Close(); return; } if (IsValidTextCharacter(key.KeyChar)) { var component = _components[_focusedComponentIndex]; if (component.Value.Length < component.MaxLength) { component.Value.Append(key.KeyChar); Invalidate(); } } }