list.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. )
  9. type List struct {
  10. Scroller // Embedded scroller
  11. styles *ListStyles // Pointer to styles
  12. single bool // Single selection flag (default is true)
  13. focus bool // has keyboard focus
  14. dropdown bool // this is used as dropdown
  15. keyNext window.Key // Code of key to select next item
  16. keyPrev window.Key // Code of key to select previous item
  17. }
  18. // All items inserted into the list are
  19. // encapsulated inside a ListItem
  20. type ListItem struct {
  21. Panel // Container panel
  22. item IPanel // Original item
  23. selected bool // Item selected flag
  24. highlighted bool // Item highlighted flag
  25. padLeft float32 // Additional left padding
  26. list *List // Pointer to list
  27. }
  28. type ListStyles struct {
  29. Scroller *ScrollerStyles
  30. Item *ListItemStyles
  31. }
  32. type ListItemStyles struct {
  33. Normal ListItemStyle
  34. Over ListItemStyle
  35. Selected ListItemStyle
  36. Highlighted ListItemStyle
  37. SelHigh ListItemStyle
  38. }
  39. type ListItemStyle struct {
  40. Border BorderSizes
  41. Paddings BorderSizes
  42. BorderColor math32.Color4
  43. BgColor math32.Color4
  44. FgColor math32.Color
  45. }
  46. // NewVList creates and returns a pointer to a new vertical list panel
  47. // with the specified dimensions
  48. func NewVList(width, height float32) *List {
  49. return newList(true, width, height)
  50. }
  51. // NewHList creates and returns a pointer to a new horizontal list panel
  52. // with the specified dimensions
  53. func NewHList(width, height float32) *List {
  54. return newList(false, width, height)
  55. }
  56. // newList creates and returns a pointer to a new list panel
  57. // with the specified orientation and dimensions
  58. func newList(vert bool, width, height float32) *List {
  59. li := new(List)
  60. li.initialize(vert, width, height)
  61. return li
  62. }
  63. func (li *List) initialize(vert bool, width, height float32) {
  64. li.styles = &StyleDefault.List
  65. li.single = true
  66. li.Scroller.initialize(vert, width, height)
  67. li.Scroller.SetStyles(li.styles.Scroller)
  68. li.Scroller.adjustItem = true
  69. li.Scroller.Subscribe(OnMouseDown, li.onMouseEvent)
  70. li.Scroller.Subscribe(OnKeyDown, li.onKeyEvent)
  71. li.Scroller.Subscribe(OnKeyRepeat, li.onKeyEvent)
  72. if vert {
  73. li.keyNext = window.KeyDown
  74. li.keyPrev = window.KeyUp
  75. } else {
  76. li.keyNext = window.KeyRight
  77. li.keyPrev = window.KeyLeft
  78. }
  79. li.update()
  80. }
  81. // SetSingle sets the single/multiple selection flag of the list
  82. func (li *List) SetSingle(state bool) {
  83. li.single = state
  84. }
  85. // Single returns the current state of the single/multiple selection flag
  86. func (li *List) Single() bool {
  87. return li.single
  88. }
  89. // SetStyles set the listr styles overriding the default style
  90. func (li *List) SetStyles(s *ListStyles) {
  91. li.styles = s
  92. li.Scroller.SetStyles(li.styles.Scroller)
  93. li.update()
  94. }
  95. // Add add a list item at the end of the list
  96. func (li *List) Add(item IPanel) {
  97. li.InsertAt(len(li.items), item)
  98. }
  99. // InsertAt inserts a list item at the specified position
  100. // Returs true if the item was successfuly inserted
  101. func (li *List) InsertAt(pos int, item IPanel) {
  102. litem := newListItem(li, item)
  103. li.Scroller.InsertAt(pos, litem)
  104. litem.Panel.Subscribe(OnMouseDown, litem.onMouse)
  105. litem.Panel.Subscribe(OnCursorEnter, litem.onCursor)
  106. }
  107. // RemoveAt removes the list item from the specified position
  108. func (li *List) RemoveAt(pos int) IPanel {
  109. return li.Scroller.RemoveAt(pos)
  110. }
  111. // Remove removes the specified item from the list
  112. func (li *List) Remove(item IPanel) {
  113. for p, curr := range li.items {
  114. if curr.(*ListItem).item == item {
  115. li.RemoveAt(p)
  116. return
  117. }
  118. }
  119. }
  120. // ItemAt returns the list item at the specified position
  121. func (li *List) ItemAt(pos int) IPanel {
  122. item := li.Scroller.ItemAt(pos)
  123. if item == nil {
  124. return nil
  125. }
  126. litem := item.(*ListItem)
  127. return litem.item
  128. }
  129. // ItemPosition returns the position of the specified item in
  130. // the list or -1 if not found
  131. func (li *List) ItemPosition(item IPanel) int {
  132. for pos := 0; pos < len(li.items); pos++ {
  133. if li.items[pos].(*ListItem).item == item {
  134. return pos
  135. }
  136. }
  137. return -1
  138. }
  139. // Selected returns list with the currently selected items
  140. func (li *List) Selected() []IPanel {
  141. sel := []IPanel{}
  142. for _, item := range li.items {
  143. litem := item.(*ListItem)
  144. if litem.selected {
  145. sel = append(sel, litem.item)
  146. }
  147. }
  148. return sel
  149. }
  150. // Select selects or unselects the specified item
  151. func (li *List) SetSelected(item IPanel, state bool) {
  152. for _, curr := range li.items {
  153. litem := curr.(*ListItem)
  154. if litem.item == item {
  155. litem.SetSelected(state)
  156. li.update()
  157. li.Dispatch(OnChange, nil)
  158. return
  159. }
  160. }
  161. }
  162. // SelectPos selects or unselects the item at the specified position
  163. func (li *List) SelectPos(pos int, state bool) {
  164. if pos < 0 || pos >= len(li.items) {
  165. return
  166. }
  167. litem := li.items[pos].(*ListItem)
  168. if litem.selected == state {
  169. return
  170. }
  171. litem.SetSelected(state)
  172. li.update()
  173. li.Dispatch(OnChange, nil)
  174. }
  175. // SetItemPadLeftAt sets the additional left padding for this item
  176. // It is used mainly by the tree control
  177. func (li *List) SetItemPadLeftAt(pos int, pad float32) {
  178. if pos < 0 || pos >= len(li.items) {
  179. return
  180. }
  181. litem := li.items[pos].(*ListItem)
  182. litem.padLeft = pad
  183. litem.update()
  184. }
  185. // selNext selects or highlights the next item, if possible
  186. func (li *List) selNext(sel bool, update bool) *ListItem {
  187. // Checks for empty list
  188. if len(li.items) == 0 {
  189. return nil
  190. }
  191. // Find currently selected item
  192. var pos int
  193. if sel {
  194. pos = li.selected()
  195. } else {
  196. pos = li.highlighted()
  197. }
  198. var newItem *ListItem
  199. newSel := true
  200. // If no item found, returns first.
  201. if pos < 0 {
  202. newItem = li.items[0].(*ListItem)
  203. if sel {
  204. newItem.SetSelected(true)
  205. } else {
  206. newItem.SetHighlighted(true)
  207. }
  208. } else {
  209. item := li.items[pos].(*ListItem)
  210. // Item is not the last, get next
  211. if pos < len(li.items)-1 {
  212. newItem = li.items[pos+1].(*ListItem)
  213. if sel {
  214. item.SetSelected(false)
  215. newItem.SetSelected(true)
  216. } else {
  217. item.SetHighlighted(false)
  218. newItem.SetHighlighted(true)
  219. }
  220. if !li.ItemVisible(pos + 1) {
  221. li.ScrollDown()
  222. }
  223. // Item is the last, don't change
  224. } else {
  225. newItem = item
  226. newSel = false
  227. }
  228. }
  229. if update {
  230. li.update()
  231. }
  232. if sel && newSel {
  233. li.Dispatch(OnChange, nil)
  234. }
  235. return newItem
  236. }
  237. // selPrev selects or highlights the next item, if possible
  238. func (li *List) selPrev(sel bool, update bool) *ListItem {
  239. // Check for empty list
  240. if len(li.items) == 0 {
  241. return nil
  242. }
  243. // Find first selected item
  244. var pos int
  245. if sel {
  246. pos = li.selected()
  247. } else {
  248. pos = li.highlighted()
  249. }
  250. var newItem *ListItem
  251. newSel := true
  252. // If no item found, returns first.
  253. if pos < 0 {
  254. newItem = li.items[0].(*ListItem)
  255. if sel {
  256. newItem.SetSelected(true)
  257. } else {
  258. newItem.SetHighlighted(true)
  259. }
  260. } else {
  261. item := li.items[pos].(*ListItem)
  262. if pos == 0 {
  263. newItem = item
  264. newSel = false
  265. } else {
  266. newItem = li.items[pos-1].(*ListItem)
  267. if sel {
  268. item.SetSelected(false)
  269. newItem.SetSelected(true)
  270. } else {
  271. item.SetHighlighted(false)
  272. newItem.SetHighlighted(true)
  273. }
  274. if (pos - 1) < li.first {
  275. li.ScrollUp()
  276. }
  277. }
  278. }
  279. if update {
  280. li.update()
  281. }
  282. if sel && newSel {
  283. li.Dispatch(OnChange, nil)
  284. }
  285. return newItem
  286. }
  287. // selected returns the position of first selected item
  288. func (li *List) selected() (pos int) {
  289. for pos, item := range li.items {
  290. if item.(*ListItem).selected {
  291. return pos
  292. }
  293. }
  294. return -1
  295. }
  296. // highlighted returns the position of first highlighted item
  297. func (li *List) highlighted() (pos int) {
  298. for pos, item := range li.items {
  299. if item.(*ListItem).highlighted {
  300. return pos
  301. }
  302. }
  303. return -1
  304. }
  305. // onMouseEvent receives subscribed mouse events for the list
  306. func (li *List) onMouseEvent(evname string, ev interface{}) {
  307. li.root.SetKeyFocus(li)
  308. li.root.StopPropagation(StopAll)
  309. }
  310. // onKeyEvent receives subscribed key events for the list
  311. func (li *List) onKeyEvent(evname string, ev interface{}) {
  312. kev := ev.(*window.KeyEvent)
  313. // Dropdown mode
  314. if li.dropdown {
  315. switch kev.Keycode {
  316. case li.keyNext:
  317. li.selNext(true, true)
  318. case li.keyPrev:
  319. li.selPrev(true, true)
  320. case window.KeyEnter:
  321. li.SetVisible(false)
  322. default:
  323. return
  324. }
  325. li.root.StopPropagation(Stop3D)
  326. return
  327. }
  328. // Listbox mode single selection
  329. if li.single {
  330. switch kev.Keycode {
  331. case li.keyNext:
  332. li.selNext(true, true)
  333. case li.keyPrev:
  334. li.selPrev(true, true)
  335. default:
  336. return
  337. }
  338. li.root.StopPropagation(Stop3D)
  339. return
  340. }
  341. // Listbox mode multiple selection
  342. switch kev.Keycode {
  343. case li.keyNext:
  344. li.selNext(false, true)
  345. case li.keyPrev:
  346. li.selPrev(false, true)
  347. case window.KeySpace:
  348. pos := li.highlighted()
  349. if pos >= 0 {
  350. litem := li.items[pos].(*ListItem)
  351. li.setSelection(litem, !litem.selected, true, true)
  352. }
  353. default:
  354. return
  355. }
  356. li.root.StopPropagation(Stop3D)
  357. }
  358. // setSelection sets the selected state of the specified item
  359. // updating the visual appearance of the list if necessary
  360. func (li *List) setSelection(litem *ListItem, state bool, force bool, dispatch bool) {
  361. // If already at this state, nothing to do
  362. if litem.selected == state && !force {
  363. return
  364. }
  365. litem.SetSelected(state)
  366. // If single selection, deselects all other items
  367. if li.single {
  368. for _, curr := range li.items {
  369. if curr.(*ListItem) != litem {
  370. curr.(*ListItem).SetSelected(false)
  371. }
  372. }
  373. }
  374. li.update()
  375. if dispatch {
  376. li.Dispatch(OnChange, nil)
  377. }
  378. }
  379. // update updates the visual state the list and its items
  380. func (li *List) update() {
  381. // Update the list items styles
  382. for _, item := range li.items {
  383. item.(*ListItem).update()
  384. }
  385. }
  386. //
  387. // ListItem methods
  388. //
  389. func newListItem(list *List, item IPanel) *ListItem {
  390. litem := new(ListItem)
  391. litem.Panel.Initialize(0, 0)
  392. litem.item = item
  393. litem.list = list
  394. litem.Panel.Add(item)
  395. litem.SetContentWidth(item.GetPanel().Width())
  396. litem.SetContentHeight(item.GetPanel().Height())
  397. litem.update()
  398. return litem
  399. }
  400. // onMouse receives mouse button events over the list item
  401. func (litem *ListItem) onMouse(evname string, ev interface{}) {
  402. if litem.list.single {
  403. litem.list.setSelection(litem, true, true, true)
  404. } else {
  405. litem.list.setSelection(litem, !litem.selected, true, true)
  406. }
  407. }
  408. // onCursor receives subscribed cursor events over the list item
  409. func (litem *ListItem) onCursor(evname string, ev interface{}) {
  410. if litem.list.dropdown {
  411. litem.list.setSelection(litem, true, true, false)
  412. return
  413. }
  414. }
  415. // setSelected sets this item selected state
  416. func (litem *ListItem) SetSelected(state bool) {
  417. litem.selected = state
  418. //litem.item.SetSelected2(state)
  419. }
  420. // setHighlighted sets this item selected state
  421. func (litem *ListItem) SetHighlighted(state bool) {
  422. litem.highlighted = state
  423. //litem.item.SetHighlighted2(state)
  424. }
  425. // updates the list item visual style accordingly to its current state
  426. func (litem *ListItem) update() {
  427. list := litem.list
  428. if litem.selected && !litem.highlighted {
  429. litem.applyStyle(&list.styles.Item.Selected)
  430. return
  431. }
  432. if !litem.selected && litem.highlighted {
  433. litem.applyStyle(&list.styles.Item.Highlighted)
  434. return
  435. }
  436. if litem.selected && litem.highlighted {
  437. litem.applyStyle(&list.styles.Item.SelHigh)
  438. return
  439. }
  440. litem.applyStyle(&list.styles.Item.Normal)
  441. }
  442. // applyStyle applies the specified style to this ListItem
  443. func (litem *ListItem) applyStyle(s *ListItemStyle) {
  444. litem.SetBordersFrom(&s.Border)
  445. litem.SetBordersColor4(&s.BorderColor)
  446. pads := s.Paddings
  447. pads.Left += litem.padLeft
  448. litem.SetPaddingsFrom(&pads)
  449. litem.SetColor4(&s.BgColor)
  450. }