Skip to content

Commit

Permalink
feat: Added options to enable/disable helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeUtan committed Apr 21, 2021
1 parent e2155fb commit 1f7fe90
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
23 changes: 22 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,28 @@
"mcmodel-viewer.assetRoots": {
"description": "A list of directories that contain minecraft assets",
"type": "array",
"default": []
"default": [],
"uniqueItems": true
},
"mcmodel-viewer.helpers.showBoundingBox": {
"description": "Show bounding box in model preview",
"type": "boolean",
"default": true
},
"mcmodel-viewer.helpers.showCardinalDirectionLabeles": {
"title": "Show labels for cardinal directions",
"type": "boolean",
"default": true
},
"mcmodel-viewer.helpers.show3x3BlocksGrid": {
"title": "Show 3x3 Blocks Grid",
"type": "boolean",
"default": true
},
"mcmodel-viewer.helpers.showVoxelGrid": {
"then": "Show voxel grid",
"type": "boolean",
"default": true
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/MCModelPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class MCModelPanel {
return true;
};

public static updateHelpersConfiguration(cfg: any) {
MCModelPanel.postMessage({command: "updateHelpersConfiguration", value: cfg});
}

static get webview() {
return this.currentPanel?._panel.webview;
}
Expand Down
31 changes: 31 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export function getAssetRoots(): vscode.Uri[] {
return roots.map(r => vscode.Uri.file(r));
}

export function getHelperConfiguration() {
return {
showBoundingBox: vscode.workspace.getConfiguration("mcmodel-viewer.helpers").get("showBoundingBox"),
showCardinalDirectionLabeles: vscode.workspace.getConfiguration("mcmodel-viewer.helpers").get("showCardinalDirectionLabeles"),
show3x3BlocksGrid: vscode.workspace.getConfiguration("mcmodel-viewer.helpers").get("show3x3BlocksGrid"),
showVoxelGrid: vscode.workspace.getConfiguration("mcmodel-viewer.helpers").get("showVoxelGrid")
};
}

export function createConfigurationListeners() {
onAssetRootsChanged();

Expand All @@ -18,12 +27,34 @@ export function createConfigurationListeners() {
if(e.affectsConfiguration("mcmodel-viewer.assetRoots")) {
onAssetRootsChanged();
}
}),
vscode.workspace.onDidChangeConfiguration((e) => {
if(e.affectsConfiguration("mcmodel-viewer.helpers")) {
notifyConfigChanged("mcmodel-viewer.helpers");
}
})
];
}

let configSubscribers: {[config: string]: Set<() => void>} = {};

export function subscribeConfiguration(config: string, onDidChange: () => void) {
if(!(config in configSubscribers)) {
configSubscribers[config] = new Set();
}
configSubscribers[config].add(onDidChange);
return new vscode.Disposable(() => configSubscribers[config].delete(onDidChange));
}

function notifyConfigChanged(config: string) {
configSubscribers[config]?.forEach((cb) => {
cb();
});
}

async function onAssetRootsChanged() {
assetRoots = (await minecraft.findAssetRootsInWorkspace()).concat(getAssetRoots());
textureAssetsRoots = await minecraft.findTextureAssetsRoots(assetRoots);
modelAssetsRoots = await minecraft.findModelAssetsRoots(assetRoots);
notifyConfigChanged("mcmodel-viewer.assetRoots");
}
8 changes: 7 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export async function activate(context: vscode.ExtensionContext) {
);

// Listen to configuration changes
context.subscriptions.push(...config.createConfigurationListeners());
context.subscriptions.push(
...config.createConfigurationListeners(),
config.subscribeConfiguration("mcmodel-viewer.helpers", () => {
MCModelPanel.updateHelpersConfiguration(config.getHelperConfiguration());
})
);

