Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all S2D/Camera mismatches between window size and engine size #1191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions h2d/Camera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ class Camera {
Requires Camera being attached to a Scene.
**/
inline function screenXToCamera( mx : Float, my : Float ) : Float {
mx *= scene.engineScaleX;
my *= scene.engineScaleY;

return sceneXToCamera((mx - scene.offsetX) / scene.viewportScaleX, (my - scene.offsetY) / scene.viewportScaleY);
}

Expand All @@ -318,25 +321,28 @@ class Camera {
Requires Camera being attached to a Scene.
**/
inline function screenYToCamera( mx : Float, my : Float ) : Float {
mx *= scene.engineScaleX;
my *= scene.engineScaleY;

return sceneYToCamera((mx - scene.offsetX) / scene.viewportScaleX, (my - scene.offsetY) / scene.viewportScaleY);
}

/**
Convert local camera position to absolute screen position.
Convert local camera position to absolute screen (engine) position.

Requires Camera being attached to a Scene.
**/
inline function cameraXToScreen( mx : Float, my : Float ) : Float {
return cameraXToScene(mx, my) * scene.viewportScaleX + scene.offsetX;
return (cameraXToScene(mx, my) * scene.viewportScaleX + scene.offsetX) * scene.engineScaleX;
}

/**
Convert local camera position to absolute screen position.
Convert local camera position to absolute screen (engine) position.

Requires Camera being attached to a Scene.
**/
inline function cameraYToScreen( mx : Float, my : Float ) : Float {
return cameraYToScene(mx, my) * scene.viewportScaleY + scene.offsetY;
return (cameraYToScene(mx, my) * scene.viewportScaleY + scene.offsetY) * scene.engineScaleY;
}

// Scene <-> Camera
Expand Down Expand Up @@ -380,8 +386,8 @@ class Camera {
@:dox(hide) @:noCompletion public function eventToCamera( e : hxd.Event ) {
var x = (e.relX - scene.offsetX) / scene.viewportScaleX - absX;
var y = (e.relY - scene.offsetY) / scene.viewportScaleY - absY;
e.relX = (x * matD - y * matC) * invDet;
e.relY = (-x * matB + y * matA) * invDet;
e.relX = (x * matD - y * matC) * invDet * scene.engineScaleX;
e.relY = (-x * matB + y * matA) * invDet * scene.engineScaleY;
}

/**
Expand All @@ -393,8 +399,8 @@ class Camera {
checkScene();
var x = (pt.x - scene.offsetX) / scene.viewportScaleX - absX;
var y = (pt.y - scene.offsetY) / scene.viewportScaleY - absY;
pt.x = (x * matD - y * matC) * invDet;
pt.y = (-x * matB + y * matA) * invDet;
pt.x = (x * matD - y * matC) * invDet * scene.engineScaleX;
pt.y = (-x * matB + y * matA) * invDet * scene.engineScaleY;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions h2d/Scene.hx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
**/
public var viewportScaleY(default, null) : Float;

/**
Ratio of the engine-to-screen X mismatch in case of manual Engine.resize() calls.
**/
public var engineScaleX(default, null): Float;
/**
Ratio of the engine-to-screen Y mismatch in case of manual Engine.resize() calls.
**/
public var engineScaleY(default, null): Float;

/**
The current mouse X coordinates (in pixels) relative to the current `Scene.interactiveCamera`.
**/
Expand Down Expand Up @@ -251,6 +260,8 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
eventListeners = new Array();
shapePoint = new h2d.col.Point();
window = hxd.Window.getInstance();
engineScaleX = e.width / window.width;
engineScaleY = e.height / window.height;
posChanged = true;
}

Expand Down Expand Up @@ -334,6 +345,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
var engine = h3d.Engine.getCurrent();
if (engine == null) return;

engineScaleX = engine.width / window.width;
engineScaleY = engine.height / window.height;

inline function setSceneSize( w : Int, h : Int ) {
if ( w != this.width || h != this.height ) {
width = w;
Expand Down Expand Up @@ -523,8 +537,10 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
@:dox(hide) @:noCompletion
public function handleEvent( event : hxd.Event, last : hxd.SceneEvents.Interactive ) : hxd.SceneEvents.Interactive {
screenToViewport(event);

var ex = event.relX;
var ey = event.relY;

var index = last == null ? 0 : interactive.indexOf(cast last) + 1;
var pt = shapePoint;
for( idx in index...interactive.length ) {
Expand All @@ -539,6 +555,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I

var dx = ex - i.absX;
var dy = ey - i.absY;

var rx = (dx * i.matD - dy * i.matC) * i.invDet;
var ry = (dy * i.matA - dx * i.matB) * i.invDet;

Expand Down