Skip to content

Commit

Permalink
Improves Scene Blocks for script prefabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhaserEditor2D committed Nov 30, 2023
1 parent eb380a9 commit e8925ce
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace phasereditor2d.ide.core.code {
return isNodeModuleFile(file.getParent());
}

export function findNodeModule(file: io.FilePath): string {
export function findNodeModuleName(file: io.FilePath): string {

if (file.isRoot() || file.getParent().isRoot()) {

Expand All @@ -43,12 +43,12 @@ namespace phasereditor2d.ide.core.code {
return parentName + "/" + fileName;
}

return findNodeModule(file.getParent());
return findNodeModuleName(file.getParent());
}

export function getImportPath(file: io.FilePath, importFile: io.FilePath): { importPath: string, asDefault: boolean } {

const nodeModule = findNodeModule(importFile);
const nodeModule = findNodeModuleName(importFile);

if (nodeModule) {

Expand Down
2 changes: 1 addition & 1 deletion source/editor/plugins/phasereditor2d.scene/src/ui/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ namespace phasereditor2d.scene.ui {
this._id = id;
}

getSceneType() {
getSceneType(): core.json.SceneType {

return this._sceneType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ namespace phasereditor2d.scene.ui.blocks {
private _getPacks: () => pack.core.AssetPack[];
private _blocksProvider: SceneEditorBlocksProvider;
protected _editor: editor.SceneEditor;
private _scriptsContentProvider: sceneobjects.ScriptsContentProvider;

constructor(editor: editor.SceneEditor, getPacks: () => pack.core.AssetPack[]) {
super();

this._blocksProvider = editor.getBlocksProvider();
this._getPacks = getPacks;
this._editor = this._blocksProvider.getEditor();
this._scriptsContentProvider = new sceneobjects.ScriptsContentProvider();

}

getPackItems() {
Expand All @@ -43,14 +46,16 @@ namespace phasereditor2d.scene.ui.blocks {
.filter(item => SCENE_EDITOR_BLOCKS_PACK_ITEM_TYPES.has(item.getType()));
}

private isScriptNodePrefabScene() {

return this._editor.getScene().isScriptNodePrefabScene();
}

getRoots(input: any) {

if (this._editor.getScene().isScriptNodePrefabScene()) {
if (this.isScriptNodePrefabScene()) {

return [
sceneobjects.ScriptNodeExtension.getInstance(),
...this.getPrefabFiles("scripts")
];
return this._scriptsContentProvider.getRoots(input);
}

const groupingType = grouping.getGroupingPreference();
Expand Down Expand Up @@ -146,6 +151,11 @@ namespace phasereditor2d.scene.ui.blocks {

getChildren(parent: any): any[] {

if (this.isScriptNodePrefabScene()) {

return this._scriptsContentProvider.getChildren(parent);
}

if (parent instanceof viewers.PhaserTypeSymbol) {

const finder = ScenePlugin.getInstance().getSceneFinder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
namespace phasereditor2d.scene.ui.blocks {

import core = colibri.core;
import code = ide.core.code;

export class SceneEditorBlocksLabelProvider extends pack.ui.viewers.AssetPackLabelProvider {

getLabel(obj: any) {

if (obj instanceof core.io.FilePath && !obj.isFolder()) {
if (obj instanceof core.io.FilePath) {

if (obj.isFolder() && code.isNodeModuleFile(obj)) {

return code.findNodeModuleName(obj);
}

return sceneobjects.getSceneDisplayName(obj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace phasereditor2d.scene.ui.blocks {

getStyledLabelProvider(): controls.viewers.IStyledLabelProvider {

return new SceneEditorBlocksStyledLabelProvider();
return new SceneEditorBlocksStyledLabelProvider(this._editor);
}

getCellRendererProvider(): controls.viewers.ICellRendererProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
namespace phasereditor2d.scene.ui.blocks {

import io = colibri.core.io;
import code = ide.core.code;

export class SceneEditorBlocksStyledLabelProvider extends SceneEditorBlocksLabelProvider implements colibri.ui.controls.viewers.IStyledLabelProvider {

private _scriptsLabelProvider = new sceneobjects.ScriptStyledLabelProvider();
private _editor: editor.SceneEditor;

constructor(editor: editor.SceneEditor) {
super();
this._editor = editor;
}


getStyledTexts(obj: any, dark: boolean): colibri.ui.controls.viewers.IStyledText[] {

if (this._editor.getScene().isScriptNodePrefabScene()) {

return this._scriptsLabelProvider.getStyledTexts(obj, dark);
}

const text = super.getLabel(obj);
const isPrefab = obj instanceof colibri.core.io.FilePath
&& ScenePlugin.getInstance().getSceneFinder().isPrefabFile(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace phasereditor2d.scene.ui.dialogs {

viewer.setContentProvider(new AddObjectContentProvider(editor));
viewer.setLabelProvider(new blocks.SceneEditorBlocksLabelProvider());
viewer.setStyledLabelProvider(new blocks.SceneEditorBlocksStyledLabelProvider());
viewer.setStyledLabelProvider(new blocks.SceneEditorBlocksStyledLabelProvider(editor));
viewer.setCellRendererProvider(new blocks.SceneEditorBlocksCellRendererProvider());
viewer.setInput([]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ namespace phasereditor2d.scene.ui.editor {
alert(errors.join("<br>"));
}

this.refreshBlocks();

} else {

alert("Invalid file format.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace phasereditor2d.scene.ui.sceneobjects {

import controls = colibri.ui.controls;
import io = colibri.core.io;
import code = ide.core.code;

export class AddScriptDialog extends controls.dialogs.ViewerDialog {

Expand All @@ -26,51 +25,11 @@ namespace phasereditor2d.scene.ui.sceneobjects {
return new controls.viewers.IconImageCellRenderer(icon)
}));

const input = AddScriptDialog.buildViewerInput();

viewer.setInput(input);
viewer.setInput([]);

return viewer;
}

private static buildViewerInput() {

const finder = ScenePlugin.getInstance().getSceneFinder();

const files = finder.getScriptPrefabFiles();

files.sort((a, b) => {

const aa = a.getFullName();
const bb = b.getFullName();

return aa.localeCompare(bb);
});

files.sort((a, b) => {

const aa = code.isNodeModuleFile(a)? -1 : 1;
const bb = code.isNodeModuleFile(b)? -1 : 1;

return aa - bb;
});

const folders: io.FilePath[] = [];

for (const file of files) {

let parent = file.getParent();

if (folders.indexOf(parent) < 0) {

folders.push(parent);
}
}

const input = [ScriptNodeExtension.getInstance(), ...folders];
return input;
}

private _editor: ui.editor.SceneEditor;

constructor(editor: ui.editor.SceneEditor) {
Expand Down Expand Up @@ -103,61 +62,4 @@ namespace phasereditor2d.scene.ui.sceneobjects {
new ui.editor.undo.CreateObjectWithAssetOperation(this._editor, [script], 0, 0));
}
}

class ScriptStyledLabelProvider implements controls.viewers.IStyledLabelProvider {

getStyledTexts(obj: any, dark: boolean) {

let text: string;
let color: string;

if (obj instanceof io.FilePath && obj.isFolder()) {

if (code.isNodeModuleFile(obj)) {

text = code.findNodeModule(obj);
color = ScenePlugin.getInstance().getScriptsLibraryColor();

} else {

text = obj.getName();
color = controls.Controls.getTheme().viewerForeground;
}

} else if (obj instanceof ScriptNodeExtension) {

text = obj.getTypeName();
color = controls.Controls.getTheme().viewerForeground;

} else {

text = getSceneDisplayName(obj);
color = ScenePlugin.getInstance().getPrefabColor();
}

return [{ text, color }];
}
}

class ScriptsContentProvider implements controls.viewers.ITreeContentProvider {

getRoots(input: any[]): any[] {

return input;
}

getChildren(parent: any): any[] {

if (parent instanceof io.FilePath) {

const finder = ScenePlugin.getInstance().getSceneFinder();

const files = finder.getScriptPrefabFiles();

return files.filter(f => f.getParent() === parent);
}

return [];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace phasereditor2d.scene.ui.sceneobjects {

import controls = colibri.ui.controls;
import io = colibri.core.io;
import code = ide.core.code;

export class ScriptsContentProvider implements controls.viewers.ITreeContentProvider {

getRoots(input: any[]): any[] {

const finder = ScenePlugin.getInstance().getSceneFinder();

const files = finder.getScriptPrefabFiles();

files.sort((a, b) => {

const aa = a.getFullName();
const bb = b.getFullName();

return aa.localeCompare(bb);
});

files.sort((a, b) => {

const aa = code.isNodeModuleFile(a)? -1 : 1;
const bb = code.isNodeModuleFile(b)? -1 : 1;

return aa - bb;
});

const folders: io.FilePath[] = [];

for (const file of files) {

let parent = file.getParent();

if (folders.indexOf(parent) < 0) {

folders.push(parent);
}
}

return [ScriptNodeExtension.getInstance(), ...folders];
}

getChildren(parent: any): any[] {

if (parent instanceof io.FilePath) {

const finder = ScenePlugin.getInstance().getSceneFinder();

const files = finder.getScriptPrefabFiles();

return files.filter(f => f.getParent() === parent);
}

return [];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace phasereditor2d.scene.ui.sceneobjects {

import controls = colibri.ui.controls;
import io = colibri.core.io;
import code = ide.core.code;

export class ScriptStyledLabelProvider implements controls.viewers.IStyledLabelProvider {

getStyledTexts(obj: any, dark: boolean) {

let text: string;
let color: string;

if (obj instanceof io.FilePath && obj.isFolder()) {

if (code.isNodeModuleFile(obj)) {

text = code.findNodeModuleName(obj);
color = ScenePlugin.getInstance().getScriptsLibraryColor();

} else {

text = obj.getName();
color = controls.Controls.getTheme().viewerForeground;
}

} else if (obj instanceof ScriptNodeExtension) {

text = obj.getTypeName();
color = controls.Controls.getTheme().viewerForeground;

} else {

text = getSceneDisplayName(obj);
color = ScenePlugin.getInstance().getPrefabColor();
}

return [{ text, color }];
}
}
}

0 comments on commit e8925ce

Please sign in to comment.