control_folder.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. func (g *ControlFolderGroup) AddCheckBox(text string) *CheckRadio {
  61. cb := NewCheckBox(text)
  62. g.node.Add(cb)
  63. return cb
  64. }
  65. func (g *ControlFolderGroup) AddSlider(text string, sf, v float32) *Slider {
  66. cont, slider := g.control.newSlider(text, sf, v)
  67. g.node.Add(cont)
  68. return slider
  69. }
  70. func (f *ControlFolder) newSlider(text string, sf, value float32) (IPanel, *Slider) {
  71. // Creates container panel for the label and slider
  72. cont := NewPanel(200, 32)
  73. hbox := NewHBoxLayout()
  74. hbox.spacing = 4
  75. cont.SetLayout(hbox)
  76. // Adds label
  77. l := NewImageLabel(text)
  78. l.SetLayoutParams(&HBoxLayoutParams{AlignV: AlignCenter})
  79. cont.Add(l)
  80. // Adds slider
  81. s := NewHSlider(100, l.Height())
  82. s.SetScaleFactor(sf)
  83. s.SetScaleFactor(sf)
  84. s.SetValue(value)
  85. s.SetText(fmt.Sprintf("%1.1f", value))
  86. s.Subscribe(OnChange, func(evname string, ev interface{}) {
  87. s.SetText(fmt.Sprintf("%1.1f", s.Value()))
  88. })
  89. s.SetLayoutParams(&HBoxLayoutParams{AlignV: AlignCenter, Expand: 1})
  90. cont.Add(s)
  91. return cont, s
  92. }