scrollbar.go 6.4 KB

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