mirror of
https://github.com/JannisX11/blockbench.git
synced 2025-01-18 15:26:19 +08:00
Add preview controller event hooks
This commit is contained in:
parent
b824a0d7e3
commit
48ae3104fa
@ -818,7 +818,8 @@ new NodePreviewController(Cube, {
|
||||
this.updateGeometry(element);
|
||||
this.updateFaces(element);
|
||||
this.updateUV(element);
|
||||
|
||||
|
||||
this.dispatchEvent('setup', {element});
|
||||
},
|
||||
updateTransform(element) {
|
||||
NodePreviewController.prototype.updateTransform(element);
|
||||
@ -835,6 +836,8 @@ new NodePreviewController(Cube, {
|
||||
if (Modes.paint) {
|
||||
element.preview_controller.updatePaintingGrid(element);
|
||||
}
|
||||
|
||||
this.dispatchEvent('update_transform', {element});
|
||||
},
|
||||
updateGeometry(element) {
|
||||
if (element.resizable) {
|
||||
@ -872,9 +875,11 @@ new NodePreviewController(Cube, {
|
||||
].map(a => new THREE.Vector3().fromArray(a))
|
||||
mesh.outline.geometry.setFromPoints(points);
|
||||
}
|
||||
|
||||
this.dispatchEvent('update_geometry', {element});
|
||||
},
|
||||
updateFaces(cube) {
|
||||
let {mesh} = cube;
|
||||
updateFaces(element) {
|
||||
let {mesh} = element;
|
||||
|
||||
let indices = [];
|
||||
let j = 0;
|
||||
@ -882,13 +887,13 @@ new NodePreviewController(Cube, {
|
||||
mesh.geometry.clearGroups();
|
||||
let last_tex;
|
||||
Canvas.face_order.forEach((fkey, i) => {
|
||||
if (cube.faces[fkey].texture !== null) {
|
||||
if (element.faces[fkey].texture !== null) {
|
||||
indices.push(0 + i*4, 2 + i*4, 1 + i*4, 2 + i*4, 3 + i*4, 1 + i*4);
|
||||
if (last_tex && cube.faces[fkey].texture === last_tex) {
|
||||
if (last_tex && element.faces[fkey].texture === last_tex) {
|
||||
mesh.geometry.groups[mesh.geometry.groups.length-1].count += 6;
|
||||
} else {
|
||||
mesh.geometry.addGroup(j*6, 6, j)
|
||||
last_tex = cube.faces[fkey].texture;
|
||||
last_tex = element.faces[fkey].texture;
|
||||
}
|
||||
mesh.geometry.faces.push(fkey)
|
||||
j++;
|
||||
@ -913,17 +918,17 @@ new NodePreviewController(Cube, {
|
||||
|
||||
} else if (Format.single_texture) {
|
||||
let tex = Texture.getDefault();
|
||||
mesh.material = tex ? tex.getMaterial() : Canvas.emptyMaterials[cube.color];
|
||||
mesh.material = tex ? tex.getMaterial() : Canvas.emptyMaterials[element.color];
|
||||
|
||||
} else {
|
||||
var materials = []
|
||||
Canvas.face_order.forEach(function(face) {
|
||||
if (cube.faces[face].texture !== null) {
|
||||
var tex = cube.faces[face].getTexture()
|
||||
if (element.faces[face].texture !== null) {
|
||||
var tex = element.faces[face].getTexture()
|
||||
if (tex && tex.uuid) {
|
||||
materials.push(Project.materials[tex.uuid])
|
||||
} else {
|
||||
materials.push(Canvas.emptyMaterials[cube.color])
|
||||
materials.push(Canvas.emptyMaterials[element.color])
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -931,14 +936,16 @@ new NodePreviewController(Cube, {
|
||||
mesh.material = materials
|
||||
}
|
||||
if (!mesh.material) mesh.material = Canvas.transparentMaterial;
|
||||
|
||||
Cube.preview_controller.dispatchEvent('update_faces', {element});
|
||||
},
|
||||
updateUV(cube, animation = true) {
|
||||
var mesh = cube.mesh
|
||||
updateUV(element, animation = true) {
|
||||
var mesh = element.mesh
|
||||
if (mesh === undefined || !mesh.geometry) return;
|
||||
|
||||
if (Project.box_uv) {
|
||||
|
||||
var size = cube.size(undefined, true)
|
||||
var size = element.size(undefined, true)
|
||||
|
||||
var face_list = [
|
||||
{face: 'east', from: [0, size[2]], size: [size[2], size[1]]},
|
||||
@ -949,7 +956,7 @@ new NodePreviewController(Cube, {
|
||||
{face: 'north', from: [size[2], size[2]], size: [size[0], size[1]]},
|
||||
]
|
||||
|
||||
if (cube.mirror_uv) {
|
||||
if (element.mirror_uv) {
|
||||
face_list.forEach(function(f) {
|
||||
f.from[0] += f.size[0]
|
||||
f.size[0] *= -1
|
||||
@ -970,22 +977,22 @@ new NodePreviewController(Cube, {
|
||||
}
|
||||
face_list.forEach(function(f, fIndex) {
|
||||
|
||||
if (cube.faces[f.face].texture == null) return;
|
||||
if (element.faces[f.face].texture == null) return;
|
||||
|
||||
var uv= [
|
||||
f.from[0] + cube.uv_offset[0],
|
||||
f.from[1] + cube.uv_offset[1],
|
||||
f.from[0] + f.size[0] + cube.uv_offset[0],
|
||||
f.from[1] + f.size[1] + cube.uv_offset[1]
|
||||
f.from[0] + element.uv_offset[0],
|
||||
f.from[1] + element.uv_offset[1],
|
||||
f.from[0] + f.size[0] + element.uv_offset[0],
|
||||
f.from[1] + f.size[1] + element.uv_offset[1]
|
||||
]
|
||||
uv.forEach(function(s, si) {
|
||||
uv[si] *= 1
|
||||
})
|
||||
|
||||
cube.faces[f.face].uv[0] = uv[0]
|
||||
cube.faces[f.face].uv[1] = uv[1]
|
||||
cube.faces[f.face].uv[2] = uv[2]
|
||||
cube.faces[f.face].uv[3] = uv[3]
|
||||
element.faces[f.face].uv[0] = uv[0]
|
||||
element.faces[f.face].uv[1] = uv[1]
|
||||
element.faces[f.face].uv[2] = uv[2]
|
||||
element.faces[f.face].uv[3] = uv[3]
|
||||
|
||||
//Fight Bleeding
|
||||
for (var si = 0; si < 2; si++) {
|
||||
@ -999,7 +1006,7 @@ new NodePreviewController(Cube, {
|
||||
|
||||
stretch = 1;
|
||||
frame = 0;
|
||||
let tex = cube.faces[f.face].getTexture();
|
||||
let tex = element.faces[f.face].getTexture();
|
||||
if (tex instanceof Texture && tex.frameCount !== 1) {
|
||||
stretch = tex.frameCount
|
||||
if (animation === true && tex.currentFrame) {
|
||||
@ -1017,22 +1024,25 @@ new NodePreviewController(Cube, {
|
||||
|
||||
Canvas.face_order.forEach((face, fIndex) => {
|
||||
|
||||
if (cube.faces[face].texture === null) return;
|
||||
if (element.faces[face].texture === null) return;
|
||||
|
||||
stretch = 1;
|
||||
frame = 0;
|
||||
let tex = cube.faces[face].getTexture();
|
||||
let tex = element.faces[face].getTexture();
|
||||
if (tex instanceof Texture && tex.frameCount !== 1) {
|
||||
stretch = tex.frameCount
|
||||
if (animation === true && tex.currentFrame) {
|
||||
frame = tex.currentFrame
|
||||
}
|
||||
}
|
||||
Canvas.updateUVFace(mesh.geometry.attributes.uv, fIndex, cube.faces[face], frame, stretch)
|
||||
Canvas.updateUVFace(mesh.geometry.attributes.uv, fIndex, element.faces[face], frame, stretch)
|
||||
})
|
||||
|
||||
}
|
||||
mesh.geometry.attributes.uv.needsUpdate = true;
|
||||
|
||||
this.dispatchEvent('update_uv', {element});
|
||||
|
||||
return mesh.geometry;
|
||||
},
|
||||
updateHighlight(element, hover_cube, force_off) {
|
||||
@ -1048,6 +1058,8 @@ new NodePreviewController(Cube, {
|
||||
mesh.geometry.attributes.highlight.array.set(Array(mesh.geometry.attributes.highlight.count).fill(highlighted));
|
||||
mesh.geometry.attributes.highlight.needsUpdate = true;
|
||||
}
|
||||
|
||||
this.dispatchEvent('update_highlight', {element});
|
||||
},
|
||||
updatePaintingGrid(cube) {
|
||||
var mesh = cube.mesh;
|
||||
@ -1163,6 +1175,8 @@ new NodePreviewController(Cube, {
|
||||
box.frustumCulled = false;
|
||||
mesh.grid_box = box;
|
||||
mesh.add(box);
|
||||
|
||||
this.dispatchEvent('update_painting_grid', {element});
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -485,9 +485,13 @@ new NodePreviewController(Group, {
|
||||
bone.name = group.uuid;
|
||||
bone.isGroup = true;
|
||||
Project.nodes_3d[group.uuid] = bone;
|
||||
|
||||
this.dispatchEvent('update_transform', {group});
|
||||
},
|
||||
updateTransform(group) {
|
||||
Canvas.updateAllBones([group]);
|
||||
|
||||
this.dispatchEvent('update_transform', {group});
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -728,6 +728,8 @@ new NodePreviewController(Mesh, {
|
||||
this.updateFaces(element);
|
||||
this.updateUV(element);
|
||||
mesh.visible = element.visibility;
|
||||
|
||||
this.dispatchEvent('setup', {element});
|
||||
},
|
||||
updateGeometry(element) {
|
||||
|
||||
@ -826,6 +828,8 @@ new NodePreviewController(Mesh, {
|
||||
if (Modes.paint) {
|
||||
Mesh.preview_controller.updatePaintingGrid(element);
|
||||
}
|
||||
|
||||
this.dispatchEvent('update_geometry', {element});
|
||||
},
|
||||
updateFaces(element) {
|
||||
let {mesh} = element;
|
||||
@ -899,6 +903,8 @@ new NodePreviewController(Mesh, {
|
||||
mesh.material = materials;
|
||||
if (!mesh.material) mesh.material = Canvas.transparentMaterial;
|
||||
}
|
||||
|
||||
this.dispatchEvent('update_faces', {element});
|
||||
},
|
||||
updateUV(element, animation = true) {
|
||||
var {mesh} = element;
|
||||
@ -920,6 +926,8 @@ new NodePreviewController(Mesh, {
|
||||
mesh.geometry.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uv_array), 2)),
|
||||
mesh.geometry.attributes.uv.needsUpdate = true;
|
||||
|
||||
this.dispatchEvent('update_uv', {element});
|
||||
|
||||
return mesh.geometry;
|
||||
},
|
||||
updateSelection(element) {
|
||||
@ -977,6 +985,8 @@ new NodePreviewController(Mesh, {
|
||||
mesh.outline.geometry.needsUpdate = true;
|
||||
|
||||
mesh.vertex_points.visible = Mode.selected.id == 'edit' && BarItems.selection_mode.value == 'vertex';
|
||||
|
||||
this.dispatchEvent('update_selection', {element});
|
||||
},
|
||||
updateHighlight(element, hover_cube, force_off) {
|
||||
var mesh = element.mesh;
|
||||
@ -1007,6 +1017,8 @@ new NodePreviewController(Mesh, {
|
||||
|
||||
mesh.geometry.attributes.highlight.array.set(array);
|
||||
mesh.geometry.attributes.highlight.needsUpdate = true;
|
||||
|
||||
this.dispatchEvent('update_highlight', {element});
|
||||
},
|
||||
updatePaintingGrid(element) {
|
||||
var mesh = element.mesh;
|
||||
@ -1072,6 +1084,8 @@ new NodePreviewController(Mesh, {
|
||||
box.frustumCulled = false;
|
||||
mesh.grid_box = box;
|
||||
mesh.add(box);
|
||||
|
||||
this.dispatchEvent('update_painting_grid', {element});
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -144,10 +144,14 @@ class NullObject extends OutlinerElement {
|
||||
setup(element) {
|
||||
NodePreviewController.prototype.setup(element);
|
||||
element.mesh.fix_position = new THREE.Vector3();
|
||||
|
||||
this.dispatchEvent('update_selection', {element});
|
||||
},
|
||||
updateTransform(element) {
|
||||
NodePreviewController.prototype.updateTransform(element);
|
||||
element.mesh.fix_position.copy(element.mesh.position);
|
||||
|
||||
this.dispatchEvent('update_transform', {element});
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -527,6 +527,7 @@ class OutlinerElement extends OutlinerNode {
|
||||
class NodePreviewController {
|
||||
constructor(type, data = {}) {
|
||||
this.type = type;
|
||||
this.events = {};
|
||||
type.preview_controller = this;
|
||||
|
||||
this.updateGeometry = null;
|
||||
@ -546,6 +547,8 @@ class NodePreviewController {
|
||||
mesh.visible = element.visibility;
|
||||
mesh.rotation.order = 'ZYX';
|
||||
this.updateTransform(element);
|
||||
|
||||
this.dispatchEvent('setup', {element});
|
||||
}
|
||||
remove(element) {
|
||||
let {mesh} = element;
|
||||
@ -558,6 +561,8 @@ class NodePreviewController {
|
||||
}
|
||||
}
|
||||
delete Project.nodes_3d[element.uuid];
|
||||
|
||||
this.dispatchEvent('remove', {element});
|
||||
}
|
||||
updateAll(element) {
|
||||
if (!element.mesh) this.setup(element);
|
||||
@ -567,6 +572,8 @@ class NodePreviewController {
|
||||
if (this.updateUV) this.updateUV(element);
|
||||
if (this.updateFaces) this.updateFaces(element);
|
||||
if (this.updatePaintingGrid) this.updatePaintingGrid(element);
|
||||
|
||||
this.dispatchEvent('update_all', {element});
|
||||
}
|
||||
updateTransform(element) {
|
||||
let mesh = element.mesh;
|
||||
@ -605,15 +612,44 @@ class NodePreviewController {
|
||||
}
|
||||
|
||||
mesh.updateMatrixWorld();
|
||||
|
||||
this.dispatchEvent('update_transform', {element});
|
||||
}
|
||||
updateVisibility(element) {
|
||||
element.mesh.visible = element.visibility;
|
||||
|
||||
this.dispatchEvent('update_visibility', {element});
|
||||
}
|
||||
updateSelection(element) {
|
||||
let {mesh} = element;
|
||||
if (mesh && mesh.outline) {
|
||||
mesh.outline.visible = element.selected
|
||||
}
|
||||
|
||||
this.dispatchEvent('update_selection', {element});
|
||||
}
|
||||
|
||||
//Events
|
||||
dispatchEvent(event_name, data) {
|
||||
if (!this.events) return;
|
||||
var list = this.events[event_name]
|
||||
if (!list) return;
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
if (typeof list[i] === 'function') {
|
||||
list[i](data)
|
||||
}
|
||||
}
|
||||
}
|
||||
on(event_name, cb) {
|
||||
if (!this.events[event_name]) {
|
||||
this.events[event_name] = []
|
||||
}
|
||||
this.events[event_name].safePush(cb)
|
||||
}
|
||||
removeListener(event_name, cb) {
|
||||
if (this.events[event_name]) {
|
||||
this.events[event_name].remove(cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
Outliner.control_menu_group = [
|
||||
|
@ -134,6 +134,8 @@ new NodePreviewController(TextureMesh, {
|
||||
this.updateGeometry(element);
|
||||
this.updateFaces(element);
|
||||
mesh.visible = element.visibility;
|
||||
|
||||
this.dispatchEvent('setup', {element});
|
||||
},
|
||||
updateGeometry(element, texture = Texture.getDefault()) {
|
||||
|
||||
@ -279,6 +281,8 @@ new NodePreviewController(TextureMesh, {
|
||||
|
||||
mesh.geometry.computeBoundingBox();
|
||||
mesh.geometry.computeBoundingSphere();
|
||||
|
||||
this.dispatchEvent('update_geometry', {element});
|
||||
},
|
||||
updateFaces(element) {
|
||||
let {mesh} = element;
|
||||
@ -299,11 +303,15 @@ new NodePreviewController(TextureMesh, {
|
||||
}
|
||||
|
||||
TextureMesh.preview_controller.updateGeometry(element);
|
||||
|
||||
this.dispatchEvent('update_faces', {element});
|
||||
},
|
||||
updateTransform(element) {
|
||||
let {mesh} = element;
|
||||
NodePreviewController.prototype.updateTransform(element);
|
||||
mesh.scale.set(1, 1, 1);
|
||||
|
||||
this.dispatchEvent('update_transform', {element});
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user