Skip to content

Commit

Permalink
better mouse handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Dec 17, 2023
1 parent 7144193 commit b8ef3b5
Showing 1 changed file with 69 additions and 136 deletions.
205 changes: 69 additions & 136 deletions haxe/ui/backend/ComponentImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -744,46 +744,23 @@ class ComponentImpl extends ComponentBase {
private var _mouseOverFlag:Bool = false;
private var _cursorSet:Bool = false;
private function __onMouseMove(event:MouseEvent) {
if (Platform.instance.isMobile) {
return;
}

var x = event.screenX;
var y = event.screenY;
lastMouseX = x;
lastMouseY = y;

var hasMember = (this.state == StateHelper.currentState);
var inCurrentState = (this.state == StateHelper.currentState);

if (Platform.instance.isMobile == false) {
if (inCurrentState == false) {
if (_mouseOverFlag == true) {
if (hasMember == false) {
_mouseOverFlag = false;
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_OUT);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_OUT);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
#if mobile
mouseEvent.touchEvent = true;
#end
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}
return;
}
}

if (hasMember == false) {
return;
}

var i = inBounds(x, y);
if (i == true) {
if (this.style != null && this.style.cursor != null) {
Screen.instance.setCursor(this.style.cursor, this.style.cursorOffsetX, this.style.cursorOffsetY);
_cursorSet = true;
}

var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_MOVE);
_mouseOverFlag = false;
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_OUT);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_MOVE);
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_OUT);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
#if mobile
Expand All @@ -793,132 +770,116 @@ class ComponentImpl extends ComponentBase {
event.canceled = mouseEvent.canceled;
}
}

if (i == true && _mouseOverFlag == false) {
if (isEventRelevant(getComponentsAtPoint(x, y, true), MouseEvent.MOUSE_OVER)) {
if (this.style != null && this.style.cursor != null) {
Screen.instance.setCursor(this.style.cursor, this.style.cursorOffsetX, this.style.cursorOffsetY);
_cursorSet = true;
}

_mouseOverFlag = true;
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_OVER);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_OVER);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
#if mobile
mouseEvent.touchEvent = true;
#end
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}
}
} else if (i == false && _mouseOverFlag == true) {
if (_cursorSet) {
Screen.instance.setCursor("default");
_cursorSet = false;
}
return;
}

_mouseOverFlag = false;
var i = inBounds(x, y);
if (i == true) {
if (hasComponentOver(cast this, x, y) == true) {
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_OUT);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_OUT);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
#if mobile
mouseEvent.touchEvent = true;
#end
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}
return;
}
if (this.style != null && this.style.cursor != null) {
Screen.instance.setCursor(this.style.cursor, this.style.cursorOffsetX, this.style.cursorOffsetY);
}
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_MOVE);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_MOVE);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
fn(mouseEvent);
}
}
if (i == true && _mouseOverFlag == false) {
if (hasComponentOver(cast this, x, y) == true) {
return;
}
_mouseOverFlag = true;
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_OVER);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_OVER);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
fn(mouseEvent);
}
} else if (i == false && _mouseOverFlag == true) {
_mouseOverFlag = false;
Screen.instance.setCursor("default");
var fn:UIEvent->Void = _eventMap.get(haxe.ui.events.MouseEvent.MOUSE_OUT);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(haxe.ui.events.MouseEvent.MOUSE_OUT);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
fn(mouseEvent);
}
}
}

private var _mouseDownFlag:Bool = false;
private var _mouseDownButton:Int = -1;
private function __onMouseDown(event:MouseEvent) {
if (Platform.instance.isMobile == false) {
if (_mouseOverFlag == false) {
return;
}
}

if (this.state != StateHelper.currentState) {
return;
}

var button:Int = event.data;
var x = event.screenX;
var y = event.screenY;

lastMouseX = x;
lastMouseY = y;
var i = inBounds(x, y);
if (i == true && _mouseDownFlag == false) {
/*
if (hasComponentOver(cast this, x, y) == true) {
return;
}
*/
if (isEventRelevant(getComponentsAtPoint(x, y, true), MouseEvent.MOUSE_DOWN)) {
_mouseDownFlag = true;
_mouseDownButton = button;
var type = button == 0 ? haxe.ui.events.MouseEvent.MOUSE_DOWN: haxe.ui.events.MouseEvent.RIGHT_MOUSE_DOWN;
var fn:UIEvent->Void = _eventMap.get(type);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(type);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
if (Platform.instance.isMobile) {
mouseEvent.touchEvent = true;
}
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}
_mouseDownFlag = true;

var type = button == 0 ? haxe.ui.events.MouseEvent.MOUSE_DOWN: haxe.ui.events.MouseEvent.RIGHT_MOUSE_DOWN;
var fn:UIEvent->Void = _eventMap.get(type);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(type);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
fn(mouseEvent);
}
}
}

