root.go 11 KB

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