menu.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package door
  2. import (
  3. "strings"
  4. "unicode"
  5. )
  6. type MenuOption struct {
  7. Ch rune
  8. Text string
  9. }
  10. type Menu struct {
  11. Chosen int
  12. SelectedR func(string) string
  13. UnselectedR func(string) string
  14. Options []rune
  15. MenuOptions []MenuOption
  16. Panel
  17. }
  18. func MakeMenuRender(c1, c2, c3, c4 string) func(string) string {
  19. f := func(text string) string {
  20. var output string
  21. var lastColor string
  22. option := true
  23. for _, c := range text {
  24. if option {
  25. if c == '[' || c == ']' {
  26. if lastColor != c1 {
  27. output += c1
  28. lastColor = c1
  29. }
  30. output += string(c)
  31. option = (c == '[')
  32. } else {
  33. if lastColor != c2 {
  34. output += c2
  35. lastColor = c2
  36. }
  37. output += string(c)
  38. }
  39. } else {
  40. if unicode.IsUpper(c) {
  41. if lastColor != c3 {
  42. output += c3
  43. lastColor = c3
  44. }
  45. output += string(c)
  46. } else {
  47. if lastColor != c4 {
  48. output += c4
  49. lastColor = c4
  50. }
  51. output += string(c)
  52. }
  53. }
  54. }
  55. return output
  56. }
  57. return f
  58. }
  59. func (m *Menu) Build() {
  60. // Take MenuOptions and build the Menu
  61. // Reset
  62. m.Lines = make([]Line, 0)
  63. m.Options = make([]rune, 0)
  64. m.Chosen = 0
  65. for _, option := range m.MenuOptions {
  66. m.Options = append(m.Options, option.Ch)
  67. text := "[" + string(option.Ch) + "] " + option.Text + strings.Repeat(" ", m.Width-(4+len(option.Text)))
  68. m.Lines = append(m.Lines, Line{Text: text, RenderF: m.UnselectedR})
  69. }
  70. }
  71. func (m *Menu) Choose(d *Door) int {
  72. var changed []int
  73. updated := true
  74. update_and_exit := false
  75. blank := ColorText("BLACK")
  76. for {
  77. if updated {
  78. for x := range m.Lines {
  79. if x == m.Chosen {
  80. m.Lines[x].RenderF = m.SelectedR
  81. } else {
  82. m.Lines[x].RenderF = m.UnselectedR
  83. }
  84. }
  85. if len(changed) == 0 {
  86. d.Write(m.Output() + blank)
  87. } else {
  88. // Update just the lines that have changed
  89. for _, line := range changed {
  90. d.Write(m.UpdateLine(line))
  91. }
  92. d.Write(m.GotoEnd() + blank)
  93. }
  94. }
  95. if update_and_exit {
  96. return m.Chosen + 1
  97. }
  98. updated = false
  99. var event int = d.WaitKey(Inactivity, 0)
  100. if event < 0 {
  101. return event
  102. }
  103. previous_choice := m.Chosen
  104. changed = make([]int, 0)
  105. use_numberpad := true
  106. for _, option := range m.MenuOptions {
  107. if option.Ch == rune('8') || option.Ch == rune('2') {
  108. use_numberpad = false
  109. }
  110. }
  111. switch event {
  112. case '2':
  113. if use_numberpad {
  114. if m.Chosen > 0 {
  115. m.Chosen--
  116. updated = true
  117. }
  118. }
  119. case XKEY_UP_ARROW:
  120. if m.Chosen > 0 {
  121. m.Chosen--
  122. updated = true
  123. }
  124. case '8':
  125. if use_numberpad {
  126. if m.Chosen < len(m.Lines)-1 {
  127. m.Chosen++
  128. updated = true
  129. }
  130. }
  131. case XKEY_DOWN_ARROW:
  132. if m.Chosen < len(m.Lines)-1 {
  133. m.Chosen++
  134. updated = true
  135. }
  136. case XKEY_HOME:
  137. if m.Chosen != 0 {
  138. m.Chosen = 0
  139. updated = true
  140. }
  141. case XKEY_END:
  142. if m.Chosen != len(m.Lines)-1 {
  143. m.Chosen = len(m.Lines) - 1
  144. updated = true
  145. }
  146. case 0x0d:
  147. // use current selection
  148. return m.Chosen + 1
  149. default:
  150. // Is the key in the list of options?
  151. for x, option := range m.MenuOptions {
  152. if int(option.Ch) == event {
  153. if m.Chosen == x {
  154. return x + 1
  155. }
  156. }
  157. updated = true
  158. m.Chosen = x
  159. update_and_exit = true
  160. }
  161. }
  162. if previous_choice != m.Chosen {
  163. changed = append(changed, previous_choice)
  164. changed = append(changed, m.Chosen)
  165. updated = true
  166. }
  167. }
  168. }