SliderWidgets.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package giu
  2. import (
  3. "fmt"
  4. "github.com/AllenDang/imgui-go"
  5. )
  6. var _ Widget = &SliderIntWidget{}
  7. // SliderIntWidget is a slider around int32 values.
  8. type SliderIntWidget struct {
  9. label string
  10. value *int32
  11. min int32
  12. max int32
  13. format string
  14. width float32
  15. onChange func()
  16. }
  17. // SliderInt constructs new SliderIntWidget.
  18. func SliderInt(value *int32, min, max int32) *SliderIntWidget {
  19. return &SliderIntWidget{
  20. label: GenAutoID("##SliderInt"),
  21. value: value,
  22. min: min,
  23. max: max,
  24. format: "%d",
  25. width: 0,
  26. onChange: nil,
  27. }
  28. }
  29. // Format sets data format displayed on the slider
  30. // NOTE: on C side of imgui, it will be processed like:
  31. // fmt.Sprintf(format, currentValue) so you can do e.g.
  32. // SLiderInt(...).Format("My age is %d") and %d will be replaced with current value.
  33. func (s *SliderIntWidget) Format(format string) *SliderIntWidget {
  34. s.format = format
  35. return s
  36. }
  37. // Size sets slider's width.
  38. func (s *SliderIntWidget) Size(width float32) *SliderIntWidget {
  39. s.width = width
  40. return s
  41. }
  42. // OnChange sets callback when slider's position gets changed.
  43. func (s *SliderIntWidget) OnChange(onChange func()) *SliderIntWidget {
  44. s.onChange = onChange
  45. return s
  46. }
  47. // Label sets slider label (id).
  48. func (s *SliderIntWidget) Label(label string) *SliderIntWidget {
  49. s.label = tStr(label)
  50. return s
  51. }
  52. // Labelf sets formated label.
  53. func (s *SliderIntWidget) Labelf(format string, args ...any) *SliderIntWidget {
  54. return s.Label(fmt.Sprintf(format, args...))
  55. }
  56. // Build implements Widget interface.
  57. func (s *SliderIntWidget) Build() {
  58. if s.width != 0 {
  59. PushItemWidth(s.width)
  60. defer PopItemWidth()
  61. }
  62. if imgui.SliderIntV(tStr(s.label), s.value, s.min, s.max, s.format) && s.onChange != nil {
  63. s.onChange()
  64. }
  65. }
  66. var _ Widget = &VSliderIntWidget{}
  67. // VSliderIntWidget stands from Vertical SliderIntWidget.
  68. type VSliderIntWidget struct {
  69. label string
  70. width float32
  71. height float32
  72. value *int32
  73. min int32
  74. max int32
  75. format string
  76. flags SliderFlags
  77. onChange func()
  78. }
  79. // VSliderInt creates new vslider int.
  80. func VSliderInt(value *int32, min, max int32) *VSliderIntWidget {
  81. return &VSliderIntWidget{
  82. label: GenAutoID("##VSliderInt"),
  83. width: 18,
  84. height: 60,
  85. value: value,
  86. min: min,
  87. max: max,
  88. format: "%d",
  89. flags: SliderFlagsNone,
  90. }
  91. }
  92. // Size sets slider's size.
  93. func (vs *VSliderIntWidget) Size(width, height float32) *VSliderIntWidget {
  94. vs.width, vs.height = width, height
  95. return vs
  96. }
  97. // Flags sets flags.
  98. func (vs *VSliderIntWidget) Flags(flags SliderFlags) *VSliderIntWidget {
  99. vs.flags = flags
  100. return vs
  101. }
  102. // Format sets format (see comment on (*SliderIntWidget).Format).
  103. func (vs *VSliderIntWidget) Format(format string) *VSliderIntWidget {
  104. vs.format = format
  105. return vs
  106. }
  107. // OnChange sets callback called when slider's position gets changed.
  108. func (vs *VSliderIntWidget) OnChange(onChange func()) *VSliderIntWidget {
  109. vs.onChange = onChange
  110. return vs
  111. }
  112. // Label sets slider's label (id).
  113. func (vs *VSliderIntWidget) Label(label string) *VSliderIntWidget {
  114. vs.label = tStr(label)
  115. return vs
  116. }
  117. // Labelf sets formated label.
  118. func (vs *VSliderIntWidget) Labelf(format string, args ...any) *VSliderIntWidget {
  119. return vs.Label(fmt.Sprintf(format, args...))
  120. }
  121. // Build implements Widget interface.
  122. func (vs *VSliderIntWidget) Build() {
  123. if imgui.VSliderIntV(
  124. tStr(vs.label),
  125. imgui.Vec2{X: vs.width, Y: vs.height},
  126. vs.value,
  127. vs.min,
  128. vs.max,
  129. vs.format,
  130. int(vs.flags),
  131. ) && vs.onChange != nil {
  132. vs.onChange()
  133. }
  134. }
  135. var _ Widget = &SliderFloatWidget{}
  136. // SliderFloatWidget does similar to SliderIntWidget but slides around
  137. // float32 values.
  138. type SliderFloatWidget struct {
  139. label string
  140. value *float32
  141. min float32
  142. max float32
  143. format string
  144. width float32
  145. onChange func()
  146. }
  147. // SliderFloat creates new slider float widget.
  148. func SliderFloat(value *float32, min, max float32) *SliderFloatWidget {
  149. return &SliderFloatWidget{
  150. label: GenAutoID("##SliderFloat"),
  151. value: value,
  152. min: min,
  153. max: max,
  154. format: "%.3f",
  155. width: 0,
  156. onChange: nil,
  157. }
  158. }
  159. // Format sets format of text displayed on the slider.
  160. // default is %.3f.
  161. func (sf *SliderFloatWidget) Format(format string) *SliderFloatWidget {
  162. sf.format = format
  163. return sf
  164. }
  165. // OnChange is callback called when slider's position gets changed.
  166. func (sf *SliderFloatWidget) OnChange(onChange func()) *SliderFloatWidget {
  167. sf.onChange = onChange
  168. return sf
  169. }
  170. // Size sets slider's width.
  171. func (sf *SliderFloatWidget) Size(width float32) *SliderFloatWidget {
  172. sf.width = width
  173. return sf
  174. }
  175. // Label sets slider's label (id).
  176. func (sf *SliderFloatWidget) Label(label string) *SliderFloatWidget {
  177. sf.label = tStr(label)
  178. return sf
  179. }
  180. // Labelf sets formated label.
  181. func (sf *SliderFloatWidget) Labelf(format string, args ...any) *SliderFloatWidget {
  182. return sf.Label(fmt.Sprintf(format, args...))
  183. }
  184. // Build implements Widget interface.
  185. func (sf *SliderFloatWidget) Build() {
  186. if sf.width != 0 {
  187. PushItemWidth(sf.width)
  188. defer PopItemWidth()
  189. }
  190. if imgui.SliderFloatV(tStr(sf.label), sf.value, sf.min, sf.max, sf.format, 1.0) && sf.onChange != nil {
  191. sf.onChange()
  192. }
  193. }