scroller.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. "math"
  9. )
  10. type Scroller struct {
  11. Panel // Embedded panel
  12. vert bool // vertical/horizonta scroller flag
  13. styles *ScrollerStyles // pointer to current styles
  14. items []IPanel // list of panels in the scroller
  15. hscroll *ScrollBar // horizontal scroll bar
  16. vscroll *ScrollBar // vertical scroll bar
  17. maxAutoWidth float32 // maximum auto width (if 0, auto width disabled)
  18. maxAutoHeight float32 // maximum auto height (if 0, auto width disabled)
  19. first int // first visible item position
  20. adjustItem bool // adjust item to width or height
  21. focus bool // has keyboard focus
  22. cursorOver bool // mouse is over the list
  23. scrollBarEvent bool
  24. }
  25. type ScrollerStyle struct {
  26. Border BorderSizes
  27. Paddings BorderSizes
  28. BorderColor math32.Color4
  29. BgColor math32.Color
  30. FgColor math32.Color
  31. }
  32. type ScrollerStyles struct {
  33. Normal ScrollerStyle
  34. Over ScrollerStyle
  35. Focus ScrollerStyle
  36. Disabled ScrollerStyle
  37. }
  38. // NewVScroller creates and returns a pointer to a new vertical scroller panel
  39. // with the specified dimensions.
  40. func NewVScroller(width, height float32) *Scroller {
  41. return newScroller(true, width, height)
  42. }
  43. // NewHScroller creates and returns a pointer to a new horizontal scroller panel
  44. // with the specified dimensions.
  45. func NewHScroller(width, height float32) *Scroller {
  46. return newScroller(false, width, height)
  47. }
  48. // newScroller creates and returns a pointer to a new Scroller panel
  49. // with the specified layout orientation and initial dimensions
  50. func newScroller(vert bool, width, height float32) *Scroller {
  51. s := new(Scroller)
  52. s.initialize(vert, width, height)
  53. return s
  54. }
  55. // Clear removes and disposes of all the scroller children
  56. func (s *Scroller) Clear() {
  57. s.Panel.DisposeChildren(true)
  58. s.hscroll = nil
  59. s.vscroll = nil
  60. s.items = s.items[0:0]
  61. }
  62. // Len return the number of items in the scroller
  63. func (s *Scroller) Len() int {
  64. return len(s.items)
  65. }
  66. // Add appends the specified item to the end of the scroller
  67. func (s *Scroller) Add(item IPanel) {
  68. s.InsertAt(len(s.items), item)
  69. }
  70. // InsertAt inserts an item at the specified position
  71. func (s *Scroller) InsertAt(pos int, item IPanel) {
  72. // Validates position
  73. if pos < 0 || pos > len(s.items) {
  74. panic("Scroller.InsertAt(): Invalid position")
  75. }
  76. item.GetPanel().SetVisible(false)
  77. // Insert item in the items array
  78. s.items = append(s.items, nil)
  79. copy(s.items[pos+1:], s.items[pos:])
  80. s.items[pos] = item
  81. // Insert item in the scroller
  82. s.Panel.Add(item)
  83. s.autoSize()
  84. s.recalc()
  85. // Scroll bar should be on the foreground,
  86. // in relation of all the other child panels.
  87. if s.vscroll != nil {
  88. s.Panel.SetTopChild(s.vscroll)
  89. }
  90. if s.hscroll != nil {
  91. s.Panel.SetTopChild(s.hscroll)
  92. }
  93. }
  94. // RemoveAt removes item from the specified position
  95. func (s *Scroller) RemoveAt(pos int) {
  96. // Validates position
  97. if pos < 0 || pos >= len(s.items) {
  98. panic("Scroller.RemoveAt(): Invalid position")
  99. }
  100. // Remove event listener
  101. item := s.items[pos]
  102. // Remove item from the items array
  103. copy(s.items[pos:], s.items[pos+1:])
  104. s.items[len(s.items)-1] = nil
  105. s.items = s.items[:len(s.items)-1]
  106. // Remove item from the scroller children
  107. s.Panel.Remove(item)
  108. s.autoSize()
  109. s.recalc()
  110. }
  111. // Remove removes the specified item from the Scroller
  112. func (s *Scroller) Remove(item IPanel) {
  113. for p, curr := range s.items {
  114. if curr == item {
  115. s.RemoveAt(p)
  116. return
  117. }
  118. }
  119. }
  120. // GetItem returns the item at the specified position.
  121. // Returns nil if the position is invalid.
  122. func (s *Scroller) ItemAt(pos int) IPanel {
  123. if pos < 0 || pos >= len(s.items) {
  124. return nil
  125. }
  126. return s.items[pos]
  127. }
  128. // ItemPosition returns the position of the specified item in
  129. // the scroller of -1 if not found
  130. func (s *Scroller) ItemPosition(item IPanel) int {
  131. for pos := 0; pos < len(s.items); pos++ {
  132. if s.items[pos] == item {
  133. return pos
  134. }
  135. }
  136. return -1
  137. }
  138. // First returns the position of the first visible item
  139. func (s *Scroller) First() int {
  140. return s.first
  141. }
  142. // SetFirst set the position of first visible if possible
  143. func (s *Scroller) SetFirst(pos int) {
  144. if pos >= 0 && pos <= s.maxFirst() {
  145. s.first = pos
  146. s.recalc()
  147. }
  148. }
  149. // ScrollDown scrolls the list down one item if possible
  150. func (s *Scroller) ScrollDown() {
  151. max := s.maxFirst()
  152. if s.first >= max {
  153. return
  154. }
  155. s.first++
  156. s.recalc()
  157. }
  158. // ScrollUp scrolls the list up one item if possible
  159. func (s *Scroller) ScrollUp() {
  160. if s.first == 0 {
  161. return
  162. }
  163. s.first--
  164. s.recalc()
  165. }
  166. // ItemVisible returns indication if the item at the specified
  167. // position is completely visible or not
  168. func (s *Scroller) ItemVisible(pos int) bool {
  169. if pos < s.first {
  170. return false
  171. }
  172. // Vertical scroller
  173. if s.vert {
  174. var height float32 = 0
  175. for i := s.first; i < len(s.items); i++ {
  176. item := s.items[pos]
  177. height += item.GetPanel().height
  178. if height > s.height {
  179. return false
  180. }
  181. if pos == i {
  182. return true
  183. }
  184. }
  185. return false
  186. // Horizontal scroller
  187. } else {
  188. var width float32 = 0
  189. for i := s.first; i < len(s.items); i++ {
  190. item := s.items[pos]
  191. width += item.GetPanel().width
  192. if width > s.width {
  193. return false
  194. }
  195. if pos == i {
  196. return true
  197. }
  198. }
  199. return false
  200. }
  201. }
  202. // SetStyles set the scroller styles overriding the default style
  203. func (s *Scroller) SetStyles(ss *ScrollerStyles) {
  204. s.styles = ss
  205. s.update()
  206. }
  207. func (s *Scroller) ApplyStyle(style int) {
  208. switch style {
  209. case OverStyle:
  210. s.applyStyle(&s.styles.Over)
  211. case FocusStyle:
  212. s.applyStyle(&s.styles.Focus)
  213. case NormalStyle:
  214. s.applyStyle(&s.styles.Normal)
  215. case DefaultStyle:
  216. s.update()
  217. }
  218. }
  219. func (s *Scroller) SetAutoWidth(maxWidth float32) {
  220. s.maxAutoWidth = maxWidth
  221. }
  222. func (s *Scroller) SetAutoHeight(maxHeight float32) {
  223. s.maxAutoHeight = maxHeight
  224. }
  225. // initialize initializes this scroller and is normally used by other types which contains a scroller
  226. func (s *Scroller) initialize(vert bool, width, height float32) {
  227. s.vert = vert
  228. s.Panel.Initialize(width, height)
  229. s.styles = &StyleDefault.Scroller
  230. s.Panel.Subscribe(OnCursorEnter, s.onCursor)
  231. s.Panel.Subscribe(OnCursorLeave, s.onCursor)
  232. s.Panel.Subscribe(OnScroll, s.onScroll)
  233. s.Panel.Subscribe(OnResize, s.onResize)
  234. s.update()
  235. s.recalc()
  236. }
  237. // onCursor receives subscribed cursor events over the panel
  238. func (s *Scroller) onCursor(evname string, ev interface{}) {
  239. switch evname {
  240. case OnCursorEnter:
  241. s.root.SetScrollFocus(s)
  242. s.cursorOver = true
  243. s.update()
  244. case OnCursorLeave:
  245. s.root.SetScrollFocus(nil)
  246. s.cursorOver = false
  247. s.update()
  248. }
  249. s.root.StopPropagation(Stop3D)
  250. }
  251. // onScroll receives subscriber mouse scroll events when this scroller has
  252. // the scroll focus (set by OnMouseEnter)
  253. func (s *Scroller) onScroll(evname string, ev interface{}) {
  254. sev := ev.(*window.ScrollEvent)
  255. if sev.Yoffset > 0 {
  256. s.ScrollUp()
  257. } else if sev.Yoffset < 0 {
  258. s.ScrollDown()
  259. }
  260. s.root.StopPropagation(Stop3D)
  261. }
  262. // onScroll receives resize events
  263. func (s *Scroller) onResize(evname string, ev interface{}) {
  264. s.recalc()
  265. }
  266. // autoSize resizes the scroller if necessary
  267. func (s *Scroller) autoSize() {
  268. if s.maxAutoWidth == 0 && s.maxAutoHeight == 0 {
  269. return
  270. }
  271. var width float32 = 0
  272. var height float32 = 0
  273. for _, item := range s.items {
  274. panel := item.GetPanel()
  275. if panel.Width() > width {
  276. width = panel.Width()
  277. }
  278. height += panel.TotalHeight()
  279. }
  280. // If auto maximum width enabled
  281. if s.maxAutoWidth > 0 {
  282. if width <= s.maxAutoWidth {
  283. s.SetContentWidth(width)
  284. }
  285. }
  286. // If auto maximum height enabled
  287. if s.maxAutoHeight > 0 {
  288. if height <= s.maxAutoHeight {
  289. s.SetContentHeight(height)
  290. }
  291. }
  292. }
  293. // recalc recalculates the positions and visibilities of all the items
  294. func (s *Scroller) recalc() {
  295. if s.vert {
  296. s.vRecalc()
  297. } else {
  298. s.hRecalc()
  299. }
  300. }
  301. // vRecalc recalculates for the vertical scroller
  302. func (s *Scroller) vRecalc() {
  303. // Checks if scroll bar should be visible or not
  304. scroll := false
  305. if s.first > 0 {
  306. scroll = true
  307. } else {
  308. var posY float32 = 0
  309. for _, item := range s.items[s.first:] {
  310. posY += item.TotalHeight()
  311. if posY >= s.height {
  312. scroll = true
  313. break
  314. }
  315. }
  316. }
  317. s.setVScrollBar(scroll)
  318. // Items width
  319. width := s.ContentWidth()
  320. if scroll {
  321. width -= s.vscroll.Width()
  322. }
  323. var posY float32 = 0
  324. // Sets positions of all items
  325. for pos, ipan := range s.items {
  326. item := ipan.GetPanel()
  327. if pos < s.first {
  328. item.SetVisible(false)
  329. continue
  330. }
  331. // If item is after last visible, sets not visible
  332. if posY > s.height {
  333. item.SetVisible(false)
  334. continue
  335. }
  336. // Sets item position
  337. item.SetVisible(true)
  338. item.SetPosition(0, posY)
  339. if s.adjustItem {
  340. item.SetWidth(width)
  341. }
  342. posY += ipan.TotalHeight()
  343. }
  344. // Set scroll bar value if recalc was not due by scroll event
  345. if scroll && !s.scrollBarEvent {
  346. s.vscroll.SetValue(float32(s.first) / float32(s.maxFirst()))
  347. }
  348. s.scrollBarEvent = false
  349. }
  350. // hRecalc recalculates for the horizontal scroller
  351. func (s *Scroller) hRecalc() {
  352. // Checks if scroll bar should be visible or not
  353. scroll := false
  354. if s.first > 0 {
  355. scroll = true
  356. } else {
  357. var posX float32 = 0
  358. for _, item := range s.items[s.first:] {
  359. posX += item.GetPanel().Width()
  360. if posX >= s.width {
  361. scroll = true
  362. break
  363. }
  364. }
  365. }
  366. s.setHScrollBar(scroll)
  367. // Items height
  368. height := s.ContentHeight()
  369. if scroll {
  370. height -= s.hscroll.Height()
  371. }
  372. var posX float32 = 0
  373. // Sets positions of all items
  374. for pos, ipan := range s.items {
  375. item := ipan.GetPanel()
  376. // If item is before first visible, sets not visible
  377. if pos < s.first {
  378. item.SetVisible(false)
  379. continue
  380. }
  381. // If item is after last visible, sets not visible
  382. if posX > s.width {
  383. item.SetVisible(false)
  384. continue
  385. }
  386. // Sets item position
  387. item.SetVisible(true)
  388. item.SetPosition(posX, 0)
  389. if s.adjustItem {
  390. item.SetHeight(height)
  391. }
  392. posX += item.Width()
  393. }
  394. // Set scroll bar value if recalc was not due by scroll event
  395. if scroll && !s.scrollBarEvent {
  396. s.hscroll.SetValue(float32(s.first) / float32(s.maxFirst()))
  397. }
  398. s.scrollBarEvent = false
  399. }
  400. // maxFirst returns the maximum position of the first visible item
  401. func (s *Scroller) maxFirst() int {
  402. // Vertical scroller
  403. if s.vert {
  404. var height float32 = 0
  405. pos := len(s.items) - 1
  406. if pos < 0 {
  407. return 0
  408. }
  409. for {
  410. item := s.items[pos]
  411. height += item.GetPanel().Height()
  412. if height > s.Height() {
  413. break
  414. }
  415. pos--
  416. if pos < 0 {
  417. break
  418. }
  419. }
  420. return pos + 1
  421. // Horizontal scroller
  422. } else {
  423. var width float32 = 0
  424. pos := len(s.items) - 1
  425. if pos < 0 {
  426. return 0
  427. }
  428. for {
  429. item := s.items[pos]
  430. width += item.GetPanel().Width()
  431. if width > s.Width() {
  432. break
  433. }
  434. pos--
  435. if pos < 0 {
  436. break
  437. }
  438. }
  439. return pos + 1
  440. }
  441. }
  442. // setVScrollBar sets the visibility state of the vertical scrollbar
  443. func (s *Scroller) setVScrollBar(state bool) {
  444. // Visible
  445. if state {
  446. var scrollWidth float32 = 20
  447. if s.vscroll == nil {
  448. s.vscroll = NewVScrollBar(0, 0)
  449. s.vscroll.SetBorders(0, 0, 0, 1)
  450. s.vscroll.Subscribe(OnChange, s.onScrollBarEvent)
  451. s.Panel.Add(s.vscroll)
  452. }
  453. s.vscroll.SetSize(scrollWidth, s.ContentHeight())
  454. s.vscroll.SetPositionX(s.ContentWidth() - scrollWidth)
  455. s.vscroll.SetPositionY(0)
  456. s.vscroll.recalc()
  457. s.vscroll.SetVisible(true)
  458. // Not visible
  459. } else {
  460. if s.vscroll != nil {
  461. s.vscroll.SetVisible(false)
  462. }
  463. }
  464. }
  465. // setHScrollBar sets the visibility state of the horizontal scrollbar
  466. func (s *Scroller) setHScrollBar(state bool) {
  467. // Visible
  468. if state {
  469. var scrollHeight float32 = 20
  470. if s.hscroll == nil {
  471. s.hscroll = NewHScrollBar(0, 0)
  472. s.hscroll.SetBorders(1, 0, 0, 0)
  473. s.hscroll.Subscribe(OnChange, s.onScrollBarEvent)
  474. s.Panel.Add(s.hscroll)
  475. }
  476. s.hscroll.SetSize(s.ContentWidth(), scrollHeight)
  477. s.hscroll.SetPositionX(0)
  478. s.hscroll.SetPositionY(s.ContentHeight() - scrollHeight)
  479. s.hscroll.recalc()
  480. s.hscroll.SetVisible(true)
  481. // Not visible
  482. } else {
  483. if s.hscroll != nil {
  484. s.hscroll.SetVisible(false)
  485. }
  486. }
  487. }
  488. // onScrollEvent is called when the list scrollbar value changes
  489. func (s *Scroller) onScrollBarEvent(evname string, ev interface{}) {
  490. var pos float64
  491. if s.vert {
  492. pos = s.vscroll.Value()
  493. } else {
  494. pos = s.hscroll.Value()
  495. }
  496. first := int(math.Floor((float64(s.maxFirst()) * pos) + 0.5))
  497. if first == s.first {
  498. return
  499. }
  500. s.scrollBarEvent = true
  501. s.first = first
  502. s.recalc()
  503. }
  504. // update updates the visual state the list and its items
  505. func (s *Scroller) update() {
  506. if s.cursorOver {
  507. s.applyStyle(&s.styles.Over)
  508. return
  509. }
  510. if s.focus {
  511. s.applyStyle(&s.styles.Focus)
  512. return
  513. }
  514. s.applyStyle(&s.styles.Normal)
  515. }
  516. // applyStyle sets the specified style
  517. func (s *Scroller) applyStyle(st *ScrollerStyle) {
  518. s.SetBordersFrom(&st.Border)
  519. s.SetBordersColor4(&st.BorderColor)
  520. s.SetPaddingsFrom(&st.Paddings)
  521. s.SetColor(&st.BgColor)
  522. }