folder.go 4.1 KB

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