2020-12-16 16:54:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 James Lyne
|
|
|
|
*
|
|
|
|
* Some portions of this file were taken from https://github.com/webbukkit/dynmap.
|
|
|
|
* These portions are Copyright 2020 Dynmap Contributors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-18 20:25:34 +00:00
|
|
|
import {LatLngExpression} from "leaflet";
|
2021-07-23 21:28:06 +00:00
|
|
|
import LiveAtlasPolyline from "@/leaflet/vector/LiveAtlasPolyline";
|
|
|
|
import LiveAtlasPolygon from "@/leaflet/vector/LiveAtlasPolygon";
|
2021-07-24 03:06:19 +00:00
|
|
|
import {LiveAtlasCircle} from "@/index";
|
2020-12-11 15:28:51 +00:00
|
|
|
|
2021-07-24 03:06:19 +00:00
|
|
|
export const createCircle = (options: LiveAtlasCircle, converter: Function): LiveAtlasPolyline | LiveAtlasPolygon => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const outline = !options.style.fillOpacity || (options.style.fillOpacity <= 0),
|
2020-12-18 20:25:34 +00:00
|
|
|
points = getCirclePoints(options, converter, outline),
|
2021-07-23 21:28:06 +00:00
|
|
|
circle = outline ? new LiveAtlasPolyline(points, {
|
2020-12-18 20:25:34 +00:00
|
|
|
...options.style,
|
|
|
|
minZoom: options.minZoom,
|
|
|
|
maxZoom: options.maxZoom,
|
2021-07-23 21:28:06 +00:00
|
|
|
}) : new LiveAtlasPolygon(points, {
|
2020-12-18 20:25:34 +00:00
|
|
|
...options.style,
|
|
|
|
minZoom: options.minZoom,
|
|
|
|
maxZoom: options.maxZoom,
|
|
|
|
});
|
2020-12-11 15:28:51 +00:00
|
|
|
|
|
|
|
if(options.label) {
|
|
|
|
circle.bindPopup(() => createPopup(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
return circle;
|
|
|
|
};
|
|
|
|
|
2021-07-24 03:06:19 +00:00
|
|
|
export const updateCircle = (circle: LiveAtlasPolyline | LiveAtlasPolygon | undefined, options: LiveAtlasCircle, converter: Function): LiveAtlasPolyline | LiveAtlasPolygon => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const outline = (options.style && options.style.fillOpacity && (options.style.fillOpacity <= 0)) as boolean,
|
|
|
|
points = getCirclePoints(options, converter, outline);
|
|
|
|
|
|
|
|
if (!circle) {
|
|
|
|
return createCircle(options, converter);
|
|
|
|
}
|
|
|
|
|
2021-01-26 14:42:31 +00:00
|
|
|
circle.closePopup();
|
2020-12-11 15:28:51 +00:00
|
|
|
circle.unbindPopup();
|
|
|
|
circle.bindPopup(() => createPopup(options));
|
|
|
|
circle.setStyle(options.style);
|
|
|
|
circle.setLatLngs(points);
|
|
|
|
circle.redraw();
|
|
|
|
|
|
|
|
return circle;
|
|
|
|
}
|
|
|
|
|
2021-07-24 03:06:19 +00:00
|
|
|
export const createPopup = (options: LiveAtlasCircle) => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const popup = document.createElement('span');
|
|
|
|
|
|
|
|
if (options.popupContent) {
|
|
|
|
popup.classList.add('CirclePopup');
|
|
|
|
popup.insertAdjacentHTML('afterbegin', options.popupContent);
|
|
|
|
} else if (options.isHTML) {
|
|
|
|
popup.classList.add('CirclePopup');
|
|
|
|
popup.insertAdjacentHTML('afterbegin', options.label);
|
|
|
|
} else {
|
|
|
|
popup.textContent = options.label;
|
|
|
|
}
|
|
|
|
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2021-07-24 03:06:19 +00:00
|
|
|
export const getCirclePoints = (options: LiveAtlasCircle, converter: Function, outline: boolean): LatLngExpression[] => {
|
2020-12-11 15:28:51 +00:00
|
|
|
const points = [];
|
|
|
|
|
|
|
|
for(let i = 0; i < 360; i++) {
|
|
|
|
const rad = i * Math.PI / 180.0,
|
|
|
|
x = options.radius[0] * Math.sin(rad) + options.location.x,
|
|
|
|
z = options.radius[1] * Math.cos(rad) + options.location.z;
|
|
|
|
|
|
|
|
points.push(converter(x, options.location.y, z));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(outline && points.length) {
|
|
|
|
points.push(points[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
};
|