folder.go 4.1 KB

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