Skip to content

Commit

Permalink
data moved to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-jg committed Sep 3, 2021
1 parent 00f72aa commit f86453a
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 36 deletions.
9 changes: 6 additions & 3 deletions src/core/editor.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { History } from './history';
import { EditorData } from 'data/editor-data';
import { Actions } from './actions';
import { History } from './history';
import { Preferences } from './preferences';

class EditorClass {
public actions: Actions;
public history: History;
public prefs: Preferences;
public data: EditorData

public init() {
this.data = new EditorData();
this.actions = new Actions();
this.history = new History();
this.history = new History(this.data);
this.prefs = new Preferences();
}
}

export const Editor = new EditorClass();
export const Editor = new EditorClass();
8 changes: 5 additions & 3 deletions src/core/history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Data, DataOrigin } from '../data/data';
import { DataOrigin, EditorData } from '../data/editor-data';

export interface HistoryEntry {
obj: PIXI.DisplayObject;
Expand All @@ -14,6 +14,8 @@ export class History {
// TODO change this name please
public readonly onHistoryWalk = new Phaser.Signal();

constructor(private readonly data: EditorData) { }

public prepare(obj: PIXI.DisplayObject, properties: { [id: string]: any }) {
const entry = this.holdingEntry = { obj, properties };
Object.keys(entry.properties).forEach(k =>
Expand All @@ -38,7 +40,7 @@ export class History {
}

private apply(entry: HistoryEntry) {
Data.selectObject(entry.obj, DataOrigin.HISTORY);
this.data.selectObject(entry.obj, DataOrigin.HISTORY);

const obj = entry.obj;
// TODO make this recursive (if necessary)
Expand All @@ -52,7 +54,7 @@ export class History {
});
} else
obj[pk] = prop;
Data.propertyChanged(pk, obj[pk], DataOrigin.HISTORY);
this.data.propertyChanged(pk, obj[pk], DataOrigin.HISTORY);
});

obj.updateTransform();
Expand Down
4 changes: 1 addition & 3 deletions src/data/data.ts → src/data/editor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum DataOrigin {
INSPECTOR = 2,
};

class DataClass {
export class EditorData {
private _selectedObject: PIXI.DisplayObject;

public get selectedObject() { return this._selectedObject; }
Expand Down Expand Up @@ -49,5 +49,3 @@ class DataClass {
});
}
}

