blockbench/js/outliner/locator.js

117 lines
2.9 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);
}
}
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();
return this;
}
2019-12-16 03:04:31 +08:00
flip(axis, center) {
var offset = this.from[axis] - center
this.from[axis] = center - offset;
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;
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([
'copy',
'rename',
'delete'
])
Locator.selected = [];
Locator.all = [];
2020-07-16 15:32:59 +08:00
new Property(Locator, 'string', 'name', {default: 'locator'})
new Property(Locator, 'vector', 'from')
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);
2019-07-18 00:02:07 +08:00
Undo.finishEdit('add locator');
Vue.nextTick(function() {
if (settings.create_rename.value) {
locator.rename();
}
})
2019-07-18 00:02:07 +08:00
}
})
})