Skip to content

Commit

Permalink
selecting on mouse down
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Aug 27, 2021
1 parent 826acf8 commit 4964c69
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/editor/editor.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,31 @@ export class EditorView extends Phaser.Group {

private onInputDown(_: any, pointer: Phaser.Pointer) {
this._isDragging = false;
if (!this.selection.selectedObject) return;
this._overSelection = this.selection.getBounds().contains(pointer.x, pointer.y);
this._lastPos.set(pointer.x, pointer.y);
this._hasSelected = this.trySelectOver(pointer);
if (this._hasSelected) {
this._lastPos.set(pointer.x, pointer.y);
}
// if (!this.selection.selectedObject) return;
// this._overSelection = this.selection.getBounds().contains(pointer.x, pointer.y);
// this._lastPos.set(pointer.x, pointer.y);
}

private onInputUp(_: any, pointer: Phaser.Pointer) {
const wasDragging = this._isDragging;
this._isDragging = false;
if (wasDragging) return;
if (this._hasSelected) return;
this.trySelectOver(pointer);
this._hasSelected = false;
}

private trySelectOver(pointer: Phaser.Pointer) {
const objects: PIXI.DisplayObject[] = []; // TODO cache
this.getObjectsUnderPoint(pointer.x, pointer.y, this.container.children, objects);

const selection = this.model.setSelectionTree(objects);
this.selection.setSelection(selection);
return selection !== null;
}

private getObjectsUnderPoint(x: number, y: number, children: PIXI.DisplayObject[], objects: PIXI.DisplayObject[]) {
Expand All @@ -77,14 +89,14 @@ export class EditorView extends Phaser.Group {
}
}

private _overSelection: boolean;
private _hasSelected: boolean;
private _lastPos = new Phaser.Point();
private _isDragging = false;

public update() {
super.update();
const pointer = this.game.input.mousePointer;
if (!(pointer.isDown && this._overSelection)) return;
if (!(pointer.isDown && this.selection.hasObject)) return;
if (!this._isDragging) {
const dx = pointer.x - pointer.positionDown.x;
const dy = pointer.y - pointer.positionDown.y;
Expand Down
5 changes: 3 additions & 2 deletions src/editor/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const ANCHOR_COLOR = 0xFFFFFF;
const ANCHOR_STROKE = 0xD9B448;

export class Selection extends Phaser.Graphics {
private _obj: PIXI.DisplayObject;
public get selectedObject() { return this._obj; }
private _obj: PIXI.DisplayObject = null;
// public get selectedObject() { return this._obj; }
public get hasObject() { return this._obj !== null; }

constructor(game: Phaser.Game) {
super(game);
Expand Down

0 comments on commit 4964c69

Please sign in to comment.