slider.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/window"
  7. )
  8. /***************************************
  9. Slider
  10. +--------------------------------+
  11. | +--------------------------+ |
  12. | | +----------+ | |
  13. | | | | | |
  14. | | | | | |
  15. | | +----------+ | |
  16. | +--------------------------+ |
  17. +--------------------------------+
  18. **/
  19. // Slider is the GUI element for sliders and progress bars
  20. type Slider struct {
  21. Panel // Embedded panel
  22. slider Panel // embedded slider panel
  23. label *Label // optional label
  24. horiz bool // orientation
  25. styles *SliderStyles // pointer to styles
  26. pos float32 // current slider position
  27. posLast float32 // last position of the mouse cursor when dragging
  28. pressed bool // mouse button is pressed and dragging
  29. cursorOver bool // mouse is over slider
  30. scaleFactor float32 // scale factor (default = 1.0)
  31. }
  32. // SliderStyle contains the styling of a Slider
  33. type SliderStyle BasicStyle
  34. // SliderStyles contains a SliderStyle for each valid GUI state
  35. type SliderStyles struct {
  36. Normal SliderStyle
  37. Over SliderStyle
  38. Focus SliderStyle
  39. Disabled SliderStyle
  40. }
  41. // NewHSlider creates and returns a pointer to a new horizontal slider
  42. // with the specified initial dimensions.
  43. func NewHSlider(width, height float32) *Slider {
  44. return newSlider(true, width, height)
  45. }
  46. // NewVSlider creates and returns a pointer to a new vertical slider
  47. // with the specified initial dimensions.
  48. func NewVSlider(width, height float32) *Slider {
  49. return newSlider(false, width, height)
  50. }
  51. // NewSlider creates and returns a pointer to a new slider with the
  52. // specified initial dimensions.
  53. func newSlider(horiz bool, width, height float32) *Slider {
  54. s := new(Slider)
  55. s.horiz = horiz
  56. s.styles = &StyleDefault().Slider
  57. s.scaleFactor = 1.0
  58. // Initialize main panel
  59. s.Panel.Initialize(width, height)
  60. s.Panel.Subscribe(OnMouseDown, s.onMouse)
  61. s.Panel.Subscribe(OnMouseUp, s.onMouse)
  62. s.Panel.Subscribe(OnCursor, s.onCursor)
  63. s.Panel.Subscribe(OnCursorEnter, s.onCursor)
  64. s.Panel.Subscribe(OnCursorLeave, s.onCursor)
  65. s.Panel.Subscribe(OnScroll, s.onScroll)
  66. s.Panel.Subscribe(OnKeyDown, s.onKey)
  67. s.Panel.Subscribe(OnKeyRepeat, s.onKey)
  68. s.Panel.Subscribe(OnResize, s.onResize)
  69. s.Panel.Subscribe(OnEnable, func(evname string, ev interface{}) { s.update() })
  70. // Initialize slider panel
  71. s.slider.Initialize(0, 0)
  72. s.Panel.Add(&s.slider)
  73. s.recalc()
  74. s.update()
  75. return s
  76. }
  77. // SetStyles set the slider styles overriding the default style
  78. func (s *Slider) SetStyles(ss *SliderStyles) *Slider {
  79. s.styles = ss
  80. s.update()
  81. return s
  82. }
  83. // SetText sets the text of the slider optional label
  84. func (s *Slider) SetText(text string) *Slider {
  85. if s.label == nil {
  86. s.label = NewLabel(text)
  87. s.Panel.Add(s.label)
  88. } else {
  89. s.label.SetText(text)
  90. }
  91. s.update()
  92. s.recalc()
  93. return s
  94. }
  95. // SetValue sets the value of the slider considering the current scale factor
  96. // and updates its visual appearance.
  97. func (s *Slider) SetValue(value float32) *Slider {
  98. pos := value / s.scaleFactor
  99. s.setPos(pos)
  100. return s
  101. }
  102. // Value returns the current value of the slider considering the current scale factor
  103. func (s *Slider) Value() float32 {
  104. return s.pos * s.scaleFactor
  105. }
  106. // SetScaleFactor set the slider scale factor (default = 1.0)
  107. func (s *Slider) SetScaleFactor(factor float32) *Slider {
  108. s.scaleFactor = factor
  109. return s
  110. }
  111. // ScaleFactor returns the slider current scale factor (default = 1.0)
  112. func (s *Slider) ScaleFactor() float32 {
  113. return s.scaleFactor
  114. }
  115. // setPos sets the slider position from 0.0 to 1.0
  116. // and updates its visual appearance.
  117. func (s *Slider) setPos(pos float32) {
  118. const eps = 0.01
  119. if pos < 0 {
  120. pos = 0
  121. } else if pos > 1.0 {
  122. pos = 1
  123. }
  124. if pos > (s.pos+eps) && pos < (s.pos+eps) {
  125. return
  126. }
  127. s.pos = pos
  128. s.recalc()
  129. s.Dispatch(OnChange, nil)
  130. }
  131. // onMouse process subscribed mouse events over the outer panel
  132. func (s *Slider) onMouse(evname string, ev interface{}) {
  133. mev := ev.(*window.MouseEvent)
  134. switch evname {
  135. case OnMouseDown:
  136. s.pressed = true
  137. if s.horiz {
  138. s.posLast = mev.Xpos
  139. } else {
  140. s.posLast = mev.Ypos
  141. }
  142. s.root.SetMouseFocus(s)
  143. s.root.SetKeyFocus(s)
  144. case OnMouseUp:
  145. s.pressed = false
  146. if !s.cursorOver {
  147. s.root.SetCursorNormal()
  148. }
  149. s.root.SetMouseFocus(nil)
  150. default:
  151. return
  152. }
  153. s.root.StopPropagation(Stop3D)
  154. }
  155. // onCursor process subscribed cursor events
  156. func (s *Slider) onCursor(evname string, ev interface{}) {
  157. if evname == OnCursorEnter {
  158. s.root.SetScrollFocus(s)
  159. if s.horiz {
  160. s.root.SetCursorHResize()
  161. } else {
  162. s.root.SetCursorVResize()
  163. }
  164. s.cursorOver = true
  165. s.update()
  166. } else if evname == OnCursorLeave {
  167. s.root.SetScrollFocus(nil)
  168. s.root.SetCursorNormal()
  169. s.cursorOver = false
  170. s.update()
  171. } else if evname == OnCursor {
  172. if !s.pressed {
  173. return
  174. }
  175. cev := ev.(*window.CursorEvent)
  176. var pos float32
  177. if s.horiz {
  178. delta := cev.Xpos - s.posLast
  179. s.posLast = cev.Xpos
  180. newpos := s.slider.Width() + delta
  181. pos = newpos / s.Panel.ContentWidth()
  182. } else {
  183. delta := cev.Ypos - s.posLast
  184. s.posLast = cev.Ypos
  185. newpos := s.slider.Height() - delta
  186. pos = newpos / s.Panel.ContentHeight()
  187. }
  188. s.setPos(pos)
  189. }
  190. s.root.StopPropagation(Stop3D)
  191. }
  192. // onScroll process subscribed scroll events
  193. func (s *Slider) onScroll(evname string, ev interface{}) {
  194. sev := ev.(*window.ScrollEvent)
  195. v := s.pos
  196. v += sev.Yoffset * 0.01
  197. s.setPos(v)
  198. s.root.StopPropagation(Stop3D)
  199. }
  200. // onKey process subscribed key events
  201. func (s *Slider) onKey(evname string, ev interface{}) {
  202. kev := ev.(*window.KeyEvent)
  203. delta := float32(0.01)
  204. // Horizontal slider
  205. if s.horiz {
  206. switch kev.Keycode {
  207. case window.KeyLeft:
  208. s.setPos(s.pos - delta)
  209. case window.KeyRight:
  210. s.setPos(s.pos + delta)
  211. default:
  212. return
  213. }
  214. // Vertical slider
  215. } else {
  216. switch kev.Keycode {
  217. case window.KeyDown:
  218. s.setPos(s.pos - delta)
  219. case window.KeyUp:
  220. s.setPos(s.pos + delta)
  221. default:
  222. return
  223. }
  224. }
  225. s.root.StopPropagation(Stop3D)
  226. }
  227. // onResize process subscribed resize events
  228. func (s *Slider) onResize(evname string, ev interface{}) {
  229. s.recalc()
  230. }
  231. // update updates the slider visual state
  232. func (s *Slider) update() {
  233. if !s.Enabled() {
  234. s.applyStyle(&s.styles.Disabled)
  235. return
  236. }
  237. if s.cursorOver {
  238. s.applyStyle(&s.styles.Over)
  239. return
  240. }
  241. s.applyStyle(&s.styles.Normal)
  242. }
  243. // applyStyle applies the specified slider style
  244. func (s *Slider) applyStyle(ss *SliderStyle) {
  245. s.Panel.ApplyStyle(&ss.PanelStyle)
  246. s.slider.SetColor4(&ss.FgColor)
  247. }
  248. // recalc recalculates the dimensions and positions of the internal panels.
  249. func (s *Slider) recalc() {
  250. if s.horiz {
  251. if s.label != nil {
  252. lx := (s.Panel.ContentWidth() - s.label.Width()) / 2
  253. if s.Panel.ContentHeight() < s.label.Height() {
  254. s.Panel.SetContentHeight(s.label.Height())
  255. }
  256. ly := (s.Panel.ContentHeight() - s.label.Height()) / 2
  257. s.label.SetPosition(lx, ly)
  258. }
  259. width := s.Panel.ContentWidth() * s.pos
  260. s.slider.SetSize(width, s.Panel.ContentHeight())
  261. } else {
  262. if s.label != nil {
  263. if s.Panel.ContentWidth() < s.label.Width() {
  264. s.Panel.SetContentWidth(s.label.Width())
  265. }
  266. lx := (s.Panel.ContentWidth() - s.label.Width()) / 2
  267. ly := (s.Panel.ContentHeight() - s.label.Height()) / 2
  268. s.label.SetPosition(lx, ly)
  269. }
  270. height := s.Panel.ContentHeight() * s.pos
  271. s.slider.SetPositionY(s.Panel.ContentHeight() - height)
  272. s.slider.SetSize(s.Panel.ContentWidth(), height)
  273. }
  274. }