root.go 9.4 KB

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