mainmenu.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "bytes"
  4. "red-green/door"
  5. "strings"
  6. "github.com/mitchellh/go-wordwrap"
  7. )
  8. // Build door.Menu (Main menu)
  9. func MainMenu() door.Menu {
  10. // Make the main menu
  11. // Width was 45
  12. var menu door.Menu = door.Menu{Panel: door.Panel{Width: 35,
  13. X: 2,
  14. Y: 2,
  15. Style: door.DOUBLE,
  16. Title: "[ Main Menu: ]",
  17. TitleOffset: 3,
  18. BorderColor: door.ColorText("BRI CYAN ON BLA")}}
  19. menu.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
  20. door.ColorText("BOLD BLUE"),
  21. door.ColorText("BOLD WHITE"),
  22. door.ColorText("BOLD CYAN"))
  23. menu.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
  24. door.ColorText("BOLD WHI ON BLUE"),
  25. door.ColorText("BOLD YEL ON BLUE"),
  26. door.ColorText("BOLD CYAN ON BLUE"))
  27. menu.AddSelection("A", "ANSI Display")
  28. // m.AddSelection("C", "Crash")
  29. menu.AddSelection("D", "Display Information")
  30. menu.AddSelection("F", "Font Demo")
  31. menu.AddSelection("I", "Input Demo")
  32. menu.AddSelection("M", "Menu Demo")
  33. menu.AddSelection("P", "Progress Bars Demo")
  34. menu.AddSelection("S", "Show Panel")
  35. menu.AddSelection("T", "Test Door About")
  36. menu.AddSelection("W", "Screen Width")
  37. menu.AddSelection("Q", "Quit")
  38. var descriptions []string = []string{
  39. // 12345678901234567890123456789012345678901234567890
  40. "Display an ANSI file. It is compiled into the door itself.",
  41. // "Crash go, see a handled error.", // The error shows up in the logs.
  42. "Display dropfile information.",
  43. "Display TheDraw Fonts. Font information is compiled into the door.",
  44. "Input some values, while updating the time.",
  45. "Isn't this is a menu?",
  46. "Display progress bar styles: half step, display percentage, and gradient.",
  47. "Show multiple panels. Panels can be mouse drag/drop around.",
  48. "Show about the door, using NoMoreSecrets effect.",
  49. "Examples using the full width of the screen.",
  50. "Exit this door.",
  51. }
  52. var widthLeft int = door.Width - (menu.Width + menu.X + 2)
  53. var panelWidth int = widthLeft - (2 + 2)
  54. var maxLines int = 1
  55. var maxLineLength int
  56. // Calculate the max lines needed for each line.
  57. for _, line := range descriptions {
  58. var wrapped string = wordwrap.WrapString(line, uint(panelWidth))
  59. var lines int = len(strings.Split(wrapped, "\n"))
  60. if lines > maxLines {
  61. maxLines = lines
  62. }
  63. if len(line) > maxLineLength {
  64. maxLineLength = len(line)
  65. }
  66. }
  67. if maxLines == 1 {
  68. // Ok! Everything fits into one line, SO use max line length as width of panel.
  69. panelWidth = maxLineLength
  70. }
  71. // Position the description panel beside the menu. m.Width + m.X + 2 (1 one to give it a space)
  72. // var descPanel door.Panel = door.Panel{X: menu.Width + menu.X + 2 + 1, Y: menu.Y, Width: panelWidth, Style: door.SINGLE, BorderColor: door.ColorText("WHI ON BLU")}
  73. var descPanel door.Panel = door.Panel{X: 2, Y: menu.Y + len(menu.Lines) + 3, Width: panelWidth, Style: door.SINGLE, BorderColor: door.ColorText("WHI ON BLU")}
  74. for x := 0; x < maxLines; x++ {
  75. descPanel.Lines = append(descPanel.Lines, &door.Line{Width: panelWidth})
  76. }
  77. menu.Activated = func(item int, d *door.Door) {
  78. var line string = descriptions[item]
  79. var lines []string = strings.Split(wordwrap.WrapString(line, uint(panelWidth)), "\n")
  80. for idx := range descPanel.Lines {
  81. if idx >= len(lines) {
  82. descPanel.Lines[idx].Text = &bytes.Buffer{}
  83. } else {
  84. descPanel.Lines[idx].Text = bytes.NewBuffer([]byte(lines[idx]))
  85. }
  86. }
  87. d.WriteA(door.SavePos, descPanel.Output(), door.RestorePos)
  88. }
  89. return menu
  90. }