// Return string of rendered menu public string Draw(int textWidth = 40, int textHeight = 7) { // long clock = DateTime.Now.Ticks; // used to scroll menu text if it is too wide string output = ""; var currentItem = this.path.Peek(); if (currentItem == null) { throw new Exception("Menu: currentItem is null"); } output += this.breadcrumbs() + "\n\n"; int selected = currentItem.children.IndexOf(this.selectedItem); int start = 0; int length = currentItem.children.Count; if (selected > (textHeight - 2) / 2) { start = selected - (textHeight - 2) / 2; } if (length > textHeight - 2) { length = textHeight - 2; } if (start + length > currentItem.children.Count) { start = currentItem.children.Count - (textHeight - 2); length = textHeight - 2; } if (start < 0) { start = 0; } for (int n = start; (n < (start + length) && n < currentItem.children.Count); n++) { EasyMenuItem child = currentItem.children[n]; if (child == this.selectedItem) { // Todo, if text is too wide, have it scroll horizontally output += "> " + child.GetText() + " <\n"; } else { output += child.GetText() + "\n"; } } return(output); }
/*** Public API ***/ // Move cursor up public void Up() { int location = this.path.Peek().children.IndexOf(this.selectedItem) - 1; if (location >= 0) { this.selectedItem = this.path.Peek().children[location]; } }
// Back action public void Back() { if (this.path.Count > 1) { this.path.Pop(); this.selectedItem = this.path.Peek().children[0]; } }
// Move cursor down public void Down() { int location = this.path.Peek().children.IndexOf(this.selectedItem) + 1; if (location < this.path.Peek().children.Count) { this.selectedItem = this.path.Peek().children[location]; } }
// Choose action public void Choose() { if (this.selectedItem.doAction()) { if (this.selectedItem.children.Count > 0) // go to sub menu if it exists { this.path.Push(this.selectedItem); this.selectedItem = this.path.Peek().children[0]; } } }
public bool toggleDoor(EasyMenuItem item) { EasyBlock door = Blocks.Named(item.Text).FindOrFail(item.Text + " not found!").GetBlock(0); if(door.Open()) door.ApplyAction("Open_Off"); else door.ApplyAction("Open_On"); return false; // don't go to a sub-menu if one is available }
/*** Constructors ***/ public EasyMenu(string text, EasyMenuItem[] children = null) { this.path = new Stack <EasyMenuItem>(); this.path.Push(new EasyMenuItem(text, children)); if (this.path.Peek().children.Count > 0) { this.selectedItem = this.path.Peek().children[0]; } }
// True means go into sub menu if it exists, false means stay where you are public bool doAction(EasyMenuItem item) { if (this.chooseAction == null) { return(true); } else { return(this.chooseAction(item)); } }
public bool toggleDoor(EasyMenuItem item) { EasyBlock door = Blocks.Named(item.Text).FindOrFail(item.Text + " not found!").GetBlock(0); if (door.Open()) { door.ApplyAction("Open_Off"); } else { door.ApplyAction("Open_On"); } return(false); // don't go to a sub-menu if one is available }
public int CompareTo(object obj) { if (obj == null) { return(1); } EasyMenuItem otherItem = obj as EasyMenuItem; if (otherItem != null) { return(this.GetText().CompareTo(otherItem.GetText())); } else { throw new ArgumentException("Object is not a EasyMenuItem"); } }
/*** Methods ***/ // Default text action which just returns the static text private string get_text(EasyMenuItem item) { return(this.Text); }
public bool playSound(EasyMenuItem item) { speaker.ApplyAction("PlaySound"); return(false); }
public string doorStatus(EasyMenuItem item) { EasyBlock door = Blocks.Named(item.Text).GetBlock(0); return(item.Text + ": " + ((door.Open())?"Open":"Closed")); }
public bool playSound(EasyMenuItem item) { speaker.ApplyAction("PlaySound"); return false; }
public string doorStatus(EasyMenuItem item) { EasyBlock door = Blocks.Named(item.Text).GetBlock(0); return item.Text + ": " + ((door.Open())?"Open":"Closed"); }
// Get menu text public string GetText(EasyMenuItem item) { return(this.textAction(item)); }