root.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. // Root is the container and dispatcher of panel events
  12. type Root struct {
  13. Panel // embedded panel
  14. core.TimerManager // embedded TimerManager
  15. gs *gls.GLS // OpenGL state
  16. win window.IWindow // Window
  17. stopPropagation int // stop event propagation bitmask
  18. keyFocus IPanel // current child panel with key focus
  19. mouseFocus IPanel // current child panel with mouse focus
  20. scrollFocus IPanel // current child panel with scroll focus
  21. modalPanel IPanel // current modal panel
  22. targets []IPanel // preallocated list of target panels
  23. }
  24. // Types of event propagation stopping.
  25. const (
  26. StopGUI = 0x01 // Stop event propagation to GUI
  27. Stop3D = 0x02 // Stop event propagation to 3D
  28. StopAll = StopGUI | Stop3D // Stop event propagation
  29. )
  30. // NewRoot creates and returns a pointer to a gui root panel for the specified window
  31. func NewRoot(gs *gls.GLS, win window.IWindow) *Root {
  32. r := new(Root)
  33. r.gs = gs
  34. r.win = win
  35. r.root = r
  36. r.Panel.Initialize(0, 0)
  37. r.TimerManager.Initialize()
  38. // for optimization, sets this root panel as not renderable as in most cases
  39. // it is used only as a container
  40. r.SetRenderable(false)
  41. // Subscribe to window events
  42. r.SubscribeWin()
  43. r.targets = []IPanel{}
  44. return r
  45. }
  46. // SubscribeWin subscribes this root panel to window events
  47. func (r *Root) SubscribeWin() {
  48. r.win.Subscribe(window.OnKeyUp, r.onKey)
  49. r.win.Subscribe(window.OnKeyDown, r.onKey)
  50. r.win.Subscribe(window.OnKeyRepeat, r.onKey)
  51. r.win.Subscribe(window.OnChar, r.onChar)
  52. r.win.Subscribe(window.OnMouseUp, r.onMouse)
  53. r.win.Subscribe(window.OnMouseDown, r.onMouse)
  54. r.win.Subscribe(window.OnCursor, r.onCursor)
  55. r.win.Subscribe(window.OnScroll, r.onScroll)
  56. r.win.Subscribe(window.OnWindowSize, r.onWindowSize)
  57. r.win.Subscribe(window.OnFrame, r.onFrame)
  58. }
  59. // Add adds the specified panel to the root container list of children
  60. // Overrides the Panel version because it needs to set the root panel field
  61. func (r *Root) Add(ipan IPanel) {
  62. // Sets the root panel field of the child to be added
  63. ipan.GetPanel().root = r
  64. // Add this panel to the root panel children.
  65. // This will also set the root panel for all the child children
  66. // and the z coordinates of all the panel tree graph.
  67. r.Panel.Add(ipan)
  68. }
  69. // SetModal sets the modal panel.
  70. // If there is a modal panel, only events for this panel are dispatched
  71. // To remove the modal panel call this function with a nil panel.
  72. func (r *Root) SetModal(ipan IPanel) {
  73. r.modalPanel = ipan
  74. }
  75. // SetKeyFocus sets the panel which will receive all keyboard events
  76. // Passing nil will remove the focus (if any)
  77. func (r *Root) SetKeyFocus(ipan IPanel) {
  78. if r.keyFocus != nil {
  79. // If this panel is already in focus, nothing to do
  80. if ipan != nil {
  81. if r.keyFocus.GetPanel() == ipan.GetPanel() {
  82. return
  83. }
  84. }
  85. r.keyFocus.LostKeyFocus()
  86. }
  87. r.keyFocus = ipan
  88. }
  89. // ClearKeyFocus clears the key focus panel (if any) without
  90. // calling LostKeyFocus() for previous focused panel
  91. func (r *Root) ClearKeyFocus() {
  92. r.keyFocus = nil
  93. }
  94. // SetMouseFocus sets the panel which will receive all mouse events
  95. // Passing nil will restore the default event processing
  96. func (r *Root) SetMouseFocus(ipan IPanel) {
  97. r.mouseFocus = ipan
  98. }
  99. // SetScrollFocus sets the panel which will receive all scroll events
  100. // Passing nil will restore the default event processing
  101. func (r *Root) SetScrollFocus(ipan IPanel) {
  102. r.scrollFocus = ipan
  103. }
  104. // HasKeyFocus checks if the specified panel has the key focus
  105. func (r *Root) HasKeyFocus(ipan IPanel) bool {
  106. if r.keyFocus == nil {
  107. return false
  108. }
  109. if r.keyFocus.GetPanel() == ipan.GetPanel() {
  110. return true
  111. }
  112. return false
  113. }
  114. // HasMouseFocus checks if the specified panel has the mouse focus
  115. func (r *Root) HasMouseFocus(ipan IPanel) bool {
  116. if r.mouseFocus == nil {
  117. return false
  118. }
  119. if r.mouseFocus.GetPanel() == ipan.GetPanel() {
  120. return true
  121. }
  122. return false
  123. }
  124. // StopPropagation stops the propagation of the current event
  125. // to outside the root panel (for example the 3D camera)
  126. func (r *Root) StopPropagation(events int) {
  127. r.stopPropagation |= events
  128. }
  129. // SetCursorNormal sets the cursor over the associated window to the standard type.
  130. func (r *Root) SetCursorNormal() {
  131. r.win.SetStandardCursor(window.ArrowCursor)
  132. }
  133. // SetCursorText sets the cursor over the associated window to the I-Beam type.
  134. func (r *Root) SetCursorText() {
  135. r.win.SetStandardCursor(window.IBeamCursor)
  136. }
  137. // SetCursorCrosshair sets the cursor over the associated window to the crosshair type.
  138. func (r *Root) SetCursorCrosshair() {
  139. r.win.SetStandardCursor(window.CrosshairCursor)
  140. }
  141. // SetCursorHand sets the cursor over the associated window to the hand type.
  142. func (r *Root) SetCursorHand() {
  143. r.win.SetStandardCursor(window.HandCursor)
  144. }
  145. // SetCursorHResize sets the cursor over the associated window to the horizontal resize type.
  146. func (r *Root) SetCursorHResize() {
  147. r.win.SetStandardCursor(window.HResizeCursor)
  148. }
  149. // SetCursorVResize sets the cursor over the associated window to the vertical resize type.
  150. func (r *Root) SetCursorVResize() {
  151. r.win.SetStandardCursor(window.VResizeCursor)
  152. }
  153. // TODO allow setting a custom cursor
  154. // onKey is called when key events are received
  155. func (r *Root) onKey(evname string, ev interface{}) {
  156. // If no panel has the key focus, nothing to do
  157. if r.keyFocus == nil {
  158. return
  159. }
  160. // Checks modal panel
  161. if !r.canDispatch(r.keyFocus) {
  162. return
  163. }
  164. // Dispatch window.KeyEvent to focused panel subscribers
  165. r.stopPropagation = 0
  166. r.keyFocus.GetPanel().Dispatch(evname, ev)
  167. // If requested, stop propagation of event outside the root gui
  168. if (r.stopPropagation & Stop3D) != 0 {
  169. r.win.CancelDispatch()
  170. }
  171. }
  172. // onChar is called when char events are received
  173. func (r *Root) onChar(evname string, ev interface{}) {
  174. // If no panel has the key focus, nothing to do
  175. if r.keyFocus == nil {
  176. return
  177. }
  178. // Checks modal panel
  179. if !r.canDispatch(r.keyFocus) {
  180. return
  181. }
  182. // Dispatch window.CharEvent to focused panel subscribers
  183. r.stopPropagation = 0
  184. r.keyFocus.GetPanel().Dispatch(evname, ev)
  185. // If requested, stopj propagation of event outside the root gui
  186. if (r.stopPropagation & Stop3D) != 0 {
  187. r.win.CancelDispatch()
  188. }
  189. }
  190. // onMouse is called when mouse button events are received
  191. func (r *Root) onMouse(evname string, ev interface{}) {
  192. mev := ev.(*window.MouseEvent)
  193. r.sendPanels(mev.Xpos, mev.Ypos, evname, ev)
  194. }
  195. // onCursor is called when (mouse) cursor events are received
  196. func (r *Root) onCursor(evname string, ev interface{}) {
  197. cev := ev.(*window.CursorEvent)
  198. r.sendPanels(cev.Xpos, cev.Ypos, evname, ev)
  199. }
  200. // sendPanel sends a mouse or cursor event to focused panel or panels
  201. // which contain the specified screen position
  202. func (r *Root) sendPanels(x, y float32, evname string, ev interface{}) {
  203. // If there is panel with MouseFocus send only to this panel
  204. if r.mouseFocus != nil {
  205. // Checks modal panel
  206. if !r.canDispatch(r.mouseFocus) {
  207. return
  208. }
  209. r.mouseFocus.GetPanel().Dispatch(evname, ev)
  210. if (r.stopPropagation & Stop3D) != 0 {
  211. r.win.CancelDispatch()
  212. }
  213. return
  214. }
  215. // Clear list of panels which contains the mouse position
  216. r.targets = r.targets[0:0]
  217. // checkPanel checks recursively if the specified panel and
  218. // any of its children contain the mouse position
  219. var checkPanel func(ipan IPanel)
  220. checkPanel = func(ipan IPanel) {
  221. pan := ipan.GetPanel()
  222. // If panel not visible or not enabled, ignore
  223. if !pan.Visible() || !pan.Enabled() {
  224. return
  225. }
  226. // Checks if this panel contains the mouse position
  227. found := pan.InsideBorders(x, y)
  228. if found {
  229. r.targets = append(r.targets, ipan)
  230. } else {
  231. // If OnCursorEnter previously sent, sends OnCursorLeave with a nil event
  232. if pan.cursorEnter {
  233. pan.Dispatch(OnCursorLeave, nil)
  234. pan.cursorEnter = false
  235. }
  236. // If mouse button was pressed, sends event informing mouse down outside of the panel
  237. if evname == OnMouseDown {
  238. pan.Dispatch(OnMouseOut, ev)
  239. }
  240. }
  241. // Checks if any of its children also contains the position
  242. for _, child := range pan.Children() {
  243. ipan, ok := child.(IPanel)
  244. if ok {
  245. checkPanel(ipan)
  246. }
  247. }
  248. }
  249. // Checks all children of this root node
  250. for _, iobj := range r.Node.Children() {
  251. ipan, ok := iobj.(IPanel)
  252. if !ok {
  253. continue
  254. }
  255. checkPanel(ipan)
  256. }
  257. // No panels found
  258. if len(r.targets) == 0 {
  259. // If event is mouse click, removes the keyboard focus
  260. if evname == OnMouseDown {
  261. r.SetKeyFocus(nil)
  262. }
  263. return
  264. }
  265. // Sorts panels by absolute Z with the most foreground panels first
  266. // and sends event to all panels or until a stop is requested
  267. sort.Slice(r.targets, func(i, j int) bool {
  268. iz := r.targets[i].GetPanel().Position().Z
  269. jz := r.targets[j].GetPanel().Position().Z
  270. return iz < jz
  271. })
  272. r.stopPropagation = 0
  273. // Send events to panels
  274. for _, ipan := range r.targets {
  275. // Checks modal panel
  276. if !r.canDispatch(ipan) {
  277. continue
  278. }
  279. pan := ipan.GetPanel()
  280. // Cursor position event
  281. if evname == OnCursor {
  282. pan.Dispatch(evname, ev)
  283. if !pan.cursorEnter {
  284. pan.Dispatch(OnCursorEnter, ev)
  285. pan.cursorEnter = true
  286. }
  287. // Mouse button event
  288. } else {
  289. pan.Dispatch(evname, ev)
  290. }
  291. if (r.stopPropagation & StopGUI) != 0 {
  292. break
  293. }
  294. }
  295. // Stops propagation of event outside the root gui
  296. if (r.stopPropagation & Stop3D) != 0 {
  297. r.win.CancelDispatch()
  298. }
  299. }
  300. // onScroll is called when scroll events are received and
  301. // is responsible to dispatch them to child panels.
  302. func (r *Root) onScroll(evname string, ev interface{}) {
  303. // If no panel with the scroll focus, nothing to do
  304. if r.scrollFocus == nil {
  305. return
  306. }
  307. // Dispatch event to panel with scroll focus
  308. r.scrollFocus.GetPanel().Dispatch(evname, ev)
  309. // Stops propagation of event outside the root gui
  310. if (r.stopPropagation & Stop3D) != 0 {
  311. r.win.CancelDispatch()
  312. }
  313. }
  314. // onSize is called when window size events are received
  315. func (r *Root) onWindowSize(evname string, ev interface{}) {
  316. // Sends event only to immediate children
  317. for _, ipan := range r.Children() {
  318. ipan.(IPanel).GetPanel().Dispatch(evname, ev)
  319. }
  320. }
  321. // onFrame is called when window finished swapping frame buffers
  322. func (r *Root) onFrame(evname string, ev interface{}) {
  323. r.TimerManager.ProcessTimers()
  324. }
  325. // canDispatch returns if event can be dispatched to the specified panel
  326. // An event cannot be dispatched if there is a modal panel and the specified
  327. // panel is not the modal panel or any of its children.
  328. func (r *Root) canDispatch(ipan IPanel) bool {
  329. if r.modalPanel == nil {
  330. return true
  331. }
  332. if r.modalPanel == ipan {
  333. return true
  334. }
  335. // Internal function to check panel children recursively
  336. var checkChildren func(iparent IPanel) bool
  337. checkChildren = func(iparent IPanel) bool {
  338. parent := iparent.GetPanel()
  339. for _, child := range parent.Children() {
  340. if child == ipan {
  341. return true
  342. }
  343. res := checkChildren(child.(IPanel))
  344. if res {
  345. return res
  346. }
  347. }
  348. return false
  349. }
  350. return checkChildren(r.modalPanel)
  351. }
  352. //func (r *Root) applyStyleRecursively(s *Style) {
  353. // // TODO
  354. // // This should probably be in Panel ?
  355. //}