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