splitter.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/math32"
  7. "github.com/g3n/engine/window"
  8. )
  9. type Splitter struct {
  10. Panel // Embedded panel
  11. P0 Panel // Left/Top panel
  12. P1 Panel // Right/Bottom panel
  13. styles *SplitterStyles // pointer to current styles
  14. spacer Panel // spacer panel
  15. horiz bool // horizontal or vertical splitter
  16. pos float32 // relative position of the center of the spacer panel (0 to 1)
  17. posLast float32 // last position in pixels of the mouse cursor when dragging
  18. pressed bool // mouse button is pressed and dragging
  19. mouseOver bool // mouse is over the spacer panel
  20. }
  21. type SplitterStyle struct {
  22. SpacerBorderColor math32.Color4
  23. SpacerColor math32.Color4
  24. SpacerSize float32
  25. }
  26. type SplitterStyles struct {
  27. Normal SplitterStyle
  28. Over SplitterStyle
  29. Drag SplitterStyle
  30. }
  31. // NewHSplitter creates and returns a pointer to a new horizontal splitter
  32. // widget with the specified initial dimensions
  33. func NewHSplitter(width, height float32) *Splitter {
  34. return newSplitter(true, width, height)
  35. }
  36. // NewVSplitter creates and returns a pointer to a new vertical splitter
  37. // widget with the specified initial dimensions
  38. func NewVSplitter(width, height float32) *Splitter {
  39. return newSplitter(false, width, height)
  40. }
  41. // newSpliter creates and returns a pointer of a new splitter with
  42. // the specified orientation and initial dimensions.
  43. func newSplitter(horiz bool, width, height float32) *Splitter {
  44. s := new(Splitter)
  45. s.horiz = horiz
  46. s.styles = &StyleDefault().Splitter
  47. s.Panel.Initialize(width, height)
  48. // Initialize left/top panel
  49. s.P0.Initialize(0, 0)
  50. s.Panel.Add(&s.P0)
  51. // Initialize right/bottom panel
  52. s.P1.Initialize(0, 0)
  53. s.Panel.Add(&s.P1)
  54. // Initialize spacer panel
  55. s.spacer.Initialize(0, 0)
  56. s.Panel.Add(&s.spacer)
  57. if horiz {
  58. s.spacer.SetBorders(0, 1, 0, 1)
  59. s.pos = 0.5
  60. } else {
  61. s.spacer.SetBorders(1, 0, 1, 0)
  62. s.pos = 0.5
  63. }
  64. s.Subscribe(OnResize, s.onResize)
  65. s.spacer.Subscribe(OnMouseDown, s.onMouse)
  66. s.spacer.Subscribe(OnMouseUp, s.onMouse)
  67. s.spacer.Subscribe(OnCursor, s.onCursor)
  68. s.spacer.Subscribe(OnCursorEnter, s.onCursor)
  69. s.spacer.Subscribe(OnCursorLeave, s.onCursor)
  70. s.update()
  71. s.recalc()
  72. return s
  73. }
  74. // SetSplit sets the position of the splitter bar.
  75. // It accepts a value from 0.0 to 1.0
  76. func (s *Splitter) SetSplit(pos float32) {
  77. s.setSplit(pos)
  78. s.recalc()
  79. }
  80. // Split returns the current position of the splitter bar.
  81. // It returns a value from 0.0 to 1.0
  82. func (s *Splitter) Split() float32 {
  83. return s.pos
  84. }
  85. // onResize receives subscribed resize events for the whole splitter panel
  86. func (s *Splitter) onResize(evname string, ev interface{}) {
  87. s.recalc()
  88. }
  89. // onMouse receives subscribed mouse events over the spacer panel
  90. func (s *Splitter) onMouse(evname string, ev interface{}) {
  91. mev := ev.(*window.MouseEvent)
  92. switch evname {
  93. case OnMouseDown:
  94. s.pressed = true
  95. if s.horiz {
  96. s.posLast = mev.Xpos
  97. } else {
  98. s.posLast = mev.Ypos
  99. }
  100. s.root.SetMouseFocus(&s.spacer)
  101. case OnMouseUp:
  102. s.pressed = false
  103. s.root.SetCursorNormal()
  104. s.root.SetMouseFocus(nil)
  105. default:
  106. }
  107. s.root.StopPropagation(Stop3D)
  108. }
  109. // onCursor receives subscribed cursor events over the spacer panel
  110. func (s *Splitter) onCursor(evname string, ev interface{}) {
  111. if evname == OnCursorEnter {
  112. if s.horiz {
  113. s.root.SetCursorHResize()
  114. } else {
  115. s.root.SetCursorVResize()
  116. }
  117. s.mouseOver = true
  118. s.update()
  119. } else if evname == OnCursorLeave {
  120. s.root.SetCursorNormal()
  121. s.mouseOver = false
  122. s.update()
  123. } else if evname == OnCursor {
  124. if !s.pressed {
  125. return
  126. }
  127. cev := ev.(*window.CursorEvent)
  128. var delta float32
  129. pos := s.pos
  130. if s.horiz {
  131. delta = cev.Xpos - s.posLast
  132. s.posLast = cev.Xpos
  133. pos += delta / s.ContentWidth()
  134. } else {
  135. delta = cev.Ypos - s.posLast
  136. s.posLast = cev.Ypos
  137. pos += delta / s.ContentHeight()
  138. }
  139. s.setSplit(pos)
  140. s.recalc()
  141. }
  142. s.root.StopPropagation(Stop3D)
  143. }
  144. // setSplit sets the validated and clamped split position from the received value.
  145. func (s *Splitter) setSplit(pos float32) {
  146. if pos < 0 {
  147. s.pos = 0
  148. } else if pos > 1 {
  149. s.pos = 1
  150. } else {
  151. s.pos = pos
  152. }
  153. }
  154. // update updates the splitter visual state
  155. func (s *Splitter) update() {
  156. if s.pressed {
  157. s.applyStyle(&s.styles.Drag)
  158. return
  159. }
  160. if s.mouseOver {
  161. s.applyStyle(&s.styles.Over)
  162. return
  163. }
  164. s.applyStyle(&s.styles.Normal)
  165. }
  166. // applyStyle applies the specified splitter style
  167. func (s *Splitter) applyStyle(ss *SplitterStyle) {
  168. s.spacer.SetBordersColor4(&ss.SpacerBorderColor)
  169. s.spacer.SetColor4(&ss.SpacerColor)
  170. if s.horiz {
  171. s.spacer.SetWidth(ss.SpacerSize)
  172. } else {
  173. s.spacer.SetHeight(ss.SpacerSize)
  174. }
  175. }
  176. // recalc relcalculates the position and sizes of the internal panels
  177. func (s *Splitter) recalc() {
  178. width := s.ContentWidth()
  179. height := s.ContentHeight()
  180. if s.horiz {
  181. // Calculate x position for spacer panel
  182. spx := width*s.pos - s.spacer.Width()/2
  183. if spx < 0 {
  184. spx = 0
  185. } else if spx > width-s.spacer.Width() {
  186. spx = width - s.spacer.Width()
  187. }
  188. // Left panel
  189. s.P0.SetPosition(0, 0)
  190. s.P0.SetSize(spx, height)
  191. // Spacer panel
  192. s.spacer.SetPosition(spx, 0)
  193. s.spacer.SetHeight(height)
  194. // Right panel
  195. s.P1.SetPosition(spx+s.spacer.Width(), 0)
  196. s.P1.SetSize(width-spx-s.spacer.Width(), height)
  197. } else {
  198. // Calculate y position for spacer panel
  199. spy := height*s.pos - s.spacer.Height()/2
  200. if spy < 0 {
  201. spy = 0
  202. } else if spy > height-s.spacer.Height() {
  203. spy = height - s.spacer.Height()
  204. }
  205. // Top panel
  206. s.P0.SetPosition(0, 0)
  207. s.P0.SetSize(width, spy)
  208. // Spacer panel
  209. s.spacer.SetPosition(0, spy)
  210. s.spacer.SetWidth(width)
  211. // Bottom panel
  212. s.P1.SetPosition(0, spy+s.spacer.Height())
  213. s.P1.SetSize(width, height-spy-s.spacer.Height())
  214. }
  215. }