control_folder.go 3.2 KB

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