scroller.go 13 KB

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