control_folder.go 3.0 KB

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