import { CornerType } from "../const.js";
import {Utils} from "@uino/thing";
import { CompassComponent } from "./CompassComponent";
/**
* 地球指北针组件类,用于在场景中显示地球指北针。
* @class EarthCompassComponent
* @memberof THING
* @extends THING.CompassComponent
* @public
* @author huyang <huyang@uino.com>
* @example
* const component = new THING.EarthCompassComponent({
position: THING.CornerType.RightTop, // 选填 默认值 RightBottom 可填写 RightTop LeftTop LeftBottom
opacity: 0.5, // 不透明度
"offset": [-25, -25], // 选填 偏移值 x y
image: " `https://cdn.uino.cn/thingjs-web/uearth/images/compass.png` ",
// backgroundImage: "./assets/img/compass_bg.png",
size: 50, // 大小
});
app.root.addComponent(component, "earthCompass");
*/
class EarthCompassComponent extends CompassComponent {
/**
* 地球指北针组件类构造函数,用于在场景中显示地球指北针。
*
* @constructor
* @author Huyang
* @public
* @param {object} param 参数列表
* @param {string} param.position - 指北针的位置,可选值为 'lt'、'rt'、'lb'、'rb',分别对应左上方、右上方、左下方、右下方。
* @param {number} param.size - 指北针的大小,单位为像素。
* @param {number} param.flyTime - 点击指北针指北时的飞行时间,单位为秒。
* @param {} param
*/
constructor(param = {}) {
super(param);
this._flyTime = Utils.parseValue(param.flyTime, 1000);
}
_initCompass() {
super._initCompass();
// 优先使用element创建
if (this._element) {
this.element.onclick = () => {
this.rotateToNorth();
};
}
else {
this.app.on("mousedown", (event) => {
if (event.button === 0) {
var windowH = document.body.clientHeight;
var windowW = document.body.clientWidth;
var compassCenter = [];
switch (this.position) {
case CornerType.LeftTop:
compassCenter = [
this.offset[0] + this.size / 2,
-this.offset[1] + this.size / 2,
];
break; // lt
case CornerType.RightTop:
compassCenter = [
windowW + this.offset[0] - this.size / 2,
-this.offset[1] + this.size / 2,
];
break; // rt
case CornerType.LeftBottom:
compassCenter = [
this.offset[0] + this.size / 2,
windowH - this.offset[1] - this.size / 2,
];
break; // lb
case CornerType.RightBottom:
compassCenter = [
windowW + this.offset[0] - this.size / 2,
windowH - this.offset[1] - this.size / 2,
];
break; // rb
}
if (
(event.x - compassCenter[0]) * (event.x - compassCenter[0]) +
(event.y - compassCenter[1]) * (event.y - compassCenter[1]) <
25 * 25
) {
this.rotateToNorth();
}
}
});
}
}
/**
* @author Huyang <huyang@uino.com>
* @summary 点击指北针指北时的飞行时间
* @type {number}
*/
get flyTime() {
return this._flyTime;
}
set flyTime(val) {
this._flyTime = val;
}
rotateToNorth() {
this.app.camera.rotateToNorth(this.flyTime);
}
/**
* The z-axis world angles of the current render camera
* @returns {number}
* @private
*/
_getRotation() {
// 没有target的时候返回上一次计算结果
if (
this.app.camera.target[0] === 0 && this.app.camera.target[1] === 0 && this.app.camera.target[2] === 0) {
return this._rotation;
}
this._rotation = this.app.camera.getEarthCameraInfo().heading;
return this._rotation;
}
}
export { EarthCompassComponent };