rt-sim-training-client/src/jmap/options.js

97 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-10 12:02:02 +08:00
class Options {
2019-07-25 13:24:40 +08:00
constructor(opts) {
2019-07-11 17:58:58 +08:00
this.scaleIndex = 0;
this.scaleList = [
2019-07-15 09:38:35 +08:00
0.5, 0.6, 0.7, 0.8, 0.9,
2019-07-11 17:58:58 +08:00
1, 1.2, 1.4, 1.6, 1.8,
2, 2.2, 2.4, 2.6, 2.8,
3, 3.2, 3.4, 3.6, 3.8,
4, 4.2, 4.4, 4.6, 4.8,
5, 5.2, 5.4, 5.6, 5.8,
6, 6.2, 6.4, 6.6, 6.8,
7, 7.2, 7.4, 7.6, 7.8,
8, 8.2, 8.4, 8.6, 8.8
];
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
if (Number.isFinite(opts.scaleRate)) {
const idx = this.scaleList.indexOf(opts.scaleRate);
if (idx >= 0) {
this.scaleIndex = idx;
}
}
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
this.scaleRate = opts.scaleRate || this.scaleList[this.scaleIndex]; // 缩放比例
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
this.offsetX = opts.offsetX || 0; // x偏移
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
this.offsetY = opts.offsetY || 0; // y偏移
2019-07-10 12:02:02 +08:00
2019-07-19 13:54:23 +08:00
this.throttle = opts.throttle || 100; // 刷新频率
this.disabled = false;
this.moveOnMouseMove = true;
this.zoomOnMouseWheel = false;
this.preventDefaultMouseMove = true;
2019-07-11 17:58:58 +08:00
}
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
update(payload) {
if (Number.isFinite(payload.dx)) {
this.offsetX -= payload.dx;
}
if (Number.isFinite(payload.dy)) {
this.offsetY -= payload.dy;
}
2019-07-19 11:11:34 +08:00
2019-07-11 17:58:58 +08:00
if (Number.isFinite(payload.offsetX)) {
this.offsetX = payload.offsetX;
}
if (Number.isFinite(payload.offsetY)) {
this.offsetY = payload.offsetY;
}
2019-07-19 11:11:34 +08:00
2019-07-11 17:58:58 +08:00
if (Number.isFinite(payload.scale)) {
if (Number.isFinite(payload.scale)) {
2019-07-19 11:11:34 +08:00
if ((this.scaleIndex + payload.scale) >= 0 && (this.scaleIndex + payload.scale) < this.scaleList.length) {
2019-07-11 17:58:58 +08:00
this.scaleIndex = this.scaleIndex + payload.scale;
}
}
this.scaleRate = this.scaleList[this.scaleIndex];
}
2019-07-19 11:11:34 +08:00
2019-07-11 17:58:58 +08:00
if (Number.isFinite(payload.scaleRate)) {
const idx = this.scaleList.indexOf(payload.scaleRate);
if (idx < 0) {
return;
}
this.scaleIndex = idx;
this.scaleRate = payload.scaleRate;
}
2019-07-19 13:54:23 +08:00
if (payload.disabled === true || payload.disabled === false) {
this.disabled = payload.disabled;
}
if (payload.moveOnMouseMove === true || payload.moveOnMouseMove === false) {
this.moveOnMouseMove = payload.moveOnMouseMove;
}
if (payload.zoomOnMouseWheel === true || payload.zoomOnMouseWheel === false) {
this.zoomOnMouseWheel = payload.zoomOnMouseWheel;
}
2019-07-11 17:58:58 +08:00
}
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
getScaleRate(scale) {
if (Number.isFinite(scale)) {
2019-07-19 11:11:34 +08:00
if ((this.scaleIndex + scale) >= 0 && (this.scaleIndex + scale) < this.scaleList.length) {
2019-07-11 17:58:58 +08:00
return this.scaleList[this.scaleIndex + scale];
}
}
return this.scaleList[this.scaleIndex];
}
2019-07-10 12:02:02 +08:00
}
2019-07-11 17:58:58 +08:00
export default Options;