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