folder.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. )
  8. // Folder represents a folder GUI element.
  9. type Folder struct {
  10. Panel // Embedded panel
  11. label Label // Folder label
  12. icon Label // Folder icon
  13. contentPanel IPanel // Content panel
  14. styles *FolderStyles
  15. cursorOver bool
  16. alignRight bool
  17. }
  18. // FolderStyle contains the styling of a Folder.
  19. type FolderStyle struct {
  20. PanelStyle
  21. FgColor math32.Color4
  22. Icons [2]string
  23. }
  24. // FolderStyles contains a FolderStyle for each valid GUI state.
  25. type FolderStyles struct {
  26. Normal FolderStyle
  27. Over FolderStyle
  28. Focus FolderStyle
  29. Disabled FolderStyle
  30. }
  31. // NewFolder creates and returns a pointer to a new folder widget
  32. // with the specified text and initial width.
  33. func NewFolder(text string, width float32, contentPanel IPanel) *Folder {
  34. f := new(Folder)
  35. f.Initialize(text, width, contentPanel)
  36. return f
  37. }
  38. // Initialize initializes the Folder with the specified text and initial width
  39. // It is normally used when the folder is embedded in another object.
  40. func (f *Folder) Initialize(text string, width float32, contentPanel IPanel) {
  41. f.Panel.Initialize(f, width, 0)
  42. f.styles = &StyleDefault().Folder
  43. // Initialize label
  44. f.label.initialize(text, StyleDefault().Font)
  45. f.Panel.Add(&f.label)
  46. // Create icon
  47. f.icon.initialize("", StyleDefault().FontIcon)
  48. f.icon.SetFontSize(StyleDefault().Label.PointSize * 1.3)
  49. f.Panel.Add(&f.icon)
  50. // Setup content panel
  51. f.contentPanel = contentPanel
  52. contentPanel.GetPanel().bounded = false
  53. contentPanel.GetPanel().SetVisible(false)
  54. f.Panel.Add(f.contentPanel)
  55. // Set event callbacks
  56. f.Panel.Subscribe(OnMouseDown, f.onMouse)
  57. f.Panel.Subscribe(OnCursorEnter, f.onCursor)
  58. f.Panel.Subscribe(OnCursorLeave, f.onCursor)
  59. f.Subscribe(OnMouseDownOut, func(s string, i interface{}) {
  60. // Hide list when clicked out
  61. if f.contentPanel.Visible() {
  62. f.contentPanel.SetVisible(false)
  63. }
  64. })
  65. f.contentPanel.Subscribe(OnCursorEnter, func(evname string, ev interface{}) {
  66. f.Dispatch(OnCursorLeave, ev)
  67. })
  68. f.contentPanel.Subscribe(OnCursorLeave, func(evname string, ev interface{}) {
  69. f.Dispatch(OnCursorEnter, ev)
  70. })
  71. f.alignRight = true
  72. f.update()
  73. f.recalc()
  74. }
  75. // SetStyles set the folder styles overriding the default style.
  76. func (f *Folder) SetStyles(fs *FolderStyles) {
  77. f.styles = fs
  78. f.update()
  79. }
  80. // SetAlignRight sets the side of the alignment of the content panel
  81. // in relation to the folder.
  82. func (f *Folder) SetAlignRight(state bool) {
  83. f.alignRight = state
  84. f.recalc()
  85. }
  86. // Height returns this folder total height
  87. // considering the contents panel, if visible.
  88. func (f *Folder) Height() float32 {
  89. height := f.Height()
  90. if f.contentPanel.GetPanel().Visible() {
  91. height += f.contentPanel.GetPanel().Height()
  92. }
  93. return height
  94. }
  95. // onMouse receives mouse button events over the folder panel.
  96. func (f *Folder) onMouse(evname string, ev interface{}) {
  97. switch evname {
  98. case OnMouseDown:
  99. cont := f.contentPanel.GetPanel()
  100. if !cont.Visible() {
  101. cont.SetVisible(true)
  102. } else {
  103. cont.SetVisible(false)
  104. }
  105. f.update()
  106. f.recalc()
  107. default:
  108. return
  109. }
  110. }
  111. // onCursor receives cursor events over the folder panel
  112. func (f *Folder) onCursor(evname string, ev interface{}) {
  113. switch evname {
  114. case OnCursorEnter:
  115. f.cursorOver = true
  116. f.update()
  117. case OnCursorLeave:
  118. f.cursorOver = false
  119. f.update()
  120. default:
  121. return
  122. }
  123. }
  124. // update updates the folder visual state
  125. func (f *Folder) update() {
  126. if f.cursorOver {
  127. f.applyStyle(&f.styles.Over)
  128. return
  129. }
  130. f.applyStyle(&f.styles.Normal)
  131. }
  132. // applyStyle applies the specified style
  133. func (f *Folder) applyStyle(s *FolderStyle) {
  134. f.Panel.ApplyStyle(&s.PanelStyle)
  135. icode := 0
  136. if f.contentPanel.GetPanel().Visible() {
  137. icode = 1
  138. }
  139. f.icon.SetText(string(s.Icons[icode]))
  140. f.icon.SetColor4(&s.FgColor)
  141. f.label.SetBgColor4(&s.BgColor)
  142. f.label.SetColor4(&s.FgColor)
  143. }
  144. func (f *Folder) recalc() {
  145. // icon position
  146. f.icon.SetPosition(0, 0)
  147. // Label position and width
  148. f.label.SetPosition(f.icon.Width()+4, 0)
  149. f.Panel.SetContentHeight(f.label.Height())
  150. // Sets position of the base folder scroller panel
  151. cont := f.contentPanel.GetPanel()
  152. if f.alignRight {
  153. cont.SetPosition(0, f.Panel.Height())
  154. } else {
  155. dx := cont.Width() - f.Panel.Width()
  156. cont.SetPosition(-dx, f.Panel.Height())
  157. }
  158. }