/
SelectionIndicatorViewModel.js
213 lines (193 loc) · 6.11 KB
/
SelectionIndicatorViewModel.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import Cartesian2 from "../../Core/Cartesian2.js";
import defaultValue from "../../Core/defaultValue.js";
import defined from "../../Core/defined.js";
import DeveloperError from "../../Core/DeveloperError.js";
import EasingFunction from "../../Core/EasingFunction.js";
import SceneTransforms from "../../Scene/SceneTransforms.js";
import knockout from "../../ThirdParty/knockout.js";
var screenSpacePos = new Cartesian2();
var offScreen = "-1000px";
/**
* The view model for {@link SelectionIndicator}.
* @alias SelectionIndicatorViewModel
* @constructor
*
* @param {Scene} scene The scene instance to use for screen-space coordinate conversion.
* @param {Element} selectionIndicatorElement The element containing all elements that make up the selection indicator.
* @param {Element} container The DOM element that contains the widget.
*/
function SelectionIndicatorViewModel(
scene,
selectionIndicatorElement,
container
) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError("scene is required.");
}
if (!defined(selectionIndicatorElement)) {
throw new DeveloperError("selectionIndicatorElement is required.");
}
if (!defined(container)) {
throw new DeveloperError("container is required.");
}
//>>includeEnd('debug')
this._scene = scene;
this._screenPositionX = offScreen;
this._screenPositionY = offScreen;
this._tweens = scene.tweens;
this._container = defaultValue(container, document.body);
this._selectionIndicatorElement = selectionIndicatorElement;
this._scale = 1;
/**
* Gets or sets the world position of the object for which to display the selection indicator.
* @type {Cartesian3}
*/
this.position = undefined;
/**
* Gets or sets the visibility of the selection indicator.
* @type {Boolean}
*/
this.showSelection = false;
knockout.track(this, [
"position",
"_screenPositionX",
"_screenPositionY",
"_scale",
"showSelection",
]);
/**
* Gets the visibility of the position indicator. This can be false even if an
* object is selected, when the selected object has no position.
* @type {Boolean}
*/
this.isVisible = undefined;
knockout.defineProperty(this, "isVisible", {
get: function () {
return this.showSelection && defined(this.position);
},
});
knockout.defineProperty(this, "_transform", {
get: function () {
return "scale(" + this._scale + ")";
},
});
/**
* Gets or sets the function for converting the world position of the object to the screen space position.
*
* @member
* @type {SelectionIndicatorViewModel.ComputeScreenSpacePosition}
* @default SceneTransforms.wgs84ToWindowCoordinates
*
* @example
* selectionIndicatorViewModel.computeScreenSpacePosition = function(position, result) {
* return Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, position, result);
* };
*/
this.computeScreenSpacePosition = function (position, result) {
return SceneTransforms.wgs84ToWindowCoordinates(scene, position, result);
};
}
/**
* Updates the view of the selection indicator to match the position and content properties of the view model.
* This function should be called as part of the render loop.
*/
SelectionIndicatorViewModel.prototype.update = function () {
if (this.showSelection && defined(this.position)) {
var screenPosition = this.computeScreenSpacePosition(
this.position,
screenSpacePos
);
if (!defined(screenPosition)) {
this._screenPositionX = offScreen;
this._screenPositionY = offScreen;
} else {
var container = this._container;
var containerWidth = container.parentNode.clientWidth;
var containerHeight = container.parentNode.clientHeight;
var indicatorSize = this._selectionIndicatorElement.clientWidth;
var halfSize = indicatorSize * 0.5;
screenPosition.x =
Math.min(
Math.max(screenPosition.x, -indicatorSize),
containerWidth + indicatorSize
) - halfSize;
screenPosition.y =
Math.min(
Math.max(screenPosition.y, -indicatorSize),
containerHeight + indicatorSize
) - halfSize;
this._screenPositionX = Math.floor(screenPosition.x + 0.25) + "px";
this._screenPositionY = Math.floor(screenPosition.y + 0.25) + "px";
}
}
};
/**
* Animate the indicator to draw attention to the selection.
*/
SelectionIndicatorViewModel.prototype.animateAppear = function () {
this._tweens.addProperty({
object: this,
property: "_scale",
startValue: 2,
stopValue: 1,
duration: 0.8,
easingFunction: EasingFunction.EXPONENTIAL_OUT,
});
};
/**
* Animate the indicator to release the selection.
*/
SelectionIndicatorViewModel.prototype.animateDepart = function () {
this._tweens.addProperty({
object: this,
property: "_scale",
startValue: this._scale,
stopValue: 1.5,
duration: 0.8,
easingFunction: EasingFunction.EXPONENTIAL_OUT,
});
};
Object.defineProperties(SelectionIndicatorViewModel.prototype, {
/**
* Gets the HTML element containing the selection indicator.
* @memberof SelectionIndicatorViewModel.prototype
*
* @type {Element}
*/
container: {
get: function () {
return this._container;
},
},
/**
* Gets the HTML element that holds the selection indicator.
* @memberof SelectionIndicatorViewModel.prototype
*
* @type {Element}
*/
selectionIndicatorElement: {
get: function () {
return this._selectionIndicatorElement;
},
},
/**
* Gets the scene being used.
* @memberof SelectionIndicatorViewModel.prototype
*
* @type {Scene}
*/
scene: {
get: function () {
return this._scene;
},
},
});
/**
* A function that converts the world position of an object to a screen space position.
* @callback SelectionIndicatorViewModel.ComputeScreenSpacePosition
* @param {Cartesian3} position The position in WGS84 (world) coordinates.
* @param {Cartesian2} result An object to return the input position transformed to window coordinates.
* @returns {Cartesian2} The modified result parameter.
*/
export default SelectionIndicatorViewModel;