checkradio.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. "github.com/g3n/engine/gui/assets/icon"
  7. "github.com/g3n/engine/math32"
  8. "github.com/g3n/engine/window"
  9. )
  10. const (
  11. checkON = string(icon.CheckBox)
  12. checkOFF = string(icon.CheckBoxOutlineBlank)
  13. radioON = string(icon.RadioButtonChecked)
  14. radioOFF = string(icon.RadioButtonUnchecked)
  15. )
  16. type CheckRadio struct {
  17. Panel // Embedded panel
  18. Label *Label // Text label
  19. icon *Label
  20. styles *CheckRadioStyles
  21. check bool
  22. group string // current group name
  23. cursorOver bool
  24. state bool
  25. codeON string
  26. codeOFF string
  27. subroot bool // indicates root subcription
  28. }
  29. type CheckRadioStyle struct {
  30. Border RectBounds
  31. Paddings RectBounds
  32. BorderColor math32.Color4
  33. BgColor math32.Color4
  34. FgColor math32.Color
  35. }
  36. type CheckRadioStyles struct {
  37. Normal CheckRadioStyle
  38. Over CheckRadioStyle
  39. Focus CheckRadioStyle
  40. Disabled CheckRadioStyle
  41. }
  42. // NewCheckBox creates and returns a pointer to a new CheckBox widget
  43. // with the specified text
  44. func NewCheckBox(text string) *CheckRadio {
  45. return newCheckRadio(true, text)
  46. }
  47. // NewRadioButton creates and returns a pointer to a new RadioButton widget
  48. // with the specified text
  49. func NewRadioButton(text string) *CheckRadio {
  50. return newCheckRadio(false, text)
  51. }
  52. // newCheckRadio creates and returns a pointer to a new CheckRadio widget
  53. // with the specified type and text
  54. func newCheckRadio(check bool, text string) *CheckRadio {
  55. cb := new(CheckRadio)
  56. cb.styles = &StyleDefault().CheckRadio
  57. // Adapts to specified type: CheckBox or RadioButton
  58. cb.check = check
  59. cb.state = false
  60. if cb.check {
  61. cb.codeON = checkON
  62. cb.codeOFF = checkOFF
  63. } else {
  64. cb.codeON = radioON
  65. cb.codeOFF = radioOFF
  66. }
  67. // Initialize panel
  68. cb.Panel.Initialize(0, 0)
  69. // Subscribe to events
  70. cb.Panel.Subscribe(OnKeyDown, cb.onKey)
  71. cb.Panel.Subscribe(OnCursorEnter, cb.onCursor)
  72. cb.Panel.Subscribe(OnCursorLeave, cb.onCursor)
  73. cb.Panel.Subscribe(OnMouseDown, cb.onMouse)
  74. cb.Panel.Subscribe(OnEnable, func(evname string, ev interface{}) { cb.update() })
  75. // Creates label
  76. cb.Label = NewLabel(text)
  77. cb.Label.Subscribe(OnResize, func(evname string, ev interface{}) { cb.recalc() })
  78. cb.Panel.Add(cb.Label)
  79. // Creates icon label
  80. cb.icon = NewLabel(" ", true)
  81. cb.Panel.Add(cb.icon)
  82. cb.recalc()
  83. cb.update()
  84. return cb
  85. }
  86. // Value returns the current state of the checkbox
  87. func (cb *CheckRadio) Value() bool {
  88. return cb.state
  89. }
  90. // SetValue sets the current state of the checkbox
  91. func (cb *CheckRadio) SetValue(state bool) *CheckRadio {
  92. if state == cb.state {
  93. return cb
  94. }
  95. cb.state = state
  96. cb.update()
  97. cb.Dispatch(OnChange, nil)
  98. return cb
  99. }
  100. // Group returns the name of the radio group
  101. func (cb *CheckRadio) Group() string {
  102. return cb.group
  103. }
  104. // SetGroup sets the name of the radio group
  105. func (cb *CheckRadio) SetGroup(group string) {
  106. cb.group = group
  107. }
  108. // SetStyles set the button styles overriding the default style
  109. func (cb *CheckRadio) SetStyles(bs *CheckRadioStyles) {
  110. cb.styles = bs
  111. cb.update()
  112. }
  113. // toggleState toggles the current state of the checkbox/radiobutton
  114. func (cb *CheckRadio) toggleState() {
  115. // Subscribes once to the root panel for OnRadioGroup events
  116. // The root panel is used to dispatch events to all checkradios
  117. if !cb.subroot {
  118. cb.root.Subscribe(OnRadioGroup, func(name string, ev interface{}) {
  119. cb.onRadioGroup(ev.(*CheckRadio))
  120. })
  121. cb.subroot = true
  122. }
  123. if cb.check {
  124. cb.state = !cb.state
  125. } else {
  126. if len(cb.group) == 0 {
  127. cb.state = !cb.state
  128. } else {
  129. if cb.state {
  130. return
  131. }
  132. cb.state = !cb.state
  133. }
  134. }
  135. cb.update()
  136. cb.Dispatch(OnChange, nil)
  137. if !cb.check && len(cb.group) > 0 {
  138. cb.root.Dispatch(OnRadioGroup, cb)
  139. }
  140. }
  141. // onMouse process OnMouseDown events
  142. func (cb *CheckRadio) onMouse(evname string, ev interface{}) {
  143. cb.root.SetKeyFocus(cb)
  144. cb.root.StopPropagation(Stop3D)
  145. cb.toggleState()
  146. // Dispatch OnClick for left mouse button down
  147. if evname == OnMouseDown {
  148. mev := ev.(*window.MouseEvent)
  149. if mev.Button == window.MouseButtonLeft {
  150. cb.Dispatch(OnClick, nil)
  151. }
  152. }
  153. cb.root.StopPropagation(StopAll)
  154. }
  155. // onCursor process OnCursor* events
  156. func (cb *CheckRadio) onCursor(evname string, ev interface{}) {
  157. if evname == OnCursorEnter {
  158. cb.cursorOver = true
  159. } else {
  160. cb.cursorOver = false
  161. }
  162. cb.update()
  163. cb.root.StopPropagation(StopAll)
  164. }
  165. // onKey receives subscribed key events
  166. func (cb *CheckRadio) onKey(evname string, ev interface{}) {
  167. kev := ev.(*window.KeyEvent)
  168. if evname == OnKeyDown && kev.Keycode == window.KeyEnter {
  169. cb.toggleState()
  170. cb.update()
  171. cb.Dispatch(OnClick, nil)
  172. cb.root.StopPropagation(Stop3D)
  173. return
  174. }
  175. return
  176. }
  177. // onRadioGroup receives subscribed OnRadioGroup events
  178. func (cb *CheckRadio) onRadioGroup(other *CheckRadio) {
  179. // If event is for this button, ignore
  180. if cb == other {
  181. return
  182. }
  183. // If other radio group is not the group of this button, ignore
  184. if cb.group != other.group {
  185. return
  186. }
  187. // Toggle this button state
  188. cb.SetValue(!other.Value())
  189. }
  190. // update updates the visual appearance of the checkbox
  191. func (cb *CheckRadio) update() {
  192. if cb.state {
  193. cb.icon.SetText(cb.codeON)
  194. } else {
  195. cb.icon.SetText(cb.codeOFF)
  196. }
  197. if !cb.Enabled() {
  198. cb.applyStyle(&cb.styles.Disabled)
  199. return
  200. }
  201. if cb.cursorOver {
  202. cb.applyStyle(&cb.styles.Over)
  203. return
  204. }
  205. cb.applyStyle(&cb.styles.Normal)
  206. }
  207. // setStyle sets the specified checkradio style
  208. func (cb *CheckRadio) applyStyle(s *CheckRadioStyle) {
  209. cb.Panel.SetBordersColor4(&s.BorderColor)
  210. cb.Panel.SetBordersFrom(&s.Border)
  211. cb.Panel.SetPaddingsFrom(&s.Paddings)
  212. cb.Panel.SetColor4(&s.BgColor)
  213. cb.icon.SetColor(&s.FgColor)
  214. cb.Label.SetColor(&s.FgColor)
  215. }
  216. // recalc recalculates dimensions and position from inside out
  217. func (cb *CheckRadio) recalc() {
  218. // Sets icon position
  219. cb.icon.SetFontSize(cb.Label.FontSize() * 1.3)
  220. cb.icon.SetPosition(0, 0)
  221. // Label position
  222. spacing := float32(4)
  223. cb.Label.SetPosition(cb.icon.Width()+spacing, 0)
  224. // Content width
  225. width := cb.icon.Width() + spacing + cb.Label.Width()
  226. cb.SetContentSize(width, cb.Label.Height())
  227. }