From c7140e8e3f979b212be1f7006bf1415a1ad4595b Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期一, 14 十月 2024 16:17:25 +0800 Subject: [PATCH] # --- zy-acs-flow/src/map/tool.js | 215 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 199 insertions(+), 16 deletions(-) diff --git a/zy-acs-flow/src/map/tool.js b/zy-acs-flow/src/map/tool.js index d8ea096..427a36b 100644 --- a/zy-acs-flow/src/map/tool.js +++ b/zy-acs-flow/src/map/tool.js @@ -7,25 +7,37 @@ import shelf from '/map/shelf.svg'; import charge from '/map/charge.svg'; +import station from '/map/station.svg'; import direction from '/map/direction.svg'; +import point from '/map/point.svg'; -let app, mapContainer; +let app, mapContainer, themeMode; +let selectedSprite, effectTick, effectHalfCircle, effectRectangle; + +export function getApp() { + return app; +} export function setApp(param) { app = param; +} + +export function getMapContainer() { + return mapContainer; } export function setMapContainer(param) { mapContainer = param; } -export function getApp() { - return app; +export function getThemeMode() { + return themeMode; } -export function getMapContainer() { - return mapContainer; +export function setThemeMode(param) { + themeMode = param; } + export const getRealPosition = (x, y) => { const rect = app.view.getBoundingClientRect(); @@ -39,31 +51,48 @@ let sprite; switch (deviceType) { case DEVICE_TYPE.SHELF: - sprite = new PIXI.Sprite(PIXI.Texture.from(shelf, { resourceOptions: { scale: 5 } })); - sprite.width = 50; - sprite.height = 50; + sprite = new PIXI.Sprite(PIXI.Texture.from(shelf, { resourceOptions: { scale: 1 } })); + // sprite.width = 50; + // sprite.height = 50; sprite.zIndex = DEVICE_Z_INDEX.SHELF; break; case DEVICE_TYPE.CHARGE: sprite = new PIXI.Sprite(PIXI.Texture.from(charge, { resourceOptions: { scale: 1 } })); - sprite.width = 60; - sprite.height = 60; + // sprite.width = 60; + // sprite.height = 60; sprite.zIndex = DEVICE_Z_INDEX.CHARGE; break; + case DEVICE_TYPE.STATION: + sprite = new PIXI.Sprite(PIXI.Texture.from(station, { resourceOptions: { scale: 1 } })); + // sprite.width = 112; + // sprite.height = 63; + sprite.zIndex = DEVICE_Z_INDEX.STATION; + break; case DEVICE_TYPE.DIRECTION: - sprite = new PIXI.Sprite(PIXI.Texture.from(direction, { resourceOptions: { scale: 5 } })); - sprite.width = 112; - sprite.height = 63; + sprite = new PIXI.Sprite(PIXI.Texture.from(direction, { resourceOptions: { scale: 1 } })); + // sprite.width = 112; + // sprite.height = 63; sprite.zIndex = DEVICE_Z_INDEX.DIRECTION; break; + + case DEVICE_TYPE.POINT: + sprite = new PIXI.Sprite(PIXI.Texture.from(point, { resourceOptions: { scale: 1 } })); + // sprite.width = 112; + // sprite.height = 63; + sprite.zIndex = DEVICE_Z_INDEX.POINT; + break default: break; + } + if (sprite) { + sprite.blendMode = PIXI.BLEND_MODES.MULTIPLY; } return sprite; } export const initSprite = (sprite, type) => { sprite.anchor.set(0.5); + // sprite.alpha = 1; sprite.cursor = 'pointer'; sprite.eventMode = 'static'; sprite.data = { @@ -117,7 +146,7 @@ } -export const spriteListBeMovable = (selectedSprites, resetFn) => { +export const spriteListBeMovable = (selectedSprites, endFn) => { if (selectedSprites && selectedSprites.length > 0) { let batchMove = false; let batchMoveStartPos = null; @@ -148,8 +177,8 @@ mapContainer.parent.off('mousemove'); mapContainer.parent.off('mouseup'); - if (resetFn) { - resetFn(); + if (endFn) { + endFn(); } } @@ -172,6 +201,17 @@ } } +export const copySprite = (sprite) => { + const copiedSprite = generateSprite(sprite.data.type) + initSprite(copiedSprite); + copiedSprite.position.set(sprite.position.x, sprite.position.y); + copiedSprite.scale.set(sprite.scale.x, sprite.scale.y); + copiedSprite.rotation = sprite.rotation; + copiedSprite.data = deepCopy(sprite.data); + copiedSprite.data.uuid = generateID(); + return copiedSprite; +} + export const isSpriteInSelectionBox = (sprite, selectionBox) => { const spriteBounds = sprite.getBounds(); const boxBounds = selectionBox.getBounds(); @@ -182,11 +222,154 @@ && spriteBounds.y < boxBounds.y + boxBounds.height; } +export const beSettings = (sprite, setSpriteSettings) => { + sprite.off('pointerup'); + sprite.off('pointermove'); + sprite.off('pointerdown'); + sprite.off('click'); + + sprite.on("click", onClick); + + function onClick(event) { + setSpriteSettings(sprite); + } +} + +export const clearMapData = () => { + if (!mapContainer) { + return; + } + let childList = []; + mapContainer.children.forEach(child => { + if (child.data?.uuid) { + childList.push(child); + } + }) + if (childList.length > 0) { + childList.forEach(child => { + mapContainer.removeChild(child); + child.destroy({ children: true, texture: false, baseTexture: false }); + }) + childList.forEach((child, index) => { + childList[index] = null; + }); + childList = []; + } +} + +export const showSelectedEffect = (sprite) => { + if (!sprite?.texture || !sprite?.texture?.valid) { + return; + } + const { width, height } = sprite; + const scale = sprite.scale.x; + const sideLen = (Math.max(width, height) + 10) * scale; + const color = themeMode === 'light' ? 0x273c75 : 0xffffff; + + effectHalfCircle = new PIXI.Graphics(); + effectHalfCircle.beginFill(color); + effectHalfCircle.lineStyle(2 * scale, color); + effectHalfCircle.arc(0, 0, sideLen, 0, Math.PI); + effectHalfCircle.endFill(); + effectHalfCircle.position.set(sprite.x, sprite.y); + effectHalfCircle.scale.set(1 / scale); + effectHalfCircle.zIndex = 9999; + + effectRectangle = new PIXI.Graphics(); + effectRectangle.lineStyle(5 * scale, color, 1); + effectRectangle.drawRoundedRect(0, 0, sideLen, sideLen, 16 * scale); + effectRectangle.endFill(); + effectRectangle.mask = effectHalfCircle; + effectRectangle.zIndex = 9999; + + const scaledWidth = sideLen * (1 / scale); + const scaledHeight = sideLen * (1 / scale); + + effectRectangle.scale.set(1 / scale); + effectRectangle.position.set(sprite.x - scaledWidth / 2, sprite.y - scaledHeight / 2); + + mapContainer.addChild(effectRectangle); + mapContainer.addChild(effectHalfCircle); + + selectedSprite = sprite; + + let phase = 0; + effectTick = (delta) => { + phase += delta / 10; + phase %= (Math.PI * 2); + if (effectHalfCircle) { + effectHalfCircle.rotation = phase; + } + }; + + app.ticker.add(effectTick); +} + +export const removeSelectedEffect = () => { + if (effectTick) { + app.ticker.remove(effectTick); + } + if (effectHalfCircle) { + mapContainer.removeChild(effectHalfCircle); + effectHalfCircle = null; + } + if (effectRectangle) { + mapContainer.removeChild(effectRectangle); + effectRectangle = null; + } + selectedSprite = null; +} + +export const updateEffect = (sprite) => { + if (!sprite || sprite !== selectedSprite || !effectRectangle || !effectHalfCircle) { + return + } + const { width, height } = sprite; + const scale = sprite?.scale.x; + const sideLen = (Math.max(width, height) + 10) * scale; + const scaledWidth = sideLen * (1 / scale); + const scaledHeight = sideLen * (1 / scale); + + effectRectangle.position.set(sprite.x - scaledWidth / 2, sprite.y - scaledHeight / 2); + + effectHalfCircle.position.set(sprite.x, sprite.y); +} + +export const rotationToNum = (rotation) => { + let res = rotation * 180 / Math.PI; + if (res < 0) { + res += 360; + } else if (res > 360) { + res -= 360; + } + return res; +} + +export const rotationParseNum = (num) => { + return num * Math.PI / 180; +} + +export const incrementSpriteNo = (str, incrementValue) => { + const match = str.match(/(\D*)(\d+)/); + if (match) { + const prefix = match[1]; + const numberPart = match[2]; + const newNumber = parseInt(numberPart, 10) + incrementValue; + const paddedNumber = newNumber.toString().padStart(numberPart.length, '0'); + return `${prefix}${paddedNumber}`; + } else { + return str; + } +} export const generateID = () => { return Date.now().toString(36) + Math.random().toString(36).substring(2); } +export const deepCopy = (data) => { + return JSON.parse(JSON.stringify(data)); +} + export const patchRaLayout = (param) => { const parentElement = document.getElementById('main-content'); if (parentElement && parentElement.classList.contains('RaLayout-content')) { -- Gitblit v1.9.1