public Control Navigate(IKeyReader reader, IField field, IFieldPrinter printer) { field.FieldCells[field.ActiveIndex].Active = true; int indexX = field.FieldCells[field.ActiveIndex].PositionX; int indexY = field.FieldCells[field.ActiveIndex].PositionY; printer.Print(); while (true) { Destination destination = reader.GetDestination(); if (destination == Destination.MoveDown) { if (indexY < Settings.FieldSize - 1) { field.FieldCells[indexY * Settings.FieldSize + indexX].Active = false; previousCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; indexY++; field.FieldCells[indexY * Settings.FieldSize + indexX].Active = true; currentCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; } } if (destination == Destination.MoveUp) { if (indexY > 0) { field.FieldCells[indexY * Settings.FieldSize + indexX].Active = false; previousCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; indexY--; field.FieldCells[indexY * Settings.FieldSize + indexX].Active = true; currentCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; } } if (destination == Destination.MoveRight) { if (indexX < Settings.FieldSize - 1) { field.FieldCells[indexY * Settings.FieldSize + indexX].Active = false; previousCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; indexX++; field.FieldCells[indexY * Settings.FieldSize + indexX].Active = true; currentCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; } } if (destination == Destination.MoveLeft) { if (indexX > 0) { field.FieldCells[indexY * Settings.FieldSize + indexX].Active = false; previousCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; indexX--; field.FieldCells[indexY * Settings.FieldSize + indexX].Active = true; currentCell = field.FieldCells[indexY * Settings.FieldSize + indexX]; } } if (destination == Destination.Esc) { return(Control.Exit); } if (destination == Destination.Select) { field.ActiveIndex = indexY * Settings.FieldSize + indexX; return(Control.Open); } if (destination == Destination.Flag) { field.ActiveIndex = indexY * Settings.FieldSize + indexX; field.FieldCells[field.ActiveIndex].Flagged = field.FieldCells[field.ActiveIndex].Flagged == false; return(Control.Flag); } printer.Print(currentCell, previousCell); } }
//Navigate by list of items. Returns selected item or null if selected other action public IListable Navigate(IList <IListable> recipes, out Action action, bool selectable) { List <IListable> sortedList = (List <IListable>)recipes; sortedList.Sort(); int index = 0; if (recipes.Count != 0) { recipes[index].Active = true; _printer.ShowItems(recipes, selectable); while (true) { Destination destination = _keyReader.GetDestination(); if (destination == Destination.MoveDown) { index++; if (index < recipes.Count) { recipes[index].Active = true; if (index > 0) { recipes[index - 1].Active = false; } } else { recipes[index - 1].Active = false; index = 0; recipes[index].Active = true; } } else if (destination == Destination.MoveUp) { index--; if (index >= 0) { recipes[index].Active = true; if (index < recipes.Count) { recipes[index + 1].Active = false; } } else { recipes[index + 1].Active = false; index = recipes.Count - 1; recipes[index].Active = true; } } else if (destination == Destination.Select) { recipes[index].Active = false; action = Action.Select; return(recipes[index]); } else if (destination == Destination.Mark && selectable) { recipes[index].Selected = recipes[index].Selected != true; action = Action.Select; } else if (destination == Destination.Esc) { recipes[index].Active = false; action = Action.Esc; return(null); } else if (destination == Destination.Create) { recipes[index].Active = false; action = Action.Create; return(null); } _printer.ShowItems(recipes, selectable); } } action = Action.None; return(null); }
//For Category tree and ICategory related types navigate and display public ICategory Navigate(IList <T> tree, IKeyReader reader, ITreePrinter <T> printer, bool autoExpandChildren) { List <T> sortedTree = (List <T>)tree; sortedTree.Sort((x, y) => x.Id - y.Id); T currentCategory = tree[0]; var neighbors = tree.Where(x => x.ParentId == currentCategory.Id).ToArray(); int parentId = 0; int horizontal = 0; sortedTree[0].Active = true; //Highlight root category currentCategory.Visible = true; printer.PrintTree(sortedTree); while (true) { Destination destination = reader.GetDestination(); if (destination == Destination.MoveDown) { if (currentCategory.ParentId == 0) // if top category move down opens childs { foreach (var id in neighbors) { sortedTree[id.Id - 1].Visible = true; //all Id in sorted list == index + 1 } parentId = currentCategory.ParentId; } else { SwitchCategory(false, currentCategory, sortedTree, autoExpandChildren); if (parentId != 0 && neighbors.Length > horizontal + 1) //can we move down? { horizontal++; currentCategory = neighbors[horizontal]; } } } else if (destination == Destination.MoveUp) { SwitchCategory(false, currentCategory, sortedTree, autoExpandChildren); if (0 <= horizontal - 1) { horizontal--; if (parentId != 0) { currentCategory = neighbors[horizontal]; } } } else if (destination == Destination.MoveRight) { SwitchCategory(false, currentCategory, sortedTree, autoExpandChildren); var children = tree.Where(x => x.ParentId == currentCategory.Id).ToArray(); if (children.Length != 0) { horizontal = 0; parentId = currentCategory.Id; currentCategory = children[horizontal]; neighbors = tree.Where(x => x.ParentId == parentId).ToArray(); foreach (var id in neighbors) { sortedTree[id.Id - 1].Visible = true; } } } else if (destination == Destination.MoveLeft) { if (currentCategory.ParentId != 0) { SwitchCategory(false, currentCategory, sortedTree, autoExpandChildren); if (parentId != 0) { foreach (var id in neighbors) { sortedTree[id.Id - 1].Visible = false; } currentCategory = sortedTree[currentCategory.ParentId - 1]; } parentId = currentCategory.ParentId; neighbors = tree.Where(x => x.ParentId == parentId).ToArray(); if (parentId != 0) { horizontal = Array.IndexOf(neighbors, currentCategory); } } } else if (destination == Destination.Select) { ResetActive(tree, currentCategory); return(currentCategory); } else if (destination == Destination.Esc) { ResetActive(sortedTree, currentCategory); HideAllCategories(sortedTree); printer.ClearView(sortedTree); return(null); } SwitchCategory(true, currentCategory, sortedTree, autoExpandChildren); printer.ClearView(sortedTree); printer.PrintTree(sortedTree); } }