menu.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 event int = d.Key()
  127. if event < 0 {
  128. return event
  129. }
  130. previous_choice := m.Chosen
  131. changed = make([]int, 0)
  132. use_numberpad := true
  133. for _, option := range m.Options {
  134. if option == rune('8') || option == rune('2') {
  135. use_numberpad = false
  136. }
  137. }
  138. if event == XKEY_MOUSE {
  139. if Mouse.Button == 65 {
  140. // Translate Mouse Wheel Up
  141. event = XKEY_UP_ARROW
  142. }
  143. if Mouse.Button == 66 {
  144. // Translate Mouse Wheel Down
  145. event = XKEY_DOWN_ARROW
  146. }
  147. // Look at Mouse X/Y to determine where they clicked
  148. if Mouse.Button == 1 || Mouse.Button == 2 {
  149. log.Println("Mouse (", Mouse.X, ",", Mouse.Y, ")")
  150. }
  151. }
  152. switch event {
  153. case '8':
  154. if use_numberpad {
  155. goto use8
  156. }
  157. break
  158. use8:
  159. fallthrough
  160. case XKEY_UP_ARROW:
  161. if m.Chosen > 0 {
  162. m.Chosen--
  163. updated = true
  164. }
  165. case '2':
  166. if use_numberpad {
  167. goto use2
  168. }
  169. break
  170. use2:
  171. fallthrough
  172. case XKEY_DOWN_ARROW:
  173. if m.Chosen < len(m.Lines)-1 {
  174. m.Chosen++
  175. updated = true
  176. }
  177. case XKEY_HOME:
  178. if m.Chosen != 0 {
  179. m.Chosen = 0
  180. updated = true
  181. }
  182. case XKEY_END:
  183. if m.Chosen != len(m.Lines)-1 {
  184. m.Chosen = len(m.Lines) - 1
  185. updated = true
  186. }
  187. case 0x0d:
  188. // use current selection
  189. return m.Chosen + 1
  190. default:
  191. // Is the key in the list of options?
  192. if event < 0x1000 {
  193. // fmt.Printf("Event: %d\n", event)
  194. for x, option := range m.Options {
  195. // fmt.Printf("Checking %d, %d\n", x, option)
  196. if unicode.ToUpper(option) == unicode.ToUpper(rune(event)) {
  197. if m.Chosen == x {
  198. return x + 1
  199. }
  200. updated = true
  201. m.Chosen = x
  202. update_and_exit = true
  203. }
  204. }
  205. }
  206. }
  207. if previous_choice != m.Chosen {
  208. changed = append(changed, previous_choice)
  209. changed = append(changed, m.Chosen)
  210. updated = true
  211. }
  212. }
  213. }