createCircle.js
8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import movePrompt from './MovePrompt';
import addCompany from './addCompany';
export default class CreateCircle {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.circle = null;
this.screenSpaceCameraController = [];
this.style = style;
this.floatPoint = null;
this.centerPoint = null;
this.center = null;
this.float = null;
this.radius = 0;
this.modifyPoint = null;
this.state = 0;
this.pointArr = [];
this.prompt = new movePrompt.movePrompt(viewer);
}
start(callBack) {
var that = this;
this.handler.setInputAction(function (evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.circle]);
if (!that.centerPoint) {
that.center = cartesian;
that.centerPoint = addCompany.createPoint.call(that, {position: cartesian});
that.centerPoint.typeAttr = "center";
that.floatPoint = addCompany.createPoint.call(that, {position: cartesian.clone()});
that.float = cartesian.clone();
that.floatPoint.typeAttr = "float";
that.circle = addCompany.createCircle.call(that);
} else {
return;
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function (evt) { //移动时绘制线
if (!that.centerPoint) {
that.prompt.updatePrompt(evt.endPosition, "单击开始绘制");
return;
}
that.prompt.updatePrompt(evt.endPosition, "右键结束");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.circle]);
if (that.floatPoint) {
that.floatPoint.position.setValue(cartesian);
that.float = cartesian.clone();
}
that.radius = Cesium.Cartesian3.distance(cartesian, that.center);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.handler.setInputAction(function () { //单机开始绘制
if (!that.circle) {
return;
}
that.state = 1;
that.handler.destroy();
if (that.floatPoint) that.floatPoint.show = false;
if (that.centerPoint) that.centerPoint.show = false;
if (that.prompt) {
that.prompt.destroy();
that.prompt = null;
}
if (callBack) callBack(that.circle);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
startModify(callback) {
if (this.state != 2 && this.state != 1) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
if (that.floatPoint) that.floatPoint.show = true;
if (that.centerPoint) that.centerPoint.show = true;
this.modifyHandler.setInputAction(function (evt) {
if (!that.circle) return;
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.modifyPoint = pick.id;
that.forbidDrawWorld(true);
} else {
if (that.floatPoint) that.floatPoint.show = false;
if (that.centerPoint) that.centerPoint.show = false;
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
if (callback) callback(that.circle);
}
that.state = 2;
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.circle]);
if (!cartesian) {
return;
}
if (that.modifyPoint.typeAttr == "center") {
that.center = cartesian
that.centerPoint.position.setValue(that.center);
that.circle.position.setValue(that.center);
} else {
that.float = cartesian
that.floatPoint.position.setValue(that.float);
}
that.radius = Cesium.Cartesian3.distance(that.float, that.center);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function (evt) {
if (!that.modifyPoint) return;
that.modifyPoint = null;
that.forbidDrawWorld(false);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
if (this.floatPoint) this.floatPoint.show = false;
if (this.centerPoint) this.centerPoint.show = false;
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
if (callback) callback(this.circle);
}
this.state = 2;
}
getCenter() {
return this.center;
}
getAttr() {
return obj;
}
setStyle(obj) {}
remove() {
if (this.circle) {
this.state = 0;
this.viewer.entities.remove(this.circle);
this.circle = null;
}
}
setVisible(vis) {
this.circle.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.circle) {
this.viewer.entities.remove(this.circle);
this.circle = null;
}
if (this.floatPoint) {
this.viewer.entities.remove(this.floatPoint);
this.floatPoint = null;
}
if (this.centerPoint) {
this.viewer.entities.remove(this.centerPoint);
this.centerPoint = null;
}
this.style = null;
this.modifyPoint = null;
if (this.prompt) this.prompt.destroy();
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}