Skip to content

Commit

Permalink
take scale mode into account for mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Sep 17, 2023
1 parent ea7ec80 commit 830aefa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions haxe/ui/backend/ScreenImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ScreenImpl extends ScreenBase {
}

private function onWindowResize() {
MouseHelper.updateScale(true);
resizeRootComponents();
}

Expand All @@ -63,6 +64,7 @@ class ScreenImpl extends ScreenBase {
}
private function set_root(value:Object):Object {
_root = value;
MouseHelper.updateScale();
return value;
}

Expand Down
37 changes: 29 additions & 8 deletions haxe/ui/backend/heaps/MouseHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,30 @@ class MouseHelper {
}
}

private static var scaleX:Null<Float> = null;
private static var scaleY:Null<Float> = null;
public static function updateScale(force:Bool = false) {
if (force) {
scaleX = null;
scaleY = null;
}
if (scaleX != null && scaleY != null) {
return;
}

switch (Screen.instance.scene.scaleMode) {
case LetterBox(width, height, integerScale, horizontalAlign, verticalAlign):
scaleX = hxd.Window.getInstance().width / width;
scaleY = hxd.Window.getInstance().height / height;
case _:
scaleX = 1;
scaleY = 1;
}
}

private static function onMouseMove(e:hxd.Event) {
currentMouseX = e.relX / Toolkit.scaleX;
currentMouseY = e.relY / Toolkit.scaleY;
currentMouseX = e.relX / (Toolkit.scaleX * scaleX);
currentMouseY = e.relY / (Toolkit.scaleY * scaleY);

var list = _callbacks.get(MouseEvent.MOUSE_MOVE);
if (list == null || list.length == 0) {
Expand All @@ -122,8 +143,8 @@ class MouseHelper {

var event = new MouseEvent(MouseEvent.MOUSE_MOVE);
@:privateAccess event._originalEvent = e;
event.screenX = e.relX / Toolkit.scaleX;
event.screenY = e.relY / Toolkit.scaleY;
event.screenX = e.relX / (Toolkit.scaleX * scaleX);
event.screenY = e.relY / (Toolkit.scaleY * scaleY);
for (l in list) {
l(event);
if (event.canceled) {
Expand All @@ -143,8 +164,8 @@ class MouseHelper {

var event = new MouseEvent(MouseEvent.MOUSE_DOWN);
@:privateAccess event._originalEvent = e;
event.screenX = e.relX / Toolkit.scaleX;
event.screenY = e.relY / Toolkit.scaleY;
event.screenX = e.relX / (Toolkit.scaleX * scaleX);
event.screenY = e.relY / (Toolkit.scaleX * scaleX);
event.data = e.button;
for (l in list) {
l(event);
Expand All @@ -165,8 +186,8 @@ class MouseHelper {

var event = new MouseEvent(MouseEvent.MOUSE_UP);
@:privateAccess event._originalEvent = e;
event.screenX = e.relX / Toolkit.scaleX;
event.screenY = e.relY / Toolkit.scaleY;
event.screenX = e.relX / (Toolkit.scaleX * scaleX);
event.screenY = e.relY / (Toolkit.scaleX * scaleX);
event.data = e.button;
for (l in list) {
l(event);
Expand Down

0 comments on commit 830aefa

Please sign in to comment.