Skip to content

Commit

Permalink
enabling actions again
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Sep 23, 2021
1 parent ffb9e12 commit 8e51579
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 136 deletions.
30 changes: 20 additions & 10 deletions src/core/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ export interface Action {
id: string;
label?: string;
icon?: string;
shortcut?: string;
shortcuts?: string[];
toggle?: boolean;
hold?: boolean;
on?: string;
command: () => void;
command?: () => void;
state?: () => any;
}

Expand All @@ -20,10 +20,18 @@ export class ActionHandler {
public getActions() { return Object.values(this.actions); }
public getAction(id: string) { return id in this.actionById ? this.actionById[id] : null; }

public addContainer(id: string, container: HTMLElement) {
this.containers[id] = container;
public setActionCommand(id: string, command: () => void, state?: () => boolean) {
const action = this.getAction(id);
if (!action) {
console.warn(`Could not find action ${id}`);
return;
}
action.command = command;
action.state = state;
}

public addContainer(id: string, container: HTMLElement) { this.containers[id] = container; }

public enable() {
Object.keys(this.containers).forEach(cid => {
const container = this.containers[cid];
Expand All @@ -46,10 +54,12 @@ export class ActionHandler {
if (!(cid in this.actions)) this.actions[cid] = {};
const container = this.actions[cid];

if (action.shortcut) {
if (action.shortcut in container)
throw new Error(`There is already an action with shortcut ${action.shortcut}: ${container[action.shortcut].label}`);
container[action.shortcut] = action;
if (action.shortcuts) {
action.shortcuts.forEach(s => {
if (s in container)
throw new Error(`There is already an action with shortcut ${s}: ${container[s].label}`);
container[s] = action;
});
}

this.actionById[action.id] = action;
Expand All @@ -64,7 +74,7 @@ export class ActionHandler {
+ e.key;
if (k in actions) {
const action = actions[k];
action.command();
action.command?.();
if (action.toggle && action.hold)
this._holdingToggleAction = action;
e.preventDefault();
Expand All @@ -73,7 +83,7 @@ export class ActionHandler {

private onKeyUp() {
if (!this._holdingToggleAction) return;
this._holdingToggleAction.command();
this._holdingToggleAction.command?.();
this._holdingToggleAction = null;
}
}
103 changes: 102 additions & 1 deletion src/core/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ComponentTags } from 'component-tags';
import { Actions } from 'core/actions';
import { EditorData } from 'data/editor-data';
import { InspectorData } from 'data/inspector-data';
import { PhaserMeta } from 'data/phaser-meta';
Expand All @@ -22,7 +23,7 @@ class EditorClass {
this.inspectorData = this.createInspectorData();
this.meta = new PhaserMeta();

this.actions = new ActionHandler();
this.actions = this.createActions();
this.history = new History(this.data);
this.prefs = new Preferences(clearPrefs);
}
Expand Down Expand Up @@ -128,6 +129,106 @@ class EditorClass {
return data;
}

private createActions() {
const actions = new ActionHandler();
actions.add(
{
id: Actions.TOGGLE_ENABLED,
label: 'edit',
icon: 'fa-edit',
toggle: true,
shortcuts: ['ctrl+F2']
},

{
id: Actions.TOGGLE_SNAP,
label: 'snap',
icon: 'fa-border-all',
toggle: true,
},
{
id: Actions.TOGGLE_GIZMOS,
label: 'gizmos',
icon: 'fa-vector-square',
toggle: true,
hold: true,
shortcuts: ['ctrl+shift+Shift', 'ctrl+shift+Control'],
},
{
id: Actions.TOGGLE_GUIDES,
toggle: true,
label: 'guides',
icon: 'fa-compress',
},
{
id: Actions.UNDO,
label: 'undo',
icon: 'fa-undo-alt',
shortcuts: ['ctrl+z'],
},

{
id: Actions.PRINT_OBJECT,
label: 'print',
icon: 'fa-terminal',
shortcuts: ['ctrl+alt+p'],
},
{
id: Actions.DESELECT,
shortcuts: ['Escape'],
},

{
id: Actions.MOVE_UP_1,
shortcuts: ['ArrowUp'],
// command: () => scene.moveSelectedObject(0, -1)
},
{
id: Actions.MOVE_DOWN_1,
shortcuts: ['ArrowDown'],
// command: () => scene.moveSelectedObject(0, 1)
},
{
id: Actions.MOVE_LEFT_1,
shortcuts: ['ArrowLeft'],
// command: () => scene.moveSelectedObject(-1, 0)
},
{
id: Actions.MOVE_RIGHT_1,
shortcuts: ['ArrowRight'],
// command: () => scene.moveSelectedObject(1, 0)
},
{
id: Actions.MOVE_UP_10,
shortcuts: ['shift+ArrowUp'],
// command: () => scene.moveSelectedObject(0, -10)
},
{
id: Actions.MOVE_DOWN_10,
shortcuts: ['shift+ArrowDown'],
// command: () => scene.moveSelectedObject(0, 10)
},
{
id: Actions.MOVE_LEFT_10,
shortcuts: ['shift+ArrowLeft'],
// command: () => scene.moveSelectedObject(-10, 0)
},
{
id: Actions.MOVE_RIGHT_10,
shortcuts: ['shift+ArrowRight'],
// command: () => scene.moveSelectedObject(10, 0)
},
);

return actions;
}

public setupInitialActions() {
const actions = this.actions;
this.prefs.setupActions(actions);
this.history.setupActions(actions);
this.data.setupActions(actions);
}

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

export interface HistoryEntry {
obj: PIXI.DisplayObject;
Expand All @@ -16,6 +18,10 @@ 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).forEach(k =>
Expand Down
22 changes: 16 additions & 6 deletions src/core/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { OnlyProperties, PanelSide } from 'types';
import { ActionHandler } from './action-handler';
import { Actions } from './actions';

export type PreferenceKey = OnlyProperties<Preferences>;

Expand Down Expand Up @@ -35,13 +37,13 @@ export class Preferences {
this.save('guides', value);
}

private _referenceImage = false;
private _refImage = false;

public get referenceImage() { return this._referenceImage; }
public get refImage() { return this._refImage; }

public set referenceImage(value: boolean) {
this._referenceImage = value;
this.notifyListeners('referenceImage', value);
public set refImage(value: boolean) {
this._refImage = value;
this.notifyListeners('refImage', value);
this.save('referenceImage', value);
}

Expand All @@ -55,7 +57,15 @@ export class Preferences {
this._snap = this.load('snap', true);
this._gizmos = this.load('gizmos', true);
this._guides = this.load('guides', false);
this._referenceImage = this.load('referenceImage', false);
this._refImage = this.load('refImage', false);
}

// hmm... this is weird
public setupActions(actions: ActionHandler) {
actions.setActionCommand(Actions.TOGGLE_SNAP, () => this.snap = !this._snap, () => this._snap);
actions.setActionCommand(Actions.TOGGLE_GIZMOS, () => this.gizmos = !this._gizmos, () => this._gizmos);
actions.setActionCommand(Actions.TOGGLE_GUIDES, () => this.guides = !this._guides, () => this._guides);
actions.setActionCommand(Actions.TOGGLE_REF_IMAGE, () => this.refImage = !this._refImage, () => this._refImage);
}


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

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

public setupActions(actions: ActionHandler) {
actions.setActionCommand(Actions.DESELECT, () => this.selectObject(null, DataOrigin.ACTION));
actions.setActionCommand(Actions.PRINT_OBJECT, () => {
if (this._selectedObject) console.info(this._selectedObject);
})
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentTags } from 'component-tags';
import { Actions } from 'core/actions';
import { Editor } from 'core/editor';
import { PreferenceKey } from 'core/preferences';
import { ComponentTags } from 'component-tags';
import { ActionButton } from '../button/action-button';
import './reference-image-panel.scss';

Expand All @@ -22,12 +22,12 @@ export class ReferenceImagePanel extends HTMLElement {
this.appendChild(slider);

Editor.prefs.onPreferenceChanged.add(this.onPreferenceChanged, this);
this.onPreferenceChanged('referenceImage', Editor.prefs.referenceImage);
this.onPreferenceChanged('refImage', Editor.prefs.refImage);
// slider.value = Editor.referenceImage.alpha.toString();
}

private onPreferenceChanged(pref: PreferenceKey, value: any) {
if (pref !== 'referenceImage') return;
if (pref !== 'refImage') return;
if (value) this.slider.removeAttribute('disabled');
else this.slider.setAttribute('disabled', 'true');
}
Expand Down
3 changes: 3 additions & 0 deletions src/editor-view/editor-view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ComponentTags } from 'component-tags';
import { ActionHandler } from 'core/action-handler';
import { Editor } from 'core/editor';
import { DataOrigin } from 'data/editor-data';
import { ActionsToolbar } from './actions/actions-toolbar';
Expand Down Expand Up @@ -32,6 +33,8 @@ export class EditorView extends Widget {
this.panels.forEach(panel => panel.init(game, root));
}

public setupActions(actions: ActionHandler) { }

private createElements() {
const leftPanel = this.appendChild(document.createElement(ComponentTags.Panel) as Panel);
leftPanel.setSide('left');
Expand Down
2 changes: 1 addition & 1 deletion src/editor-view/inspector/inspector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataOrigin } from 'data/editor-data';
import { Widget } from 'editor-view/widget/widget';
import { DataOrigin } from 'index';
import './inspector.scss';

export abstract class Inspector extends Widget {
Expand Down
Loading

0 comments on commit 8e51579

Please sign in to comment.