itemscroller.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. "math"
  7. "github.com/g3n/engine/window"
  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.cursorOver = true
  250. s.update()
  251. case OnCursorLeave:
  252. s.cursorOver = false
  253. s.update()
  254. }
  255. }
  256. // onScroll receives mouse scroll events
  257. func (s *ItemScroller) 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. }
  265. // onResize receives resize events
  266. func (s *ItemScroller) onResize(evname string, ev interface{}) {
  267. s.recalc()
  268. }
  269. // autoSize resizes the scroller if necessary
  270. func (s *ItemScroller) autoSize() {
  271. if s.maxAutoWidth == 0 && s.maxAutoHeight == 0 {
  272. return
  273. }
  274. var width float32
  275. var height float32
  276. for _, item := range s.items {
  277. panel := item.GetPanel()
  278. if panel.Width() > width {
  279. width = panel.Width()
  280. }
  281. height += panel.Height()
  282. }
  283. // If auto maximum width enabled
  284. if s.maxAutoWidth > 0 {
  285. if width <= s.maxAutoWidth {
  286. s.SetContentWidth(width)
  287. }
  288. }
  289. // If auto maximum height enabled
  290. if s.maxAutoHeight > 0 {
  291. if height <= s.maxAutoHeight {
  292. s.SetContentHeight(height)
  293. }
  294. }
  295. }
  296. // recalc recalculates the positions and visibilities of all the items
  297. func (s *ItemScroller) recalc() {
  298. if s.vert {
  299. s.vRecalc()
  300. } else {
  301. s.hRecalc()
  302. }
  303. }
  304. // vRecalc recalculates for the vertical scroller
  305. func (s *ItemScroller) vRecalc() {
  306. // Checks if scroll bar should be visible or not
  307. scroll := false
  308. if s.first > 0 {
  309. scroll = true
  310. } else {
  311. var posY float32
  312. for _, item := range s.items[s.first:] {
  313. posY += item.Height()
  314. if posY > s.height {
  315. scroll = true
  316. break
  317. }
  318. }
  319. }
  320. s.setVScrollBar(scroll)
  321. // Compute size of scroll button
  322. if scroll && s.autoButtonSize {
  323. var totalHeight float32
  324. for _, item := range s.items {
  325. // TODO OPTIMIZATION
  326. // Break when the view/content proportion becomes smaller than the minimum button size
  327. totalHeight += item.Height()
  328. }
  329. s.vscroll.SetButtonSize(s.height * s.height / totalHeight)
  330. }
  331. // Items width
  332. width := s.ContentWidth()
  333. if scroll {
  334. width -= s.vscroll.Width()
  335. }
  336. var posY float32
  337. // Sets positions of all items
  338. for pos, ipan := range s.items {
  339. item := ipan.GetPanel()
  340. if pos < s.first {
  341. item.SetVisible(false)
  342. continue
  343. }
  344. // If item is after last visible, sets not visible
  345. if posY > s.height {
  346. item.SetVisible(false)
  347. continue
  348. }
  349. // Sets item position
  350. item.SetVisible(true)
  351. item.SetPosition(0, posY)
  352. if s.adjustItem {
  353. item.SetWidth(width)
  354. }
  355. posY += ipan.Height()
  356. }
  357. // Set scroll bar value if recalc was not due by scroll event
  358. if scroll && !s.scrollBarEvent {
  359. s.vscroll.SetValue(float32(s.first) / float32(s.maxFirst()))
  360. }
  361. s.scrollBarEvent = false
  362. }
  363. // hRecalc recalculates for the horizontal scroller
  364. func (s *ItemScroller) hRecalc() {
  365. // Checks if scroll bar should be visible or not
  366. scroll := false
  367. if s.first > 0 {
  368. scroll = true
  369. } else {
  370. var posX float32
  371. for _, item := range s.items[s.first:] {
  372. posX += item.GetPanel().Width()
  373. if posX > s.width {
  374. scroll = true
  375. break
  376. }
  377. }
  378. }
  379. s.setHScrollBar(scroll)
  380. // Compute size of scroll button
  381. if scroll && s.autoButtonSize {
  382. var totalWidth float32
  383. for _, item := range s.items {
  384. // TODO OPTIMIZATION
  385. // Break when the view/content proportion becomes smaller than the minimum button size
  386. totalWidth += item.GetPanel().Width()
  387. }
  388. s.hscroll.SetButtonSize(s.width * s.width / totalWidth)
  389. }
  390. // Items height
  391. height := s.ContentHeight()
  392. if scroll {
  393. height -= s.hscroll.Height()
  394. }
  395. var posX float32
  396. // Sets positions of all items
  397. for pos, ipan := range s.items {
  398. item := ipan.GetPanel()
  399. // If item is before first visible, sets not visible
  400. if pos < s.first {
  401. item.SetVisible(false)
  402. continue
  403. }
  404. // If item is after last visible, sets not visible
  405. if posX > s.width {
  406. item.SetVisible(false)
  407. continue
  408. }
  409. // Sets item position
  410. item.SetVisible(true)
  411. item.SetPosition(posX, 0)
  412. if s.adjustItem {
  413. item.SetHeight(height)
  414. }
  415. posX += item.Width()
  416. }
  417. // Set scroll bar value if recalc was not due by scroll event
  418. if scroll && !s.scrollBarEvent {
  419. s.hscroll.SetValue(float32(s.first) / float32(s.maxFirst()))
  420. }
  421. s.scrollBarEvent = false
  422. }
  423. // maxFirst returns the maximum position of the first visible item
  424. func (s *ItemScroller) maxFirst() int {
  425. // Vertical scroller
  426. if s.vert {
  427. var height float32
  428. pos := len(s.items) - 1
  429. if pos < 0 {
  430. return 0
  431. }
  432. for {
  433. item := s.items[pos]
  434. height += item.GetPanel().Height()
  435. if height > s.Height() {
  436. break
  437. }
  438. pos--
  439. if pos < 0 {
  440. break
  441. }
  442. }
  443. return pos + 1
  444. }
  445. // Horizontal scroller
  446. var width float32
  447. pos := len(s.items) - 1
  448. if pos < 0 {
  449. return 0
  450. }
  451. for {
  452. item := s.items[pos]
  453. width += item.GetPanel().Width()
  454. if width > s.Width() {
  455. break
  456. }
  457. pos--
  458. if pos < 0 {
  459. break
  460. }
  461. }
  462. return pos + 1
  463. }
  464. // setVScrollBar sets the visibility state of the vertical scrollbar
  465. func (s *ItemScroller) setVScrollBar(state bool) {
  466. // Visible
  467. if state {
  468. var scrollWidth float32 = 20
  469. if s.vscroll == nil {
  470. s.vscroll = NewVScrollBar(0, 0)
  471. s.vscroll.SetBorders(0, 0, 0, 1)
  472. s.vscroll.Subscribe(OnChange, s.onScrollBarEvent)
  473. s.Panel.Add(s.vscroll)
  474. }
  475. s.vscroll.SetSize(scrollWidth, s.ContentHeight())
  476. s.vscroll.SetPositionX(s.ContentWidth() - scrollWidth)
  477. s.vscroll.SetPositionY(0)
  478. s.vscroll.recalc()
  479. s.vscroll.SetVisible(true)
  480. // Not visible
  481. } else {
  482. if s.vscroll != nil {
  483. s.vscroll.SetVisible(false)
  484. }
  485. }
  486. }
  487. // setHScrollBar sets the visibility state of the horizontal scrollbar
  488. func (s *ItemScroller) setHScrollBar(state bool) {
  489. // Visible
  490. if state {
  491. var scrollHeight float32 = 20
  492. if s.hscroll == nil {
  493. s.hscroll = NewHScrollBar(0, 0)
  494. s.hscroll.SetBorders(1, 0, 0, 0)
  495. s.hscroll.Subscribe(OnChange, s.onScrollBarEvent)
  496. s.Panel.Add(s.hscroll)
  497. }
  498. s.hscroll.SetSize(s.ContentWidth(), scrollHeight)
  499. s.hscroll.SetPositionX(0)
  500. s.hscroll.SetPositionY(s.ContentHeight() - scrollHeight)
  501. s.hscroll.recalc()
  502. s.hscroll.SetVisible(true)
  503. // Not visible
  504. } else {
  505. if s.hscroll != nil {
  506. s.hscroll.SetVisible(false)
  507. }
  508. }
  509. }
  510. // onScrollEvent is called when the list scrollbar value changes
  511. func (s *ItemScroller) onScrollBarEvent(evname string, ev interface{}) {
  512. var pos float64
  513. if s.vert {
  514. pos = s.vscroll.Value()
  515. } else {
  516. pos = s.hscroll.Value()
  517. }
  518. first := int(math.Floor((float64(s.maxFirst()) * pos) + 0.5))
  519. if first == s.first {
  520. return
  521. }
  522. s.scrollBarEvent = true
  523. s.first = first
  524. s.recalc()
  525. }
  526. // update updates the visual state the list and its items
  527. func (s *ItemScroller) update() {
  528. if s.cursorOver {
  529. s.applyStyle(&s.styles.Over)
  530. return
  531. }
  532. if s.focus {
  533. s.applyStyle(&s.styles.Focus)
  534. return
  535. }
  536. s.applyStyle(&s.styles.Normal)
  537. }
  538. // applyStyle sets the specified style
  539. func (s *ItemScroller) applyStyle(st *ItemScrollerStyle) {
  540. s.Panel.ApplyStyle(&st.PanelStyle)
  541. }