root.go 11 KB

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