Skip to content

Commit

Permalink
added toggle lock selection
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Dec 14, 2021
1 parent 348f00e commit 419d9cc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion example/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function create() {
for (let i = 0; i < game.width; i += 100) grid.moveTo(i, 0).lineTo(i, game.height);
for (let i = 0; i < game.height; i += 100) grid.moveTo(0, i).lineTo(game.width, i);

grid.__skip = true;
grid.__locked = true;
game.world.add(grid);

const SIZE = 200;
Expand Down
1 change: 1 addition & 0 deletions src/core/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export class Actions {
public static readonly TOGGLE_RIGHT_PANEL = 'TOGGLE_RIGHT_PANEL';
public static readonly HELP = 'HELP';
public static readonly SELECT_PARENT = 'SELECT_PARENT';
public static readonly LOCK_SELECTION = 'LOCK_SELECTION';
}
7 changes: 7 additions & 0 deletions src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,17 @@ class EditorClass {
category: 'general',
},

{
id: Actions.LOCK_SELECTION,
tooltip: 'Toggle lock selection',
icon: 'fa-lock',
category: 'object-tree',
},
{
id: Actions.SELECT_PARENT,
tooltip: 'Select parent',
icon: 'fa-level-up-alt',
toggle: true,
category: 'object-tree',
}
);
Expand Down
11 changes: 10 additions & 1 deletion src/data/editor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class EditorData {
private _hasScheduledEvents = false;
private _scheduledEvents: { [id: string]: { value: any, from: DataOrigin } } = {};

public readonly onObjectLocked = new Phaser.Signal();

public dispatchScheduledEvents() {
if (!this._hasScheduledEvents) return;
this._hasScheduledEvents = false;
Expand All @@ -59,7 +61,14 @@ export class EditorData {
});

actions.setActionCommand(Actions.SELECT_PARENT, () => {
if (this.selectedObject) this.selectObject(this.selectedObject.parent, DataOrigin.ACTION);
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);
}
}
45 changes: 31 additions & 14 deletions src/editor-view/object-tree/inspector/object-tree-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class ObjectTreeInspector extends Inspector {
el.onClear = this.onFilterClear.bind(this);

Editor.data.onPropertyChanged.add(this.onPropertyChanged, this);
Editor.data.onObjectLocked.add(this.onObjectLocked, this);

this.addAction(Editor.actions.getAction(Actions.LOCK_SELECTION), 'right');
this.addAction(Editor.actions.getAction(Actions.SELECT_PARENT), 'right');
}

Expand All @@ -45,12 +47,13 @@ export class ObjectTreeInspector extends Inspector {
}

private createNode(obj: PIXI.DisplayObject, parent: HTMLElement, model: ObjectTreeModel) {
if (obj.__skip) return;
// if (obj.__locked) return;
const m = model.getById(obj.__instanceId);
if (!m) throw new Error(`Model not found for ${obj.__instanceId}`);

const node = parent.appendChild(document.createElement(ObjectTreeNode.tagName)) as ObjectTreeNode;
node.classList.add(`level-${m.level}`);

node.onNodeSelect = this.onNodeSelected.bind(this);
node.onCollapseStateChanged = this.onNodeCollapseStateChanged.bind(this);
node.setContent(m);
Expand All @@ -75,23 +78,11 @@ export class ObjectTreeInspector extends Inspector {
}
}

private onNodeSelected(node: ObjectTreeNode) {
if (node?.model === this._lastSelectedModel) return;
if (this._lastSelectedModel) this._lastSelectedModel.node.clearSelection();
this._lastSelectedModel = node.model;
Editor.data.selectObject(node.model.obj, DataOrigin.INSPECTOR);
}

private onNodeCollapseStateChanged(node: ObjectTreeNode, collapsed: boolean, all: boolean) {
const m = node.model;
this.changeCollapseState(m, collapsed, all);
}