export const Data = new DataClass();
5 changes: 3 additions & 2 deletions src/editor-view/editor-view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Data, DataOrigin } from 'data/data';
import { Editor } from 'core/editor';
import { DataOrigin } from 'data/editor-data';
import { ActionsToolbar } from './actions/actions-toolbar';
import './editor-view.scss';
import { ObjectTreeInspector } from './object-tree/inspector/object-tree-inspector';
Expand All @@ -17,7 +18,7 @@ export class EditorView extends Widget {
public connectedCallback() {
super.connectedCallback();

Data.onSelectedObjectChanged.add(this.selectObject, this);
Editor.data.onSelectedObjectChanged.add(this.selectObject, this);

const script = document.createElement('script');
script.src = 'https://kit.fontawesome.com/7ba4e59e46.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Data, DataOrigin } from 'data/data';
import { Editor } from 'core/editor';
import { DataOrigin } from 'data/editor-data';
import { Inspector } from 'editor-view/inspector/inspector';
import { ObjectMapItemModel, ObjectTreeModel } from '../model/object-tree-model';
import { TreeNode } from '../tree-node/tree-node';
Expand All @@ -11,7 +12,7 @@ export class ObjectTreeInspector extends Inspector {
public connectedCallback() {
super.connectedCallback();
this.title = 'Objects';
Data.onPropertyChanged.add(this.onPropertyChanged, this);
Editor.data.onPropertyChanged.add(this.onPropertyChanged, this);
}

private onPropertyChanged(_: DataOrigin, property: string, value: any) {
Expand Down Expand Up @@ -56,7 +57,7 @@ export class ObjectTreeInspector extends Inspector {
if (node?.model === this._lastSelectedModel) return;
if (this._lastSelectedModel) this._lastSelectedModel.node.clearSelection();
this._lastSelectedModel = node.model;
Data.selectObject(node.model.obj, DataOrigin.INSPECTOR);
Editor.data.selectObject(node.model.obj, DataOrigin.INSPECTOR);
}

private onNodeCollapseStateChanged(node: TreeNode, collapsed: boolean, all: boolean) {
Expand Down
6 changes: 3 additions & 3 deletions src/editor-view/properties/editors/property-editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Data, DataOrigin } from 'data/data';
import { Editor } from 'core/editor';
import { DataOrigin } from 'data/editor-data';
import { PropertyInspectionData } from 'editor-view/properties-editors';
import { IdUtil } from 'util/id.util';
import './property-editor.scss';
Expand Down Expand Up @@ -59,15 +59,15 @@ export abstract class PropertyEditor<T> extends HTMLElement {
}
if (save) this.savePreviousValue();
this.updateInternalValue(e);
Data.propertyChanged(this.prop.name, this.getInternalValue(), DataOrigin.INSPECTOR);
Editor.data.propertyChanged(this.prop.name, this.getInternalValue(), DataOrigin.INSPECTOR);
}

public getInternalValue(): T { return this._internalValue; }
public abstract setInternalValue(value: T): void;
public abstract updateInternalValue(e: Event): void;

public savePreviousValue() {
Editor.history.prepare(Data.selectedObject, {
Editor.history.prepare(Editor.data.selectedObject, {
[this.prop.name]: this.getInternalValue()
}).commit();
}
Expand Down
5 changes: 3 additions & 2 deletions src/editor-view/properties/inspector/properties-inspector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Data, DataOrigin } from 'data/data';
import { Editor } from 'core/editor';
import { DataOrigin } from 'data/editor-data';
import { Inspector } from 'editor-view/inspector/inspector';
import { PropertiesEditors, PropertyInspectionData } from 'editor-view/properties-editors';
import { PropertyEditor } from '../editors/property-editor';
Expand All @@ -11,7 +12,7 @@ export class PropertiesInspector extends Inspector {
public connectedCallback() {
super.connectedCallback();
this.title = 'Properties';
Data.onPropertyChanged.add(this.onPropertyChanged, this);
Editor.data.onPropertyChanged.add(this.onPropertyChanged, this);
}

private onPropertyChanged(origin: DataOrigin, property: string, value: any) {
Expand Down
3 changes: 1 addition & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Data } from 'data/data';
import Phaser from 'phaser-ce';
import { Editor } from './core/editor';
import { EditorView } from './editor-view/editor-view';
Expand Down Expand Up @@ -98,6 +97,6 @@ export class Plugin extends Phaser.Plugin {
}

public postUpdate() {
Data.dispatchScheduledEvents();
Editor.data.dispatchScheduledEvents();
}
}
16 changes: 8 additions & 8 deletions src/scene-view/scene-view.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor } from 'core/editor';
import { Data, DataOrigin } from 'data/data';
import { DataOrigin } from 'data/editor-data';
import { DragUtil } from '../util/drag.util';
import { SceneModel } from './scene-model';
import { Selection } from './selection/selection';
Expand Down Expand Up @@ -39,8 +39,8 @@ export class SceneView extends Phaser.Group {
this.selection = new Selection(game);
this.addChild(this.selection);

Data.onPropertyChanged.add(this.onPropertyChanged.bind(this));
Data.onSelectedObjectChanged.add(this.onObjectSelected.bind(this));
Editor.data.onPropertyChanged.add(this.onPropertyChanged.bind(this));
Editor.data.onSelectedObjectChanged.add(this.onObjectSelected.bind(this));
}

private onPropertyChanged(origin: DataOrigin, property: string, value: any, obj: PIXI.DisplayObject) {
Expand Down Expand Up @@ -77,10 +77,10 @@ export class SceneView extends Phaser.Group {
&& this.trySelectOver(pointer);
this._lastPos.set(pointer.x, pointer.y);

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

Editor.history.prepare(Data.selectedObject, { position: obj.position.clone() });
Editor.history.prepare(Editor.data.selectedObject, { position: obj.position.clone() });
}

private onInputUp(_: any, pointer: Phaser.Pointer) {
Expand Down Expand Up @@ -108,7 +108,7 @@ export class SceneView extends Phaser.Group {

private selectObject(obj: PIXI.DisplayObject, dispatch: boolean) {
this.selection.select(obj);
if (dispatch) Data.selectObject(obj, DataOrigin.SCENE);
if (dispatch) Editor.data.selectObject(obj, DataOrigin.SCENE);
}

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

public moveSelectedObject(deltaX: number, deltaY: number) {
const obj = Data.selectedObject;
const obj = Editor.data.selectedObject;
if (!obj) return;
Editor.history.prepare(obj, { position: obj.position.clone() }).commit();
obj.position.set(obj.position.x + deltaX, obj.position.y + deltaY);
obj.updateTransform();
this.selection.redraw();
Data.propertyChanged('position', obj.position.clone(), DataOrigin.SCENE);
Editor.data.propertyChanged('position', obj.position.clone(), DataOrigin.SCENE);
}

public update() {
Expand Down
8 changes: 4 additions & 4 deletions src/scene-view/selection/scale/scale.handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor } from 'core/editor';
import { Data, DataOrigin } from 'data/data';
import { DataOrigin } from 'data/editor-data';
import { ScaleGizmo } from './scale.gizmo';
import { Scaler } from './scaler';

Expand Down Expand Up @@ -55,7 +55,7 @@ export class ScaleHandler extends Phaser.Group {
}

private startScaling(gizmos: ScaleGizmo) {
const obj = Data.selectedObject;
const obj = Editor.data.selectedObject;
Editor.history.prepare(obj, {
scale: obj.scale.clone(),
position: obj.position.clone(),
Expand All @@ -77,8 +77,8 @@ export class ScaleHandler extends Phaser.Group {
this.scaler.scaleToPoint(pointer.x, pointer.y);

const pos = this.scaler.getObjectStopPosition();
Data.propertyChanged('scale', this.selectedObject.scale, DataOrigin.SCENE);
Data.propertyChanged('position', pos, DataOrigin.SCENE);
Editor.data.propertyChanged('scale', this.selectedObject.scale, DataOrigin.SCENE);
Editor.data.propertyChanged('position', pos, DataOrigin.SCENE);

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/scene-view/selection/selection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Data, DataOrigin } from 'data/data';
import { PreferenceKey } from 'core/preferences';
import { Editor } from 'core/editor';
import { PreferenceKey } from 'core/preferences';
import { DataOrigin } from 'data/editor-data';
import { PointUtil } from 'util/math.util';
import { ANCHOR_COLOR, ANCHOR_STROKE, BORDER_COLOR, BORDER_STROKE, PIVOT_COLOR, PIVOT_STROKE } from '../scene-colors';
import { ScaleHandler } from './scale/scale.handler';
Expand Down Expand Up @@ -102,7 +102,7 @@ export class Selection extends Phaser.Group {
const scale = this._selectedObject.parent?.worldScale ?? PointUtil.one;
this.moveFn(pos, scale, deltaX, deltaY);
this.redraw();
Data.propertyChanged('position', pos, DataOrigin.SCENE);
Editor.data.propertyChanged('position', pos, DataOrigin.SCENE);
}

private moveFn: (pos: PIXI.Point, scale: PIXI.Point, deltaX: number, deltaY: number) => void;
Expand Down

0 comments on commit f86453a

Please sign in to comment.