root.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/core"
  7. "github.com/g3n/engine/gls"
  8. "github.com/g3n/engine/window"
  9. "sort"
  10. )
  11. type Root struct {
  12. Panel // embedded panel
  13. core.TimerManager // embedded TimerManager
  14. gs *gls.GLS // OpenGL state
  15. win window.IWindow // Window
  16. stopPropagation int // stop event propagation bitmask
  17. keyFocus IPanel // current child panel with key focus
  18. mouseFocus IPanel // current child panel with mouse focus
  19. scrollFocus IPanel // current child panel with scroll focus
  20. targets listPanelZ // preallocated list of target panels
  21. }
  22. const (
  23. StopGUI = 0x01 // Stop event propagation to GUI
  24. Stop3D = 0x02 // Stop event propagation to 3D
  25. StopAll = StopGUI | Stop3D // Stop event propagation
  26. )
  27. // NewRoot creates and returns a pointer to a gui root panel for the specified window
  28. func NewRoot(gs *gls.GLS, win window.IWindow) *Root {
  29. r := new(Root)
  30. r.gs = gs
  31. r.win = win
  32. r.Panel.Initialize(0, 0)
  33. r.TimerManager.Initialize()
  34. // for optimization, sets this root panel as not renderable as in most cases
  35. // it is used only as a container
  36. r.SetRenderable(false)
  37. // Subscribe to window events
  38. r.SubscribeWin()
  39. r.targets = []IPanel{}
  40. return r
  41. }
  42. // SubscribeWin subscribes this root panel to window events
  43. func (r *Root) SubscribeWin() {
  44. r.win.Subscribe(window.OnKeyUp, r.onKey)
  45. r.win.Subscribe(window.OnKeyDown, r.onKey)
  46. r.win.Subscribe(window.OnChar, r.onChar)
  47. r.win.Subscribe(window.OnMouseUp, r.onMouse)
  48. r.win.Subscribe(window.OnMouseDown, r.onMouse)
  49. r.win.Subscribe(window.OnCursor, r.onCursor)
  50. r.win.Subscribe(window.OnScroll, r.onScroll)
  51. r.win.Subscribe(window.OnWindowSize, r.onWindowSize)
  52. r.win.Subscribe(window.OnFrame, r.onFrame)
  53. }
  54. // Add adds the specified panel to the root container list of children
  55. func (r *Root) Add(ipan IPanel) {
  56. r.Panel.Add(ipan)
  57. ipan.GetNode().SetParent(r)
  58. }
  59. // SetKeyFocus sets the panel which will receive all keyboard events
  60. // Passing nil will remove the focus (if any)
  61. func (r *Root) SetKeyFocus(ipan IPanel) {
  62. if r.keyFocus != nil {
  63. // If this panel is already in focus, nothing to do
  64. if ipan != nil {
  65. if r.keyFocus.GetPanel() == ipan.GetPanel() {
  66. return
  67. }
  68. }
  69. r.keyFocus.LostKeyFocus()
  70. }
  71. r.keyFocus = ipan
  72. }
  73. // ClearKeyFocus clears the key focus panel (if any) without
  74. // calling LostKeyFocus() for previous focused panel
  75. func (r *Root) ClearKeyFocus() {
  76. r.keyFocus = nil
  77. }
  78. // SetMouseFocus sets the panel which will receive all mouse events
  79. // Passing nil will restore the default event processing
  80. func (r *Root) SetMouseFocus(ipan IPanel) {
  81. r.mouseFocus = ipan
  82. }
  83. // SetScrollFocus sets the panel which will receive all scroll events
  84. // Passing nil will restore the default event processing
  85. func (r *Root) SetScrollFocus(ipan IPanel) {
  86. r.scrollFocus = ipan
  87. }
  88. // HasKeyFocus checks if the specified panel has the key focus
  89. func (r *Root) HasKeyFocus(ipan IPanel) bool {
  90. if r.keyFocus == nil {
  91. return false
  92. }
  93. if r.keyFocus.GetPanel() == ipan.GetPanel() {
  94. return true
  95. }
  96. return false
  97. }
  98. // HasMouseFocus checks if the specified panel has the mouse focus
  99. func (r *Root) HasMouseFocus(ipan IPanel) bool {
  100. if r.mouseFocus == nil {
  101. return false
  102. }
  103. if r.mouseFocus.GetPanel() == ipan.GetPanel() {
  104. return true
  105. }
  106. return false
  107. }
  108. // StopPropagation stops the propagation of the current event
  109. // to outside the root panel (for example the 3D camera)
  110. func (r *Root) StopPropagation(events int) {
  111. r.stopPropagation |= events
  112. }
  113. // SetCursorNormal sets the cursor of the associated window to
  114. // standard type
  115. func (r *Root) SetCursorNormal() {
  116. r.win.SetStandardCursor(window.ArrowCursor)
  117. }
  118. // SetCursorDrag sets the cursor of the associated window to
  119. // drag type
  120. func (r *Root) SetCursorDrag() {
  121. r.win.SetStandardCursor(window.HandCursor)
  122. }
  123. // SetCursorHResize sets the cursor of the associated window to
  124. // horizontal resize type
  125. func (r *Root) SetCursorHResize() {
  126. r.win.SetStandardCursor(window.HResizeCursor)
  127. }
  128. // SetCursorVResize sets the cursor of the associated window to
  129. // vertical resize type
  130. func (r *Root) SetCursorVResize() {
  131. r.win.SetStandardCursor(window.VResizeCursor)
  132. }
  133. // onKey is called when key events are received
  134. func (r *Root) onKey(evname string, ev interface{}) {
  135. // If no panel has the key focus, nothing to do
  136. if r.keyFocus == nil {
  137. return
  138. }
  139. // Dispatch window.KeyEvent to focused panel subscribers
  140. r.stopPropagation = 0
  141. r.keyFocus.GetPanel().Dispatch(evname, ev)
  142. // If requested, stop propagation of event outside the root gui
  143. if (r.stopPropagation & Stop3D) != 0 {
  144. r.win.CancelDispatch()
  145. }
  146. }
  147. // onChar is called when char events are received
  148. func (r *Root) onChar(evname string, ev interface{}) {
  149. // If no panel has the key focus, nothing to do
  150. if r.keyFocus == nil {
  151. return
  152. }
  153. // Dispatch window.CharEvent to focused panel subscribers
  154. r.stopPropagation = 0
  155. r.keyFocus.GetPanel().Dispatch(evname, ev)
  156. // If requested, stopj propagation of event outside the root gui
  157. if (r.stopPropagation & Stop3D) != 0 {
  158. r.win.CancelDispatch()
  159. }
  160. }
  161. // onMouse is called when mouse button events are received
  162. func (r *Root) onMouse(evname string, ev interface{}) {
  163. mev := ev.(*window.MouseEvent)
  164. r.sendPanels(mev.Xpos, mev.Ypos, evname, ev)
  165. }
  166. // onCursor is called when (mouse) cursor events are received
  167. func (r *Root) onCursor(evname string, ev interface{}) {
  168. cev := ev.(*window.CursorEvent)
  169. r.sendPanels(cev.Xpos, cev.Ypos, evname, ev)
  170. }
  171. // sendPanel sends mouse or cursor event to focused panel or panels
  172. // which contains the specified screen position
  173. func (r *Root) sendPanels(x, y float32, evname string, ev interface{}) {
  174. // If there is panel with MouseFocus send only to this panel
  175. if r.mouseFocus != nil {
  176. r.mouseFocus.GetPanel().Dispatch(evname, ev)
  177. if (r.stopPropagation & Stop3D) != 0 {
  178. r.win.CancelDispatch()
  179. }
  180. return
  181. }
  182. // Clear list of panels which contains the mouse position
  183. r.targets = r.targets[0:0]
  184. // checkPanel checks recursively if the specified panel and
  185. // any of its child contains the mouse position
  186. var checkPanel func(ipan IPanel)
  187. checkPanel = func(ipan IPanel) {
  188. pan := ipan.GetPanel()
  189. // If panel not visible or not enabled, ignore
  190. if !pan.Visible() || !pan.Enabled() {
  191. return
  192. }
  193. // Checks if this panel contains the mouse position
  194. found := pan.ContainsPosition(x, y)
  195. if found {
  196. r.targets = append(r.targets, ipan)
  197. } else {
  198. ipan.SetRoot(r)
  199. // If OnCursorEnter previously sent, sends OnCursorLeave with a nil event
  200. if pan.cursorEnter {
  201. pan.Dispatch(OnCursorLeave, nil)
  202. pan.cursorEnter = false
  203. }
  204. // If mouse button was pressed, sends event informing mouse down outside of the panel
  205. if evname == OnMouseDown {
  206. pan.Dispatch(OnMouseOut, ev)
  207. }
  208. }
  209. // Checks if any of its children also contains the position
  210. for _, child := range pan.Children() {
  211. ipan := child.(IPanel)
  212. checkPanel(ipan)
  213. }
  214. }
  215. // Checks all children of this root node
  216. for _, iobj := range r.Node.Children() {
  217. ipan, ok := iobj.(IPanel)
  218. if !ok {
  219. continue
  220. }
  221. checkPanel(ipan)
  222. }
  223. // No panels found
  224. if len(r.targets) == 0 {
  225. // If event is mouse click, removes the keyboard focus
  226. if evname == OnMouseDown {
  227. r.SetKeyFocus(nil)
  228. }
  229. return
  230. }
  231. // Sorts panels by absolute Z with the most foreground panels first
  232. // and sends event to all panels or until a stop is requested
  233. sort.Sort(r.targets)
  234. r.stopPropagation = 0
  235. // Send events to panels
  236. for _, ipan := range r.targets {
  237. ipan.SetRoot(r)
  238. pan := ipan.GetPanel()
  239. // Cursor position event
  240. if evname == OnCursor {
  241. pan.Dispatch(evname, ev)
  242. if !pan.cursorEnter {
  243. pan.Dispatch(OnCursorEnter, ev)
  244. pan.cursorEnter = true
  245. }
  246. // Mouse button event
  247. } else {
  248. pan.Dispatch(evname, ev)
  249. }
  250. if (r.stopPropagation & StopGUI) != 0 {
  251. break
  252. }
  253. }
  254. // Stops propagation of event outside the root gui
  255. if (r.stopPropagation & Stop3D) != 0 {
  256. r.win.CancelDispatch()
  257. }
  258. }
  259. // onScroll is called when scroll events are received and
  260. // is responsible to dispatch them to child panels.
  261. func (r *Root) onScroll(evname string, ev interface{}) {
  262. // If no panel with the scroll focus, nothing to do
  263. if r.scrollFocus == nil {
  264. return
  265. }
  266. // Dispatch event to panel with scroll focus
  267. r.scrollFocus.GetPanel().Dispatch(evname, ev)
  268. // Stops propagation of event outside the root gui
  269. if (r.stopPropagation & Stop3D) != 0 {
  270. r.win.CancelDispatch()
  271. }
  272. }
  273. // onSize is called when window size events are received
  274. func (r *Root) onWindowSize(evname string, ev interface{}) {
  275. // Sends event only to immediate children
  276. for _, ipan := range r.Children() {
  277. ipan.(IPanel).GetPanel().Dispatch(evname, ev)
  278. }
  279. }
  280. // onFrame is called when window finished swapping frame buffers
  281. func (r *Root) onFrame(evname string, ev interface{}) {
  282. r.TimerManager.ProcessTimers()
  283. }
  284. // For sorting panels by Z coordinate
  285. type listPanelZ []IPanel
  286. func (p listPanelZ) Len() int { return len(p) }
  287. func (p listPanelZ) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  288. func (p listPanelZ) Less(i, j int) bool {
  289. iz := p[i].GetPanel().pospix.Z
  290. jz := p[j].GetPanel().pospix.Z
  291. return iz < jz
  292. }