scrollbar.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. style *ScrollBarStyle // pointer to current style
  22. vertical bool // type of scrollbar
  23. button scrollBarButton // scrollbar button
  24. }
  25. type scrollBarButton struct {
  26. Panel // Embedded panel
  27. sb *ScrollBar // pointer to parent scroll bar
  28. pressed bool // mouse button pressed flag
  29. mouseX float32 // last mouse click x position
  30. mouseY float32 // last mouse click y position
  31. Size float32 // button size
  32. }
  33. type ScrollBarStyle struct {
  34. Paddings BorderSizes
  35. Borders BorderSizes
  36. BordersColor math32.Color4
  37. Color math32.Color
  38. Button ScrollBarButtonStyle
  39. }
  40. type ScrollBarButtonStyle struct {
  41. Borders BorderSizes
  42. BordersColor math32.Color4
  43. Color math32.Color
  44. Size float32 // This is the default/minimum button size
  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.style = &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.style.Button.Size
  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.style.Button.Size {
  85. sb.button.Size = size
  86. } else {
  87. sb.button.Size = sb.style.Button.Size
  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. return float64(sb.button.Position().Y) / (float64(sb.content.Height) - float64(sb.button.height))
  96. } else {
  97. return float64(sb.button.Position().X) / (float64(sb.content.Width) - float64(sb.button.width))
  98. }
  99. }
  100. // SetValue sets the position of the button of the scrollbar
  101. // from 0.0 (minimum) to 1.0 (maximum).
  102. func (sb *ScrollBar) SetValue(v float32) {
  103. v = math32.Clamp(v, 0.0, 1.0)
  104. if sb.vertical {
  105. pos := v * (float32(sb.content.Height) - float32(sb.button.height))
  106. sb.button.SetPositionY(pos)
  107. } else {
  108. pos := v * (float32(sb.content.Width) - float32(sb.button.width))
  109. sb.button.SetPositionX(pos)
  110. }
  111. }
  112. // onMouse receives subscribed mouse events over the scrollbar outer panel
  113. func (sb *ScrollBar) onMouse(evname string, ev interface{}) {
  114. e := ev.(*window.MouseEvent)
  115. if e.Button != window.MouseButtonLeft {
  116. return
  117. }
  118. if sb.vertical {
  119. posy := e.Ypos - sb.pospix.Y
  120. newY := math32.Clamp(posy-(sb.button.height/2), 0, sb.content.Height-sb.button.height)
  121. sb.button.SetPositionY(newY)
  122. } else {
  123. posx := e.Xpos - sb.pospix.X
  124. newX := math32.Clamp(posx-(sb.button.width/2), 0, sb.content.Width-sb.button.width)
  125. sb.button.SetPositionX(newX)
  126. }
  127. sb.root.StopPropagation(StopAll)
  128. sb.Dispatch(OnChange, nil)
  129. }
  130. // recalc recalculates sizes and positions
  131. func (sb *ScrollBar) recalc() {
  132. if sb.vertical {
  133. sb.button.SetSize(sb.content.Width, sb.button.Size)
  134. } else {
  135. sb.button.SetSize(sb.button.Size, sb.content.Height)
  136. }
  137. }
  138. // update updates border sizes and colors
  139. func (sb *ScrollBar) update() {
  140. sb.SetBordersFrom(&sb.style.Borders)
  141. sb.SetPaddingsFrom(&sb.style.Paddings)
  142. sb.SetBordersColor4(&sb.style.BordersColor)
  143. sb.SetColor(&sb.style.Color)
  144. sb.button.SetBordersFrom(&sb.style.Button.Borders)
  145. sb.button.SetBordersColor4(&sb.style.Button.BordersColor)
  146. sb.button.SetColor(&sb.style.Button.Color)
  147. }
  148. // onMouse receives subscribed mouse events for the scroll bar button
  149. func (button *scrollBarButton) onMouse(evname string, ev interface{}) {
  150. e := ev.(*window.MouseEvent)
  151. if e.Button != window.MouseButtonLeft {
  152. return
  153. }
  154. switch evname {
  155. case OnMouseDown:
  156. button.pressed = true
  157. button.mouseX = e.Xpos
  158. button.mouseY = e.Ypos
  159. button.sb.root.SetMouseFocus(button)
  160. case OnMouseUp:
  161. button.pressed = false
  162. button.sb.root.SetMouseFocus(nil)
  163. default:
  164. return
  165. }
  166. button.sb.root.StopPropagation(StopAll)
  167. }
  168. // onCursor receives subscribed cursor events for the scroll bar button
  169. func (button *scrollBarButton) onCursor(evname string, ev interface{}) {
  170. e := ev.(*window.CursorEvent)
  171. if !button.pressed {
  172. return
  173. }
  174. if button.sb.vertical {
  175. dy := button.mouseY - e.Ypos
  176. py := button.Position().Y
  177. button.SetPositionY(math32.Clamp(py-dy, 0, button.sb.content.Height-button.Size))
  178. } else {
  179. dx := button.mouseX - e.Xpos
  180. px := button.Position().X
  181. button.SetPositionX(math32.Clamp(px-dx, 0, button.sb.content.Width-button.Size))
  182. }
  183. button.mouseX = e.Xpos
  184. button.mouseY = e.Ypos
  185. button.sb.Dispatch(OnChange, nil)
  186. button.sb.root.StopPropagation(StopAll)
  187. }