Skip to content

Commit

Permalink
Merge branch 'feature/one-class-to-rule-them-all' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-jg committed Sep 3, 2021
2 parents d31aa62 + 9e31101 commit bc9449a
Show file tree
Hide file tree
Showing 47 changed files with 267 additions and 241 deletions.
4 changes: 1 addition & 3 deletions src/data/actions.ts → src/core/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Action {
state?: () => any;
}

class ActionsClass {
export class Actions {
private readonly actions: Record<string, Action> = {};
private readonly actionMap: Record<string, Action> = {};

Expand Down Expand Up @@ -55,5 +55,3 @@ class ActionsClass {
this._holdingToggleAction = null;
}
}

export const Actions = new ActionsClass();
28 changes: 28 additions & 0 deletions src/core/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { EditorData } from 'data/editor-data';
import { InspectorData } from 'data/inspector-data';
import { PhaserMeta } from 'data/phaser-meta';
import { Actions } from './actions';
import { History } from './history';
import { Preferences } from './preferences';

class EditorClass {
public data: EditorData
public inspectorData: InspectorData;
public meta: PhaserMeta;

public actions: Actions;
public history: History;
public prefs: Preferences;

public init() {
this.data = new EditorData();
this.inspectorData = new InspectorData();
this.meta = new PhaserMeta();

this.actions = new Actions();
this.history = new History(this.data);
this.prefs = new Preferences();
}
}

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

export interface HistoryEntry {
obj: PIXI.DisplayObject;
Expand All @@ -7,15 +7,17 @@ export interface HistoryEntry {

const HISTORY_LIMIT = 100;

class HistoryClass {
export class History {
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;
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 =>
entry.properties[k] = JSON.stringify(entry.properties[k]));
return this;
Expand All @@ -38,7 +40,7 @@ class HistoryClass {
}

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,12 +54,10 @@ class HistoryClass {
});
} else
obj[pk] = prop;
Data.propertyChanged(pk, obj[pk], DataOrigin.HISTORY);
this.data.propertyChanged(pk, obj[pk], DataOrigin.HISTORY);
});

obj.updateTransform();
this.onHistoryWalk.dispatch(entry);
}
}

export const History = new HistoryClass();
6 changes: 2 additions & 4 deletions src/data/preferences.ts → src/core/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type PreferenceKey = keyof PreferencesClass;
export type PreferenceKey = keyof Preferences;

