dropdown.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/gui/assets/icon"
  7. )
  8. // DropDown represents a dropdown GUI element.
  9. type DropDown struct {
  10. Panel // Embedded panel
  11. icon *Label // internal label with icon
  12. list *List // internal list
  13. styles *DropDownStyles // pointer to dropdown styles
  14. litem *ImageLabel // Item shown in drop box (copy of selected)
  15. selItem *ImageLabel // selected item from list
  16. overDropdown bool
  17. overList bool
  18. focus bool
  19. }
  20. // DropDownStyle contains the styling of a DropDown.
  21. type DropDownStyle BasicStyle
  22. // DropDownStyles contains a DropDownStyle for each valid GUI state.
  23. type DropDownStyles struct {
  24. Normal DropDownStyle
  25. Over DropDownStyle
  26. Focus DropDownStyle
  27. Disabled DropDownStyle
  28. }
  29. // NewDropDown creates and returns a pointer to a new drop down widget with the specified width.
  30. func NewDropDown(width float32, item *ImageLabel) *DropDown {
  31. dd := new(DropDown)
  32. dd.styles = &StyleDefault().DropDown
  33. dd.litem = item
  34. dd.Panel.Initialize(dd, width, 0)
  35. dd.Panel.Subscribe(OnMouseDown, dd.onMouse)
  36. dd.Panel.Subscribe(OnCursorEnter, dd.onCursor)
  37. dd.Panel.Subscribe(OnCursorLeave, dd.onCursor)
  38. dd.Panel.Subscribe(OnResize, func(name string, ev interface{}) { dd.recalc() })
  39. // ListItem
  40. dd.Panel.Add(dd.litem)
  41. // Create icon
  42. dd.icon = NewIcon(" ")
  43. dd.icon.SetFontSize(StyleDefault().Label.PointSize * 1.3)
  44. dd.icon.SetText(string(icon.ArrowDropDown))
  45. dd.Panel.Add(dd.icon)
  46. /// Create list
  47. dd.list = NewVList(0, 0)
  48. dd.list.bounded = false
  49. dd.list.dropdown = true
  50. dd.list.SetVisible(false)
  51. dd.Panel.Subscribe(OnKeyDown, dd.list.onKeyEvent)
  52. dd.Subscribe(OnMouseDownOut, func(s string, i interface{}) {
  53. // Hide list when clicked out
  54. if dd.list.Visible() {
  55. dd.list.SetVisible(false)
  56. }
  57. })
  58. dd.list.Subscribe(OnCursorEnter, func(evname string, ev interface{}) {
  59. dd.Dispatch(OnCursorLeave, ev)
  60. })
  61. dd.list.Subscribe(OnCursorLeave, func(evname string, ev interface{}) {
  62. dd.Dispatch(OnCursorEnter, ev)
  63. })
  64. dd.list.Subscribe(OnChange, dd.onListChangeEvent)
  65. dd.Panel.Add(dd.list)
  66. dd.update()
  67. // This will trigger recalc()
  68. dd.Panel.SetContentHeight(item.Height())
  69. return dd
  70. }
  71. // Add adds a list item at the end of the list
  72. func (dd *DropDown) Add(item *ImageLabel) {
  73. dd.list.Add(item)
  74. }
  75. // InsertAt inserts a list item at the specified position
  76. // Returs true if the item was successfully inserted
  77. func (dd *DropDown) InsertAt(pos int, item *ImageLabel) {
  78. dd.list.InsertAt(pos, item)
  79. }
  80. // RemoveAt removes the list item from the specified position
  81. // Returs true if the item was successfully removed
  82. func (dd *DropDown) RemoveAt(pos int) {
  83. dd.list.RemoveAt(pos)
  84. }
  85. // ItemAt returns the list item at the specified position
  86. func (dd *DropDown) ItemAt(pos int) *ImageLabel {
  87. return dd.list.ItemAt(pos).(*ImageLabel)
  88. }
  89. // Len returns the number of items in the dropdown's list.
  90. func (dd *DropDown) Len() int {
  91. return dd.list.Len()
  92. }
  93. // Selected returns the currently selected item or nil if no item was selected
  94. func (dd *DropDown) Selected() *ImageLabel {
  95. return dd.selItem
  96. }
  97. // SelectedPos returns the currently selected position or -1 if no item was selected
  98. func (dd *DropDown) SelectedPos() int {
  99. return dd.list.selected()
  100. }
  101. // SetSelected sets the selected item
  102. func (dd *DropDown) SetSelected(item *ImageLabel) {
  103. dd.list.SetSelected(dd.selItem, false)
  104. dd.list.SetSelected(item, true)
  105. dd.copySelected()
  106. dd.update()
  107. }
  108. // SelectPos selects the item at the specified position
  109. func (dd *DropDown) SelectPos(pos int) {
  110. dd.list.SetSelected(dd.selItem, false)
  111. dd.list.SelectPos(pos, true)
  112. dd.Dispatch(OnChange, nil)
  113. }
  114. // SetStyles sets the drop down styles overriding the default style
  115. func (dd *DropDown) SetStyles(dds *DropDownStyles) {
  116. dd.styles = dds
  117. dd.update()
  118. }
  119. // onMouse receives subscribed mouse events over the dropdown
  120. func (dd *DropDown) onMouse(evname string, ev interface{}) {
  121. Manager().SetKeyFocus(dd.list)
  122. if evname == OnMouseDown {
  123. dd.list.SetVisible(!dd.list.Visible())
  124. return
  125. }
  126. }
  127. // onCursor receives subscribed cursor events over the dropdown
  128. func (dd *DropDown) onCursor(evname string, ev interface{}) {
  129. if evname == OnCursorEnter {
  130. dd.overDropdown = true
  131. }
  132. if evname == OnCursorLeave {
  133. dd.overDropdown = false
  134. }
  135. dd.update()
  136. }
  137. // copySelected copy to the dropdown panel the selected item
  138. // from the list.
  139. func (dd *DropDown) copySelected() {
  140. selected := dd.list.Selected()
  141. if len(selected) > 0 {
  142. dd.selItem = selected[0].(*ImageLabel)
  143. dd.litem.CopyFields(dd.selItem)
  144. dd.litem.SetWidth(dd.selItem.Width())
  145. dd.recalc()
  146. dd.Dispatch(OnChange, nil)
  147. } else {
  148. return
  149. }
  150. }
  151. // onListChangeEvent is called when an item in the list is selected
  152. func (dd *DropDown) onListChangeEvent(evname string, ev interface{}) {
  153. dd.copySelected()
  154. }
  155. // recalc recalculates the dimensions and positions of the dropdown
  156. // panel, children and list
  157. func (dd *DropDown) recalc() {
  158. // Dropdown icon position
  159. posx := dd.Panel.ContentWidth() - dd.icon.Width()
  160. dd.icon.SetPosition(posx, 0)
  161. // List item position and width
  162. ipan := dd.litem.GetPanel()
  163. ipan.SetPosition(0, 0)
  164. height := ipan.Height()
  165. // List position
  166. dd.list.SetWidth(dd.Panel.Width())
  167. dd.list.SetHeight(6*height + 1)
  168. dd.list.SetPositionX(0)
  169. dd.list.SetPositionY(dd.Panel.Height())
  170. }
  171. // update updates the visual state
  172. func (dd *DropDown) update() {
  173. if dd.overDropdown || dd.overList {
  174. dd.applyStyle(&dd.styles.Over)
  175. dd.list.ApplyStyle(StyleOver)
  176. return
  177. }
  178. if dd.focus {
  179. dd.applyStyle(&dd.styles.Focus)
  180. dd.list.ApplyStyle(StyleFocus)
  181. return
  182. }
  183. dd.applyStyle(&dd.styles.Normal)
  184. dd.list.ApplyStyle(StyleNormal)
  185. }
  186. // applyStyle applies the specified style
  187. func (dd *DropDown) applyStyle(s *DropDownStyle) {
  188. dd.Panel.ApplyStyle(&s.PanelStyle)
  189. }