splitter.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. cev := ev.(*window.CursorEvent)
  119. switch evname {
  120. case OnCursorEnter:
  121. if s.horiz {
  122. s.root.SetCursorHResize()
  123. } else {
  124. s.root.SetCursorVResize()
  125. }
  126. s.mouseOver = true
  127. s.update()
  128. case OnCursorLeave:
  129. s.root.SetCursorNormal()
  130. s.mouseOver = false
  131. s.update()
  132. case OnCursor:
  133. if !s.pressed {
  134. return
  135. }
  136. var delta float32
  137. if s.horiz {
  138. delta = cev.Xpos - s.posLast
  139. s.posLast = cev.Xpos
  140. } else {
  141. delta = cev.Ypos - s.posLast
  142. s.posLast = cev.Ypos
  143. }
  144. s.setSplit(s.pos + delta)
  145. s.recalc()
  146. default:
  147. return
  148. }
  149. s.root.StopPropagation(Stop3D)
  150. }
  151. // setSplit sets the validated and clamped split position from the received value.
  152. func (s *Splitter) setSplit(pos float32) {
  153. var max float32
  154. var halfspace float32
  155. if s.horiz {
  156. halfspace = s.spacer.Width() / 2
  157. max = s.Width()
  158. } else {
  159. halfspace = s.spacer.Height() / 2
  160. max = s.Height()
  161. }
  162. if pos > max-halfspace {
  163. s.pos = max - halfspace
  164. } else if pos <= halfspace {
  165. s.pos = halfspace
  166. } else {
  167. s.pos = pos
  168. }
  169. }
  170. // update updates the splitter visual state
  171. func (s *Splitter) update() {
  172. if s.pressed {
  173. s.applyStyle(&s.styles.Drag)
  174. return
  175. }
  176. if s.mouseOver {
  177. s.applyStyle(&s.styles.Over)
  178. return
  179. }
  180. s.applyStyle(&s.styles.Normal)
  181. }
  182. // applyStyle applies the specified splitter style
  183. func (s *Splitter) applyStyle(ss *SplitterStyle) {
  184. s.spacer.SetBordersColor4(&ss.SpacerBorderColor)
  185. s.spacer.SetColor(&ss.SpacerColor)
  186. if s.horiz {
  187. s.spacer.SetWidth(ss.SpacerSize)
  188. } else {
  189. s.spacer.SetHeight(ss.SpacerSize)
  190. }
  191. }
  192. // recalc relcalculates the position and sizes of the internal panels
  193. func (s *Splitter) recalc() {
  194. width := s.Width()
  195. height := s.Height()
  196. if s.horiz {
  197. halfspace := s.spacer.Width() / 2
  198. // First panel
  199. s.P0.SetPosition(0, 0)
  200. s.P0.SetSize(s.pos-halfspace, height)
  201. // Spacer panel
  202. s.spacer.SetPosition(s.pos-halfspace, 0)
  203. s.spacer.SetHeight(height)
  204. // Second panel
  205. s.P1.SetPosition(s.pos+halfspace, 0)
  206. s.P1.SetSize(width-s.pos-halfspace, height)
  207. } else {
  208. halfspace := s.spacer.Height() / 2
  209. // First panel
  210. s.P0.SetPosition(0, 0)
  211. s.P0.SetSize(width, s.pos-halfspace)
  212. // Spacer panel
  213. s.spacer.SetPosition(0, s.pos-halfspace)
  214. s.spacer.SetWidth(width)
  215. // Second panel
  216. s.P1.SetPosition(0, s.pos+halfspace)
  217. s.P1.SetSize(width, height-s.pos-halfspace)
  218. }
  219. }