splitter.go 5.8 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 // central position of the spacer panel bar in pixels
  17. posLast float32 // last position 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.Color
  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.spacer.SetWidth(6)
  60. s.pos = width / 2
  61. } else {
  62. s.spacer.SetBorders(1, 0, 1, 0)
  63. s.spacer.SetHeight(6)
  64. s.pos = height / 2
  65. }
  66. s.spacer.Subscribe(OnMouseDown, s.onMouse)
  67. s.spacer.Subscribe(OnMouseUp, s.onMouse)
  68. s.spacer.Subscribe(OnCursor, s.onCursor)
  69. s.spacer.Subscribe(OnCursorEnter, s.onCursor)
  70. s.spacer.Subscribe(OnCursorLeave, s.onCursor)
  71. s.update()
  72. s.recalc()
  73. return s
  74. }
  75. // SetSplit sets the position of the splitter bar.
  76. // It accepts a value from 0.0 to 1.0
  77. func (s *Splitter) SetSplit(pos float32) {
  78. if s.horiz {
  79. s.setSplit(pos * s.Width())
  80. } else {
  81. s.setSplit(pos * s.Height())
  82. }
  83. s.recalc()
  84. }
  85. // Split returns the current position of the splitter bar.
  86. // It returns a value from 0.0 to 1.0
  87. func (s *Splitter) Split() float32 {
  88. var pos float32
  89. if s.horiz {
  90. pos = s.pos / s.Width()
  91. } else {
  92. pos = s.pos / s.Height()
  93. }
  94. return pos
  95. }
  96. // onMouse receives subscribed mouse events over the spacer panel
  97. func (s *Splitter) onMouse(evname string, ev interface{}) {
  98. mev := ev.(*window.MouseEvent)
  99. switch evname {
  100. case OnMouseDown:
  101. s.pressed = true
  102. if s.horiz {
  103. s.posLast = mev.Xpos
  104. } else {
  105. s.posLast = mev.Ypos
  106. }
  107. s.root.SetMouseFocus(&s.spacer)
  108. case OnMouseUp:
  109. s.pressed = false
  110. s.root.SetCursorNormal()
  111. s.root.SetMouseFocus(nil)
  112. default:
  113. }
  114. s.root.StopPropagation(Stop3D)
  115. }
  116. // onCursor receives subscribed cursor events over the spacer panel
  117. func (s *Splitter) onCursor(evname string, ev interface{}) {
  118. if evname == OnCursorEnter {
  119. if s.horiz {
  120. s.root.SetCursorHResize()
  121. } else {
  122. s.root.SetCursorVResize()
  123. }
  124. s.mouseOver = true
  125. s.update()
  126. } else if evname == OnCursorLeave {
  127. s.root.SetCursorNormal()
  128. s.mouseOver = false
  129. s.update()
  130. } else if evname == OnCursor {
  131. if !s.pressed {
  132. return
  133. }
  134. cev := ev.(*window.CursorEvent)
  135. var delta float32
  136. if s.horiz {
  137. delta = cev.Xpos - s.posLast
  138. s.posLast = cev.Xpos
  139. } else {
  140. delta = cev.Ypos - s.posLast
  141. s.posLast = cev.Ypos
  142. }
  143. s.setSplit(s.pos + delta)
  144. s.recalc()
  145. }
  146. s.root.StopPropagation(Stop3D)
  147. }
  148. // setSplit sets the validated and clamped split position from the received value.
  149. func (s *Splitter) setSplit(pos float32) {
  150. var max float32
  151. var halfspace float32
  152. if s.horiz {
  153. halfspace = s.spacer.Width() / 2
  154. max = s.Width()
  155. } else {
  156. halfspace = s.spacer.Height() / 2
  157. max = s.Height()
  158. }
  159. if pos > max-halfspace {
  160. s.pos = max - halfspace
  161. } else if pos <= halfspace {
  162. s.pos = halfspace
  163. } else {
  164. s.pos = pos
  165. }
  166. }
  167. // update updates the splitter visual state
  168. func (s *Splitter) update() {
  169. if s.pressed {
  170. s.applyStyle(&s.styles.Drag)
  171. return
  172. }
  173. if s.mouseOver {
  174. s.applyStyle(&s.styles.Over)
  175. return
  176. }
  177. s.applyStyle(&s.styles.Normal)
  178. }
  179. // applyStyle applies the specified splitter style
  180. func (s *Splitter) applyStyle(ss *SplitterStyle) {
  181. s.spacer.SetBordersColor4(&ss.SpacerBorderColor)
  182. s.spacer.SetColor(&ss.SpacerColor)
  183. if s.horiz {
  184. s.spacer.SetWidth(ss.SpacerSize)
  185. } else {
  186. s.spacer.SetHeight(ss.SpacerSize)
  187. }
  188. }
  189. // recalc relcalculates the position and sizes of the internal panels
  190. func (s *Splitter) recalc() {
  191. width := s.Width()
  192. height := s.Height()
  193. if s.horiz {
  194. halfspace := s.spacer.Width() / 2
  195. // First panel
  196. s.P0.SetPosition(0, 0)
  197. s.P0.SetSize(s.pos-halfspace, height)
  198. // Spacer panel
  199. s.spacer.SetPosition(s.pos-halfspace, 0)
  200. s.spacer.SetHeight(height)
  201. // Second panel
  202. s.P1.SetPosition(s.pos+halfspace, 0)
  203. s.P1.SetSize(width-s.pos-halfspace, height)
  204. } else {
  205. halfspace := s.spacer.Height() / 2
  206. // First panel
  207. s.P0.SetPosition(0, 0)
  208. s.P0.SetSize(width, s.pos-halfspace)
  209. // Spacer panel
  210. s.spacer.SetPosition(0, s.pos-halfspace)
  211. s.spacer.SetWidth(width)
  212. // Second panel
  213. s.P1.SetPosition(0, s.pos+halfspace)
  214. s.P1.SetSize(width, height-s.pos-halfspace)
  215. }
  216. }