panels.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package main
  2. import (
  3. "bytes"
  4. "log"
  5. "red-green/door"
  6. "strings"
  7. )
  8. type TrackPanels struct {
  9. Panel *door.Panel
  10. XPos int
  11. YPos int
  12. BColor []byte
  13. }
  14. // Find the Panel that was mouse clicked.
  15. func FindPanel(m door.Mouse, panels []TrackPanels) int {
  16. for idx, p := range panels {
  17. hit, _, _ := p.Panel.Within(int(m.X), int(m.Y))
  18. if hit {
  19. return idx
  20. }
  21. }
  22. return -1
  23. }
  24. func panel_demo(d *door.Door) {
  25. var width int = 55
  26. var panel door.Panel = door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
  27. var moveColor = door.ColorText("CYAN ON BLACK")
  28. var lineColor = door.ColorText("BRIGHT WHI ON BLUE")
  29. // Add lines to the panel
  30. for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
  31. if door.Unicode {
  32. line = strings.Replace(line, "(C)", "\u00a9", -1)
  33. }
  34. var l *door.Line = &door.Line{Text: bytes.NewBuffer([]byte(line)), Width: width, DefaultColor: lineColor}
  35. panel.Lines = append(panel.Lines, l)
  36. }
  37. panel.Lines = append(panel.Lines, panel.Spacer())
  38. panel.Lines = append(panel.Lines, &door.Line{Text: bytes.NewBuffer([]byte("Welcome to golang!")), Width: width, DefaultColor: lineColor})
  39. width = 10
  40. var single door.Panel = door.Panel{X: 6, Y: 12, Width: width, Style: door.SINGLE, BorderColor: door.ColorText("WHITE ON BLUE"), Title: "< Single >"}
  41. single.Lines = append(single.Lines, &door.Line{Text: bytes.NewBuffer([]byte("Example")), Width: width, DefaultColor: door.ColorText("WHI ON BLACK")})
  42. single.Lines = append(single.Lines, single.Spacer())
  43. single.Lines = append(single.Lines, &door.Line{Text: bytes.NewBuffer([]byte("More Text")), Width: width, DefaultColor: door.ColorText("BRI GREEN ON BLACK")})
  44. width = 15
  45. var double_single door.Panel = door.Panel{X: 26, Y: 12, Width: width, Style: door.DOUBLE_SINGLE, BorderColor: door.ColorText("BRI CYAN ON GREEN"), Title: "Double", TitleOffset: 3}
  46. double_single.Lines = append(double_single.Lines, &door.Line{Text: bytes.NewBuffer([]byte("Double / Single")), Width: width, DefaultColor: door.ColorText("BRI WHI ON GREEN")})
  47. double_single.Lines = append(double_single.Lines, double_single.Spacer())
  48. double_single.Lines = append(double_single.Lines, &door.Line{Text: bytes.NewBuffer([]byte("Some Other Text")), Width: width, DefaultColor: door.ColorText("BRI CYAN ON GREEN")})
  49. var single_double door.Panel = door.Panel{X: 46, Y: 12, Width: width, Style: door.SINGLE_DOUBLE, BorderColor: door.ColorText("BRI YELL ON RED")}
  50. single_double.Lines = append(single_double.Lines, &door.Line{Text: bytes.NewBuffer([]byte("Single / Double")), Width: width, DefaultColor: door.ColorText("BRI WHI ON RED")})
  51. single_double.Lines = append(single_double.Lines, single_double.Spacer())
  52. single_double.Lines = append(single_double.Lines, &door.Line{Text: bytes.NewBuffer([]byte("Text Goes Here ")), Width: width, DefaultColor: door.ColorText("BRI GREEN ON RED")})
  53. d.WriteA(door.Clrscr, panel.Output(), single.Output(), double_single.Output(), single_double.Output())
  54. d.WriteA(door.Goto(1, 20), door.Reset, "Use MOUSE to click/drag panels, Right-Click Exits, R to Reset, Q to quit...")
  55. var panels []TrackPanels = []TrackPanels{
  56. {&panel, panel.X, panel.Y, panel.BorderColor},
  57. {&single, single.X, single.Y, single.BorderColor},
  58. {&double_single, double_single.X, double_single.Y, single.BorderColor},
  59. {&single_double, single_double.X, single_double.Y, single_double.BorderColor}}
  60. var movingPanel *door.Panel
  61. var moveX, moveY int
  62. var panelColor []byte
  63. for {
  64. key, ex, err := d.WaitKey(door.Inactivity)
  65. if err != nil {
  66. return
  67. }
  68. if ex == door.MOUSE {
  69. m, ok := d.GetMouse()
  70. if ok {
  71. // Process Mouse Event
  72. if m.Button == 3 {
  73. // Exit on right click
  74. return
  75. }
  76. if m.Button == 1 {
  77. idx := FindPanel(m, panels)
  78. if idx != -1 {
  79. movingPanel = panels[idx].Panel
  80. panelColor = movingPanel.BorderColor
  81. moveX = int(m.X)
  82. moveY = int(m.Y)
  83. } else {
  84. continue
  85. }
  86. // Should we do something to the panel? Yes!
  87. movingPanel.BorderColor = moveColor
  88. d.Update(movingPanel.Output())
  89. } else if m.Button == 4 {
  90. if movingPanel != nil {
  91. PR, PB := movingPanel.RightBottomPos()
  92. log.Printf("Panel (%d,%d) End(%d,%d) W %d, L %d\n", movingPanel.X, movingPanel.Y, PR, PB, movingPanel.Width, movingPanel.Length())
  93. // Ok, panel is move!
  94. d.Update(movingPanel.Clear())
  95. // Restore panel border
  96. movingPanel.BorderColor = panelColor
  97. // move panel
  98. movingPanel.X -= (moveX - int(m.X))
  99. movingPanel.Y -= (moveY - int(m.Y))
  100. // sanity checks
  101. if movingPanel.X < 1 {
  102. movingPanel.X = 1
  103. }
  104. // var edgeX bool
  105. if movingPanel.X+movingPanel.Width >= door.Width {
  106. movingPanel.X = door.Width - movingPanel.Width - 1
  107. // edgeX = true
  108. }
  109. if movingPanel.Y < 1 {
  110. movingPanel.Y = 1
  111. }
  112. // var edgeY bool
  113. if movingPanel.Y+movingPanel.Length() >= door.Height {
  114. movingPanel.Y = door.Height - movingPanel.Length() - 1
  115. // edgeY = true
  116. }
  117. PR, PB = movingPanel.RightBottomPos()
  118. log.Printf("Panel Now (%d,%d) End (%d,%d) %d, %d\n", movingPanel.X, movingPanel.Y, PR, PB, movingPanel.Width, movingPanel.Length()) //, edgeX, edgeY)
  119. // If panel is at the end of the screen -- it scrolls (syncterm)
  120. // This "fixes" it. (Maybe a better way would be to not use last
  121. // line on the screen?)
  122. // Or another way:
  123. if PR == door.Width && PB == door.Height {
  124. movingPanel.X--
  125. }
  126. /*
  127. if edgeX && edgeY {
  128. movingPanel.X--
  129. log.Printf("Panel adjust X\n")
  130. }
  131. */
  132. d.Update(movingPanel.Output())
  133. }
  134. movingPanel = nil
  135. }
  136. }
  137. } else {
  138. if (key == 'Q') || (key == 'q') || (key == '\x1b') {
  139. return
  140. }
  141. if (key == 'R') || (key == 'r') {
  142. for _, p := range panels {
  143. d.Update(p.Panel.Clear())
  144. }
  145. for _, p := range panels {
  146. p.Panel.X = p.XPos
  147. p.Panel.Y = p.YPos
  148. p.Panel.BorderColor = p.BColor
  149. d.Update(p.Panel.Output())
  150. }
  151. }
  152. if key == '0' {
  153. d.Write(panels[0].Panel.GotoEnd())
  154. }
  155. if key == '1' {
  156. d.Write(panels[1].Panel.GotoEnd())
  157. }
  158. }
  159. }
  160. }