Skip to content

Commit

Permalink
saving locked objects path
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Dec 15, 2021
1 parent 3962fac commit 71e8a2e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
33 changes: 4 additions & 29 deletions src/core/actions-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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);
public setup(actions: ActionHandler, data: EditorData, history: History, prefs: Preferences, root: Container) {
this.setupDataActions(data, actions, root, prefs);
this.setuphistoryActions(history, actions);
this.setupPreferencesActions(prefs, actions);
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export class ActionsSetup {
);
}

public setupDataActions(data: EditorData, actions: ActionHandler, prefs: Preferences) {
public setupDataActions(data: EditorData, actions: ActionHandler, root: Container, prefs: Preferences) {
actions.setActionCommand(Actions.CLEAR_SELECTION, () => data.selectObject(null, DataOrigin.ACTION));
actions.setActionCommand(Actions.PRINT_OBJECT, () => {
if (data.selectedObject) console.info(data.selectedObject);
Expand All @@ -89,33 +89,8 @@ export class ActionsSetup {

actions.setActionCommand(
Actions.LOCK_SELECTION,
() => this.toggleLockSelection(data, prefs),
() => data.toggleLockSelection(root, 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);
}
}
5 changes: 3 additions & 2 deletions src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,14 @@ class EditorClass {
return actions;
}

public setupInitialActions() {
new ActionsSetup().setup(this.actions, this.data, this.history, this.prefs);
public setupInitialActions(root: Container) {
new ActionsSetup().setup(this.actions, this.data, this.history, this.prefs, root);
}

public enable(config: PluginConfig) {
this.actions.enable();
this.referenceImageController.enable(config);
this.data.enable(config.root, this.prefs);
}

public disable() {
Expand Down
50 changes: 50 additions & 0 deletions src/data/editor-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Preferences } from 'core/preferences/preferences';

export enum DataOrigin {
ACTION = 0,
SCENE = 1,
INSPECTOR = 2,
}

export class EditorData {
public root: Container;
private _selectedObject: PIXI.DisplayObject;

public get selectedObject() { return this._selectedObject; }
Expand Down Expand Up @@ -50,4 +53,51 @@ export class EditorData {
this.onPropertyChanged.dispatch(e.from, k, e.value, this._selectedObject);
});
}

public enable(root: Container, prefs: Preferences) {
this.root = root;
const lockedObjects = prefs.get('lockedObjects') as string[];
for (let i = lockedObjects.length - 1; i >= 0; i--) {
let o: PIXI.DisplayObject = root;
const path = lockedObjects[i].split(',').map(e => parseInt(e, 10));

for (let p = 0; p < path.length; p++) {
const index = path[p];
if (index >= 0 && index < o.children.length) {
o = o.children[index];
continue;
}
o = null;
break;
}

if (o) o.__locked = true;
else lockedObjects.splice(i, 1);
}
}

public toggleLockSelection(root: Container, prefs: Preferences) {
if (!this._selectedObject) return;
const obj = this._selectedObject;
obj.__locked = !obj.__locked;

const path: number[] = [];
let o = obj;
while (o.parent && o !== root) {
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);

this.onObjectLocked.dispatch(obj);
}
}
4 changes: 2 additions & 2 deletions src/editor.state-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class EditorStateHandler {
}

private setupInitialActions() {
Editor.setupInitialActions();
Editor.setupInitialActions(this.config.root);

const actions = Editor.actions;
actions.setActionCommand(
Expand All @@ -61,8 +61,8 @@ export class EditorStateHandler {
this.disabledUI.disable();
if (!this._initialized) this.init(this.config);

this.editorView.enable(this.config);
Editor.enable(this.config);
this.editorView.enable(this.config);
if (this.onshow) this.onshow();
}

Expand Down

0 comments on commit 71e8a2e

Please sign in to comment.