menu.go 5.0 KB

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