scrollbar.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /***************************************
  10. ScrollBar Panel
  11. +--------------------------------+
  12. | scroll button |
  13. | +--------------+ |
  14. | | | |
  15. | | | |
  16. | +--------------+ |
  17. +--------------------------------+
  18. **/
  19. type ScrollBar struct {
  20. Panel // Embedded panel
  21. styles *ScrollBarStyles
  22. vertical bool // type of scrollbar
  23. button scrollBarButton // scrollbar button
  24. cursorOver bool
  25. }
  26. type scrollBarButton struct {
  27. Panel // Embedded panel
  28. sb *ScrollBar // pointer to parent scroll bar
  29. pressed bool // mouse button pressed flag
  30. mouseX float32 // last mouse click x position
  31. mouseY float32 // last mouse click y position
  32. Size float32 // button size
  33. MinSize float32 // minimum button size
  34. }
  35. type ScrollBarStyles struct {
  36. Normal ScrollBarStyle
  37. Over ScrollBarStyle
  38. Disabled ScrollBarStyle
  39. }
  40. type ScrollBarStyle struct {
  41. PanelStyle
  42. Button ScrollBarButtonStyle
  43. }
  44. type ScrollBarButtonStyle struct {
  45. Borders RectBounds
  46. BordersColor math32.Color4
  47. Color math32.Color4
  48. Size float32 // This is the default/minimum button size
  49. }
  50. // NewVScrollBar creates and returns a pointer to a new vertical scroll bar
  51. // with the specified dimensions.
  52. func NewVScrollBar(width, height float32) *ScrollBar {
  53. return newScrollBar(width, height, true)
  54. }
  55. // NewHScrollBar creates and returns a pointer to a new horizontal scroll bar
  56. // with the specified dimensions.
  57. func NewHScrollBar(width, height float32) *ScrollBar {
  58. return newScrollBar(width, height, false)
  59. }
  60. // newScrollBar creates and returns a pointer to a new scroll bar panel
  61. // with the specified width, height, orientation and target.
  62. func newScrollBar(width, height float32, vertical bool) *ScrollBar {
  63. sb := new(ScrollBar)
  64. sb.initialize(width, height, vertical)
  65. return sb
  66. }
  67. // initialize initializes this scrollbar
  68. func (sb *ScrollBar) initialize(width, height float32, vertical bool) {
  69. sb.styles = &StyleDefault().ScrollBar
  70. sb.vertical = vertical
  71. sb.Panel.Initialize(width, height)
  72. sb.Panel.Subscribe(OnMouseDown, sb.onMouse)
  73. // Initialize scrollbar button
  74. sb.button.Panel.Initialize(0, 0)
  75. sb.button.Panel.Subscribe(OnMouseDown, sb.button.onMouse)
  76. sb.button.Panel.Subscribe(OnMouseUp, sb.button.onMouse)
  77. sb.button.Panel.Subscribe(OnCursor, sb.button.onCursor)
  78. sb.button.SetMargins(1, 1, 1, 1)
  79. sb.button.Size = sb.styles.Normal.Button.Size
  80. sb.button.sb = sb
  81. sb.Add(&sb.button)
  82. sb.update()
  83. sb.recalc()
  84. }
  85. // SetButtonSize sets the button size
  86. func (sb *ScrollBar) SetButtonSize(size float32) {
  87. // Clamp to minimum size if requested size smaller than minimum
  88. if size > sb.button.MinSize {
  89. sb.button.Size = size
  90. } else {
  91. sb.button.Size = sb.button.MinSize
  92. }
  93. sb.recalc()
  94. }
  95. // Value returns the current position of the button in the scrollbar
  96. // The returned value is between 0.0 and 1.0
  97. func (sb *ScrollBar) Value() float64 {
  98. if sb.vertical {
  99. return float64(sb.button.Position().Y) / (float64(sb.content.Height) - float64(sb.button.height))
  100. } else {
  101. return float64(sb.button.Position().X) / (float64(sb.content.Width) - float64(sb.button.width))
  102. }
  103. }
  104. // SetValue sets the position of the button of the scrollbar
  105. // from 0.0 (minimum) to 1.0 (maximum).
  106. func (sb *ScrollBar) SetValue(v float32) {
  107. v = math32.Clamp(v, 0.0, 1.0)
  108. if sb.vertical {
  109. pos := v * (float32(sb.content.Height) - float32(sb.button.height))
  110. sb.button.SetPositionY(pos)
  111. } else {
  112. pos := v * (float32(sb.content.Width) - float32(sb.button.width))
  113. sb.button.SetPositionX(pos)
  114. }
  115. }
  116. // onMouse receives subscribed mouse events over the scrollbar outer panel
  117. func (sb *ScrollBar) onMouse(evname string, ev interface{}) {
  118. e := ev.(*window.MouseEvent)
  119. if e.Button != window.MouseButtonLeft {
  120. return
  121. }
  122. if sb.vertical {
  123. posy := e.Ypos - sb.pospix.Y
  124. newY := math32.Clamp(posy-(sb.button.height/2), 0, sb.content.Height-sb.button.height)
  125. sb.button.SetPositionY(newY)
  126. } else {
  127. posx := e.Xpos - sb.pospix.X
  128. newX := math32.Clamp(posx-(sb.button.width/2), 0, sb.content.Width-sb.button.width)
  129. sb.button.SetPositionX(newX)
  130. }
  131. sb.root.StopPropagation(StopAll)
  132. sb.Dispatch(OnChange, nil)
  133. }
  134. // recalc recalculates sizes and positions
  135. func (sb *ScrollBar) recalc() {
  136. if sb.vertical {
  137. sb.button.SetSize(sb.content.Width, sb.button.Size)
  138. } else {
  139. sb.button.SetSize(sb.button.Size, sb.content.Height)
  140. }
  141. }
  142. // update updates the visual state
  143. func (sb *ScrollBar) update() {
  144. // TODO disabling the scrollbar only affects style, needs to affect behavior
  145. if !sb.Enabled() {
  146. sb.applyStyle(&sb.styles.Disabled)
  147. return
  148. }
  149. // TODO cursorOver is never set to true for the scrollbar
  150. if sb.cursorOver {
  151. sb.applyStyle(&sb.styles.Over)
  152. return
  153. }
  154. sb.applyStyle(&sb.styles.Normal)
  155. }
  156. // update updates border sizes and colors
  157. func (sb *ScrollBar) applyStyle(sbs *ScrollBarStyle) {
  158. sb.Panel.ApplyStyle(&sbs.PanelStyle)
  159. sb.button.SetBordersFrom(&sbs.Button.Borders)
  160. sb.button.SetBordersColor4(&sbs.Button.BordersColor)
  161. sb.button.SetColor4(&sbs.Button.Color)
  162. sb.button.MinSize = sbs.Button.Size
  163. }
  164. // onMouse receives subscribed mouse events for the scroll bar button
  165. func (button *scrollBarButton) onMouse(evname string, ev interface{}) {
  166. e := ev.(*window.MouseEvent)
  167. if e.Button != window.MouseButtonLeft {
  168. return
  169. }
  170. switch evname {
  171. case OnMouseDown:
  172. button.pressed = true
  173. button.mouseX = e.Xpos
  174. button.mouseY = e.Ypos
  175. button.sb.root.SetMouseFocus(button)
  176. case OnMouseUp:
  177. button.pressed = false
  178. button.sb.root.SetMouseFocus(nil)
  179. default:
  180. return
  181. }
  182. button.sb.root.StopPropagation(StopAll)
  183. }
  184. // onCursor receives subscribed cursor events for the scroll bar button
  185. func (button *scrollBarButton) onCursor(evname string, ev interface{}) {
  186. e := ev.(*window.CursorEvent)
  187. if !button.pressed {
  188. return
  189. }
  190. if button.sb.vertical {
  191. dy := button.mouseY - e.Ypos
  192. py := button.Position().Y
  193. button.SetPositionY(math32.Clamp(py-dy, 0, button.sb.content.Height-button.Size))
  194. } else {
  195. dx := button.mouseX - e.Xpos
  196. px := button.Position().X
  197. button.SetPositionX(math32.Clamp(px-dx, 0, button.sb.content.Width-button.Size))
  198. }
  199. button.mouseX = e.Xpos
  200. button.mouseY = e.Ypos
  201. button.sb.Dispatch(OnChange, nil)
  202. button.sb.root.StopPropagation(StopAll)
  203. }