Skip to content

Commit

Permalink
added history
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Sep 2, 2021
1 parent f084281 commit 2e5b1d1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/data/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface HistoryEntry {
obj: PIXI.DisplayObject;
properties: { [id: string]: any };
}

const HISTORY_LIMIT = 100;

class HistoryClass {
private readonly entries: HistoryEntry[] = [];
private holdingEntry: HistoryEntry;

// TODO change this name please
public readonly onHistoryWalk = new Phaser.Signal();

public holdEntry(entry: HistoryEntry) { this.holdingEntry = entry; }
public cancel() { this.holdingEntry = null; }

public commit() {
if (!this.holdingEntry) return;
if (this.entries.length === HISTORY_LIMIT)
this.entries.shift();
this.entries.push(this.holdingEntry);
this.holdingEntry = null;
}

public undo() {
if (this.entries.length === 0) return;
const entry = this.entries.pop();
Object.keys(entry.properties)
.forEach(k => entry.obj[k] = entry.properties[k]);
entry.obj.updateTransform();
this.onHistoryWalk.dispatch(entry);
}
}

export const History = new HistoryClass();
1 change: 0 additions & 1 deletion src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Panel } from './panel/panel';
import { PropertiesInspector } from './properties/inspector/properties-inspector';
import { Widget } from './widget/widget';


export class Editor extends Widget {
public static readonly tagName: string = 'phred-editor';

Expand Down
6 changes: 6 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Data, DataOrigin } from 'data/data';
import { History } from 'data/history';
import Phaser from 'phaser-ce';
import { Editor } from './editor/editor';
import './plugin.scss';
Expand All @@ -25,6 +26,11 @@ export class Plugin extends Phaser.Plugin {
this.update = update;
editor.setup(game, group);
}

document.onkeypress = (e: KeyboardEvent) => {
if (e.ctrlKey && e.key === 'z')
History.undo();
}
}

public postUpdate() {
Expand Down
21 changes: 20 additions & 1 deletion src/scene/scene-editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Data, DataOrigin } from 'data/data';
import { History, HistoryEntry } from 'data/history';
import { DragUtil } from '../util/drag.util';
import { SceneMovel } from './scene-model';
import { Selection } from './selection/selection';
Expand Down Expand Up @@ -37,8 +38,10 @@ export class SceneEditor extends Phaser.Group {

this.selection = new Selection(game);
this.addChild(this.selection);

Data.addPropertyChangedListener(DataOrigin.INSPECTOR, this.onPropertyChangedInsideInspector.bind(this));
Data.addObjectSelectionChangedListener(DataOrigin.INSPECTOR, this.onObjectSelectedInsideInspector.bind(this));
History.onHistoryWalk.add(this.onHistory, this);
}

private onPropertyChangedInsideInspector(property: string, value: any, obj: PIXI.DisplayObject) {
Expand All @@ -50,6 +53,10 @@ export class SceneEditor extends Phaser.Group {

private onObjectSelectedInsideInspector(obj: PIXI.DisplayObject) { this.selectObject(obj, false); }

private onHistory(entry: HistoryEntry) {
this.selection.select(entry.obj);
}

private createTouchArea(game: Phaser.Game): Phaser.Graphics {
const area = new Phaser.Graphics(game);
area.inputEnabled = true;
Expand All @@ -71,13 +78,25 @@ export class SceneEditor extends Phaser.Group {
this._hasSelected = !this.selection.getBounds().contains(pointer.x, pointer.y)
&& this.trySelectOver(pointer);
this._lastPos.set(pointer.x, pointer.y);

const obj = Data.selectedObject
if (!obj) return;

History.holdEntry({
obj: Data.selectedObject,
properties: { x: obj.x, y: obj.y },
});
}

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

0 comments on commit 2e5b1d1

Please sign in to comment.