Skip to content

Commit

Permalink
fixed worldScale with globalScale
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-jg committed Dec 7, 2021
1 parent 20d2391 commit 7b428d1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function el(x, y, w, h, parent, name) {
g.addChild(t);

g.inputEnabled = true;
g.useHandCursor = true;
// g.useHandCursor = true;
return g;
}

Expand Down
19 changes: 9 additions & 10 deletions src/editor-view/selectors/gizmos/selection-gizmo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ phred-selection-gizmo {
box-sizing: border-box;
box-shadow: inset $gizmo-shadow, $gizmo-shadow;

div {
box-sizing: content-box;
position: absolute;
padding: 0;
box-shadow: $gizmo-shadow;
pointer-events: none;
}

.handlers {
width: 100%;
height: 100%;
pointer-events: none;
position: absolute;

& > div {
box-sizing: content-box;
position: absolute;
padding: 0;
box-shadow: $gizmo-shadow;
pointer-events: none;
}
box-shadow: none;

.pivot {
width: 8px;
Expand Down
38 changes: 25 additions & 13 deletions src/editor-view/selectors/gizmos/selection-gizmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class SelectionGizmo extends HTMLElement implements Gizmo {
this._showHitArea = value;
if (value) {
this.setupHitArea(this._object);
if (this._object) this.drawHitArea(this._object.worldScale);
if (this._object) {
this.drawHitArea(this._object.globalScale);
}
} else {
this.setupHitArea(null);
}
Expand Down Expand Up @@ -95,8 +97,9 @@ export class SelectionGizmo extends HTMLElement implements Gizmo {
public redraw() {
if (!this._object) return;
const object = this._object;
const bounds = this._object.getBounds();
const scale = object.worldScale;
object.updateTransform();
const scale = object.globalScale;
const bounds = object.getBounds();

const { style, _rectCache, _pointCache1 } = this;
SelectionUtil.rectFromGameToArea(bounds, _rectCache);
Expand All @@ -122,7 +125,9 @@ export class SelectionGizmo extends HTMLElement implements Gizmo {
this.anchor.style.left = _pointCache1.x + 'px';
}

if (this._objectHitArea) this.drawHitArea(scale);
if (this._objectHitArea) {
this.drawHitArea(scale);
}
}

private setupHitArea(object: PIXI.DisplayObject) {
Expand All @@ -133,35 +138,42 @@ export class SelectionGizmo extends HTMLElement implements Gizmo {
}

this.hitArea.style.display = 'block';
this._objectHitArea = object.hitArea as any
?? { x: 0, y: 0, width: object.width, height: object.height };
const b = object.getBounds();
this._objectHitArea = object.hitArea as any ?? {
x: 0, y: 0,
width: Math.abs(b.width / object.scale.x),
height: Math.abs(b.height / object.scale.y)
};
this.hitArea.style.borderRadius = this._objectHitArea.radius ? '50%' : '0';
}

private drawHitArea(scale: PIXI.Point) {
const { _pointCache1, _pointCache2 } = this;
const hitArea = this._objectHitArea;
const style = this.hitArea.style;

SelectionUtil.pointFromGameToArea(hitArea.x * scale.x, hitArea.y * scale.y, _pointCache1);
const haStyle = this.hitArea.style;

if (hitArea.radius) {
SelectionUtil.pointFromGameToArea(
hitArea.radius * 2 * scale.x,
hitArea.radius * 2 * scale.y,
_pointCache2
);
haStyle.left = (_pointCache1.x - _pointCache2.x * 0.5) + 'px';
haStyle.top = (_pointCache1.y - _pointCache2.y * 0.5) + 'px';
_pointCache1.x -= _pointCache2.x * 0.5;
_pointCache1.y -= _pointCache2.y * 0.5;
} else {
SelectionUtil.pointFromGameToArea(
hitArea.width * scale.x,
hitArea.height * scale.y,
_pointCache2
);
haStyle.left = _pointCache1.x + 'px';
haStyle.top = _pointCache1.y + 'px';
}
haStyle.width = _pointCache2.x + 'px';
haStyle.height = _pointCache2.y + 'px';

style.left = _pointCache1.x + 'px';
style.top = _pointCache1.y + 'px';
style.width = Math.abs(_pointCache2.x) + 'px';
style.height = Math.abs(_pointCache2.y) + 'px';
}

public startMoving() {
Expand Down
3 changes: 1 addition & 2 deletions src/editor-view/selectors/handlers/move.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ export class MoveHandler implements DraggingHandler {
public handle(e: MouseEvent) {
const lastPoint = this._point;
const newPoint = SelectionUtil.mouseEventToGamePoint(e, { x: 0, y: 0 });

const object = this._object;
const scale = object.worldScale.clone();
const scale = object.globalScale;

scale.x = Math.abs(scale.x / object.scale.x);
scale.y = Math.abs(scale.y / object.scale.y);
Expand Down
2 changes: 1 addition & 1 deletion src/editor-view/selectors/handlers/scale.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class ScaleHandler implements DraggingHandler {
dscale = 1;
}

const scale = object.worldScale.clone();
const scale = object.globalScale;
scale.x = Math.abs(scale.x / object.scale.x);
scale.y = Math.abs(scale.y / object.scale.y);

Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare namespace PIXI {
children?: DisplayObject[];
getBounds?(): Rectangle;
getLocalBounds(): Rectangle;
readonly globalScale: Point;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,9 @@ export class Plugin extends Phaser.Plugin {
}

(Phaser.Plugin as any).RuntimeEditor = Plugin;

Object.defineProperty(PIXI.DisplayObject.prototype, 'globalScale', {
enumerable: true,
configurable: true,
get() { return new PIXI.Point(this.worldTransform.a, this.worldTransform.d); }
})
2 changes: 1 addition & 1 deletion src/scene-view/selection/selection.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class SelectionView extends Phaser.Group {

public move(deltaX: number, deltaY: number) {
const pos = this._selectedObject.position;
const scale = this._selectedObject.parent?.worldScale ?? PointUtil.one;
const scale = this._selectedObject.parent?.globalScale ?? PointUtil.one;
this.moveFn(pos, scale, deltaX, deltaY);
this.redraw();
Editor.data.propertyChanged('position', pos, DataOrigin.SCENE);
Expand Down

0 comments on commit 7b428d1

Please sign in to comment.