// Register commands
context.subscriptions.push(
Expand All @@ -20,6 +25,7 @@ export async function activate(context: vscode.ExtensionContext) {

MCModelPanel.createOrShow(context.extensionUri);
MCModelPanel.loadModel(modelUri);
MCModelPanel.updateHelpersConfiguration(config.getHelperConfiguration());
}),
vscode.commands.registerCommand('mcmodel-viewer.refresh', () => {
MCModelPanel.kill();
Expand Down
25 changes: 12 additions & 13 deletions webviews/components/ModelCanvas.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { MinecraftModelMesh, MinecraftTextureLoader } from '@oran9e/three-mcmodel';
import { Text2D } from '../utils/Text2D'
import { modelStore, texturesStore } from '../data/model'
import { helpersCfgStore } from '../data/config'
let canvas: HTMLCanvasElement;
Expand All @@ -19,12 +20,15 @@
// Helpers
const voxelGrid = new THREE.GridHelper(48, 48, 0x444444, 0x444444)
const blockGrid = new THREE.GridHelper(48, 3)
let cardinalDirectionLabels = []
let cardinalDirectionLabels: Text2D[] = []
createCardinalDirectionLabels()
const boundingBox = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(48, 48, 48)).translate(0, 8, 0),
new THREE.LineBasicMaterial({ color: 0x444444, linewidth: 3 })
)
init()
modelStore.subscribe(mesh => {
if(modelMesh) {
scene.remove(modelMesh)
Expand All @@ -37,6 +41,13 @@
texturesStore.subscribe(t => textures = t)
helpersCfgStore.subscribe(cfg => {
cfg.showBoundingBox ? scene.add(boundingBox) : scene.remove(boundingBox)
cfg.show3x3BlocksGrid ? scene.add(blockGrid) : scene.remove(blockGrid)
cfg.showVoxelGrid ? scene.add(voxelGrid) : scene.remove(voxelGrid)
cfg.showCardinalDirectionLabeles ? scene.add(...cardinalDirectionLabels) : scene.remove(...cardinalDirectionLabels)
});
$: if(modelMesh != null && textures != null) {
const textureLoader = new MinecraftTextureLoader()
modelMesh.resolveTextures(p => textureLoader.load(textures![p]))
Expand All @@ -61,12 +72,6 @@
controls.enableKeys = false
controls.screenSpacePanning = true
// Add helpers
scene.add(boundingBox)
scene.add(voxelGrid)
scene.add(blockGrid)
createCardinalDirectionLabels()
animate()
}
Expand All @@ -82,8 +87,6 @@
renderer.render(scene, camera)
}
init()
function createCardinalDirectionLabels() {
const loader = new THREE.FontLoader();
loader.load(MEDIA_ROOT + '/helvetiker_regular.typeface.json', function ( font ) {
Expand All @@ -93,10 +96,6 @@
new Text2D("S", font, [-Math.PI / 2, Math.PI, 0], [2, 0, 26]),
new Text2D("W", font, [0, Math.PI / 2, Math.PI / 2], [-26, 0, 2]),
]
for(const label of cardinalDirectionLabels) {
scene.add(label)
}
})
}
</script>
Expand Down
6 changes: 6 additions & 0 deletions webviews/components/ModelPreviewPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { MinecraftModelLoader, MinecraftModelMesh } from '@oran9e/three-mcmodel';
import ModelCanvas from './ModelCanvas.svelte'
import { modelStore, texturesStore } from '../data/model'
import { helpersCfgStore } from '../data/config'
let modelCanvas: ModelCanvas
Expand All @@ -11,9 +12,14 @@
new MinecraftModelLoader().load(e.data.value.model, mesh => {
texturesStore.set(e.data.value.textures)
modelStore.set(mesh)
console.log("Loaded model");
})
break;
case "updateHelpersConfiguration":
helpersCfgStore.set(e.data.value)
}
});
</script>

<ModelCanvas bind:this={modelCanvas} />
12 changes: 12 additions & 0 deletions webviews/data/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { writable } from 'svelte/store';

export class HelperConfiguration {
constructor(
public showBoundingBox = true,
public showCardinalDirectionLabeles = true,
public show3x3BlocksGrid = true,
public showVoxelGrid = true
) {}
}

export const helpersCfgStore = writable<HelperConfiguration>(new HelperConfiguration());
1 change: 1 addition & 0 deletions webviews/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",
"target": "es6",
"include": ["./**/*", ],
"exclude": ["../node_modules/*"],
"compilerOptions": {"strict": true, }
Expand Down

0 comments on commit 1f7fe90

Please sign in to comment.