@soonspacejs/plugin-follow-mouse 2.13.16 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Vector3 } from 'three';
2
- import SoonSpace, { IColor } from 'soonspacejs';
3
- import type { BaseObject3D } from 'soonspacejs';
2
+ import { default as SoonSpace, IColor, BaseObject3D } from 'soonspacejs';
4
3
  export interface StartOptions {
5
4
  offset?: Vector3;
6
5
  openHelper?: boolean;
package/dist/index.esm.js CHANGED
@@ -1 +1,131 @@
1
- import{Vector3 as s,Box3 as e}from"three";const t="followMouseBoxHelper";class o{constructor(t){this.ssp=t,this.mouseDownModelPosition=new s,this.object=null,this.objectBbox=new e(new s,new s),this.objectSourcePosition=new s,this.sceneMesh=[],this.helper=null,this.placedObject=[],this.options={},this.onMouseMove=this.onMouseMove.bind(this),this.onMouseDown=this.onMouseDown.bind(this),this.onClick=this.onClick.bind(this),this.onRightClick=this.onRightClick.bind(this),this.onDblClick=this.onDblClick.bind(this),this.onKeyUp=this.onKeyUp.bind(this)}start(s,e={}){this.options=e,this.object=s,this.objectBbox.setFromObject(s),this.objectSourcePosition.copy(s.position.clone()),this.sceneMesh=this.ssp.viewport.scener.intersectsList.getAll().filter(s=>{let e=s.visible;return s.traverseAncestors(s=>{!1===s.visible&&(e=!1)}),e});const{openHelper:o=!0,helperColor:i=16776960}=this.options;o&&"Model"===this.object.stype&&(s.updateWorldMatrix(!0,!0),this.helper=this.ssp.addBoxHelper({id:t,box:this.objectBbox,color:i}),this.ssp.signals.sceneChanged.dispatch()),s.traverse(s=>{const e=this.sceneMesh.findIndex(e=>e.uuid===s.uuid);-1!==e&&this.sceneMesh.splice(e,1)}),this.ssp.signals.mouseMove.add(this.onMouseMove),this.ssp.signals.mouseDown.add(this.onMouseDown),this.ssp.signals.click.add(this.onClick),this.ssp.signals.rightClick.add(this.onRightClick),this.ssp.signals.dblClick.add(this.onDblClick),this.ssp.signals.keyUp.add(this.onKeyUp)}stop(){var o;null===(o=this.object)||void 0===o||o.position.copy(this.objectSourcePosition.clone()),this.ssp.removeObjectById(t),this.object=null,this.objectBbox=new e(new s,new s),this.objectSourcePosition=new s,this.sceneMesh=[],this.helper=null,this.placedObject=[],this.options={},this.mouseDownModelPosition=new s,this.ssp.signals.mouseMove.remove(this.onMouseMove),this.ssp.signals.mouseDown.remove(this.onMouseDown),this.ssp.signals.click.remove(this.onClick),this.ssp.signals.dblClick.remove(this.onDblClick),this.ssp.signals.keyUp.remove(this.onKeyUp)}onMouseMove(t){if(t.preventDefault(),!this.object)return;const{offset:o=new s(1,1,1)}=this.options,i=this.ssp.viewport.getIntersects(t,this.sceneMesh),n=this.object.getWorldPosition(new s).sub(this.object.position),h=new s;let c="y";if(i.length>0){const{point:s,face:e}=i[0];if(h.copy(s.clone()),e){const{normal:s}=e;0===s.y&&0===s.z&&(1===s.x?c="x":-1===s.x&&(c="-x")),0===s.x&&0===s.z&&(1===s.y?c="y":-1===s.y&&(c="-y")),0===s.x&&0===s.y&&(1===s.z?c="z":-1===s.z&&(c="-z"))}}else h.copy(this.ssp.getPositionByOffset(t,.1));const l=h.sub(n),p=this.objectBbox.getSize(new s);switch(c){case"x":l.setX(l.x+p.x/2+o.x);break;case"-x":l.setX(l.x-p.x/2-o.x);break;case"y":l.setY(l.y+o.y);break;case"-y":l.setY(l.y-p.y-o.y);break;case"z":l.setZ(l.z+p.z/2+o.z);break;case"-z":l.setZ(l.z-p.z/2-o.z)}this.object.position.copy(l).setY(l.y),this.helper&&this.helper.box.copy(new e(new s,new s).setFromObject(this.object)),this.ssp.signals.sceneChanged.dispatch()}onMouseDown(e){this.object&&this.mouseDownModelPosition.copy(this.object.getWorldPosition(new s))}onClick(s){this.onPlace()}onRightClick(s){this.onDelPrev()}async onDblClick(s){await this.onPlace(),this.options.onDone&&this.options.onDone(this.placedObject),this.stop()}onKeyUp(s){switch(s.code){case"Backspace":this.onDelPrev();break;case"Enter":this.options.onDone&&this.options.onDone(this.placedObject),this.stop();break;case"Escape":this.options.onCancel&&this.options.onCancel(),this.stop()}}async onPlace(){var s,e,t,o;if("Model"===(null===(s=this.object)||void 0===s?void 0:s.stype))return this.ssp.cloneModel(this.object,{id:`clone_${this.object.sid}_${this.placedObject.length+1}`,position:this.mouseDownModelPosition.clone()}).then(s=>{var e,t;this.placedObject.push(s),null===(t=(e=this.options).onPlace)||void 0===t||t.call(e,s)});if("Poi"!==(null===(e=this.object)||void 0===e?void 0:e.stype))return Promise.reject("In soonspacejs: 插件(plugin-follow-mouse)传入对象类型错误,仅支持 sbm、model、poi !");{const s=this.ssp.clonePoi(this.object,{id:`clone_${this.object.sid}_${this.placedObject.length+1}`,position:this.mouseDownModelPosition.clone()});if(!s)return Promise.reject("In soonspacejs: 插件(plugin-follow-mouse)摆放 Poi 对象时错误!");this.placedObject.push(s),null===(o=(t=this.options).onPlace)||void 0===o||o.call(t,s)}}onDelPrev(){if(0===this.placedObject.length)return;const s=this.placedObject[this.placedObject.length-1];this.ssp.removeObject(s),this.placedObject.splice(this.placedObject.length-1,1),this.options.onBack&&this.options.onBack(s)}}export{o as default};
1
+ import { Box3 as r, Vector3 as e } from "three";
2
+ const d = "followMouseBoxHelper";
3
+ class f {
4
+ constructor(s) {
5
+ this.ssp = s, this.object = null, this.objectBbox = new r(new e(), new e()), this.objectSourcePosition = new e(), this.sceneMesh = [], this.helper = null, this.placedObject = [], this.options = {}, this.onMouseMove = this.onMouseMove.bind(this), this.onMouseDown = this.onMouseDown.bind(this), this.onClick = this.onClick.bind(this), this.onRightClick = this.onRightClick.bind(this), this.onDblClick = this.onDblClick.bind(this), this.onKeyUp = this.onKeyUp.bind(this);
6
+ }
7
+ object;
8
+ objectBbox;
9
+ objectSourcePosition;
10
+ sceneMesh;
11
+ helper;
12
+ placedObject;
13
+ options;
14
+ mouseDownModelPosition = new e();
15
+ start(s, c = {}) {
16
+ this.options = c, this.object = s, this.objectBbox.setFromObject(s), this.objectSourcePosition.copy(s.position.clone()), this.sceneMesh = this.ssp.viewport.scener.intersectsList.getAll().filter((h) => {
17
+ let n = h.visible;
18
+ return h.traverseAncestors((i) => {
19
+ i.visible === !1 && (n = !1);
20
+ }), n;
21
+ });
22
+ const {
23
+ openHelper: p = !0,
24
+ helperColor: a = 16776960
25
+ } = this.options;
26
+ p && this.object.stype === "Model" && (s.updateWorldMatrix(!0, !0), this.helper = this.ssp.addBoxHelper({
27
+ id: d,
28
+ box: this.objectBbox,
29
+ color: a
30
+ }), this.ssp.signals.sceneChanged.dispatch()), s.traverse((h) => {
31
+ const n = this.sceneMesh.findIndex((i) => i.uuid === h.uuid);
32
+ n !== -1 && this.sceneMesh.splice(n, 1);
33
+ }), this.ssp.signals.mouseMove.add(this.onMouseMove), this.ssp.signals.mouseDown.add(this.onMouseDown), this.ssp.signals.click.add(this.onClick), this.ssp.signals.rightClick.add(this.onRightClick), this.ssp.signals.dblClick.add(this.onDblClick), this.ssp.signals.keyUp.add(this.onKeyUp);
34
+ }
35
+ stop() {
36
+ this.object?.position.copy(this.objectSourcePosition.clone()), this.ssp.removeObjectById(d), this.object = null, this.objectBbox = new r(new e(), new e()), this.objectSourcePosition = new e(), this.sceneMesh = [], this.helper = null, this.placedObject = [], this.options = {}, this.mouseDownModelPosition = new e(), this.ssp.signals.mouseMove.remove(this.onMouseMove), this.ssp.signals.mouseDown.remove(this.onMouseDown), this.ssp.signals.click.remove(this.onClick), this.ssp.signals.dblClick.remove(this.onDblClick), this.ssp.signals.keyUp.remove(this.onKeyUp);
37
+ }
38
+ onMouseMove(s) {
39
+ if (s.preventDefault(), !this.object) return;
40
+ const {
41
+ offset: c = new e(1, 1, 1)
42
+ } = this.options, p = this.ssp.viewport.getIntersects(s, this.sceneMesh), h = this.object.getWorldPosition(new e()).sub(this.object.position), n = new e();
43
+ let i = "y";
44
+ if (p.length > 0) {
45
+ const { point: u, face: b } = p[0];
46
+ if (n.copy(u.clone()), b) {
47
+ const { normal: o } = b;
48
+ o.y === 0 && o.z === 0 && (o.x === 1 ? i = "x" : o.x === -1 && (i = "-x")), o.x === 0 && o.z === 0 && (o.y === 1 ? i = "y" : o.y === -1 && (i = "-y")), o.x === 0 && o.y === 0 && (o.z === 1 ? i = "z" : o.z === -1 && (i = "-z"));
49
+ }
50
+ } else
51
+ n.copy(this.ssp.getPositionByOffset(s, 0.1));
52
+ const t = n.sub(h), l = this.objectBbox.getSize(new e());
53
+ switch (i) {
54
+ case "x":
55
+ t.setX(t.x + l.x / 2 + c.x);
56
+ break;
57
+ case "-x":
58
+ t.setX(t.x - l.x / 2 - c.x);
59
+ break;
60
+ case "y":
61
+ t.setY(t.y + c.y);
62
+ break;
63
+ case "-y":
64
+ t.setY(t.y - l.y - c.y);
65
+ break;
66
+ case "z":
67
+ t.setZ(t.z + l.z / 2 + c.z);
68
+ break;
69
+ case "-z":
70
+ t.setZ(t.z - l.z / 2 - c.z);
71
+ break;
72
+ }
73
+ this.object.position.copy(t).setY(t.y), this.helper && this.helper.box.copy(new r(new e(), new e()).setFromObject(this.object)), this.ssp.signals.sceneChanged.dispatch();
74
+ }
75
+ onMouseDown(s) {
76
+ this.object && this.mouseDownModelPosition.copy(this.object.getWorldPosition(new e()));
77
+ }
78
+ onClick(s) {
79
+ this.onPlace();
80
+ }
81
+ onRightClick(s) {
82
+ this.onDelPrev();
83
+ }
84
+ async onDblClick(s) {
85
+ await this.onPlace(), this.options.onDone && this.options.onDone(this.placedObject), this.stop();
86
+ }
87
+ onKeyUp(s) {
88
+ switch (s.code) {
89
+ case "Backspace":
90
+ this.onDelPrev();
91
+ break;
92
+ case "Enter":
93
+ this.options.onDone && this.options.onDone(this.placedObject), this.stop();
94
+ break;
95
+ case "Escape":
96
+ this.options.onCancel && this.options.onCancel(), this.stop();
97
+ break;
98
+ }
99
+ }
100
+ async onPlace() {
101
+ if (this.object?.stype === "Model")
102
+ return this.ssp.cloneModel(
103
+ this.object,
104
+ {
105
+ id: `clone_${this.object.sid}_${this.placedObject.length + 1}`,
106
+ position: this.mouseDownModelPosition.clone()
107
+ }
108
+ ).then((s) => {
109
+ this.placedObject.push(s), this.options.onPlace?.(s);
110
+ });
111
+ if (this.object?.stype === "Poi") {
112
+ const s = this.ssp.clonePoi(this.object, {
113
+ id: `clone_${this.object.sid}_${this.placedObject.length + 1}`,
114
+ position: this.mouseDownModelPosition.clone()
115
+ });
116
+ if (s)
117
+ this.placedObject.push(s), this.options.onPlace?.(s);
118
+ else
119
+ return Promise.reject("In soonspacejs: 插件(plugin-follow-mouse)摆放 Poi 对象时错误!");
120
+ } else
121
+ return Promise.reject("In soonspacejs: 插件(plugin-follow-mouse)传入对象类型错误,仅支持 sbm、model、poi !");
122
+ }
123
+ onDelPrev() {
124
+ if (this.placedObject.length === 0) return;
125
+ const s = this.placedObject[this.placedObject.length - 1];
126
+ this.ssp.removeObject(s), this.placedObject.splice(this.placedObject.length - 1, 1), this.options.onBack && this.options.onBack(s);
127
+ }
128
+ }
129
+ export {
130
+ f as default
131
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@soonspacejs/plugin-follow-mouse",
3
3
  "pluginName": "FollowMousePlugin",
4
- "version": "2.13.16",
4
+ "version": "2.14.0",
5
5
  "description": "Scene object follow mouse plugin for SoonSpace.js",
6
6
  "main": "dist/index.esm.js",
7
7
  "module": "dist/index.esm.js",
@@ -13,8 +13,8 @@
13
13
  ],
14
14
  "author": "xunwei",
15
15
  "license": "UNLICENSED",
16
- "gitHead": "aaf0c46565c740277fad34bc3a3ba9fa61d48278",
16
+ "gitHead": "4c85e8b7b8ad24ccb9b42f3a1826bca377c42a6d",
17
17
  "peerDependencies": {
18
- "soonspacejs": "2.13.16"
18
+ "soonspacejs": "2.14.0"
19
19
  }
20
20
  }