private function __onMouseUp(event:MouseEvent) {
if (Platform.instance.isMobile == false) {
if (_mouseOverFlag == false) {
//return;
}
}

if (this.state != StateHelper.currentState) {
return;
}
var button:Int = _mouseDownButton;

var button:Int = event.data;
var x = event.screenX;
var y = event.screenY;

lastMouseX = x;
lastMouseY = y;

var i = inBounds(x, y);
if (i == true) {
/*
if (hasComponentOver(cast this, x, y) == true) {
return;
}
*/


if (_mouseDownFlag == true) {
var type = button == 0 ? haxe.ui.events.MouseEvent.CLICK: haxe.ui.events.MouseEvent.RIGHT_CLICK;
var fn:UIEvent->Void = _eventMap.get(type);
if (fn != null) {
var mouseEvent = new haxe.ui.events.MouseEvent(type);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
if (Platform.instance.isMobile) {
mouseEvent.touchEvent = true;
}
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}

if (type == haxe.ui.events.MouseEvent.CLICK) {
_lastClickTimeDiff = Timer.stamp() - _lastClickTime;
_lastClickTime = Timer.stamp();
Expand All @@ -936,14 +897,10 @@ class ComponentImpl extends ComponentBase {
var mouseEvent = new haxe.ui.events.MouseEvent(type);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
if (Platform.instance.isMobile) {
mouseEvent.touchEvent = true;
}
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}
}
_mouseDownFlag = false;
_mouseDownFlag = false;
}

#if haxeui_dont_impose_base_class
Expand All @@ -952,24 +909,18 @@ class ComponentImpl extends ComponentBase {
#end

private function __onDoubleClick(event:MouseEvent) {
if (this.state != StateHelper.currentState) {
return;
}

var button:Int = _mouseDownButton;
var button:Int = event.data;
var x = event.screenX;
var y = event.screenY;

lastMouseX = x;
lastMouseY = y;
var i = inBounds(x, y);
if (i == true && button == 0) {
/*
if (hasComponentOver(cast this, x, y) == true) {
return;
}
*/


_mouseDownFlag = false;
var mouseDelta:Float = MathUtil.distance(x, y, _lastClickX, _lastClickY);
if (_lastClickTimeDiff < 0.5 && mouseDelta < 5) { // 0.5 seconds
Expand All @@ -979,11 +930,7 @@ class ComponentImpl extends ComponentBase {
var mouseEvent = new haxe.ui.events.MouseEvent(type);
mouseEvent.screenX = x / Toolkit.scaleX;
mouseEvent.screenY = y / Toolkit.scaleY;
if (Platform.instance.isMobile) {
mouseEvent.touchEvent = true;
}
fn(mouseEvent);
event.canceled = mouseEvent.canceled;
}
}
}
Expand Down Expand Up @@ -1017,20 +964,6 @@ class ComponentImpl extends ComponentBase {
event.canceled = mouseEvent.canceled;
}

private function isEventRelevant(children:Array<Component>, eventType:String):Bool {
var relevant = false;
for (c in children) {
if (c == this) {
relevant = true;
}
if (c.parentComponent == null) {
break;
}
}

return relevant;
}

//***********************************************************************************************************
// Text related
//***********************************************************************************************************
Expand Down Expand Up @@ -1194,7 +1127,7 @@ class ComponentImpl extends ComponentBase {
repositionChildren();

_updates++;
if (_updates == 1) {
if (_updates == 2) {
if (asComponent.hidden == false) {
applyVisibility(true);
} else {
Expand Down

0 comments on commit b8ef3b5

Please sign in to comment.