folder.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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
  19. type FolderStyle struct {
  20. PanelStyle
  21. FgColor math32.Color4
  22. Icons [2]string
  23. }
  24. // FolderStyles
  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(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(f.label.FontSize() * 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.alignRight = true
  60. f.update()
  61. f.recalc()
  62. }
  63. // SetStyles set the folder styles overriding the default style
  64. func (f *Folder) SetStyles(fs *FolderStyles) {
  65. f.styles = fs
  66. f.update()
  67. }
  68. // SetAlignRight sets the side of the alignment of the content panel
  69. // in relation to the folder
  70. func (f *Folder) SetAlignRight(state bool) {
  71. f.alignRight = state
  72. f.recalc()
  73. }
  74. // TotalHeight returns this folder total height
  75. // considering the contents panel, if visible
  76. func (f *Folder) TotalHeight() float32 {
  77. height := f.Height()
  78. if f.contentPanel.GetPanel().Visible() {
  79. height += f.contentPanel.GetPanel().Height()
  80. }
  81. return height
  82. }
  83. // onMouse receives mouse button events over the folder panel
  84. func (f *Folder) onMouse(evname string, ev interface{}) {
  85. switch evname {
  86. case OnMouseDown:
  87. cont := f.contentPanel.GetPanel()
  88. if !cont.Visible() {
  89. cont.SetVisible(true)
  90. } else {
  91. cont.SetVisible(false)
  92. }
  93. f.update()
  94. f.recalc()
  95. default:
  96. return
  97. }
  98. }
  99. // onCursor receives cursor events over the folder panel
  100. func (f *Folder) onCursor(evname string, ev interface{}) {
  101. switch evname {
  102. case OnCursorEnter:
  103. f.cursorOver = true
  104. f.update()
  105. case OnCursorLeave:
  106. f.cursorOver = false
  107. f.update()
  108. default:
  109. return
  110. }
  111. }
  112. // update updates the folder visual state
  113. func (f *Folder) update() {
  114. if f.cursorOver {
  115. f.applyStyle(&f.styles.Over)
  116. return
  117. }
  118. f.applyStyle(&f.styles.Normal)
  119. }
  120. // applyStyle applies the specified style
  121. func (f *Folder) applyStyle(s *FolderStyle) {
  122. f.Panel.ApplyStyle(&s.PanelStyle)
  123. icode := 0
  124. if f.contentPanel.GetPanel().Visible() {
  125. icode = 1
  126. }
  127. f.icon.SetText(string(s.Icons[icode]))
  128. f.icon.SetColor4(&s.FgColor)
  129. f.label.SetBgColor4(&s.BgColor)
  130. f.label.SetColor4(&s.FgColor)
  131. }
  132. func (f *Folder) recalc() {
  133. // icon position
  134. f.icon.SetPosition(0, 0)
  135. // Label position and width
  136. f.label.SetPosition(f.icon.Width()+4, 0)
  137. f.Panel.SetContentHeight(f.label.Height())
  138. // Sets position of the base folder scroller panel
  139. cont := f.contentPanel.GetPanel()
  140. if f.alignRight {
  141. cont.SetPosition(0, f.Panel.Height())
  142. } else {
  143. dx := cont.Width() - f.Panel.Width()
  144. cont.SetPosition(-dx, f.Panel.Height())
  145. }
  146. }