class PreferencesClass {
export class Preferences {
private _snap = false;

public get snap() { return this._snap; }
Expand All @@ -25,5 +25,3 @@ class PreferencesClass {
this.onPreferenceChanged.dispatch(field, value);
}
}

export const Preferences = new PreferencesClass();
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();
49 changes: 49 additions & 0 deletions src/data/inspector-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export type InspectableTypes = 'string' | 'number' | 'boolean' | 'point' | 'default';

export interface InspectorPropertyModel {
name: string;
typeHint: InspectableTypes;
data?: any;
label?: string;
}

export class InspectorData {
private readonly editors: Partial<Record<InspectableTypes, string>> = {};

public addEditors(editors: Partial<Record<InspectableTypes, string>>) {
Object.keys(editors).forEach(e => this.editors[e] = editors[e]);
}

public readonly inspectableProperties: InspectorPropertyModel[] = [];

public addInspectableProperties(properties: InspectorPropertyModel[]) {
const iproperties = this.inspectableProperties;
properties.forEach(prop => {
const i = iproperties.findIndex(e => e.name === prop.name);
if (i < 0) iproperties.push(prop);
else iproperties.splice(i, 1, prop);
});
}

public findEditorFor(value: any, data: InspectorPropertyModel) {
// TODO what abount null / undefined values?

const editors = this.editors;

// TODO this should never fail
// first, it tries to find based on the type hint
if (data.typeHint in editors) return editors[data.typeHint];


// next, it tries to find by its type
const type = typeof value;
if (type !== 'object' && type in editors)
return editors[type];

// next, since it's an object, it tries to find by its contructor name
const ctor = value.contructor?.name;
if (ctor && ctor in editors) return editors[ctor];

return editors.default;
}
}
44 changes: 0 additions & 44 deletions src/data/phaser-data.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/data/phaser-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export interface PhaserObjectType {
name: string;
icon: string;
}

export class PhaserMeta {
private readonly sceneObjects: Record<string, PhaserObjectType> = {
[Phaser.BITMAPTEXT]: { name: 'Phaser.BitmapText', icon: 'fa-font' },
[Phaser.BUTTON]: { name: 'Phaser.Button', icon: '' },
[Phaser.GRAPHICS]: { name: 'Phaser.Graphics', icon: 'fa-square' },
[Phaser.GROUP]: { name: 'Phaser.Group', icon: 'fa-dice-d6' },
[Phaser.IMAGE]: { name: 'Phaser.Image', icon: 'fa-image' },
[Phaser.RETROFONT]: { name: 'Phaser.RetroFont', icon: 'fa-font' },
[Phaser.SPRITE]: { name: 'Phaser.Sprite', icon: 'fa-image' },
[Phaser.TEXT]: { name: 'Phaser.Text', icon: 'fa-font' },
// [Phaser.EMITTER]: { name: 'Phaser.Emitter', icon: '' },
// [Phaser.SPRITEBATCH]: { name: 'Phaser.SpriteBatch', icon: 'fa-images' },
// [Phaser.TILEMAP]: { name: 'Phaser.TileMap', icon: '' },
// [Phaser.TILEMAPLAYER]: { name: 'Phaser.TileMapPlayer', icon: '' },
// [Phaser.TILESPRITE]: { name: 'Phaser.TileSprite', icon: '' },
// [Phaser.WEBGL_FILTER]: { name: 'Phaser.WebGLFilter', icon: '' },
// [Phaser.ROPE]: { name: 'Phaser.Rope', icon: '' },
// [Phaser.CREATURE]: { name: 'Phaser.Creature', icon: '' },
// [Phaser.VIDEO]: { name: 'Phaser.Video', icon: '' },
};

public addSceneObjects(objects: Record<string, PhaserObjectType>) {
Object.keys(objects).forEach(k => this.sceneObjects[k] = objects[k]);
}

private readonly defaultType: PhaserObjectType = { name: 'default', icon: 'fa-question' };

public getType(t: number) { return t in this.sceneObjects ? this.sceneObjects[t] : this.defaultType; }
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Actions } from 'data/actions';
import { Widget } from 'editor/widget/widget';
import { Widget } from 'editor-view/widget/widget';
import { Editor } from 'core/editor';
import './actions-toolbar.scss';
import { ActionButton } from './button/action-button';

Expand All @@ -14,7 +14,7 @@ export class ActionsToolbar extends Widget {

private createButton(actionId: string) {
const btn = document.createElement(ActionButton.tagName) as ActionButton;
btn.setAction(Actions.getAction(actionId));
btn.setAction(Editor.actions.getAction(actionId));
this.appendChild(btn);
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
phred-editor {
phred-editor-view {
flex: 1;
display: flex;
align-items: stretch;
Expand Down
13 changes: 7 additions & 6 deletions src/editor/editor.ts → src/editor-view/editor-view.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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.scss';
import './editor-view.scss';
import { ObjectTreeInspector } from './object-tree/inspector/object-tree-inspector';
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';
export class EditorView extends Widget {
public static readonly tagName: string = 'phred-editor-view';

private _game: Phaser.Game;
private actions: ActionsToolbar;
Expand All @@ -17,7 +18,7 @@ export class Editor 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 Expand Up @@ -72,4 +73,4 @@ export class Editor extends Widget {
}
}

customElements.define(Editor.tagName, Editor);
customElements.define(EditorView.tagName, EditorView);
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Widget } from 'editor/widget/widget';
import { Widget } from 'editor-view/widget/widget';
import './inspector.scss';

export abstract class Inspector extends Widget {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Data, DataOrigin } from 'data/data';
import { Inspector } from 'editor/inspector/inspector';
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';
import './object-tree-inspector.scss';
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PhaserData, PhaserObjectType } from 'data/phaser-data';
import { Editor } from 'core/editor';
import { PhaserObjectType } from 'data/phaser-meta';
import { IdUtil } from 'util/id.util';
import { TreeNode } from '../tree-node/tree-node';

Expand All @@ -23,7 +24,7 @@ export class ObjectTreeModel {
}

private createNode(child: PIXI.DisplayObject, map: Record<number, ObjectMapItemModel>, parent: ObjectMapItemModel, level: number) {
const type = PhaserData.getType(child.type);
const type = Editor.meta.getType(child.type);
child.__instanceId = IdUtil.genIntId();
child.__type = type.name;
const isLeaf = !(child.children && child.children.length > 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PhaserObjectType } from 'data/phaser-data';
import { PhaserObjectType } from 'data/phaser-meta';
import { ObjectMapItemModel } from '../model/object-tree-model';
import './tree-node.scss';

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/editor/panel/panel.ts → src/editor-view/panel/panel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inspector } from 'editor/inspector/inspector';
import { Widget } from 'editor/widget/widget';
import { Inspector } from 'editor-view/inspector/inspector';
import { Widget } from 'editor-view/widget/widget';
import './panel.scss';

export class Panel extends Widget {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PropertyInspectionData } from 'editor/properties-editors';
import { InspectorPropertyModel } from 'data/inspector-data';
import { PropertyEditor } from '../property-editor';

export class BooleanPropertyEditor extends PropertyEditor<boolean> {
public static readonly tagName: string = 'phed-boolean-property-editor';

private input: HTMLInputElement;

protected createInnerContent(fieldId: string, _value: boolean, prop: PropertyInspectionData) {
protected createInnerContent(fieldId: string, _value: boolean, prop: InspectorPropertyModel) {
const input = this.input = document.createElement('input');
input.id = fieldId;
input.setAttribute('type', 'checkbox');
Expand Down
Loading

0 comments on commit bc9449a

Please sign in to comment.