My mirror of the Barnard terminal client for Mumble.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

textbox.go 1.6 KiB

10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
10 lat temu
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package uiterm
  2. import (
  3. "strings"
  4. "unicode/utf8"
  5. "github.com/nsf/termbox-go"
  6. )
  7. type Textbox struct {
  8. Text string
  9. Fg Attribute
  10. Bg Attribute
  11. Input func(ui *Ui, textbox *Textbox, text string)
  12. ui *Ui
  13. active bool
  14. x0, y0, x1, y1 int
  15. }
  16. func (t *Textbox) uiInitialize(ui *Ui) {
  17. t.ui = ui
  18. }
  19. func (t *Textbox) uiSetActive(active bool) {
  20. t.active = active
  21. }
  22. func (t *Textbox) uiSetBounds(x0, y0, x1, y1 int) {
  23. t.x0 = x0
  24. t.y0 = y0
  25. t.x1 = x1
  26. t.y1 = y1
  27. }
  28. func (t *Textbox) uiDraw() {
  29. var setCursor = false
  30. reader := strings.NewReader(t.Text)
  31. for y := t.y0; y < t.y1; y++ {
  32. for x := t.x0; x < t.x1; x++ {
  33. var chr rune
  34. if ch, _, err := reader.ReadRune(); err != nil {
  35. if t.active && !setCursor {
  36. termbox.SetCursor(x, y)
  37. setCursor = true
  38. }
  39. chr = ' '
  40. } else {
  41. chr = ch
  42. }
  43. termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
  44. }
  45. }
  46. }
  47. func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
  48. redraw := false
  49. switch key {
  50. case KeyCtrlC:
  51. t.Text = ""
  52. redraw = true
  53. case KeyEnter:
  54. if t.Input != nil {
  55. t.Input(t.ui, t, t.Text)
  56. }
  57. t.Text = ""
  58. redraw = true
  59. case KeySpace:
  60. t.Text = t.Text + " "
  61. redraw = true
  62. case KeyBackspace:
  63. case KeyBackspace2:
  64. if len(t.Text) > 0 {
  65. if r, size := utf8.DecodeLastRuneInString(t.Text); r != utf8.RuneError {
  66. t.Text = t.Text[:len(t.Text)-size]
  67. redraw = true
  68. }
  69. }
  70. }
  71. if redraw {
  72. t.uiDraw()
  73. termbox.Flush()
  74. }
  75. }
  76. func (t *Textbox) uiCharacterEvent(chr rune) {
  77. t.Text = t.Text + string(chr)
  78. t.uiDraw()
  79. termbox.Flush()
  80. }