itemscroller.go 14 KB

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