Skip to content

Commit

Permalink
rotation handler and gizmos
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Aug 30, 2021
1 parent 7fe520f commit 93fac42
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 23 deletions.
15 changes: 15 additions & 0 deletions src/editor/selection/rotation/rotation.gizmo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class RotationGizmo extends Phaser.Graphics {
constructor(game: Phaser.Game) {
super(game);
this.__skip = true;
this
.beginFill(0, 0)
.drawCircle(0, 0, 18)
.endFill()
.lineStyle(2, 0xFFFFFF, 1)
.beginFill(0x1ABC9C, 1)
.drawCircle(0, 0, 14);

this.inputEnabled = true;
}
}
35 changes: 35 additions & 0 deletions src/editor/selection/rotation/rotation.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { RotationGizmo } from './rotation.gizmo';

export class RotationHandler extends Phaser.Group {
private gizmos: RotationGizmo[];

constructor(game: Phaser.Game) {
super(game);
this.gizmos = this.createGizmos(game);
}

private createGizmos(game: Phaser.Game) {
const gizmos: RotationGizmo[] = [
new RotationGizmo(game), // top left
new RotationGizmo(game), // top right
new RotationGizmo(game), // bottom right
new RotationGizmo(game), // bottom left
];

gizmos.forEach(gizmo => {
// gizmo.events.onInputDown.add(() => this.startScaling(gizmo), this);
// gizmo.events.onInputUp.add(this.stopScaling, this);
this.addChild(gizmo);
});

return gizmos;
}

public redraw(bounds: PIXI.Rectangle) {
const knobs = this.gizmos;
knobs[0].position.set(0, 0); // top left
knobs[1].position.set(bounds.width, 0); // top right
knobs[2].position.set(bounds.width, bounds.height); // bottom right
knobs[3].position.set(0, bounds.height); // bottom right
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class ScaleKnob extends Phaser.Graphics {
export class ScaleGizmo extends Phaser.Graphics {
private readonly _cursor: string;

constructor(game: Phaser.Game, public readonly factorH: number, public readonly factorV: number) {
Expand Down
42 changes: 21 additions & 21 deletions src/editor/selection/scale/scale.handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ScaleKnob } from './scale.knob';
import { ScaleGizmo } from './scale.gizmo';
import { Scaler } from './scaler';

export class ScaleHandler extends Phaser.Group {
private readonly knobs: ScaleKnob[];
private readonly gizmos: ScaleGizmo[];
private _scaling = false;

public readonly scaler: Scaler;
Expand All @@ -14,36 +14,36 @@ export class ScaleHandler extends Phaser.Group {
super(game);
this.__skip = true;
this.scaler = new Scaler();
this.knobs = this.createScaleKnobs(game);
this.gizmos = this.createGizmos(game);

this._tmpTransformPivotGizmo = new Phaser.Graphics(game);
this.addChild(this._tmpTransformPivotGizmo);
}

private createScaleKnobs(game: Phaser.Game) {
const knobs: ScaleKnob[] = [
new ScaleKnob(game, 1, 1), // top left
new ScaleKnob(game, 0, 1), // top right
new ScaleKnob(game, 0, 0), // bottom right
new ScaleKnob(game, 1, 0), // bottom left

new ScaleKnob(game, 0.5, 1), // top
new ScaleKnob(game, 1, 0.5), // right
new ScaleKnob(game, 0.5, 0), // bottom
new ScaleKnob(game, 0, 0.5), // left
private createGizmos(game: Phaser.Game) {
const gizmos: ScaleGizmo[] = [
new ScaleGizmo(game, 1, 1), // top left
new ScaleGizmo(game, 0, 1), // top right
new ScaleGizmo(game, 0, 0), // bottom right
new ScaleGizmo(game, 1, 0), // bottom left

new ScaleGizmo(game, 0.5, 1), // top
new ScaleGizmo(game, 1, 0.5), // right
new ScaleGizmo(game, 0.5, 0), // bottom
new ScaleGizmo(game, 0, 0.5), // left
];

knobs.forEach(k => {
k.events.onInputDown.add(() => this.startScaling(k), this);
k.events.onInputUp.add(this.stopScaling, this);
this.addChild(k);
gizmos.forEach(gizmo => {
gizmo.events.onInputDown.add(() => this.startScaling(gizmo), this);
gizmo.events.onInputUp.add(this.stopScaling, this);
this.addChild(gizmo);
});

return knobs;
return gizmos;
}

public redraw(bounds: PIXI.Rectangle) {
const knobs = this.knobs;
const knobs = this.gizmos;
knobs[0].position.set(0, 0); // top left
knobs[1].position.set(bounds.width, 0); // top right
knobs[2].position.set(bounds.width, bounds.height); // bottom right
Expand All @@ -57,7 +57,7 @@ export class ScaleHandler extends Phaser.Group {
this._tmpDrawTransformPivot(this.scaler.transformPivot);
}

private startScaling(knob: ScaleKnob) {
private startScaling(knob: ScaleGizmo) {
this.scaler.startScaling(this.selectedObject, knob.factorH, knob.factorV);
this._scaling = true;
}
Expand Down
10 changes: 9 additions & 1 deletion src/editor/selection/selection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ANCHOR_COLOR, ANCHOR_STROKE, BORDER_COLOR, BORDER_STROKE, PIVOT_COLOR, PIVOT_STROKE } from '../editor.colors';
import { RotationHandler } from './rotation/rotation.handler';
import { ScaleHandler } from './scale/scale.handler';

export class Selection extends Phaser.Group {
Expand All @@ -7,6 +8,7 @@ export class Selection extends Phaser.Group {

private readonly view: Phaser.Graphics;
private readonly scaleHandler: ScaleHandler;
private readonly rotationHandler: RotationHandler;

constructor(game: Phaser.Game) {
super(game);
Expand All @@ -17,8 +19,13 @@ export class Selection extends Phaser.Group {
this.addChild(this.view);

this.scaleHandler = new ScaleHandler(game);
this.scaleHandler.visible = false;
this.addChild(this.scaleHandler);

this.rotationHandler = new RotationHandler(game);
this.rotationHandler.visible = true;
this.addChild(this.rotationHandler);

this.setSelection(null);
}

Expand All @@ -38,7 +45,8 @@ export class Selection extends Phaser.Group {
? this.scaleHandler.scaler.originalPivot
: this._obj.pivot);
this.drawAnchor(this._obj.anchor, bounds);
this.scaleHandler.redraw(bounds);
//this.scaleHandler.redraw(bounds);
this.rotationHandler.redraw(bounds);
this.position.set(bounds.x, bounds.y);
}

Expand Down

0 comments on commit 93fac42

Please sign in to comment.