| | |
| | | import * as PIXI from 'pixi.js'; |
| | | import * as TWEEDLE from 'tweedle.js'; |
| | | import { |
| | | MAP_DEFAULT_ROTATION, |
| | | MAP_MIRROR, |
| | | DEVICE_TYPE, |
| | | DEVICE_Z_INDEX, |
| | | AGV_STATUS_MODE, |
| | | DEVICE_SPRITE_TINT, |
| | | DEVICE_SPRITE_TINT_DARK, |
| | | DEVICE_SELECTED_EFFECT_PADDING, |
| | | DEVICE_SELECTED_EFFECT_COLOR, |
| | | POINT_ROUTE_DIRECTION, |
| | | DEVICE_SPRITE_TINT_DARK, |
| | | ANIMATE_DURING_TIME, |
| | | AGV_ANGLE_OFFSET_VAL, |
| | | } from './constants'; |
| | | import { getRouteList } from './http'; |
| | | import { getRouteList, fetchAreaList } from './http'; |
| | | import PointRoute from "./PointRoute"; |
| | | |
| | | import shelf from '/map/shelf.svg'; |
| | |
| | | import ctuLoadedFullBattery from '/map/agv/ctuLoadedFullBattery.svg'; |
| | | |
| | | let app, mapContainer, themeMode; |
| | | let selectedSprite, effectCircle, effectTicker; |
| | | let selectedSprite, effectOverlay, effectTicker; |
| | | let tooltip; |
| | | |
| | | export function getApp() { |
| | |
| | | export function setThemeMode(param) { |
| | | themeMode = param; |
| | | if (mapContainer) { |
| | | // mapContainer.children.forEach(child => { |
| | | // const deviceType = child.data?.type; |
| | | // if (deviceType) { |
| | | // if (themeMode === 'dark') { |
| | | // const tint = DEVICE_SPRITE_TINT_DARK[deviceType] |
| | | // if (tint) { |
| | | // child.tint = tint; |
| | | // } |
| | | // } else { |
| | | // DEVICE_SPRITE_TINT[deviceType] != null && (child.tint = DEVICE_SPRITE_TINT[deviceType]); |
| | | // } |
| | | // } |
| | | // }) |
| | | mapContainer.children.forEach(child => { |
| | | const deviceType = child.data?.type; |
| | | if (deviceType) { |
| | | if (themeMode === 'dark') { |
| | | DEVICE_SPRITE_TINT_DARK[deviceType] != null && (child.tint = DEVICE_SPRITE_TINT_DARK[deviceType]); |
| | | } else { |
| | | DEVICE_SPRITE_TINT[deviceType] != null && (child.tint = DEVICE_SPRITE_TINT[deviceType]); |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | } |
| | | |
| | | |
| | | export const getRealPosition = (x, y) => { |
| | | const rect = app.view.getBoundingClientRect(); |
| | | return { |
| | | mapX: (x - rect.left) / mapContainer.scale.x - mapContainer.x / mapContainer.scale.x, |
| | | mapY: (y - rect.top) / mapContainer.scale.y - mapContainer.y / mapContainer.scale.y |
| | | } |
| | | const globalPoint = new PIXI.Point(); |
| | | app.renderer.plugins.interaction.mapPositionToPoint(globalPoint, x, y); |
| | | const mapPoint = mapContainer.toLocal(globalPoint); |
| | | return { mapX: mapPoint.x, mapY: mapPoint.y }; |
| | | } |
| | | |
| | | export const generateSprite = (deviceType, source) => { |
| | | export const generateSprite = (deviceType, texture) => { |
| | | let sprite; |
| | | switch (deviceType) { |
| | | case DEVICE_TYPE.SHELF: |
| | |
| | | |
| | | |
| | | case DEVICE_TYPE.AGV: |
| | | if (source) { |
| | | sprite = new PIXI.Sprite(PIXI.Texture.from(source, { resourceOptions: { scale: 1 } })); |
| | | if (texture) { |
| | | sprite = new PIXI.Sprite(PIXI.Texture.from(texture, { resourceOptions: { scale: 1 } })); |
| | | } |
| | | sprite.width = 300; |
| | | sprite.height = 500; |
| | | sprite.width = 750; |
| | | sprite.height = 1300; |
| | | sprite.zIndex = DEVICE_Z_INDEX.AGV; |
| | | break |
| | | case DEVICE_TYPE.POINT: |
| | |
| | | let batchMoveStartPos = null; |
| | | |
| | | const batchMoving = (event) => { |
| | | const scale = mapContainer.scale.x; |
| | | const scaleX = mapContainer.scale.x; |
| | | const scaleY = mapContainer.scale.y; |
| | | if (batchMove && batchMoveStartPos) { |
| | | // offset move val |
| | | var mouseMovement = { |
| | | x: (event.global.x - batchMoveStartPos.x) / scale, |
| | | y: (event.global.y - batchMoveStartPos.y) / scale |
| | | x: (event.global.x - batchMoveStartPos.x) / scaleX, |
| | | y: (event.global.y - batchMoveStartPos.y) / scaleY |
| | | }; |
| | | for (let sprite of selectedSprites) { |
| | | sprite.position.x = sprite.batchMoveStartPos.x + mouseMovement.x; |
| | | sprite.position.y = sprite.batchMoveStartPos.y + mouseMovement.y; |
| | | sprite.position.x = sprite.batchMoveStartPos.x - mouseMovement.x; |
| | | sprite.position.y = sprite.batchMoveStartPos.y - mouseMovement.y; |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | function onSpriteMouseOver(event) { |
| | | if (tooltip) { |
| | | app.stage.removeChild(tooltip); |
| | | app?.stage.removeChild(tooltip); |
| | | } |
| | | tooltip = createSpriteTooltip(this);// this => sprite |
| | | tooltip.x = event.data.global.x + 10; |
| | |
| | | |
| | | function onSpriteMouseOut() { |
| | | if (tooltip && tooltip.parent) { |
| | | tooltip.parent.removeChild(tooltip); |
| | | tooltip.parent?.removeChild(tooltip); |
| | | tooltip = null; |
| | | } |
| | | } |
| | |
| | | return; |
| | | } |
| | | |
| | | mapContainer.scale.set(0.3); |
| | | const focusScale = 0.1; |
| | | mapContainer.scale.set(MAP_MIRROR ? -focusScale : focusScale, focusScale); |
| | | mapContainer.position.set(0, 0); |
| | | |
| | | let bounds = sprite.getBounds(); |
| | |
| | | } |
| | | |
| | | export const showSelectedEffect = (sprite) => { |
| | | if (!sprite) { |
| | | return; |
| | | } |
| | | // area |
| | | if (sprite.data?.type === DEVICE_TYPE.AREA) { |
| | | const { start, end } = sprite.data || {}; |
| | | const hasBounds = start && end; |
| | | const minX = hasBounds ? Math.min(start.x, end.x) : 0; |
| | | const maxX = hasBounds ? Math.max(start.x, end.x) : 0; |
| | | const minY = hasBounds ? Math.min(start.y, end.y) : 0; |
| | | const maxY = hasBounds ? Math.max(start.y, end.y) : 0; |
| | | const width = hasBounds ? Math.abs(maxX - minX) : Math.abs(sprite.width); |
| | | const height = hasBounds ? Math.abs(maxY - minY) : Math.abs(sprite.height); |
| | | const centerX = hasBounds ? (minX + maxX) / 2 : sprite.x; |
| | | const centerY = hasBounds ? (minY + maxY) / 2 : sprite.y; |
| | | const color = DEVICE_SELECTED_EFFECT_COLOR[DEVICE_TYPE.AREA] || (themeMode === 'light' ? '#747d8c' : '#718093'); |
| | | const lineWidth = Math.max(2, 4 / Math.abs(mapContainer.scale.x || 1)); |
| | | |
| | | effectOverlay = new PIXI.Graphics(); |
| | | effectOverlay.lineStyle(lineWidth, color, 1); |
| | | effectOverlay.drawRect(-width / 2, -height / 2, width, height); |
| | | effectOverlay.position.set(centerX, centerY); |
| | | effectOverlay.zIndex = sprite.zIndex + 1; |
| | | effectOverlay.blendMode = PIXI.BLEND_MODES.NORMAL; |
| | | mapContainer.addChild(effectOverlay); |
| | | |
| | | selectedSprite = sprite; |
| | | effectTicker = null; |
| | | return; |
| | | } |
| | | |
| | | // others |
| | | if (!sprite?.texture || !sprite?.texture?.valid) { |
| | | return; |
| | | } |
| | |
| | | |
| | | const alpha = 1; |
| | | |
| | | effectCircle = new PIXI.Graphics(); |
| | | effectCircle.beginFill(color, alpha); |
| | | effectCircle.drawCircle(0, 0, radius); |
| | | effectCircle.endFill(); |
| | | effectCircle.position.set(sprite.x, sprite.y); |
| | | effectCircle.zIndex = -1; |
| | | effectCircle.blendMode = PIXI.BLEND_MODES.NORMAL; |
| | | effectOverlay = new PIXI.Graphics(); |
| | | effectOverlay.beginFill(color, alpha); |
| | | effectOverlay.drawCircle(0, 0, radius); |
| | | effectOverlay.endFill(); |
| | | effectOverlay.position.set(sprite.x, sprite.y); |
| | | effectOverlay.zIndex = -1; |
| | | effectOverlay.blendMode = PIXI.BLEND_MODES.NORMAL; |
| | | |
| | | mapContainer.addChild(effectCircle); |
| | | mapContainer.addChild(effectOverlay); |
| | | |
| | | selectedSprite = sprite; |
| | | |
| | |
| | | scalingUp = true; |
| | | } |
| | | } |
| | | effectCircle.scale.set(pulseScale); |
| | | effectCircle.position.set(sprite.x, sprite.y); |
| | | effectOverlay.scale.set(pulseScale); |
| | | effectOverlay.position.set(sprite.x, sprite.y); |
| | | }; |
| | | |
| | | app.ticker.add(effectTicker); |
| | |
| | | app.ticker.remove(effectTicker); |
| | | effectTicker = null; |
| | | } |
| | | if (effectCircle) { |
| | | mapContainer.removeChild(effectCircle); |
| | | effectCircle = null; |
| | | if (effectOverlay) { |
| | | mapContainer?.removeChild(effectOverlay); |
| | | effectOverlay = null; |
| | | } |
| | | selectedSprite = null; |
| | | }; |
| | | |
| | | export const updateEffect = () => { |
| | | if (!selectedSprite || !effectCircle) { |
| | | return; |
| | | } |
| | | effectCircle.position.set(selectedSprite.x, selectedSprite.y); |
| | | }; |
| | | |
| | | export const showRoutes = (curZone, setShowRoutes) => { |
| | | setShowRoutes(true); |
| | | getRouteList(curZone, (routeList) => { |
| | | routeList.forEach(route => { |
| | | const startPoint = querySprite(DEVICE_TYPE.POINT, route.startCodeStr); |
| | | const endPoint = querySprite(DEVICE_TYPE.POINT, route.endCodeStr); |
| | | const pointRoute = new PointRoute(POINT_ROUTE_DIRECTION[route.direction]); |
| | | pointRoute.setPoint(startPoint, endPoint); |
| | | pointRoute.clear(); |
| | | pointRoute.lineStyle(Math.max(7, 2 * (1 / mapContainer.scale.x)), themeMode === 'light' ? '#ced6e0' : '#535c68'); |
| | | pointRoute.moveTo(startPoint.position.x, startPoint.position.y); |
| | | pointRoute.lineTo(endPoint.position.x, endPoint.position.y); |
| | | pointRoute.alpha = 1; |
| | | mapContainer.addChild(pointRoute); |
| | | }) |
| | | }) |
| | | export const showRoutes = (curZone, setShowRoutes, setLoading) => { |
| | | setLoading(true); |
| | | getRouteList( |
| | | curZone, |
| | | (routeList) => { |
| | | routeList.forEach(route => { |
| | | const startPoint = querySprite(DEVICE_TYPE.POINT, route.startCodeStr); |
| | | const endPoint = querySprite(DEVICE_TYPE.POINT, route.endCodeStr); |
| | | const pointRoute = new PointRoute(POINT_ROUTE_DIRECTION[route.direction]); |
| | | pointRoute.setPoint(startPoint, endPoint); |
| | | pointRoute.clear(); |
| | | pointRoute.lineStyle(Math.max(7, 2 * (1 / Math.abs(mapContainer.scale.x))), themeMode === 'light' ? '#ced6e0' : '#535c68'); |
| | | pointRoute.moveTo(startPoint.position.x, startPoint.position.y); |
| | | pointRoute.lineTo(endPoint.position.x, endPoint.position.y); |
| | | pointRoute.alpha = 1; |
| | | mapContainer.addChild(pointRoute); |
| | | }) |
| | | setLoading(false); |
| | | setShowRoutes(true); |
| | | }, |
| | | () => { |
| | | setLoading(false); |
| | | }, |
| | | ) |
| | | }; |
| | | |
| | | export const hideRoutes = (curZone, setShowRoutes) => { |
| | |
| | | } |
| | | |
| | | export const rotationParseNum = (num) => { |
| | | return num * Math.PI / 180; |
| | | return num * (Math.PI / 180); |
| | | } |
| | | |
| | | const animateRotation = (sprite, targetRotation, rotationOffset = 0) => { |
| | | if (!mapContainer || !sprite) { |
| | | return; |
| | | } |
| | | // origin |
| | | let currentRotation = sprite.rotation + rotationOffset; |
| | | // target |
| | | targetRotation += rotationOffset; |
| | | |
| | | // diff |
| | | let rotationDifference = targetRotation - currentRotation; |
| | | if (rotationDifference === 0) { |
| | | return; |
| | | } |
| | | rotationDifference = ((rotationDifference + Math.PI) % (2 * Math.PI)) - Math.PI; |
| | | |
| | | // destination |
| | | const endRotation = sprite.rotation + rotationDifference; |
| | | |
| | | new TWEEDLE.Tween(sprite) |
| | | .to({ rotation: endRotation }, ANIMATE_DURING_TIME) |
| | | .easing(TWEEDLE.Easing.Linear.None) |
| | | .onUpdate(() => { |
| | | // agv |
| | | if (sprite.updateTextRotation) { |
| | | sprite.updateTextRotation(); |
| | | } |
| | | }) |
| | | .start(); |
| | | }; |
| | | |
| | | export const incrementSpriteNo = (str, incrementValue) => { |
| | | const match = str.match(/(\D*)(\d+)/); |
| | |
| | | parentElement.style.paddingRight = param; |
| | | } |
| | | } |
| | | |
| | | |
| | | // service ------------------------ |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | export function createAgvJobPanel(parentContainer, text) { |
| | | const panelScale = 4; |
| | | |
| | | const panel = new PIXI.Container(); |
| | | |
| | | const sideLen = 50 * panelScale; |
| | | const halfCircle = new PIXI.Graphics(); |
| | | halfCircle.beginFill(themeMode === 'light' ? '#333333' : '#eee'); |
| | | halfCircle.lineStyle(2 * panelScale, themeMode === 'light' ? '#333333' : '#eee'); |
| | | halfCircle.arc(0, 0, sideLen, 0, Math.PI); |
| | | halfCircle.endFill(); |
| | | |
| | | const rectangle = new PIXI.Graphics(); |
| | | rectangle.lineStyle(2 * panelScale, themeMode === 'light' ? '#333333' : '#eee', .8); |
| | | rectangle.drawRoundedRect( |
| | | -sideLen / 2, // 左上角 x |
| | | -sideLen / 2, // 左上角 y |
| | | sideLen, // 宽 |
| | | sideLen - (20 * panelScale), // 高 (留一点空间,看起来更像半圆容器) |
| | | 16 * panelScale // 圆角 |
| | | ); |
| | | rectangle.endFill(); |
| | | rectangle.mask = halfCircle; |
| | | |
| | | panel.addChild(rectangle, halfCircle); |
| | | |
| | | const msg = new PIXI.Text(text, { |
| | | fill: themeMode === 'light' ? '#2b2b2b' : '#eee', |
| | | fontSize: 12 * panelScale, |
| | | fontFamily: 'MicrosoftYaHei', |
| | | fontWeight: 'bolder', |
| | | align: 'center', |
| | | }); |
| | | msg.anchor.set(0.5); |
| | | msg.position.set(0, -10 * panelScale); |
| | | |
| | | panel.addChild(msg); |
| | | parentContainer.addChild(panel); |
| | | |
| | | let phase = 0; |
| | | function onTick(delta) { |
| | | phase += delta / 6; // 旋转速度可调 |
| | | phase %= (Math.PI * 2); |
| | | halfCircle.rotation = phase; |
| | | } |
| | | app.ticker.add(onTick); |
| | | |
| | | panel._onTick = onTick; |
| | | return panel; |
| | | } |
| | | |
| | | // dynamic graphic ---------------- |
| | | |
| | | export const generateDynamicGraphic = (curZone, data, setCurSprite) => { |
| | | // console.log("ws", curZone, data); |
| | | console.log(JSON.stringify(data)); |
| | | for (const agvVo of data.agvVos) { |
| | | showAgvSprite(curZone, agvVo, setCurSprite); |
| | | drawerAgvPath(curZone, agvVo); |
| | | } |
| | | |
| | | } |
| | | |
| | | const agvRotationOffset = rotationParseNum(AGV_ANGLE_OFFSET_VAL); |
| | | |
| | | const showAgvSprite = (curZone, agvVo, setCurSprite) => { |
| | | const { agvNo, code, direction, backpack, dynamicRoute, battery, ...rest } = agvVo; |
| | | if (!code) { return } |
| | | const { agvNo, code, direction, battery, jobType, backpack: backpackCount, slots, error } = agvVo; |
| | | if (!code) { return; } |
| | | const codeSprite = querySprite(DEVICE_TYPE.POINT, code); |
| | | if (!codeSprite) { return } |
| | | if (!codeSprite) { return; } |
| | | |
| | | const targetRotation = rotationParseNum(direction); |
| | | // const backpackCount = backpack?.filter(item => item.loaded === true).length || 0; |
| | | |
| | | let agvSprite = querySprite(DEVICE_TYPE.AGV, agvNo); |
| | | if (!agvSprite) { |
| | | agvSprite = generateSprite(DEVICE_TYPE.AGV, |
| | | // generateAgvSpriteTexture(getAgvStatusMode(backpack.filter(item => item.loaded === true).length, battery)); |
| | | generateAgvSpriteTexture(getAgvStatusMode(1, battery)) |
| | | ); |
| | | const agvStatusMode = getAgvStatusMode(backpackCount, battery); |
| | | const agvTexture = generateAgvSpriteTexture(agvStatusMode); |
| | | agvSprite = generateSprite(DEVICE_TYPE.AGV, agvTexture); |
| | | |
| | | initSprite(agvSprite, DEVICE_TYPE.AGV); |
| | | |
| | | agvSprite.data.no = agvNo; |
| | | agvSprite.data.backpackCount = backpackCount; |
| | | agvSprite.data.battery = battery; |
| | | |
| | | agvSprite.position.set(codeSprite.position.x, codeSprite.position.y); |
| | | agvSprite.rotation = rotationParseNum(direction); |
| | | agvSprite.rotation = targetRotation + agvRotationOffset; |
| | | mapContainer.addChild(agvSprite); |
| | | beInsight(agvSprite, setCurSprite); |
| | | // agv no on sprite |
| | | |
| | | // agvNo sprite |
| | | const agvText = new PIXI.Text(agvNo.toString(), { |
| | | fontSize: 50, |
| | | fill: 0x000000, |
| | | }); |
| | | agvText.anchor.set(0.5, 0.5); |
| | | agvText.position.set(0, 8); |
| | | agvText.scale.set(MAP_MIRROR ? -1 : 1, 1) |
| | | agvSprite.addChild(agvText); |
| | | |
| | | agvSprite.updateTextRotation = () => { |
| | | if (agvText && agvSprite) { |
| | | agvText.rotation = -agvSprite.rotation - rotationParseNum(MAP_DEFAULT_ROTATION); |
| | | } |
| | | }; |
| | | agvSprite.updateTextRotation(); |
| | | |
| | | } else { |
| | | const prevBackpackCount = agvSprite.data.backpackCount; |
| | | const prevBattery = agvSprite.data.battery; |
| | | |
| | | if (backpackCount !== prevBackpackCount || battery !== prevBattery) { |
| | | const agvStatusMode = getAgvStatusMode(backpackCount, battery); |
| | | const agvTexture = generateAgvSpriteTexture(agvStatusMode); |
| | | agvSprite.texture = PIXI.Texture.from(agvTexture, { resourceOptions: { scale: 1 } }); |
| | | // update backpackCount and battery |
| | | agvSprite.data.backpackCount = backpackCount; |
| | | agvSprite.data.battery = battery; |
| | | } |
| | | |
| | | animateRotation(agvSprite, targetRotation + agvRotationOffset, agvRotationOffset); |
| | | } |
| | | |
| | | new TWEEDLE.Tween(agvSprite?.position).easing(TWEEDLE.Easing.Linear.None).to({ |
| | | x: codeSprite.position.x, |
| | | y: codeSprite.position.y |
| | | }, 1000).onUpdate(() => { |
| | | updateEffect(agvSprite); |
| | | }).start(); |
| | | // job effect |
| | | if (jobType) { |
| | | if (!agvSprite.data.jobEffect) { |
| | | agvSprite.data.jobEffect = createAgvJobPanel(agvSprite, jobType + " (" + backpackCount + "/" + slots + ")"); |
| | | agvSprite.data.jobEffect.x = -80; |
| | | agvSprite.data.jobEffect.y = 0; |
| | | } |
| | | if (agvSprite.data.jobEffect) { |
| | | agvSprite.data.jobEffect.rotation = -agvSprite.rotation - rotationParseNum(MAP_DEFAULT_ROTATION); |
| | | } |
| | | } else { |
| | | if (agvSprite.data.jobEffect) { |
| | | app.ticker.remove(agvSprite.data.jobEffect._onTick); |
| | | agvSprite.removeChild(agvSprite.data.jobEffect); |
| | | agvSprite.data.jobEffect.destroy(true); |
| | | agvSprite.data.jobEffect = null; |
| | | } |
| | | } |
| | | |
| | | if (error) { |
| | | agvSprite.tint = 0xff3f34; |
| | | } else { |
| | | agvSprite.tint = themeMode === 'dark' |
| | | ? DEVICE_SPRITE_TINT_DARK[DEVICE_TYPE.AGV] |
| | | : DEVICE_SPRITE_TINT[DEVICE_TYPE.AGV]; |
| | | } |
| | | |
| | | beInsight(agvSprite, setCurSprite); |
| | | |
| | | new TWEEDLE.Tween(agvSprite.position) |
| | | .to({ |
| | | x: codeSprite.position.x, |
| | | y: codeSprite.position.y |
| | | }, ANIMATE_DURING_TIME) |
| | | .easing(TWEEDLE.Easing.Linear.None) |
| | | .start(); |
| | | } |
| | | |
| | | const drawerAgvPath = (curZone, agvVo) => { |
| | |
| | | return; |
| | | } |
| | | const { agvNo, code: curCode, dynamicRoute } = agvVo; |
| | | // if (dynamicRoute?.length <= 1) { |
| | | // return; |
| | | // } |
| | | if (!dynamicRoute || dynamicRoute.indexOf(curCode) === -1) { |
| | | return; |
| | | } |
| | | |
| | | const agvPathName = 'agvPath-' + agvNo; |
| | | let agvPath = mapContainer.getChildByName(agvPathName); |
| | |
| | | } |
| | | agvPath = new PIXI.Graphics(); |
| | | agvPath.name = agvPathName; |
| | | agvPath.lineStyle(Math.max(20, 4 * (1 / mapContainer.scale.x)), 0x2f68ac, 0.8); |
| | | agvPath.lineStyle( |
| | | Math.max(200, 4 * (1 / mapContainer?.scale.x || 1)), |
| | | themeMode === 'dark' ? 0x40739e : 0x2f68ac, |
| | | 0.8, |
| | | ); |
| | | agvPath.zIndex = DEVICE_Z_INDEX.DYNAMIC_ROUTE; |
| | | agvPath.blendMode = PIXI.BLEND_MODES.NORMAL; |
| | | // agvPath.blendMode = PIXI.BLEND_MODES.NORMAL; |
| | | |
| | | let firstNode = true; |
| | | for (let i = Math.max(0, dynamicRoute.indexOf(curCode)); i < dynamicRoute.length; i++) { |
| | |
| | | } |
| | | |
| | | mapContainer.addChild(agvPath); |
| | | } |
| | | } |
| | | |
| | | export const removeAgvGraphics = () => { |
| | | if (!mapContainer) { |
| | | return; |
| | | } |
| | | for (let i = mapContainer.children.length - 1; i >= 0; i--) { |
| | | const child = mapContainer.children[i]; |
| | | if (child?.data?.type === DEVICE_TYPE.AGV) { |
| | | if (child.data.jobEffect) { |
| | | app?.ticker.remove(child.data.jobEffect._onTick); |
| | | child.removeChild(child.data.jobEffect); |
| | | child.data.jobEffect.destroy(true); |
| | | child.data.jobEffect = null; |
| | | } |
| | | mapContainer.removeChild(child); |
| | | child.destroy({ children: true, texture: false, baseTexture: false }); |
| | | } else if (child?.name?.startsWith('agvPath-')) { |
| | | mapContainer.removeChild(child); |
| | | child.destroy({ children: true, texture: false, baseTexture: false }); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | // area operator ------------------------------- |
| | | |
| | | let areaDrawingCleanup = null; |
| | | const AREA_COLOR = 0x3498db; |
| | | |
| | | const addAreaLabel = (draft, text, from, to) => { |
| | | const centerX = (from.x + to.x) / 2; |
| | | const centerY = (from.y + to.y) / 2; |
| | | const label = new PIXI.Text(text, { |
| | | fill: themeMode === 'dark' ? '#f1f2f6' : '#535353ff', |
| | | fontSize: 20 / Math.abs(mapContainer.scale.x || 1), |
| | | fontWeight: 'bold', |
| | | }); |
| | | label.anchor.set(0.5); |
| | | label.position.set(centerX, centerY); |
| | | label.rotation = rotationParseNum(MAP_DEFAULT_ROTATION); |
| | | label.scale.set(MAP_MIRROR ? -1 : 1, 1); |
| | | draft.addChild(label); |
| | | }; |
| | | |
| | | export const loadAreas = (curZone, setCurSprite) => { |
| | | if (!mapContainer) return; |
| | | clearAreas(); |
| | | fetchAreaList(curZone).then((areas) => { |
| | | areas.forEach((area) => { |
| | | const { name, color, id } = area || {}; |
| | | const start = area?.start; |
| | | const end = area?.end; |
| | | if (!start || !end || !name || !id) { |
| | | return; |
| | | } |
| | | const graphics = createAreaGraphic({ name, start, end, color, id }, setCurSprite); |
| | | if (graphics) { |
| | | mapContainer.addChild(graphics); |
| | | } |
| | | }); |
| | | }); |
| | | }; |
| | | |
| | | export const clearAreas = () => { |
| | | if (!mapContainer) return; |
| | | for (let i = mapContainer.children.length - 1; i >= 0; i--) { |
| | | const child = mapContainer.children[i]; |
| | | if (child?.data?.type === DEVICE_TYPE.AREA) { |
| | | mapContainer.removeChild(child); |
| | | child.destroy({ children: true, texture: false, baseTexture: false }); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | const createAreaGraphic = ({ name, start, end, color, id }, setCurSprite) => { |
| | | if (!mapContainer) return null; |
| | | const from = start || { x: 0, y: 0 }; |
| | | const to = end || { x: 0, y: 0 }; |
| | | const areaColor = color || AREA_COLOR; |
| | | |
| | | const draft = new PIXI.Graphics(); |
| | | draft.name = id ? `area_${id}` : 'area_' + generateID(); |
| | | draft.zIndex = DEVICE_Z_INDEX.AREA; |
| | | draft.lineStyle(2 / Math.abs(mapContainer.scale.x || 1), 0x6c7a89, 0.9); |
| | | draft.beginFill(areaColor, 0.18); |
| | | draft.drawRect( |
| | | Math.min(from.x, to.x), |
| | | Math.min(from.y, to.y), |
| | | Math.abs(from.x - to.x), |
| | | Math.abs(from.y - to.y), |
| | | ); |
| | | draft.endFill(); |
| | | addAreaLabel(draft, name, from, to); |
| | | draft.data = { ...(draft.data || {}), type: DEVICE_TYPE.AREA, name, color: areaColor, id, start: from, end: to }; |
| | | draft.eventMode = 'static'; |
| | | draft.cursor = 'pointer'; |
| | | if (setCurSprite) { |
| | | draft.off('click'); |
| | | draft.on('click', () => setCurSprite(draft)); |
| | | } |
| | | return draft; |
| | | }; |
| | | |
| | | export const startAreaDrawing = ({ promptText, onComplete, onFinish, setCurSprite } = {}) => { |
| | | if (!mapContainer || !mapContainer.parent) { |
| | | return false; |
| | | } |
| | | if (areaDrawingCleanup) { |
| | | areaDrawingCleanup(); |
| | | } |
| | | |
| | | const stage = mapContainer.parent; |
| | | stage.off('mousedown'); |
| | | const draft = new PIXI.Graphics(); |
| | | draft.name = 'area_' + generateID(); |
| | | draft.zIndex = DEVICE_Z_INDEX.AREA; |
| | | |
| | | let drawing = false; |
| | | let startPoint = null; |
| | | const areaColor = AREA_COLOR; |
| | | const originalCursor = stage.cursor; |
| | | stage.cursor = 'crosshair'; |
| | | |
| | | const drawRect = (from, to) => { |
| | | draft.clear(); |
| | | const strokeColor = areaColor; |
| | | draft.lineStyle(2 / Math.abs(mapContainer.scale.x || 1), 0x6c7a89, 0.9); |
| | | draft.beginFill(strokeColor, 0.18); |
| | | draft.drawRect( |
| | | Math.min(from.x, to.x), |
| | | Math.min(from.y, to.y), |
| | | Math.abs(from.x - to.x), |
| | | Math.abs(from.y - to.y), |
| | | ); |
| | | draft.endFill(); |
| | | }; |
| | | |
| | | const cleanupListeners = () => { |
| | | stage.off('pointermove', handleMove); |
| | | stage.off('pointerup', handleUp); |
| | | stage.off('pointerdown', handleDown); |
| | | areaDrawingCleanup = null; |
| | | stage.cursor = originalCursor; |
| | | if (onFinish) { |
| | | onFinish(); |
| | | } |
| | | }; |
| | | areaDrawingCleanup = cleanupListeners; |
| | | |
| | | const handleDown = (event) => { |
| | | if (event.button !== undefined && event.button !== 0) { |
| | | return; |
| | | } |
| | | drawing = true; |
| | | startPoint = mapContainer.toLocal(event.data.global); |
| | | mapContainer.addChild(draft); |
| | | }; |
| | | |
| | | const handleMove = (event) => { |
| | | if (!drawing || !startPoint) { |
| | | return; |
| | | } |
| | | const cur = mapContainer.toLocal(event.data.global); |
| | | drawRect(startPoint, cur); |
| | | }; |
| | | |
| | | const handleUp = (event) => { |
| | | if (!drawing || !startPoint) { |
| | | return; |
| | | } |
| | | drawing = false; |
| | | const endPoint = mapContainer.toLocal(event.data.global); |
| | | drawRect(startPoint, endPoint); |
| | | cleanupListeners(); |
| | | |
| | | const areaName = prompt(promptText || 'Please enter area name'); |
| | | if (!areaName) { |
| | | mapContainer.removeChild(draft); |
| | | draft.destroy({ children: true, texture: false, baseTexture: false }); |
| | | return; |
| | | } |
| | | |
| | | addAreaLabel(draft, areaName, startPoint, endPoint); |
| | | draft.data = { ...(draft.data || {}), type: DEVICE_TYPE.AREA, name: areaName, color: areaColor, start: startPoint, end: endPoint }; |
| | | draft.eventMode = 'static'; |
| | | draft.cursor = 'pointer'; |
| | | if (setCurSprite) { |
| | | draft.off('click'); |
| | | draft.on('click', () => setCurSprite(draft)); |
| | | } |
| | | |
| | | if (onComplete) { |
| | | onComplete({ |
| | | name: areaName, |
| | | start: startPoint, |
| | | end: endPoint, |
| | | color: areaColor, |
| | | graphics: draft |
| | | }); |
| | | } |
| | | }; |
| | | |
| | | stage.on('pointerdown', handleDown); |
| | | stage.on('pointermove', handleMove); |
| | | stage.on('pointerup', handleUp); |
| | | return true; |
| | | }; |