blockbench/js/outliner/locator.js

154 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-07-18 00:02:07 +08:00
class Locator extends OutlinerElement {
2019-07-18 00:02:07 +08:00
constructor(data, uuid) {
super(data, uuid);
2020-07-16 15:32:59 +08:00
for (var key in Locator.properties) {
Locator.properties[key].reset(this);
}
2019-07-18 00:02:07 +08:00
if (data) {
this.extend(data);
}
}
2021-05-28 19:19:09 +08:00
get origin() {
return this.from;
}
2019-07-18 00:02:07 +08:00
extend(object) {
2020-07-16 15:32:59 +08:00
for (var key in Locator.properties) {
Locator.properties[key].merge(this, object)
}
2019-12-16 03:04:31 +08:00
this.sanitizeName();
2020-04-26 02:25:07 +08:00
Merge.boolean(this, object, 'locked')
2019-12-16 03:04:31 +08:00
Merge.boolean(this, object, 'export');
2019-07-18 00:02:07 +08:00
return this;
}
getUndoCopy() {
var copy = new Locator(this)
copy.uuid = this.uuid
2019-07-26 19:33:29 +08:00
copy.type = this.type;
2019-07-18 00:02:07 +08:00
delete copy.parent;
return copy;
}
getSaveCopy() {
2020-09-23 01:17:28 +08:00
let save = {};
2020-07-16 15:32:59 +08:00
for (var key in Locator.properties) {
2020-09-23 01:17:28 +08:00
Locator.properties[key].copy(this, save)
2020-07-16 15:32:59 +08:00
}
2020-09-23 01:17:28 +08:00
save.export = this.export ? undefined : false;
save.locked = this.locked;
save.uuid = this.uuid;
save.type = 'locator';
return save;
2019-07-18 00:02:07 +08:00
}
init() {
if (this.parent instanceof Group == false) {
this.addTo(Group.selected)
}
super.init();
if (!this.mesh || !this.mesh.parent) {
this.mesh = new THREE.Object3D();
Project.nodes_3d[this.uuid] = this.mesh;
this.mesh.name = this.uuid;
this.mesh.type = 'locator';
Canvas.adaptObjectPosition(this, this.mesh);
}
2019-07-18 00:02:07 +08:00
return this;
}
2019-12-16 03:04:31 +08:00
flip(axis, center) {
var offset = this.from[axis] - center
this.from[axis] = center - offset;
2021-05-28 19:19:09 +08:00
this.rotation.forEach((n, i) => {
if (i != axis) this.rotation[i] = -n;
})
2020-10-18 03:51:17 +08:00
// Name
if (axis == 0 && this.name.includes('right')) {
this.name = this.name.replace(/right/g, 'left').replace(/2$/, '');
} else if (axis == 0 && this.name.includes('left')) {
this.name = this.name.replace(/left/g, 'right').replace(/2$/, '');
}
this.createUniqueName();
2019-12-16 03:04:31 +08:00
return this;
}
2019-09-04 15:37:38 +08:00
getWorldCenter() {
2019-12-16 03:04:31 +08:00
var pos = new THREE.Vector3();
var q = new THREE.Quaternion();
if (this.parent instanceof Group) {
THREE.fastWorldPosition(this.parent.mesh, pos);
2019-12-16 03:04:31 +08:00
this.parent.mesh.getWorldQuaternion(q);
var offset2 = new THREE.Vector3().fromArray(this.parent.origin).applyQuaternion(q);
pos.sub(offset2);
}
2019-09-06 06:16:54 +08:00
var offset = new THREE.Vector3().fromArray(this.from).applyQuaternion(q);
2019-12-16 03:04:31 +08:00
pos.add(offset);
2019-09-04 15:37:38 +08:00
return pos;
}
2019-07-18 00:02:07 +08:00
}
2019-12-16 03:04:31 +08:00
Locator.prototype.title = tl('data.locator');
Locator.prototype.type = 'locator';
Locator.prototype.icon = 'fa fa-anchor';
2020-04-26 02:25:07 +08:00
Locator.prototype.name_regex = 'a-z0-9_'
2019-12-16 03:04:31 +08:00
Locator.prototype.movable = true;
2021-05-28 19:19:09 +08:00
Locator.prototype.rotatable = true;
2019-12-16 03:04:31 +08:00
Locator.prototype.visibility = true;
Locator.prototype.buttons = [
2020-12-30 02:50:02 +08:00
Outliner.buttons.export,
2020-04-26 02:25:07 +08:00
Outliner.buttons.locked,
2019-12-16 03:04:31 +08:00
];
Locator.prototype.needsUniqueName = true;
Locator.prototype.menu = new Menu([
2021-08-08 01:46:54 +08:00
{
id: 'ignore_inherited_scale',
name: 'menu.locator.ignore_inherited_scale',
icon: locator => locator.ignore_inherited_scale ? 'check_box' : 'check_box_outline_blank',
click(clicked_locator) {
let value = !clicked_locator.ignore_inherited_scale;
let affected = Locator.selected.filter(locator => locator.ignore_inherited_scale != value);
Undo.initEdit({elements: affected});
affected.forEach(locator => {
locator.ignore_inherited_scale = value;
})
Undo.finishEdit('Change locator ignore inherit scale option');
}
},
'_',
'group_elements',
'_',
2019-12-16 03:04:31 +08:00
'copy',
'paste',
'duplicate',
'_',
2019-12-16 03:04:31 +08:00
'rename',
'delete'
])
2020-07-16 15:32:59 +08:00
new Property(Locator, 'string', 'name', {default: 'locator'})
new Property(Locator, 'vector', 'from')
2021-05-28 19:19:09 +08:00
new Property(Locator, 'vector', 'rotation')
2021-08-08 01:46:54 +08:00
new Property(Locator, 'boolean', 'ignore_inherited_scale')
OutlinerElement.registerType(Locator, 'locator');
2019-07-18 00:02:07 +08:00
BARS.defineActions(function() {
2019-08-18 00:26:14 +08:00
new Action('add_locator', {
2019-07-18 00:02:07 +08:00
icon: 'fa-anchor',
category: 'edit',
condition: () => {return Format.locators && Modes.edit},
click: function () {
2019-09-06 06:16:54 +08:00
var objs = []
Undo.initEdit({elements: objs, outliner: true});
2019-12-16 03:04:31 +08:00
var locator = new Locator().addTo(Group.selected||selected[0]).init();
locator.select().createUniqueName();
objs.push(locator);
2021-06-06 15:28:22 +08:00
Undo.finishEdit('Add locator');
Vue.nextTick(function() {
if (settings.create_rename.value) {
locator.rename();
}
})
2019-07-18 00:02:07 +08:00
}
})
})