control_folder.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "fmt"
  7. )
  8. // ControlFolder represents a folder with controls
  9. type ControlFolder struct {
  10. Folder // Embedded folder
  11. tree Tree // control tree
  12. styles *ControlFolderStyles // Pointer to styles
  13. }
  14. // ControlFolderStyles
  15. type ControlFolderStyles struct {
  16. Folder *FolderStyles
  17. Tree *TreeStyles
  18. }
  19. // ControlFolderGroup
  20. type ControlFolderGroup struct {
  21. control *ControlFolder
  22. node *TreeNode
  23. }
  24. // NewControlFolder creates and returns a pointer to a new control folder widget
  25. // with the specified text and initial width
  26. func NewControlFolder(text string, width float32) *ControlFolder {
  27. f := new(ControlFolder)
  28. f.Initialize(text, width)
  29. return f
  30. }
  31. // Initialize initializes the control folder with the specified text and initial width
  32. // It is normally used when the control folder is embedded in another object
  33. func (f *ControlFolder) Initialize(text string, width float32) {
  34. f.styles = &StyleDefault().ControlFolder
  35. f.tree.Initialize(width, width)
  36. f.tree.SetStyles(f.styles.Tree)
  37. f.tree.SetAutoHeight(600)
  38. f.tree.SetAutoWidth(400)
  39. f.Folder.Initialize(text, width, &f.tree)
  40. f.Folder.SetStyles(f.styles.Folder)
  41. f.Folder.SetAlignRight(false)
  42. }
  43. // Clear clears the control folder's tree
  44. func (f *ControlFolder) Clear() {
  45. f.tree.Clear()
  46. }
  47. // RemoveAt removes the IPanel at the specified position from the control folder's tree
  48. func (f *ControlFolder) RemoveAt(pos int) IPanel {
  49. return f.tree.RemoveAt(pos)
  50. }
  51. // AddPanel adds an IPanel to the control folder's tree
  52. func (f *ControlFolder) AddPanel(pan IPanel) {
  53. f.tree.Add(pan)
  54. }
  55. // AddCheckBox adds a checkbox to the control folder's tree
  56. func (f *ControlFolder) AddCheckBox(text string) *CheckRadio {
  57. cb := NewCheckBox(text)
  58. f.tree.Add(cb)
  59. return cb
  60. }
  61. // AddSlider adds a slider to the control folder's tree
  62. func (f *ControlFolder) AddSlider(text string, sf, v float32) *Slider {
  63. cont, slider := f.newSlider(text, sf, v)
  64. f.tree.Add(cont)
  65. return slider
  66. }
  67. // AddGroup adds a group to the control folder
  68. func (f *ControlFolder) AddGroup(text string) *ControlFolderGroup {
  69. g := new(ControlFolderGroup)
  70. g.control = f
  71. g.node = f.tree.AddNode(text)
  72. return g
  73. }
  74. // SetStyles set the folder styles overriding the default style
  75. func (f *ControlFolder) SetStyles(fs *ControlFolderStyles) {
  76. f.styles = fs
  77. f.Folder.styles = fs.Folder
  78. f.tree.styles = fs.Tree
  79. f.tree.update()
  80. f.Folder.update()
  81. }
  82. // AddCheckBox adds a checkbox to the control folder group
  83. func (g *ControlFolderGroup) AddCheckBox(text string) *CheckRadio {
  84. cb := NewCheckBox(text)
  85. g.node.Add(cb)
  86. return cb
  87. }
  88. // AddSlider adds a slider to the control folder group
  89. func (g *ControlFolderGroup) AddSlider(text string, sf, v float32) *Slider {
  90. cont, slider := g.control.newSlider(text, sf, v)
  91. g.node.Add(cont)
  92. return slider
  93. }
  94. // AddPanel adds a panel to the control folder group
  95. func (g *ControlFolderGroup) AddPanel(pan IPanel) {
  96. g.node.Add(pan)
  97. }
  98. func (f *ControlFolder) newSlider(text string, sf, value float32) (IPanel, *Slider) {
  99. // Creates container panel for the label and slider
  100. cont := NewPanel(200, 32)
  101. hbox := NewHBoxLayout()
  102. hbox.spacing = 4
  103. cont.SetLayout(hbox)
  104. // Adds label
  105. l := NewImageLabel(text)
  106. l.SetLayoutParams(&HBoxLayoutParams{AlignV: AlignCenter})
  107. cont.Add(l)
  108. // Adds slider
  109. s := NewHSlider(100, l.Height())
  110. s.SetScaleFactor(sf)
  111. s.SetScaleFactor(sf)
  112. s.SetValue(value)
  113. s.SetText(fmt.Sprintf("%1.1f", value))
  114. s.Subscribe(OnChange, func(evname string, ev interface{}) {
  115. s.SetText(fmt.Sprintf("%1.1f", s.Value()))
  116. })
  117. s.SetLayoutParams(&HBoxLayoutParams{AlignV: AlignCenter, Expand: 1})
  118. cont.Add(s)
  119. return cont, s
  120. }