checkradio.go 6.1 KB

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