checkradio.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(cb, 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 = NewIcon(" ")
  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) *CheckRadio {
  102. cb.group = group
  103. return cb
  104. }
  105. // SetStyles set the button styles overriding the default style
  106. func (cb *CheckRadio) SetStyles(bs *CheckRadioStyles) {
  107. cb.styles = bs
  108. cb.update()
  109. }
  110. // toggleState toggles the current state of the checkbox/radiobutton
  111. func (cb *CheckRadio) toggleState() {
  112. // Subscribes once to the root panel for OnRadioGroup events
  113. // The root panel is used to dispatch events to all checkradios
  114. if !cb.subroot {
  115. Manager().Subscribe(OnRadioGroup, func(name string, ev interface{}) {
  116. cb.onRadioGroup(ev.(*CheckRadio))
  117. })
  118. cb.subroot = true
  119. }
  120. if cb.check {
  121. cb.state = !cb.state
  122. } else {
  123. if len(cb.group) == 0 {
  124. cb.state = !cb.state
  125. } else {
  126. if cb.state {
  127. return
  128. }
  129. cb.state = !cb.state
  130. }
  131. }
  132. cb.update()
  133. cb.Dispatch(OnChange, nil)
  134. if !cb.check && len(cb.group) > 0 {
  135. Manager().Dispatch(OnRadioGroup, cb)
  136. }
  137. }
  138. // onMouse process OnMouseDown events
  139. func (cb *CheckRadio) onMouse(evname string, ev interface{}) {
  140. // Dispatch OnClick for left mouse button down
  141. if evname == OnMouseDown {
  142. mev := ev.(*window.MouseEvent)
  143. if mev.Button == window.MouseButtonLeft && cb.Enabled() {
  144. Manager().SetKeyFocus(cb)
  145. cb.toggleState()
  146. cb.Dispatch(OnClick, nil)
  147. }
  148. }
  149. }
  150. // onCursor process OnCursor* events
  151. func (cb *CheckRadio) onCursor(evname string, ev interface{}) {
  152. if evname == OnCursorEnter {
  153. cb.cursorOver = true
  154. } else {
  155. cb.cursorOver = false
  156. }
  157. cb.update()
  158. }
  159. // onKey receives subscribed key events
  160. func (cb *CheckRadio) onKey(evname string, ev interface{}) {
  161. kev := ev.(*window.KeyEvent)
  162. if evname == OnKeyDown && kev.Key == window.KeyEnter {
  163. cb.toggleState()
  164. cb.update()
  165. cb.Dispatch(OnClick, nil)
  166. return
  167. }
  168. return
  169. }
  170. // onRadioGroup receives subscribed OnRadioGroup events
  171. func (cb *CheckRadio) onRadioGroup(other *CheckRadio) {
  172. // If event is for this button, ignore
  173. if cb == other {
  174. return
  175. }
  176. // If other radio group is not the group of this button, ignore
  177. if cb.group != other.group {
  178. return
  179. }
  180. // Toggle this button state
  181. cb.SetValue(!other.Value())
  182. }
  183. // update updates the visual appearance of the checkbox
  184. func (cb *CheckRadio) update() {
  185. if cb.state {
  186. cb.icon.SetText(cb.codeON)
  187. } else {
  188. cb.icon.SetText(cb.codeOFF)
  189. }
  190. if !cb.Enabled() {
  191. cb.applyStyle(&cb.styles.Disabled)
  192. return
  193. }
  194. if cb.cursorOver {
  195. cb.applyStyle(&cb.styles.Over)
  196. return
  197. }
  198. cb.applyStyle(&cb.styles.Normal)
  199. }
  200. // setStyle sets the specified checkradio style
  201. func (cb *CheckRadio) applyStyle(s *CheckRadioStyle) {
  202. cb.Panel.ApplyStyle(&s.PanelStyle)
  203. cb.icon.SetColor4(&s.FgColor)
  204. cb.Label.SetColor4(&s.FgColor)
  205. }
  206. // recalc recalculates dimensions and position from inside out
  207. func (cb *CheckRadio) recalc() {
  208. // Sets icon position
  209. cb.icon.SetFontSize(cb.Label.FontSize() * 1.3)
  210. cb.icon.SetPosition(0, 0)
  211. // Label position
  212. spacing := float32(4)
  213. cb.Label.SetPosition(cb.icon.Width()+spacing, 0)
  214. // Content width
  215. width := cb.icon.Width() + spacing + cb.Label.Width()
  216. cb.SetContentSize(width, cb.Label.Height())
  217. }