Skip to content

Commit

Permalink
dedicated object to setup actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Dec 15, 2021
1 parent ab47838 commit 3962fac
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 99 deletions.
1 change: 1 addition & 0 deletions example/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function preload() {

function create() {
const plugin = game.plugins.add(new Phaser.Plugin.RuntimeEditor(game, {
saveLockedObjectsPath: true,
referenceImageUrl(width, height, _responsive) {
return width > height
? './refs/ref_landscape.jpg'
Expand Down
121 changes: 121 additions & 0 deletions src/core/actions-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { DataOrigin, EditorData } from 'data/editor-data';
import { Size } from 'plugin.model';
import { ActionHandler } from './action-handler';
import { Actions } from './actions';
import { History } from './history';
import { Preferences } from './preferences/preferences';

export class ActionsSetup {
public setup(actions: ActionHandler, data: EditorData, history: History, prefs: Preferences) {
this.setupDataActions(data, actions, prefs);
this.setuphistoryActions(history, actions);
this.setupPreferencesActions(prefs, actions);
}

public setuphistoryActions(history: History, actions: ActionHandler) {
actions.setActionCommand(Actions.UNDO, history.undo.bind(history));
}

private setupPreferencesActions(prefs: Preferences, actions: ActionHandler) {
actions.setActionCommand(
Actions.TOGGLE_SNAP,
() => prefs.toggle('snap'),
() => prefs.get('snap') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_GIZMOS,
() => prefs.toggle('gizmos'),
() => prefs.get('gizmos') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_GUIDES,
() => prefs.toggle('guides'),
() => prefs.get('guides') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_HIT_AREA,
() => prefs.toggle('hitArea'),
() => prefs.get('hitArea') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_RESPONSIVE,
() => prefs.toggle('responsive'),
() => prefs.get('responsive') as boolean
);

actions.setActionCommand(Actions.TOGGLE_ORIENTATION, () => {
const size = prefs.get('responsiveSize') as Size;
prefs.set('responsiveSize', { width: size.height, height: size.width });
});

actions.setActionCommand(
Actions.TOGGLE_HIT_AREAS_SNAPSHOT,
() => prefs.toggle('hitAreasSnapshot'),
() => prefs.get('hitAreasSnapshot') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_REF_IMAGE,
() => prefs.toggle('referenceImageVisible'),
() => prefs.get('referenceImageVisible') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_LEFT_PANEL,
() => prefs.toggle('leftPanelVisible'),
() => prefs.get('leftPanelVisible') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_RIGHT_PANEL,
() => prefs.toggle('rightPanelVisible'),
() => prefs.get('rightPanelVisible') as boolean
);
}

public setupDataActions(data: EditorData, actions: ActionHandler, prefs: Preferences) {
actions.setActionCommand(Actions.CLEAR_SELECTION, () => data.selectObject(null, DataOrigin.ACTION));
actions.setActionCommand(Actions.PRINT_OBJECT, () => {
if (data.selectedObject) console.info(data.selectedObject);
});

actions.setActionCommand(Actions.SELECT_PARENT, () => {
if (data.selectedObject) data.selectObject(data.selectedObject.parent, DataOrigin.ACTION);
});

actions.setActionCommand(
Actions.LOCK_SELECTION,
() => this.toggleLockSelection(data, prefs),
() => data.selectedObject?.__locked
);
}

private toggleLockSelection(data: EditorData, prefs: Preferences) {
if (!data.selectedObject) return;
const obj = data.selectedObject;
obj.__locked = !obj.__locked;

const path: number[] = [];
let o = obj;
while (o.parent) {
path.unshift(o.parent.getChildIndex(o));
o = o.parent;
}

const pathId = path.join(',');
const locked = prefs.get('lockedObjects') as string[];
const i = locked.indexOf(pathId);

if (obj.__locked) {
if (i < 0) locked.push(pathId);
} else if (i >= 0) locked.splice(i, 1);

prefs.set('lockedObjects', locked);

data.onObjectLocked.dispatch(obj);
}
}
71 changes: 3 additions & 68 deletions src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Actions } from 'core/actions';
import { DataOrigin, EditorData } from 'data/editor-data';
import { InspectorData } from 'data/inspector-data';
import { PhaserMeta } from 'data/phaser-meta';
import { PluginConfig, Size } from 'plugin.model';
import { PluginConfig } from 'plugin.model';
import { PropertyElementTag } from 'property-element-tag';
import { ActionHandler } from './action-handler';
import { ActionsSetup } from './actions-setup';
import { History } from './history';
import { Preferences } from './preferences/preferences';
import { ReferenceImageController } from './reference-image.controller';
Expand Down Expand Up @@ -380,74 +381,8 @@ class EditorClass {
return actions;
}

private setupPreferencesActions(actions: ActionHandler) {
const prefs = this.prefs;

actions.setActionCommand(
Actions.TOGGLE_SNAP,
() => prefs.toggle('snap'),
() => prefs.get('snap') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_GIZMOS,
() => prefs.toggle('gizmos'),
() => prefs.get('gizmos') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_GUIDES,
() => prefs.toggle('guides'),
() => prefs.get('guides') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_HIT_AREA,
() => prefs.toggle('hitArea'),
() => prefs.get('hitArea') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_RESPONSIVE,
() => prefs.toggle('responsive'),
() => prefs.get('responsive') as boolean
);

actions.setActionCommand(Actions.TOGGLE_ORIENTATION, () => {
const size = prefs.get('responsiveSize') as Size;
prefs.set('responsiveSize', { width: size.height, height: size.width });
});

actions.setActionCommand(
Actions.TOGGLE_HIT_AREAS_SNAPSHOT,
() => prefs.toggle('hitAreasSnapshot'),
() => prefs.get('hitAreasSnapshot') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_REF_IMAGE,
() => prefs.toggle('referenceImageVisible'),
() => prefs.get('referenceImageVisible') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_LEFT_PANEL,
() => prefs.toggle('leftPanelVisible'),
() => prefs.get('leftPanelVisible') as boolean
);

actions.setActionCommand(
Actions.TOGGLE_RIGHT_PANEL,
() => prefs.toggle('rightPanelVisible'),
() => prefs.get('rightPanelVisible') as boolean
);
}

public setupInitialActions() {
const actions = this.actions;
this.setupPreferencesActions(actions);
this.history.setupActions(actions);
this.data.setupActions(actions);
new ActionsSetup().setup(this.actions, this.data, this.history, this.prefs);
}

public enable(config: PluginConfig) {
Expand Down
6 changes: 0 additions & 6 deletions src/core/history.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { DataOrigin, EditorData } from '../data/editor-data';
import { ActionHandler } from './action-handler';
import { Actions } from './actions';

export interface HistoryEntry {
obj: PIXI.DisplayObject;
Expand All @@ -18,10 +16,6 @@ export class History {

constructor(private readonly data: EditorData) { }

public setupActions(actions: ActionHandler) {
actions.setActionCommand(Actions.UNDO, this.undo.bind(this));
}

public prepare(obj: PIXI.DisplayObject, properties: { [id: string]: any }) {
const entry = this.holdingEntry = { obj, properties };
Object.keys(entry.properties)
Expand Down
1 change: 1 addition & 0 deletions src/core/preferences/preferences.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface PersistentData {
gizmos: boolean;
guides: boolean;
hitArea: boolean;
lockedObjects: string[]

leftPanelVisible: boolean;
leftPanelSize: string;
Expand Down
2 changes: 2 additions & 0 deletions src/core/preferences/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class Preferences {
gizmos: true,
guides: false,
hitArea: false,
lockedObjects: [],

responsive: false,
responsiveSize: Preferences.DefaultResponsiveSize,
responsiveTemplateIndex: 0,
Expand Down
21 changes: 0 additions & 21 deletions src/data/editor-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { ActionHandler } from 'core/action-handler';
import { Actions } from 'core/actions';

export enum DataOrigin {
ACTION = 0,
SCENE = 1,
Expand Down Expand Up @@ -53,22 +50,4 @@ export class EditorData {
this.onPropertyChanged.dispatch(e.from, k, e.value, this._selectedObject);
});
}

public setupActions(actions: ActionHandler) {
actions.setActionCommand(Actions.CLEAR_SELECTION, () => this.selectObject(null, DataOrigin.ACTION));
actions.setActionCommand(Actions.PRINT_OBJECT, () => {
if (this._selectedObject) console.info(this._selectedObject);
});

actions.setActionCommand(Actions.SELECT_PARENT, () => {
if (this._selectedObject) this.selectObject(this._selectedObject.parent, DataOrigin.ACTION);
});

actions.setActionCommand(Actions.LOCK_SELECTION, () => {
if (this._selectedObject) {
this._selectedObject.__locked = !this._selectedObject.__locked;
this.onObjectLocked.dispatch(this._selectedObject);
}
}, () => this._selectedObject?.__locked);
}
}
1 change: 1 addition & 0 deletions src/editor.state-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class EditorStateHandler {
return {
clearPreferences: builder.clearPreferences,
pauseGame: builder.pauseGame,
saveLockedObjectsPath: builder.saveLockedObjectsPath ?? false,
referenceImageUrl: builder.referenceImageUrl,
root: builder.root(),
};
Expand Down
10 changes: 6 additions & 4 deletions src/plugin.model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
export interface PluginConfigBuilder {
root?: () => Container;
referenceImageUrl?: (width: number, height: number, responsive: boolean) => string;
pauseGame?: boolean;
clearPreferences?: boolean;
saveLockedObjectsPath?: boolean;
root?: () => Container;
referenceImageUrl?: (width: number, height: number, responsive: boolean) => string;
onShow?: () => void;
onHide?: () => void;
}

export interface PluginConfig {
root: Container;
referenceImageUrl: (width: number, height: number, responsive: boolean) => string;
pauseGame: boolean;
clearPreferences: boolean;
saveLockedObjectsPath: boolean;
root: Container;
referenceImageUrl: (width: number, height: number, responsive: boolean) => string;
}

export interface Point {
Expand Down

0 comments on commit 3962fac

Please sign in to comment.