123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package door
- import (
- "strings"
- "unicode"
- )
- type MenuOption struct {
- Ch rune
- Text string
- }
- type Menu struct {
- Chosen int
- SelectedR func(string) string
- UnselectedR func(string) string
- Options []rune
- MenuOptions []MenuOption
- Panel
- }
- func MakeMenuRender(c1, c2, c3, c4 string) func(string) string {
- f := func(text string) string {
- var output string
- var lastColor string
- option := true
- for _, c := range text {
- if option {
- if c == '[' || c == ']' {
- if lastColor != c1 {
- output += c1
- lastColor = c1
- }
- output += string(c)
- option = (c == '[')
- } else {
- if lastColor != c2 {
- output += c2
- lastColor = c2
- }
- output += string(c)
- }
- } else {
- if unicode.IsUpper(c) {
- if lastColor != c3 {
- output += c3
- lastColor = c3
- }
- output += string(c)
- } else {
- if lastColor != c4 {
- output += c4
- lastColor = c4
- }
- output += string(c)
- }
- }
- }
- return output
- }
- return f
- }
- func (m *Menu) Build() {
- // Take MenuOptions and build the Menu
- // Reset
- m.Lines = make([]Line, 0)
- m.Options = make([]rune, 0)
- m.Chosen = 0
- for _, option := range m.MenuOptions {
- m.Options = append(m.Options, option.Ch)
- text := "[" + string(option.Ch) + "] " + option.Text + strings.Repeat(" ", m.Width-(4+len(option.Text)))
- m.Lines = append(m.Lines, Line{Text: text, RenderF: m.UnselectedR})
- }
- }
- func (m *Menu) Choose(d *Door) int {
- var changed []int
- updated := true
- update_and_exit := false
- blank := ColorText("BLACK")
- for {
- if updated {
- for x := range m.Lines {
- if x == m.Chosen {
- m.Lines[x].RenderF = m.SelectedR
- } else {
- m.Lines[x].RenderF = m.UnselectedR
- }
- }
- if len(changed) == 0 {
- d.Write(m.Output() + blank)
- } else {
- // Update just the lines that have changed
- for _, line := range changed {
- d.Write(m.UpdateLine(line))
- }
- d.Write(m.GotoEnd() + blank)
- }
- }
- if update_and_exit {
- return m.Chosen + 1
- }
- updated = false
- var event int = d.WaitKey(Inactivity, 0)
- if event < 0 {
- return event
- }
- previous_choice := m.Chosen
- changed = make([]int, 0)
- use_numberpad := true
- for _, option := range m.MenuOptions {
- if option.Ch == rune('8') || option.Ch == rune('2') {
- use_numberpad = false
- }
- }
- switch event {
- case '2':
- if use_numberpad {
- if m.Chosen > 0 {
- m.Chosen--
- updated = true
- }
- }
- case XKEY_UP_ARROW:
- if m.Chosen > 0 {
- m.Chosen--
- updated = true
- }
- case '8':
- if use_numberpad {
- if m.Chosen < len(m.Lines)-1 {
- m.Chosen++
- updated = true
- }
- }
- case XKEY_DOWN_ARROW:
- if m.Chosen < len(m.Lines)-1 {
- m.Chosen++
- updated = true
- }
- case XKEY_HOME:
- if m.Chosen != 0 {
- m.Chosen = 0
- updated = true
- }
- case XKEY_END:
- if m.Chosen != len(m.Lines)-1 {
- m.Chosen = len(m.Lines) - 1
- updated = true
- }
- case 0x0d:
- // use current selection
- return m.Chosen + 1
- default:
- // Is the key in the list of options?
- for x, option := range m.MenuOptions {
- if int(option.Ch) == event {
- if m.Chosen == x {
- return x + 1
- }
- }
- updated = true
- m.Chosen = x
- update_and_exit = true
- }
- }
- if previous_choice != m.Chosen {
- changed = append(changed, previous_choice)
- changed = append(changed, m.Chosen)
- updated = true
- }
- }
- }
|