private changeCollapseState(model: ObjectTreeNodeModel, collapsed: boolean, all: boolean) {
model.collapsed = collapsed;
if (!(all && model.obj.children?.length)) return;
model.obj.children.forEach(child => {
if (child.__skip) return;
if (child.__locked) return;
const n = this.model.getById(child.__instanceId);
if (collapsed) n.node.collapse();
else n.node.expand();
Expand Down Expand Up @@ -126,6 +117,32 @@ export class ObjectTreeInspector extends Inspector {
this.model.filter(filter);
}

private onObjectLocked(object: PIXI.DisplayObject) {
if (!object) return;
let model: ObjectTreeNodeModel;

if (this._lastSelectedModel?.obj?.__instanceId === object.__instanceId) {
model = this._lastSelectedModel;
} else {
model = this.model.getById(object.__instanceId);
}

if (!model?.obj) return;
model.node.locked = object.__locked;
}

private onNodeSelected(node: ObjectTreeNode) {
if (node?.model === this._lastSelectedModel) return;
if (this._lastSelectedModel) this._lastSelectedModel.node.clearSelection();
this._lastSelectedModel = node.model;
Editor.data.selectObject(node.model.obj, DataOrigin.INSPECTOR);
}

private onNodeCollapseStateChanged(node: ObjectTreeNode, collapsed: boolean, all: boolean) {
const m = node.model;
this.changeCollapseState(m, collapsed, all);
}

private onFilterClear() {
if (this._lastSelectedModel?.parent) {
this.expandParents(this._lastSelectedModel.parent);
Expand Down
12 changes: 12 additions & 0 deletions src/editor-view/object-tree/tree-node/object-tree-node.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ phred-object-tree-node {
}
}

&.locked {
font-style: italic;
.node-head i.fas.fa-lock {
display: flex;
}
}

.node-head {
height: 24px;
display: flex;
Expand All @@ -47,6 +54,11 @@ phred-object-tree-node {
display: flex;
align-items: center;
justify-content: center;

&.fa-lock {
font-size: 9px;
display: none;
}
}

.collapse-icon {
Expand Down
8 changes: 8 additions & 0 deletions src/editor-view/object-tree/tree-node/object-tree-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class ObjectTreeNode extends HTMLElement {
public onNodeSelect: (node: ObjectTreeNode) => void;
public onCollapseStateChanged: (node: ObjectTreeNode, collapsed: boolean, all: boolean) => void;

public set locked(value: boolean) {
this.classList.addOrRemove('locked', value);
}

public updateTitle(type: PhaserObjectType, value: string) {
this.label.textContent = value?.length > 0 ? value : type.name;
}
Expand Down Expand Up @@ -42,6 +46,10 @@ export class ObjectTreeNode extends HTMLElement {

this.label = head.appendChild(document.createElement('div'));
this.label.classList.add('node-name');
this.locked = model.obj.__locked;

// head.appendChild(document.createElement('div')).classList.add('spacer');
head.appendChild(document.createElement('i')).classList.add('fas', 'fa-lock');

this.updateTitle(model.type, model.obj.name);
this.updateObjectVisibility(model.obj.visible);
Expand Down
2 changes: 1 addition & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare namespace PIXI {
__instanceId?: number;
__type?: string;
__baseType?: string;
__skip?: boolean;
__locked?: boolean;
__isLeaf?: boolean;
type?: number;
name?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SelectionHandler {
private getObjectsUnderPoint(x: number, y: number, children: PIXI.DisplayObject[], objects: PIXI.DisplayObject[]) {
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
if (!child.visible || child.__skip || !('getBounds' in child)) continue;
if (!child.visible || child.__locked || !('getBounds' in child)) continue;
const bounds: PIXI.Rectangle = child.getBounds();
if (!child.__isLeaf) this.getObjectsUnderPoint(x, y, child.children, objects);
if (bounds.contains(x, y)) objects.push(child);
Expand Down

0 comments on commit 419d9cc

Please sign in to comment.