MCPcopy
hub / github.com/g3n/engine / onCursor

Method onCursor

gui/manager.go:167–220  ·  view source on GitHub ↗

onCursor is called when (mouse) cursor events are received. Updates the target/click panels and dispatches OnCursor, OnCursorEnter, OnCursorLeave events.

(evname string, ev interface{})

Source from the content-addressed store, hash-verified

165// onCursor is called when (mouse) cursor events are received.
166// Updates the target/click panels and dispatches OnCursor, OnCursorEnter, OnCursorLeave events.
167func (gm *manager) onCursor(evname string, ev interface{}) {
168
169 // If an IDispatcher is capturing cursor events dispatch to it and return
170 if gm.cursorFocus != nil {
171 gm.cursorFocus.Dispatch(evname, ev)
172 return
173 }
174
175 // If gm.scene is nil then there are no IPanels to send events to
176 if gm.scene == nil {
177 gm.Dispatch(evname, ev) // Dispatch event to non-GUI since event was not filtered by any GUI component
178 return
179 }
180
181 // Get and store CursorEvent
182 gm.cev = ev.(*window.CursorEvent)
183
184 // Temporarily store last target and clear current one
185 oldTarget := gm.target
186 gm.target = nil
187
188 // Find IPanel immediately under the cursor and store it in gm.target
189 gm.forEachIPanel(func(ipan IPanel) {
190 if ipan.InsideBorders(gm.cev.Xpos, gm.cev.Ypos) && (gm.target == nil || ipan.Position().Z < gm.target.GetPanel().Position().Z) {
191 gm.target = ipan
192 }
193 })
194
195 // If the cursor is now over a different panel, dispatch OnCursorLeave/OnCursorEnter
196 if gm.target != oldTarget {
197 // We are only interested in sending events up to the lowest common ancestor of target and oldTarget
198 var commonAnc IPanel
199 if gm.target != nil && oldTarget != nil {
200 commonAnc, _ = gm.target.LowestCommonAncestor(oldTarget).(IPanel)
201 }
202 // If just left a panel and the new panel is not a descendant of the old panel
203 if oldTarget != nil && !oldTarget.IsAncestorOf(gm.target) && (gm.modal == nil || gm.modal.IsAncestorOf(oldTarget)) {
204 sendAncestry(oldTarget, true, commonAnc, gm.modal, OnCursorLeave, ev)
205 }
206 // If just entered a panel and it's not an ancestor of the old panel
207 if gm.target != nil && !gm.target.IsAncestorOf(oldTarget) && (gm.modal == nil || gm.modal.IsAncestorOf(gm.target)) {
208 sendAncestry(gm.target, true, commonAnc, gm.modal, OnCursorEnter, ev)
209 }
210 }
211
212 // Appropriately dispatch the event to target panel's lowest subscribed ancestor or to non-GUI or not at all
213 if gm.target != nil {
214 if gm.modal == nil || gm.modal.IsAncestorOf(gm.target) {
215 sendAncestry(gm.target, false, nil, gm.modal, evname, ev)
216 }
217 } else if gm.modal == nil {
218 gm.Dispatch(evname, ev)
219 }
220}
221
222// sendAncestry sends the specified event (evname/ev) to the specified target panel and its ancestors.
223// If all is false, then the event is only sent to the lowest subscribed ancestor.

Callers 1

SetCursorFocusMethod · 0.95

Calls 8

forEachIPanelMethod · 0.95
sendAncestryFunction · 0.85
DispatchMethod · 0.65
InsideBordersMethod · 0.65
PositionMethod · 0.65
GetPanelMethod · 0.65
LowestCommonAncestorMethod · 0.65
IsAncestorOfMethod · 0.65

Tested by

no test coverage detected