menu.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package door
  2. import (
  3. "log"
  4. "strings"
  5. "unicode"
  6. )
  7. /*
  8. type MenuOption struct {
  9. Ch rune
  10. Text string
  11. }
  12. */
  13. type Menu struct {
  14. Chosen int
  15. SelectedR func(string) string
  16. UnselectedR func(string) string
  17. Options []rune
  18. Activated func(int, *Door) // Which option is active
  19. // MenuOptions []MenuOption
  20. Panel
  21. }
  22. func MakeMenuRender(bracketColor, optionColor, upperColor, lowerColor string) func(string) string {
  23. f := func(text string) string {
  24. var output string
  25. var lastColor string
  26. option := true
  27. for _, c := range text {
  28. if option {
  29. if c == '[' || c == ']' {
  30. if lastColor != bracketColor {
  31. output += bracketColor
  32. lastColor = bracketColor
  33. }
  34. output += string(c)
  35. option = (c == '[')
  36. } else {
  37. if lastColor != optionColor {
  38. output += optionColor
  39. lastColor = optionColor
  40. }
  41. output += string(c)
  42. }
  43. } else {
  44. if unicode.IsUpper(c) {
  45. if lastColor != upperColor {
  46. output += upperColor
  47. lastColor = upperColor
  48. }
  49. output += string(c)
  50. } else {
  51. if lastColor != lowerColor {
  52. output += lowerColor
  53. lastColor = lowerColor
  54. }
  55. output += string(c)
  56. }
  57. }
  58. }
  59. return output
  60. }
  61. return f
  62. }
  63. func (m *Menu) AddSelection(key string, text string) {
  64. key = key[:1] // Make sure it is just 1 character
  65. m.Options = append(m.Options, rune(key[0]))
  66. if len(text)+4 > m.Width {
  67. log.Panicf("Menu (not wide enough) Width %d : text size %d + 4 = %d\n", m.Width, len(text), len(text)+4)
  68. }
  69. linetext := "[" + key + "] " + text + strings.Repeat(" ", m.Width-(4+len(text)))
  70. m.Lines = append(m.Lines, Line{Text: linetext, RenderF: m.UnselectedR})
  71. }
  72. // Should I be using this, or write a function like the original --
  73. // m.AddSelection("K", "Text") to build? (And throw away the
  74. // redundant) MenuOptions/MenuOption parts? They do let me create
  75. // the menu in larger chunks -- but it is redundant.
  76. // Once the menu is built, I really could delete the MenuOptions.
  77. /*
  78. func (m *Menu) Build() {
  79. // Take MenuOptions and build the Menu
  80. // Reset
  81. m.Lines = make([]Line, 0)
  82. m.Options = make([]rune, 0)
  83. m.Chosen = 0
  84. for _, option := range m.MenuOptions {
  85. m.Options = append(m.Options, option.Ch)
  86. text := "[" + string(option.Ch) + "] " + option.Text + strings.Repeat(" ", m.Width-(4+len(option.Text)))
  87. m.Lines = append(m.Lines, Line{Text: text, RenderF: m.UnselectedR})
  88. }
  89. }
  90. */
  91. // Get the character that was pressed from the choice
  92. func (m *Menu) GetOption(choice int) rune {
  93. return m.Options[choice-1]
  94. }
  95. func (m *Menu) Choose(d *Door) int {
  96. var changed []int
  97. updated := true
  98. update_and_exit := false
  99. blank := ColorText("BLACK")
  100. for {
  101. if updated {
  102. for x := range m.Lines {
  103. if x == m.Chosen {
  104. m.Lines[x].RenderF = m.SelectedR
  105. } else {
  106. m.Lines[x].RenderF = m.UnselectedR
  107. }
  108. }
  109. if len(changed) == 0 {
  110. d.Write(m.Output() + blank)
  111. } else {
  112. // Update just the lines that have changed
  113. for _, line := range changed {
  114. d.Write(m.UpdateLine(line))
  115. }
  116. d.Write(m.GotoEnd() + blank)
  117. }
  118. if m.Activated != nil {
  119. m.Activated(m.Chosen, d)
  120. }
  121. }
  122. if update_and_exit {
  123. return m.Chosen + 1
  124. }
  125. updated = false
  126. var r rune
  127. var ex Extended
  128. var err error
  129. r, ex, err = d.WaitKey(DefaultTimeout)
  130. if err != nil {
  131. return -1
  132. }
  133. previous_choice := m.Chosen
  134. changed = make([]int, 0)
  135. use_numberpad := true
  136. for _, option := range m.Options {
  137. if option == rune('8') || option == rune('2') {
  138. use_numberpad = false
  139. }
  140. }
  141. if ex == MOUSE {
  142. mouse, ok := d.GetMouse()
  143. if ok {
  144. if mouse.Button == 65 {
  145. // Translate Mouse Wheel Up
  146. ex = UP_ARROW
  147. }
  148. if mouse.Button == 66 {
  149. // Translate Mouse Wheel Down
  150. ex = DOWN_ARROW
  151. }
  152. // Look at Mouse X/Y to determine where they clicked
  153. if mouse.Button == 1 || mouse.Button == 2 {
  154. log.Println("Mouse (", mouse.X, ",", mouse.Y, ")")
  155. }
  156. }
  157. }
  158. switch ex {
  159. case UP_ARROW:
  160. if m.Chosen > 0 {
  161. m.Chosen--
  162. updated = true
  163. }
  164. case DOWN_ARROW:
  165. if m.Chosen < len(m.Lines)-1 {
  166. m.Chosen++
  167. updated = true
  168. }
  169. case HOME:
  170. if m.Chosen != 0 {
  171. m.Chosen = 0
  172. updated = true
  173. }
  174. case END:
  175. if m.Chosen != len(m.Lines)-1 {
  176. m.Chosen = len(m.Lines) - 1
  177. updated = true
  178. }
  179. }
  180. switch r {
  181. case '8':
  182. if use_numberpad {
  183. if m.Chosen > 0 {
  184. m.Chosen--
  185. updated = true
  186. }
  187. }
  188. case '2':
  189. if use_numberpad {
  190. if m.Chosen < len(m.Lines)-1 {
  191. m.Chosen++
  192. updated = true
  193. }
  194. }
  195. case 0x0d:
  196. // use current selection
  197. return m.Chosen + 1
  198. default:
  199. // Is the key in the list of options?
  200. // fmt.Printf("Event: %d\n", event)
  201. for x, option := range m.Options {
  202. // fmt.Printf("Checking %d, %d\n", x, option)
  203. if unicode.ToUpper(option) == unicode.ToUpper(r) {
  204. if m.Chosen == x {
  205. return x + 1
  206. }
  207. updated = true
  208. m.Chosen = x
  209. update_and_exit = true
  210. }
  211. }
  212. }
  213. if previous_choice != m.Chosen {
  214. changed = append(changed, previous_choice)
  215. changed = append(changed, m.Chosen)
  216. updated = true
  217. }
  218. }
  219. }