Skip to content

Commit

Permalink
Now drag'n'drop textures from texture viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Moreau-Mathis authored and Julien Moreau-Mathis committed May 28, 2019
1 parent 160e23f commit 02bbb23
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/tools/textures/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ export default class TextureViewer extends EditorPlugin {
const texture = new Texture('file:' + file.name, this.editor.core.scene);
texture.name = texture.url = texture.name.replace('file:', '');
}

// Drag'n'drop
const dropListener = this.dragEnd(originalTexture, true);
img.addEventListener('dragstart', () => this.editor.core.engine.getRenderingCanvas().addEventListener('drop', dropListener));
img.addEventListener('dragend', () => this.editor.core.engine.getRenderingCanvas().removeEventListener('drop', dropListener));
}
else {
const data = await Tools.ReadFileAsBase64(file);
Expand All @@ -391,6 +396,11 @@ export default class TextureViewer extends EditorPlugin {
const texture = new Texture('file:' + file.name, this.editor.core.scene);
texture.name = texture.url = texture.name.replace('file:', '');
}

// Drag'n'drop
const dropListener = this.dragEnd(originalTexture, false);
img.addEventListener('dragstart', () => this.editor.core.engine.getRenderingCanvas().addEventListener('drop', dropListener));
img.addEventListener('dragend', () => this.editor.core.engine.getRenderingCanvas().removeEventListener('drop', dropListener));
}

// Add text
Expand All @@ -409,6 +419,40 @@ export default class TextureViewer extends EditorPlugin {
parent.appendChild(text);
}

/**
* Returns an event called when the user drops a texture on the preview canvas
* @param texture: the texture to drop on a mesh/instanced-mesh
*/
protected dragEnd (texture: BaseTexture, isCube: boolean): (ev: DragEvent) => void {
return (ev: DragEvent) => {
const scene = this.editor.core.scene;
const pick = scene.pick(ev.offsetX, ev.offsetY);

if (!pick.pickedMesh || !pick.pickedMesh.material)
return;

// Apply
const material = pick.pickedMesh.material;

if (isCube) {
UndoRedo.Push({ baseObject: material, object: material, property: 'reflectionTexture', from: material['reflectionTexture'], to: texture });
material['reflectionTexture'] = texture;
}
else {
if (material instanceof PBRMaterial) {
UndoRedo.Push({ baseObject: material, object: material, property: 'albedoTexture', from: material.albedoTexture, to: texture });
material.albedoTexture = texture;
}
else {
UndoRedo.Push({ baseObject: material, object: material, property: 'diffuseTexture', from: material['diffuseTexture'], to: texture });
material['diffuseTexture'] = texture;
}
}

this.editor.core.onSelectObject.notifyObservers(pick.pickedMesh);
};
}

/**
* Convets a cube texture to
*/
Expand Down

0 comments on commit 02bbb23

Please sign in to comment.