Vue.component('map-canvas', { template: `
{{ shelfTooltip.text }}
FPS:{{mapFps}}
`, props: ['lev', 'crnParam', 'rgvParam', 'devpParam', 'highlightOnParamChange'], data() { return { map: [], currentLev: 1, mapFps: 0, ws: null, wsReconnectTimer: null, wsReconnectAttempts: 0, wsReconnectBaseDelay: 1000, wsReconnectMaxDelay: 15000, pixiApp: null, pixiStageList: [], pixiStaMap: new Map(), pixiCrnMap: new Map(), pixiDualCrnMap: new Map(), pixiRgvMap: new Map(), mapRoot: null, mapRotation: 0, mapMirrorX: false, mapContentSize: { width: 0, height: 0 }, mapConfigCodes: { rotate: 'map_canvas_rotation', mirror: 'map_canvas_mirror_x' }, pixiShelfMap: new Map(), pixiTrackMap: new Map(), pixiDevpTextureMap: new Map(), pixiCrnColorTextureMap: new Map(), pixiDevpTextureMap: new Map(), pixiCrnColorTextureMap: new Map(), pixiRgvColorTextureMap: new Map(), crnList: [], dualCrnList: [], rgvList: [], locListMap: new Map(), locListLoaded: false, locListLoading: false, mapRowOffsets: [], mapRowHeights: [], mapColOffsets: [], mapColWidths: [], mapRowColOffsets: [], mapRowColWidths: [], mapRowShelfCells: [], hoveredShelfCell: null, hoverPointer: { x: 0, y: 0 }, hoverRaf: null, objectsContainer: null, objectsContainer2: null, tracksContainer: null, tracksGraphics: null, shelvesContainer: null, graphicsCrn: null, graphicsCrnTrack: null, graphicsRgvTrack: null, graphicsRgv: null, shelfTooltip: { visible: false, x: 0, y: 0, text: '', item: null }, shelfTooltipMinScale: 0.4, timer: null, adjustLabelTimer: null, isSwitchingFloor: false } }, mounted() { this.currentLev = this.lev || 1; this.createMap(); this.loadMapTransformConfig(); this.loadLocList(); this.connectWs(); setTimeout(() => { this.getMap(this.currentLev); }, 1000); this.timer = setInterval(() => { this.getCrnInfo(); this.getDualCrnInfo(); this.getSiteInfo(); this.getRgvInfo(); }, 1000); }, beforeDestroy() { if (this.timer) { clearInterval(this.timer); } if (this.hoverRaf) { cancelAnimationFrame(this.hoverRaf); this.hoverRaf = null; } if (this.pixiApp) { this.pixiApp.destroy(true, { children: true }); } window.removeEventListener('resize', this.resizeToContainer); if (this.wsReconnectTimer) { clearTimeout(this.wsReconnectTimer); this.wsReconnectTimer = null; } if (this.ws && (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)) { try { this.ws.close(); } catch (e) {} } }, watch: { lev(newLev) { if (newLev != null) { this.changeFloor(newLev); } }, crnParam: { deep: true, handler(v) { if (!this.highlightOnParamChange) { return; } if (v && v.crnNo && this.pixiCrnMap) { const id = parseInt(v.crnNo, 10); const sprite = this.pixiCrnMap.get(id); if (sprite && window.gsap) { window.gsap.killTweensOf(sprite); window.gsap.fromTo(sprite, { alpha: 1 }, { alpha: 0.2, yoyo: true, repeat: 6, duration: 0.15 }); } } } }, rgvParam: { deep: true, handler(v) { if (!this.highlightOnParamChange) { return; } if (v && v.rgvNo && this.pixiRgvMap) { const id = parseInt(v.rgvNo, 10); const sprite = this.pixiRgvMap.get(id); if (sprite && window.gsap) { window.gsap.killTweensOf(sprite); window.gsap.fromTo(sprite, { alpha: 1 }, { alpha: 0.2, yoyo: true, repeat: 6, duration: 0.15 }); } } } }, devpParam: { deep: true, handler(v) { if (!this.highlightOnParamChange) { return; } if (v && v.stationId && this.pixiStaMap) { const id = parseInt(v.stationId, 10); const sprite = this.pixiStaMap.get(id); if (sprite && window.gsap) { window.gsap.killTweensOf(sprite); window.gsap.fromTo(sprite, { alpha: 1 }, { alpha: 0.2, yoyo: true, repeat: 6, duration: 0.15 }); } } } } }, methods: { createMap() { this.pixiApp = new PIXI.Application({ backgroundColor: 0xF5F7F9, antialias: false, powerPreference: 'high-performance', autoDensity: true, resolution: Math.min(window.devicePixelRatio || 1, 2) }); PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.LINEAR; this.$refs.pixiView.appendChild(this.pixiApp.view); this.pixiApp.view.style.width = '100%'; this.pixiApp.view.style.height = '100%'; this.pixiApp.view.style.display = 'block'; this.resizeToContainer(); window.addEventListener('resize', this.resizeToContainer); this.graphicsCrnTrack = this.createTrackTexture(25, 25, 10); this.graphicsRgvTrack = this.createTrackTexture(25, 25, 10); this.objectsContainer = new PIXI.Container(); this.objectsContainer2 = new PIXI.Container(); this.tracksContainer = new PIXI.ParticleContainer(10000, { scale: true, position: true, rotation: false, uvs: false, alpha: false }); this.tracksGraphics = new PIXI.Graphics(); this.shelvesContainer = new PIXI.ParticleContainer(10000, { scale: true, position: true, rotation: false, uvs: false, alpha: false }); this.tracksContainer.autoResize = true; this.shelvesContainer.autoResize = true; this.mapRoot = new PIXI.Container(); this.pixiApp.stage.addChild(this.mapRoot); this.mapRoot.addChild(this.tracksGraphics); this.mapRoot.addChild(this.tracksContainer); this.mapRoot.addChild(this.shelvesContainer); this.mapRoot.addChild(this.objectsContainer); this.mapRoot.addChild(this.objectsContainer2); this.pixiApp.renderer.roundPixels = true; this.hoveredShelfCell = null; this.hoverPointer = { x: 0, y: 0 }; this.hoverRaf = null; //*******************shelf hover******************* this.pixiApp.renderer.plugins.interaction.on('pointermove', (event) => { if (!this.isShelfTooltipAllowed()) { this.hideShelfTooltip(); return; } if (!this.map || !this.mapRoot) { return; } const pos = event.data.global; this.hoverPointer.x = pos.x; this.hoverPointer.y = pos.y; if (this.hoverRaf) { return; } this.hoverRaf = requestAnimationFrame(() => { this.hoverRaf = null; this.updateShelfHoverFromPointer(this.hoverPointer); }); }); this.pixiApp.view.addEventListener('mouseleave', () => { this.hoveredShelfCell = null; this.hideShelfTooltip(); }); //*******************shelf hover******************* let stageOriginalPos; let mouseDownPoint; let touchBlank = false; this.pixiApp.renderer.plugins.interaction.on('pointerdown', (event) => { const globalPos = event.data.global; stageOriginalPos = [this.pixiApp.stage.position.x, this.pixiApp.stage.position.y]; mouseDownPoint = [globalPos.x, globalPos.y]; if (!event.target || (event.target && event.target._kind === 'shelf')) { touchBlank = true; } }); this.pixiApp.renderer.plugins.interaction.on('pointermove', (event) => { const globalPos = event.data.global; if (touchBlank) { const dx = globalPos.x - mouseDownPoint[0]; const dy = globalPos.y - mouseDownPoint[1]; this.pixiApp.stage.position.set(stageOriginalPos[0] + dx, stageOriginalPos[1] + dy); } }); this.pixiApp.renderer.plugins.interaction.on('pointerup', () => { touchBlank = false; }); //*******************缩放画布******************* this.pixiApp.view.addEventListener('wheel', (event) => { event.stopPropagation(); event.preventDefault(); const rect = this.pixiApp.view.getBoundingClientRect(); const sx = event.clientX - rect.left; const sy = event.clientY - rect.top; const oldZoomX = this.pixiApp.stage.scale.x || 1; const oldZoomY = this.pixiApp.stage.scale.y || 1; const oldZoomAbs = Math.abs(oldZoomX) || 1; const delta = event.deltaY; let newZoomAbs = oldZoomAbs * 0.999 ** delta; const mirrorX = this.mapMirrorX ? -1 : 1; const newZoomX = mirrorX * newZoomAbs; const newZoomY = newZoomAbs; const worldX = (sx - this.pixiApp.stage.position.x) / oldZoomX; const worldY = (sy - this.pixiApp.stage.position.y) / oldZoomY; const newPosX = sx - worldX * newZoomX; const newPosY = sy - worldY * newZoomY; this.pixiApp.stage.setTransform(newPosX, newPosY, newZoomX, newZoomY, 0, 0, 0, 0, 0); this.scheduleAdjustLabels(); }); //*******************缩放画布******************* //*******************FPS******************* let g_Time = 0; let fpsLastUpdateTs = 0; let fpsDeltaSumMs = 0; let fpsFrameCount = 0; const fpsUpdateInterval = 200; this.pixiApp.ticker.add((delta) => { const timeNow = (new Date()).getTime(); const timeDiff = timeNow - g_Time; g_Time = timeNow; fpsDeltaSumMs += timeDiff; fpsFrameCount += 1; if (timeNow - fpsLastUpdateTs >= fpsUpdateInterval) { const avgFps = fpsDeltaSumMs > 0 ? (fpsFrameCount * 1000 / fpsDeltaSumMs) : 0; this.mapFps = Math.round(avgFps); fpsDeltaSumMs = 0; fpsFrameCount = 0; fpsLastUpdateTs = timeNow; } }); //*******************FPS******************* }, resizeToContainer() { const w = this.$el.clientWidth || 0; const h = this.$el.clientHeight || 0; if (w > 0 && h > 0 && this.pixiApp) { this.pixiApp.renderer.resize(w, h); } }, getMap() { this.sendWs(JSON.stringify({ url: "/basMap/lev/" + this.currentLev + "/auth", data: {} })); }, changeFloor(lev) { this.currentLev = lev; this.isSwitchingFloor = true; this.hideShelfTooltip(); this.hoveredShelfCell = null; this.mapRowOffsets = []; this.mapRowHeights = []; this.mapColOffsets = []; this.mapColWidths = []; if (this.adjustLabelTimer) { clearTimeout(this.adjustLabelTimer); this.adjustLabelTimer = null; } this.objectsContainer.removeChildren(); this.objectsContainer2.removeChildren(); if (this.tracksContainer) { this.tracksContainer.removeChildren(); } if (this.tracksGraphics) { this.tracksGraphics.clear(); } if (this.shelvesContainer) { this.shelvesContainer.removeChildren(); } this.crnList = []; this.dualCrnList = []; this.rgvList = []; this.pixiCrnMap = new Map(); this.pixiDualCrnMap = new Map(); this.pixiRgvMap = new Map(); this.pixiStaMap = new Map(); this.pixiStageList = []; this.getMap(); }, createMapData(map) { this.hideShelfTooltip(); this.hoveredShelfCell = null; this.mapRowOffsets = []; this.mapRowHeights = []; this.mapColOffsets = []; this.mapColWidths = []; if (window.gsap) { this.pixiStaMap && this.pixiStaMap.forEach((s) => { try { window.gsap.killTweensOf(s); } catch (e) {} }); this.pixiCrnMap && this.pixiCrnMap.forEach((s) => { try { window.gsap.killTweensOf(s); } catch (e) {} }); this.pixiDualCrnMap && this.pixiDualCrnMap.forEach((s) => { try { window.gsap.killTweensOf(s); } catch (e) {} }); this.pixiRgvMap && this.pixiRgvMap.forEach((s) => { try { window.gsap.killTweensOf(s); } catch (e) {} }); } this.objectsContainer.removeChildren(); this.objectsContainer2.removeChildren(); if (this.tracksContainer) { this.tracksContainer.removeChildren(); } if (this.tracksGraphics) { this.tracksGraphics.clear(); } if (this.shelvesContainer) { this.shelvesContainer.removeChildren(); } this.crnList = []; this.dualCrnList = []; this.rgvList = []; this.pixiCrnMap = new Map(); this.pixiDualCrnMap = new Map(); this.pixiRgvMap = new Map(); this.pixiStaMap = new Map(); this.pixiStageList = []; this.pixiStageList = [map.length]; const bayHeightList = this.initHeight(map); const bayWidthList = this.initWidth(map); map.forEach((item, index) => { for (let idx = 0; idx < item.length; idx++) { let val = item[idx]; if (val.cellHeight == undefined || val.cellHeight === '') { val.cellHeight = bayHeightList[index]; } if (val.cellWidth == undefined || val.cellWidth === '') { val.cellWidth = bayWidthList[idx]; } } }); map.forEach((item, index) => { for (let idx = 0; idx < item.length; idx++) { let val = item[idx]; let cellWidth = val.cellWidth / 40; let cellHeight = val.cellHeight / 8; val.width = cellWidth; val.height = cellHeight; let mergeHeight = cellHeight; if (val.rowSpan > 1) { for (let i = 1; i < val.rowSpan; i++) { let nextMerge = map[index + i][idx]; if (nextMerge.type != 'merge') { continue; } let mergeCellHeight = nextMerge.cellHeight / 8; mergeHeight += mergeCellHeight; } val.height = mergeHeight; } let mergeWidth = cellWidth; if (val.colSpan > 1) { for (let i = 1; i < val.colSpan; i++) { let nextMerge = map[index][idx + i]; if (!nextMerge) { continue; } let mergeCellWidth = nextMerge.cellWidth / 40; mergeWidth += mergeCellWidth; nextMerge.isMergedPart = true; } val.width = mergeWidth; } } }); const rowHeightScaled = []; for (let r = 0; r < map.length; r++) { const h = bayHeightList[r]; if (h != null && h !== -1) { rowHeightScaled[r] = h / 8; } else { let fallback = 0; for (let c = 0; c < map[r].length; c++) { const v = map[r][c]; if (v && v.type !== 'merge' && v.height != null && v.height > 0) { fallback = v.height; break; } } rowHeightScaled[r] = fallback > 0 ? fallback : 25; } } let yOffsets = []; let yCursor = 0; for (let r = 0; r < map.length; r++) { yOffsets[r] = yCursor; yCursor += (rowHeightScaled[r] || 0); } map.forEach((row, rowIndex) => { let xCursor = 0; let anchorX = 0; for (let colIndex = 0; colIndex < row.length; colIndex++) { let val = row[colIndex]; let cellWidth = val.width; let cellHeight = val.height; val.rowIndex = rowIndex; val.colIndex = colIndex; if (val.isMergedPart) { val.posX = anchorX; val.posY = yOffsets[rowIndex]; continue; } val.posX = xCursor; val.posY = yOffsets[rowIndex]; anchorX = xCursor; if (val.colSpan > 1) { for (let i = 1; i < val.colSpan; i++) { const next = row[colIndex + i]; if (!next) { break; } next.posX = anchorX; next.posY = yOffsets[rowIndex]; } } xCursor += cellWidth; } }); this.buildShelfHitGrid(map, rowHeightScaled, yOffsets); this.drawTracks(map); map.forEach((item, index) => { this.pixiStageList[index] = [item.length]; for (let idx = 0; idx < item.length; idx++) { let val = item[idx]; val.rowIndex = index; val.colIndex = idx; if (val.type === 'merge') { continue; } if (val.type == undefined || val.type === 'none') { continue; } if (this.isTrackType(val)) { this.collectTrackItem(val); continue; } let sprite = this.getSprite(val, (e) => { //回调 }); if (sprite == null) { continue; } if (sprite._kind === 'shelf') { this.shelvesContainer.addChild(sprite); } else { this.objectsContainer.addChild(sprite); } this.pixiStageList[index][idx] = sprite; } }); this.crnList.forEach((item) => { if (this.graphicsCrn == null) { this.graphicsCrn = this.createCrnTexture(item.width * 0.9, item.height * 0.9); } let sprite = new PIXI.Sprite(this.graphicsCrn); const deviceNo = this.getDeviceNo(item.value); const taskNo = this.getTaskNo(item.value); const style = new PIXI.TextStyle({ fontFamily: 'Arial', fontSize: 12, fill: '#000000', stroke: '#ffffff', strokeThickness: 1 }); const txt = taskNo > 0 ? (deviceNo + "(" + taskNo + ")") : String(deviceNo); const text = new PIXI.Text(txt, style); text.anchor.set(0.5); text.position.set(sprite.width / 2, sprite.height / 2); sprite.addChild(text); sprite.textObj = text; sprite.position.set(item.posX, item.posY); sprite.interactive = true; // 必须要设置才能接收事件 sprite.buttonMode = true; // 让光标在hover时变为手型指事件 sprite.on('pointerdown', () => { if (window.gsap) { window.gsap.killTweensOf(sprite); } sprite.alpha = 1; const id = parseInt(deviceNo, 10); this.$emit('crn-click', id); }); let rowIndexForCrn = 0; for (let r = 0; r < map.length; r++) { if (map[r].length > 0) { const rowY = map[r][0].posY; if (Math.abs(rowY - item.posY) < 0.5) { rowIndexForCrn = r; break; } } } sprite.rowIndex = rowIndexForCrn; this.pixiCrnMap.set(parseInt(deviceNo), sprite); this.objectsContainer2.addChild(sprite); }); this.dualCrnList.forEach((item) => { if (this.graphicsCrn == null) { this.graphicsCrn = this.createCrnTexture(item.width * 0.9, item.height * 0.9); } let sprite = new PIXI.Sprite(this.graphicsCrn); const deviceNo = this.getDeviceNo(item.value); const taskNo = this.getTaskNo(item.value); const style = new PIXI.TextStyle({ fontFamily: 'Arial', fontSize: 12, fill: '#000000', stroke: '#ffffff', strokeThickness: 1 }); const txt = taskNo > 0 ? (deviceNo + "(" + taskNo + ")") : String(deviceNo); const text = new PIXI.Text(txt, style); text.anchor.set(0.5); text.position.set(sprite.width / 2, sprite.height / 2); sprite.addChild(text); sprite.textObj = text; sprite.position.set(item.posX, item.posY); sprite.interactive = true; sprite.buttonMode = true; sprite.on('pointerdown', () => { if (window.gsap) { window.gsap.killTweensOf(sprite); } sprite.alpha = 1; const id = parseInt(deviceNo, 10); this.$emit('dual-crn-click', id); }); let rowIndexForCrn = 0; for (let r = 0; r < map.length; r++) { if (map[r].length > 0) { const rowY = map[r][0].posY; if (Math.abs(rowY - item.posY) < 0.5) { rowIndexForCrn = r; break; } } } sprite.rowIndex = rowIndexForCrn; this.pixiDualCrnMap.set(parseInt(deviceNo), sprite); this.objectsContainer2.addChild(sprite); }); this.rgvList.forEach((item) => { if (this.graphicsRgv == null) { this.graphicsRgv = this.createRgvTexture(item.width * 0.9, item.height * 0.9); } let sprite = new PIXI.Sprite(this.graphicsRgv); const deviceNo = this.getDeviceNo(item.value); const taskNo = this.getTaskNo(item.value); const style = new PIXI.TextStyle({ fontFamily: 'Arial', fontSize: 12, fill: '#000000', stroke: '#ffffff', strokeThickness: 1 }); const txt = taskNo > 0 ? (deviceNo + "(" + taskNo + ")") : String(deviceNo); const text = new PIXI.Text(txt, style); text.anchor.set(0.5); text.position.set(sprite.width / 2, sprite.height / 2); sprite.addChild(text); sprite.textObj = text; sprite.position.set(item.posX, item.posY); sprite.interactive = true; // 必须要设置才能接收事件 sprite.buttonMode = true; // 让光标在hover时变为手型指事件 sprite.on('pointerdown', () => { if (window.gsap) { window.gsap.killTweensOf(sprite); } sprite.alpha = 1; const id = parseInt(deviceNo, 10); this.$emit('rgv-click', id); }); let rowIndexForRgv = 0; for (let r = 0; r < map.length; r++) { if (map[r].length > 0) { const rowY = map[r][0].posY; if (Math.abs(rowY - item.posY) < 0.5) { rowIndexForRgv = r; break; } } } sprite.rowIndex = rowIndexForRgv; this.pixiRgvMap.set(parseInt(deviceNo), sprite); this.objectsContainer2.addChild(sprite); }); let contentW = 0; let contentH = 0; for (let r = 0; r < map.length; r++) { for (let c = 0; c < map[r].length; c++) { const cell = map[r][c]; if (!cell || cell.type === 'merge') { continue; } const right = cell.posX + cell.width; const bottom = cell.posY + cell.height; if (right > contentW) { contentW = right; } if (bottom > contentH) { contentH = bottom; } } } this.mapContentSize = { width: contentW, height: contentH }; this.applyMapTransform(true); this.map = map; this.isSwitchingFloor = false; }, initWidth(map) { let maxRow = map.length; let maxBay = map[0].length; let bayWidthList = []; for (let bay = 0; bay < maxBay; bay++) { let bayWidth = -1; for (let row = 0; row < maxRow; row++) { let val = map[row][bay]; if (val.cellWidth == undefined || val.cellWidth === '') { continue; } bayWidth = Math.max(bayWidth, val.cellWidth); break; } bayWidthList.push(bayWidth); } return bayWidthList; }, initHeight(map) { let maxRow = map.length; let maxBay = map[0].length; let bayHeightList = []; for (let row = 0; row < maxRow; row++) { let bayHeight = -1; for (let bay = 0; bay < maxBay; bay++) { let val = map[row][bay]; if (val.cellHeight == undefined || val.cellHeight === '') { continue; } bayHeight = Math.max(bayHeight, val.cellHeight); break; } bayHeightList.push(bayHeight); } return bayHeightList; }, setSiteInfo(res) { let sites = Array.isArray(res) ? res : (res && res.code === 200 ? res.data : null); if (res && !Array.isArray(res)) { if (res.code === 403) { parent.location.href = baseUrl + "/login"; return; } if (res.code !== 200) { return; } } if (!sites) { return; } sites.forEach((item) => { let id = item.siteId != null ? item.siteId : item.stationId; let status = item.siteStatus != null ? item.siteStatus : item.stationStatus; let workNo = item.workNo != null ? item.workNo : item.taskNo; if (id == null) { return; } let sta = this.pixiStaMap.get(parseInt(id)); if (sta == undefined) { return; } if (workNo != null && workNo > 0) { sta.textObj.text = id + "(" + workNo + ")"; } else { sta.textObj.text = String(id); } if (sta.statusObj) { this.objectsContainer.removeChild(sta.statusObj); sta.statusObj = null; if (sta.textObj.parent !== sta) { sta.addChild(sta.textObj); sta.textObj.position.set(sta.width / 2, sta.height / 2); } } if (status === "site-auto") { this.updateColor(sta, 0x78ff81); } else if (status === "site-auto-run" || status === "site-auto-id" || status === "site-auto-run-id") { this.updateColor(sta, 0xfa51f6); } else if (status === "site-unauto") { this.updateColor(sta, 0xb8b8b8); } else if (status === "machine-pakin") { this.updateColor(sta, 0x30bffc); } else if (status === "machine-pakout") { this.updateColor(sta, 0x97b400); } else if (status === "site-run-block") { this.updateColor(sta, 0xe69138); } else { this.updateColor(sta, 0xb8b8b8); } }); }, getCrnInfo() { if (this.isSwitchingFloor) { return; } this.sendWs(JSON.stringify({ url: "/console/latest/data/crn", data: {} })); }, getDualCrnInfo() { if (this.isSwitchingFloor) { return; } this.sendWs(JSON.stringify({ url: "/console/latest/data/dualcrn", data: {} })); }, getSiteInfo() { if (this.isSwitchingFloor) { return; } this.sendWs(JSON.stringify({ url: "/console/latest/data/station", data: {} })); }, getRgvInfo() { if (this.isSwitchingFloor) { return; } this.sendWs(JSON.stringify({ url: "/console/latest/data/rgv", data: {} })); }, setCrnInfo(res) { let crns = Array.isArray(res) ? res : (res && res.code === 200 ? res.data : null); if (!crns) { return; } for (var i = 0; i < crns.length; i++) { const id = parseInt(crns[i].crnId); const sprite = this.pixiCrnMap.get(id); if (!sprite) { continue; } const taskNo = crns[i].taskNo; if (taskNo != null && taskNo > 0) { sprite.textObj.text = id + "(" + taskNo + ")"; } else { sprite.textObj.text = String(id); } const status = crns[i].crnStatus; const statusColor = this.getCrnStatusColor(status); this.updateCrnTextureColor(sprite, statusColor); let bay = parseInt(crns[i].bay, 10); if (isNaN(bay) || bay < 1 || bay === -2) { bay = 1; } let rowIndex = (sprite.rowIndex != null) ? sprite.rowIndex : -1; if (rowIndex === -1) { for (let r = 0; r < this.map.length; r++) { if (this.map[r].length > 0) { const rowY = this.map[r][0].posY; if (Math.abs(rowY - sprite.y) < 0.5) { rowIndex = r; break; } } } if (rowIndex === -1) { rowIndex = 0; } } let targetCell = null; let crnCount = 0; for (let c = 0; c < this.map[rowIndex].length; c++) { const cell = this.map[rowIndex][c]; if (cell && cell.type === 'crn') { crnCount++; if (crnCount === bay) { targetCell = cell; break; } } } if (!targetCell) { for (let c = this.map[rowIndex].length - 1; c >= 0; c--) { const cell = this.map[rowIndex][c]; if (cell && cell.type === 'crn') { targetCell = cell; break; } } } if (!targetCell) { continue; } const targetX = targetCell.posX + (targetCell.width - sprite.width) / 2; const dx = Math.abs(targetX - sprite.x); if (dx < 1) { } else if (dx < 5) { sprite.x = targetX; } else if (window.gsap) { window.gsap.killTweensOf(sprite); window.gsap.to(sprite, { x: targetX, duration: 0.3, ease: "power1.inOut" }); } else { sprite.x = targetX; } } this.scheduleAdjustLabels(); }, setDualCrnInfo(res) { let crns = Array.isArray(res) ? res : (res && res.code === 200 ? res.data : null); if (!crns) { return; } for (var i = 0; i < crns.length; i++) { const id = parseInt(crns[i].crnId); const sprite = this.pixiDualCrnMap.get(id); if (!sprite) { continue; } const taskNo = crns[i].taskNo; if (taskNo != null && taskNo > 0) { sprite.textObj.text = id + "(" + taskNo + ")"; } else { sprite.textObj.text = String(id); } const status = crns[i].crnStatus; const statusColor = this.getCrnStatusColor(status); this.updateCrnTextureColor(sprite, statusColor); let bay = parseInt(crns[i].bay, 10); if (isNaN(bay) || bay < 1 || bay === -2) { bay = 1; } let rowIndex = (sprite.rowIndex != null) ? sprite.rowIndex : -1; if (rowIndex === -1) { for (let r = 0; r < this.map.length; r++) { if (this.map[r].length > 0) { const rowY = this.map[r][0].posY; if (Math.abs(rowY - sprite.y) < 0.5) { rowIndex = r; break; } } } if (rowIndex === -1) { rowIndex = 0; } } let targetCell = null; let crnCount = 0; for (let c = 0; c < this.map[rowIndex].length; c++) { const cell = this.map[rowIndex][c]; if (cell && (cell.type === 'dualCrn' || cell.type === 'dualcrn')) { crnCount++; if (crnCount === bay) { targetCell = cell; break; } } } if (!targetCell) { for (let c = this.map[rowIndex].length - 1; c >= 0; c--) { const cell = this.map[rowIndex][c]; if (cell && (cell.type === 'dualCrn' || cell.type === 'dualcrn')) { targetCell = cell; break; } } } if (!targetCell) { continue; } const targetX = targetCell.posX + (targetCell.width - sprite.width) / 2; const dx = Math.abs(targetX - sprite.x); if (dx < 1) { } else if (dx < 5) { sprite.x = targetX; } else if (window.gsap) { window.gsap.killTweensOf(sprite); window.gsap.to(sprite, { x: targetX, duration: 0.3, ease: "power1.inOut" }); } else { sprite.x = targetX; } } this.scheduleAdjustLabels(); }, setRgvInfo(res) { let rgvs = Array.isArray(res) ? res : (res && res.code === 200 ? res.data : null); if (!rgvs) { return; } for (let i = 0; i < rgvs.length; i++) { const id = parseInt(rgvs[i].rgvNo, 10); const sprite = this.pixiRgvMap.get(id); if (!sprite) { continue; } const taskNo = rgvs[i].taskNo; if (sprite.textObj) { if (taskNo != null && taskNo > 0) { sprite.textObj.text = id + "(" + taskNo + ")"; } else { sprite.textObj.text = String(id); } } const statusColor = this.getRgvStatusColor(rgvs[i].rgvStatus); this.updateRgvTextureColor(sprite, statusColor); let trackSiteNo = parseInt(rgvs[i].trackSiteNo, 10); if (!trackSiteNo || trackSiteNo <= 0) { continue; } let rowIndex = (sprite.rowIndex != null) ? sprite.rowIndex : 0; let targetCell = null; for (let c = 0; c < this.map[rowIndex].length; c++) { const cell = this.map[rowIndex][c]; if (!cell || cell.type !== 'rgv') { continue; } const ts = this.getTrackSiteNo(cell.value); if (ts === trackSiteNo) { targetCell = cell; break; } } if (!targetCell) { for (let c = this.map[rowIndex].length - 1; c >= 0; c--) { const cell = this.map[rowIndex][c]; if (cell && cell.type === 'rgv') { targetCell = cell; break; } } } if (!targetCell) { continue; } const targetX = targetCell.posX + (targetCell.width - sprite.width) / 2; const dx = Math.abs(targetX - sprite.x); if (dx < 1) { } else if (dx < 5) { sprite.x = targetX; } else if (window.gsap) { window.gsap.killTweensOf(sprite); window.gsap.to(sprite, { x: targetX, duration: 0.3, ease: "power1.inOut" }); } else { sprite.x = targetX; } } this.scheduleAdjustLabels(); }, setMap(res) { this.createMapData(JSON.parse(res.data)); }, webSocketOnOpen(e) { if (this.wsReconnectTimer) { clearTimeout(this.wsReconnectTimer); this.wsReconnectTimer = null; } this.wsReconnectAttempts = 0; this.getMap(this.currentLev); }, webSocketOnError(e) { this.scheduleReconnect(); }, webSocketOnMessage(e) { const result = JSON.parse(e.data); if (result.url === "/console/latest/data/station" || result.url === "/console/latest/data/site") { this.setSiteInfo(JSON.parse(result.data)); } else if (result.url === "/console/latest/data/crn") { this.setCrnInfo(JSON.parse(result.data)); } else if (result.url === "/console/latest/data/dualcrn") { this.setDualCrnInfo(JSON.parse(result.data)); } else if (result.url === "/console/latest/data/rgv") { this.setRgvInfo(JSON.parse(result.data)); } else if (typeof result.url === "string" && result.url.indexOf("/basMap/lev/") === 0) { this.setMap(JSON.parse(result.data)); } }, webSocketClose(e) { this.scheduleReconnect(); }, sendWs(message) { if (this.ws && this.ws.readyState === WebSocket.OPEN) { this.ws.send(message); } }, connectWs() { if (this.ws && (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)) { return; } this.ws = new WebSocket("ws://" + window.location.host + baseUrl + "/console/websocket"); this.ws.onopen = this.webSocketOnOpen; this.ws.onerror = this.webSocketOnError; this.ws.onmessage = this.webSocketOnMessage; this.ws.onclose = this.webSocketClose; }, scheduleReconnect() { if (this.wsReconnectTimer) { return; } const attempt = this.wsReconnectAttempts + 1; const jitter = Math.floor(Math.random() * 300); const delay = Math.min(this.wsReconnectMaxDelay, this.wsReconnectBaseDelay * Math.pow(2, this.wsReconnectAttempts)) + jitter; this.wsReconnectTimer = setTimeout(() => { this.wsReconnectTimer = null; this.wsReconnectAttempts = attempt; this.connectWs(); }, delay); }, createShelfSprite(width, height) { let idx = width + "-" + height; let texture = this.pixiShelfMap.get(idx); if (texture == undefined) { let graphics = this.getContainer('shelf', width, height); texture = this.pixiApp.renderer.generateTexture(graphics); this.pixiShelfMap.set(idx, texture); } return new PIXI.Sprite(texture); }, createTrackSprite(width, height, mask) { const trackMask = mask != null ? mask : 10; let idx = width + "-" + height + "-" + trackMask; let texture = this.pixiTrackMap.get(idx); if (texture == undefined) { texture = this.createTrackTexture(width, height, trackMask); this.pixiTrackMap.set(idx, texture); } return new PIXI.Sprite(texture); }, getContainer(type, width, height) { let graphics = new PIXI.Graphics(); let drawBorder = true; if (type == 'shelf') { graphics.beginFill(0xb6e2e2); } else if (type == 'devp') { graphics.beginFill(0x00ff7f); graphics.visible = true; } else if (type == 'crn') { graphics.beginFill(0xaaffff); } if (drawBorder) { graphics.lineStyle(1, 0xffffff, 1); graphics.drawRect(0, 0, width, height); } graphics.endFill(); return graphics; }, createTrackTexture(width, height, mask) { const TRACK_N = 1; const TRACK_E = 2; const TRACK_S = 4; const TRACK_W = 8; const trackMask = mask != null ? mask : (TRACK_E | TRACK_W); const g = new PIXI.Graphics(); const size = Math.max(1, Math.min(width, height)); const rail = Math.max(2, Math.round(size * 0.12)); const gap = Math.max(4, Math.round(size * 0.38)); const midX = Math.round(width / 2); const midY = Math.round(height / 2); const y1 = midY - Math.round(gap / 2); const y2 = midY + Math.round(gap / 2); const x1 = midX - Math.round(gap / 2); const x2 = midX + Math.round(gap / 2); const hasN = (trackMask & TRACK_N) !== 0; const hasE = (trackMask & TRACK_E) !== 0; const hasS = (trackMask & TRACK_S) !== 0; const hasW = (trackMask & TRACK_W) !== 0; const hStart = hasW ? 0 : midX; const hEnd = hasE ? width : midX; const vStart = hasN ? 0 : midY; const vEnd = hasS ? height : midY; const railColor = 0x555555; const drawLine = (x1p, y1p, x2p, y2p, w, color) => { g.lineStyle(w, color, 1); g.moveTo(x1p, y1p); g.lineTo(x2p, y2p); }; const hasH = hasW || hasE; const hasV = hasN || hasS; const isCorner = hasH && hasV && !(hasW && hasE) && !(hasN && hasS); if (hasH && !isCorner) { const w = Math.max(1, hEnd - hStart); g.beginFill(railColor); g.drawRect(hStart, midY - Math.round(rail / 2), w, rail); g.endFill(); } if (hasV && !isCorner) { const h = Math.max(1, vEnd - vStart); g.beginFill(railColor); g.drawRect(midX - Math.round(rail / 2), vStart, rail, h); g.endFill(); } if (isCorner) { const cw = hasE; const ch = hasS; const cx = cw ? (width - 1) : 0; const cy = ch ? (height - 1) : 0; const angStart = (cw && ch) ? Math.PI : (cw ? Math.PI / 2 : (ch ? -Math.PI / 2 : 0)); const angEnd = (cw && ch) ? Math.PI * 1.5 : (cw ? Math.PI : (ch ? 0 : Math.PI / 2)); const rX = Math.abs(cx - midX); const rY = Math.abs(cy - midY); const rMid = Math.min(rX, rY); g.lineStyle(rail, railColor, 1); g.arc(cx, cy, rMid, angStart, angEnd); g.lineStyle(0, 0, 0); } // no sleepers; keep a single continuous line const rt = PIXI.RenderTexture.create({ width: width, height: height }); this.pixiApp.renderer.render(g, rt); return rt; }, createCrnTexture(width, height) { const g = new PIXI.Graphics(); const yTop = Math.round(height * 0.1); let deviceWidth = width * 2; g.beginFill(0x999999); g.drawRect(2, yTop, 3, height - yTop - 2); g.drawRect(deviceWidth - 5, yTop, 3, height - yTop - 2); g.endFill(); g.beginFill(0x999999); g.drawRect(0, yTop, deviceWidth, 3); g.endFill(); const cabW = Math.round(deviceWidth * 0.68); const cabH = Math.round(height * 0.38); const cabX = Math.round((deviceWidth - cabW) / 2); const cabY = Math.round(height * 0.52 - cabH / 2); g.beginFill(0x245a9a); g.drawRect(cabX, cabY, cabW, cabH); g.endFill(); const winW = Math.round(cabW * 0.6); const winH = Math.round(cabH * 0.45); const winX = cabX + Math.round((cabW - winW) / 2); const winY = cabY + Math.round((cabH - winH) / 2); g.beginFill(0xd0e8ff); g.drawRect(winX, winY, winW, winH); g.endFill(); const forkW = Math.round(deviceWidth * 0.8); const forkH = Math.max(2, Math.round(height * 0.08)); const forkX = Math.round((deviceWidth - forkW) / 2); const forkY = cabY + cabH; g.beginFill(0x666666); g.drawRect(forkX, forkY, forkW, forkH); g.endFill(); const rt = PIXI.RenderTexture.create({ width: deviceWidth, height: height }); this.pixiApp.renderer.render(g, rt); return rt; }, createCrnTextureColoredDevice(deviceWidth, height, color) { const g = new PIXI.Graphics(); const yTop = Math.round(height * 0.1); g.beginFill(0x999999); g.drawRect(2, yTop, 3, height - yTop - 2); g.drawRect(deviceWidth - 5, yTop, 3, height - yTop - 2); g.endFill(); g.beginFill(0x999999); g.drawRect(0, yTop, deviceWidth, 3); g.endFill(); const cabW = Math.round(deviceWidth * 0.68); const cabH = Math.round(height * 0.38); const cabX = Math.round((deviceWidth - cabW) / 2); const cabY = Math.round(height * 0.52 - cabH / 2); g.beginFill(color); g.drawRect(cabX, cabY, cabW, cabH); g.endFill(); const winW = Math.round(cabW * 0.6); const winH = Math.round(cabH * 0.45); const winX = cabX + Math.round((cabW - winW) / 2); const winY = cabY + Math.round((cabH - winH) / 2); g.beginFill(0xd0e8ff); g.drawRect(winX, winY, winW, winH); g.endFill(); const forkW = Math.round(deviceWidth * 0.8); const forkH = Math.max(2, Math.round(height * 0.08)); const forkX = Math.round((deviceWidth - forkW) / 2); const forkY = cabY + cabH; g.beginFill(0x666666); g.drawRect(forkX, forkY, forkW, forkH); g.endFill(); const rt = PIXI.RenderTexture.create({ width: deviceWidth, height: height }); this.pixiApp.renderer.render(g, rt); return rt; }, createDevpTextureColoredRect(width, height, color) { const g = new PIXI.Graphics(); g.beginFill(color); g.lineStyle(1, 0xffffff, 1); g.drawRect(0, 0, width, height); g.endFill(); const rt = PIXI.RenderTexture.create({ width: width, height: height }); this.pixiApp.renderer.render(g, rt); return rt; }, createRgvTexture(width, height) { const g = new PIXI.Graphics(); const bodyW = Math.round(width * 0.8); const bodyH = Math.round(height * 0.55); const bodyX = Math.round((width - bodyW) / 2); const bodyY = Math.round((height - bodyH) / 2); g.beginFill(0x245a9a); g.drawRect(bodyX, bodyY, bodyW, bodyH); g.endFill(); const winW = Math.round(bodyW * 0.55); const winH = Math.round(bodyH * 0.45); const winX = bodyX + Math.round((bodyW - winW) / 2); const winY = bodyY + Math.round((bodyH - winH) / 2); g.beginFill(0xd0e8ff); g.drawRect(winX, winY, winW, winH); g.endFill(); const wheelW = Math.max(2, Math.round(width * 0.12)); const wheelH = Math.max(2, Math.round(height * 0.1)); const wheelY = bodyY + bodyH; const wheelGap = Math.round((width - wheelW * 2) / 3); const wheelX1 = wheelGap; const wheelX2 = width - wheelGap - wheelW; g.beginFill(0x333333); g.drawRect(wheelX1, wheelY - Math.round(wheelH / 2), wheelW, wheelH); g.drawRect(wheelX2, wheelY - Math.round(wheelH / 2), wheelW, wheelH); g.endFill(); const rt = PIXI.RenderTexture.create({ width: width, height: height }); this.pixiApp.renderer.render(g, rt); return rt; }, createRgvTextureColoredDevice(width, height, color) { const g = new PIXI.Graphics(); const bodyW = Math.round(width * 0.8); const bodyH = Math.round(height * 0.55); const bodyX = Math.round((width - bodyW) / 2); const bodyY = Math.round((height - bodyH) / 2); g.beginFill(color); g.drawRect(bodyX, bodyY, bodyW, bodyH); g.endFill(); const winW = Math.round(bodyW * 0.55); const winH = Math.round(bodyH * 0.45); const winX = bodyX + Math.round((bodyW - winW) / 2); const winY = bodyY + Math.round((bodyH - winH) / 2); g.beginFill(0xd0e8ff); g.drawRect(winX, winY, winW, winH); g.endFill(); const wheelW = Math.max(2, Math.round(width * 0.12)); const wheelH = Math.max(2, Math.round(height * 0.1)); const wheelY = bodyY + bodyH; const wheelGap = Math.round((width - wheelW * 2) / 3); const wheelX1 = wheelGap; const wheelX2 = width - wheelGap - wheelW; g.beginFill(0x333333); g.drawRect(wheelX1, wheelY - Math.round(wheelH / 2), wheelW, wheelH); g.drawRect(wheelX2, wheelY - Math.round(wheelH / 2), wheelW, wheelH); g.endFill(); const rt = PIXI.RenderTexture.create({ width: width, height: height }); this.pixiApp.renderer.render(g, rt); return rt; }, updateRgvTextureColor(sprite, color) { const key = Math.round(sprite.width) + '-' + Math.round(sprite.height) + '-' + color; let tex = this.pixiRgvColorTextureMap.get(key); if (!tex) { tex = this.createRgvTextureColoredDevice(Math.round(sprite.width), Math.round(sprite.height), color); this.pixiRgvColorTextureMap.set(key, tex); } sprite.texture = tex; if (sprite.textObj) { const fill = this.getContrastColor(color); sprite.textObj.style.fill = fill; sprite.textObj.style.stroke = (fill === '#000000' ? '#ffffff' : '#000000'); sprite.textObj.style.strokeThickness = 1; } }, updateCrnTextureColor(sprite, color) { const key = Math.round(sprite.width) + '-' + Math.round(sprite.height) + '-' + color; let tex = this.pixiCrnColorTextureMap.get(key); if (!tex) { tex = this.createCrnTextureColoredDevice(Math.round(sprite.width), Math.round(sprite.height), color); this.pixiCrnColorTextureMap.set(key, tex); } sprite.texture = tex; if (sprite.textObj) { const fill = this.getContrastColor(color); sprite.textObj.style.fill = fill; sprite.textObj.style.stroke = (fill === '#000000' ? '#ffffff' : '#000000'); sprite.textObj.style.strokeThickness = 1; } }, getContrastColor(color) { const r = (color >> 16) & 0xFF; const g = (color >> 8) & 0xFF; const b = color & 0xFF; const brightness = (r * 299 + g * 587 + b * 114) / 1000; return brightness > 150 ? '#000000' : '#ffffff'; }, getCrnStatusColor(status) { if (status === "machine-auto") { return 0x21BA45; } if (status === "machine-un-auto") { return 0xBBBBBB; } if (status === "machine-error") { return 0xDB2828; } if (status === "machine-pakin") { return 0x30bffc; } if (status === "machine-pakout") { return 0x97b400; } return 0xBBBBBB; }, getRgvStatusColor(status) { if (status === "idle") { return 0x21BA45; } if (status === "working") { return 0xffd60b; } if (status === "waiting") { return 0xffd60b; } if (status === "fetching") { return 0xffd60b; } if (status === "putting") { return 0xffd60b; } return 0xb8b8b8; }, getSprite(item, pointerDownEvent) { let sprite; let value = item.value; if (item.type == 'shelf') { sprite = this.createShelfSprite(item.width, item.height); sprite._kind = 'shelf'; } else if (item.type == 'devp') { const key = Math.round(item.width) + '-' + Math.round(item.height) + '-' + 0x00ff7f; let texture = this.pixiDevpTextureMap.get(key); if (!texture) { texture = this.createDevpTextureColoredRect(Math.round(item.width), Math.round(item.height), 0x00ff7f); this.pixiDevpTextureMap.set(key, texture); } sprite = new PIXI.Sprite(texture); sprite._kind = 'devp'; let siteId = this.getStationId(value); if (siteId === -1) { siteId = item.data; } const style = new PIXI.TextStyle({ fontFamily: 'Arial', fontSize: 10, fill: '#000000', stroke: '#ffffff', strokeThickness: 1 }); const text = new PIXI.Text(String(siteId), style); text.anchor.set(0.5); text.position.set(sprite.width / 2, sprite.height / 2); sprite.addChild(text); sprite.textObj = text; if (siteId != null && siteId !== -1) { this.pixiStaMap.set(parseInt(siteId), sprite); } sprite.interactive = true; sprite.buttonMode = true; sprite.on('pointerdown', () => { if (window.gsap) { window.gsap.killTweensOf(sprite); } sprite.alpha = 1; const id = parseInt(siteId, 10); if (!isNaN(id)) { this.$emit('station-click', id); } }); } else if (item.type == 'crn') { sprite = this.createTrackSprite(item.width, item.height, item.trackMask); sprite._kind = 'crn-track'; if (this.getDeviceNo(value) > 0) { this.crnList.push(item); } } else if (item.type == 'dualCrn') { sprite = this.createTrackSprite(item.width, item.height, item.trackMask); sprite._kind = 'crn-track'; if (this.getDeviceNo(value) > 0) { this.dualCrnList.push(item); } } else if (item.type == 'rgv') { sprite = this.createTrackSprite(item.width, item.height, item.trackMask); sprite._kind = 'rgv-track'; if (this.getDeviceNo(value) > 0) { this.rgvList.push(item); } } else { return null; } sprite.position.set(item.posX, item.posY); return sprite; }, collectTrackItem(item) { const value = item.value; if (item.type === 'crn') { if (this.getDeviceNo(value) > 0) { this.crnList.push(item); } } else if (item.type === 'dualCrn') { if (this.getDeviceNo(value) > 0) { this.dualCrnList.push(item); } } else if (item.type === 'rgv') { if (this.getDeviceNo(value) > 0) { this.rgvList.push(item); } } }, isTrackType(cell) { return cell && (cell.type === 'crn' || cell.type === 'dualCrn' || cell.type === 'rgv'); }, resolveMergedCell(map, rowIndex, colIndex) { if (!map || rowIndex < 0 || colIndex < 0) { return null; } const row = map[rowIndex]; if (!row || colIndex >= row.length) { return null; } const cell = row[colIndex]; if (!cell) { return null; } if (!cell.isMergedPart && cell.type !== 'merge') { return cell; } if (cell.isMergedPart) { for (let c = colIndex - 1; c >= 0; c--) { const left = row[c]; if (!left) { continue; } if (!left.isMergedPart && left.type !== 'merge' && left.posX === cell.posX) { return left; } } } if (cell.type === 'merge') { for (let r = rowIndex - 1; r >= 0; r--) { const upRow = map[r]; if (!upRow || colIndex >= upRow.length) { continue; } const up = upRow[colIndex]; if (!up) { continue; } if (up.type !== 'merge') { return up; } } } return null; }, getTrackMask(map, rowIndex, colIndex) { const TRACK_N = 1; const TRACK_E = 2; const TRACK_S = 4; const TRACK_W = 8; const baseRow = map[rowIndex]; if (!baseRow) { return 0; } const base = baseRow[colIndex]; if (!this.isTrackType(base)) { return 0; } const rowSpan = base.rowSpan || 1; const colSpan = base.colSpan || 1; let mask = 0; const n = this.resolveMergedCell(map, rowIndex - 1, colIndex); const s = this.resolveMergedCell(map, rowIndex + rowSpan, colIndex); const w = this.resolveMergedCell(map, rowIndex, colIndex - 1); const e = this.resolveMergedCell(map, rowIndex, colIndex + colSpan); if (n && n !== base && this.isTrackType(n)) { mask |= TRACK_N; } if (e && e !== base && this.isTrackType(e)) { mask |= TRACK_E; } if (s && s !== base && this.isTrackType(s)) { mask |= TRACK_S; } if (w && w !== base && this.isTrackType(w)) { mask |= TRACK_W; } if (mask === 0) { mask = TRACK_E | TRACK_W; } return mask; }, drawTracks(map) { if (!this.tracksGraphics) { return; } this.tracksGraphics.clear(); const rail = 3; const color = 0x555555; this.tracksGraphics.lineStyle({ width: rail, color: color, alpha: 1, cap: PIXI.LINE_CAP.ROUND, join: PIXI.LINE_JOIN.ROUND }); const drawn = new Set(); const toKey = (p) => { const x = Math.round(p.x * 100) / 100; const y = Math.round(p.y * 100) / 100; return x + "," + y; }; const edgeKey = (a, b) => { const ka = toKey(a); const kb = toKey(b); return ka < kb ? (ka + "|" + kb) : (kb + "|" + ka); }; const centerOf = (cell) => ({ x: cell.posX + cell.width / 2, y: cell.posY + cell.height / 2 }); for (let r = 0; r < map.length; r++) { const row = map[r]; if (!row) { continue; } for (let c = 0; c < row.length; c++) { const cell = row[c]; if (!cell || cell.type === 'merge' || !this.isTrackType(cell) || cell.isMergedPart) { continue; } const rowSpan = cell.rowSpan || 1; const colSpan = cell.colSpan || 1; const n = this.resolveMergedCell(map, r - 1, c); const s = this.resolveMergedCell(map, r + rowSpan, c); const w = this.resolveMergedCell(map, r, c - 1); const e = this.resolveMergedCell(map, r, c + colSpan); const hasN = n && this.isTrackType(n); const hasE = e && this.isTrackType(e); const hasS = s && this.isTrackType(s); const hasW = w && this.isTrackType(w); const count = (hasN ? 1 : 0) + (hasE ? 1 : 0) + (hasS ? 1 : 0) + (hasW ? 1 : 0); const straight = (hasN && hasS) || (hasE && hasW); if (count === 2 && !straight) { const cPos = centerOf(cell); let p1 = null; let p2 = null; if (hasN && hasE) { p1 = centerOf(n); p2 = centerOf(e); } else if (hasE && hasS) { p1 = centerOf(e); p2 = centerOf(s); } else if (hasS && hasW) { p1 = centerOf(s); p2 = centerOf(w); } else if (hasW && hasN) { p1 = centerOf(w); p2 = centerOf(n); } if (p1 && p2) { const k1 = edgeKey(cPos, p1); const k2 = edgeKey(cPos, p2); if (!drawn.has(k1) || !drawn.has(k2)) { this.tracksGraphics.moveTo(p1.x, p1.y); this.tracksGraphics.lineTo(cPos.x, cPos.y); this.tracksGraphics.lineTo(p2.x, p2.y); } drawn.add(k1); drawn.add(k2); } } } } for (let r = 0; r < map.length; r++) { const row = map[r]; if (!row) { continue; } for (let c = 0; c < row.length; c++) { const cell = row[c]; if (!cell || cell.type === 'merge' || !this.isTrackType(cell) || cell.isMergedPart) { continue; } const cPos = centerOf(cell); const rowSpan = cell.rowSpan || 1; const colSpan = cell.colSpan || 1; const e = this.resolveMergedCell(map, r, c + colSpan); const s = this.resolveMergedCell(map, r + rowSpan, c); if (e && this.isTrackType(e)) { const p = centerOf(e); const k = edgeKey(cPos, p); if (!drawn.has(k)) { this.tracksGraphics.moveTo(cPos.x, cPos.y); this.tracksGraphics.lineTo(p.x, p.y); drawn.add(k); } } if (s && this.isTrackType(s)) { const p = centerOf(s); const k = edgeKey(cPos, p); if (!drawn.has(k)) { this.tracksGraphics.moveTo(cPos.x, cPos.y); this.tracksGraphics.lineTo(p.x, p.y); drawn.add(k); } } } } }, updateColor(sprite, color) { if (sprite && sprite._kind === 'devp') { const key = sprite.width + '-' + sprite.height + '-' + color; let texture = this.pixiDevpTextureMap.get(key); if (!texture) { texture = this.createDevpTextureColoredRect(Math.round(sprite.width), Math.round(sprite.height), color); this.pixiDevpTextureMap.set(key, texture); } const textObj = sprite.textObj; sprite.texture = texture; if (textObj) { if (textObj.parent !== sprite) { sprite.addChild(textObj); } textObj.position.set(sprite.width / 2, sprite.height / 2); const fill = this.getContrastColor(color); textObj.style.fill = fill; textObj.style.stroke = (fill === '#000000' ? '#ffffff' : '#000000'); textObj.style.strokeThickness = 1; } return; } sprite.tint = color; }, isJson(str) { try { JSON.parse(str); return true; } catch (e) { return false; } }, getDeviceNo(obj) { if (this.isJson(obj)) { let data = JSON.parse(obj); if (data.deviceNo == null || data.deviceNo == undefined) { return -1; } return data.deviceNo; } else { return -1; } }, getTaskNo(obj) { if (this.isJson(obj)) { let data = JSON.parse(obj); if (data.taskNo == null || data.taskNo == undefined) { return -1; } return data.taskNo; } else { return -1; } }, getStationId(obj) { if (this.isJson(obj)) { let data = JSON.parse(obj); if (data.stationId == null || data.stationId == undefined) { return -1; } return data.stationId; } else { return -1; } }, getTrackSiteNo(obj) { if (this.isJson(obj)) { let data = JSON.parse(obj); if (data.trackSiteNo == null || data.trackSiteNo == undefined) { return -1; } return data.trackSiteNo; } else { return -1; } }, buildShelfHitGrid(map, rowHeights, rowOffsets) { if (!map || !Array.isArray(map)) { return; } this.mapRowOffsets = Array.isArray(rowOffsets) ? rowOffsets.slice() : []; this.mapRowHeights = Array.isArray(rowHeights) ? rowHeights.slice() : []; const rowColOffsets = []; const rowColWidths = []; const rowShelfCells = new Array(map.length); let maxCols = 0; for (let r = 0; r < map.length; r++) { const row = map[r]; if (row && row.length > maxCols) { maxCols = row.length; } rowShelfCells[r] = []; } const colWidths = new Array(maxCols); for (let c = 0; c < maxCols; c++) { let w = null; for (let r = 0; r < map.length; r++) { const cell = map[r] && map[r][c]; if (!cell) { continue; } if (cell.cellWidth != null && cell.cellWidth !== '') { const base = Number(cell.cellWidth); if (isFinite(base) && base > 0) { w = base / 40; break; } } } colWidths[c] = (w && isFinite(w) && w > 0) ? w : 25; } const colOffsets = new Array(maxCols); let xCursor = 0; for (let c = 0; c < maxCols; c++) { colOffsets[c] = xCursor; xCursor += colWidths[c]; } for (let r = 0; r < map.length; r++) { const row = map[r]; if (!row || row.length === 0) { rowColOffsets[r] = []; rowColWidths[r] = []; continue; } const widths = new Array(row.length); for (let c = 0; c < row.length; c++) { const cell = row[c]; let w = null; if (cell && cell.cellWidth != null && cell.cellWidth !== '') { const base = Number(cell.cellWidth); if (isFinite(base) && base > 0) { w = base / 40; } } widths[c] = (w && isFinite(w) && w > 0) ? w : 25; } const offsets = new Array(row.length); let x = 0; for (let c = 0; c < row.length; c++) { offsets[c] = x; x += widths[c]; } rowColOffsets[r] = offsets; rowColWidths[r] = widths; } this.mapColWidths = colWidths; this.mapColOffsets = colOffsets; this.mapRowColOffsets = rowColOffsets; this.mapRowColWidths = rowColWidths; this.mapRowShelfCells = rowShelfCells; for (let r = 0; r < map.length; r++) { const row = map[r]; if (!row) { continue; } for (let c = 0; c < row.length; c++) { const cell = row[c]; if (!cell || cell.type !== 'shelf') { continue; } const startRow = this.findIndexByOffsets(this.mapRowOffsets, this.mapRowHeights, cell.posY + 0.01); const endRow = this.findIndexByOffsets(this.mapRowOffsets, this.mapRowHeights, cell.posY + cell.height - 0.01); if (startRow < 0) { continue; } const last = endRow >= 0 ? endRow : startRow; for (let rr = startRow; rr <= last; rr++) { if (!rowShelfCells[rr]) { rowShelfCells[rr] = []; } rowShelfCells[rr].push(cell); } } } }, findIndexByOffsets(offsets, sizes, value) { if (!offsets || !sizes || offsets.length === 0) { return -1; } for (let i = 0; i < offsets.length; i++) { const start = offsets[i]; const end = start + (sizes[i] || 0); if (value >= start && value < end) { return i; } } return -1; }, updateShelfHoverFromPointer(globalPos) { if (!this.map || !this.mapRoot) { return; } if (!this.mapRowOffsets.length || !this.mapColOffsets.length) { return; } const local = this.mapRoot.toLocal(new PIXI.Point(globalPos.x, globalPos.y)); const rowIndex = this.findIndexByOffsets(this.mapRowOffsets, this.mapRowHeights, local.y); if (rowIndex < 0) { if (this.hoveredShelfCell) { this.hoveredShelfCell = null; this.hideShelfTooltip(); } return; } let cell = null; if (this.mapRowShelfCells && this.mapRowShelfCells[rowIndex]) { const list = this.mapRowShelfCells[rowIndex]; for (let i = 0; i < list.length; i++) { const it = list[i]; if (!it) { continue; } if (local.x >= it.posX && local.x < it.posX + it.width && local.y >= it.posY && local.y < it.posY + it.height) { cell = it; break; } } } if (!cell || cell.type !== 'shelf') { if (this.hoveredShelfCell) { this.hoveredShelfCell = null; this.hideShelfTooltip(); } return; } if (this.hoveredShelfCell !== cell) { this.hoveredShelfCell = cell; this.shelfTooltip.item = cell; this.shelfTooltip.text = this.getShelfArrangeInfo(cell); this.shelfTooltip.visible = true; } this.updateShelfTooltipPositionByGlobal(globalPos); }, normalizeLocTypeKey(value) { if (value == null) { return null; } const str = String(value).trim(); if (!str) { return null; } const parts = str.split('-').filter(p => p !== ''); if (parts.length >= 3) { return parts.slice(0, parts.length - 1).join('-'); } return str; }, loadLocList() { if (!window.$ || typeof baseUrl === 'undefined') { return; } if (this.locListLoading) { return; } this.locListLoading = true; $.ajax({ url: baseUrl + "/console/map/locList", headers: { 'token': localStorage.getItem('token') }, dataType: 'json', method: 'GET', success: (res) => { if (res && !Array.isArray(res)) { if (res.code === 403) { parent.location.href = baseUrl + "/login"; return; } if (res.code !== 200) { return; } } const list = Array.isArray(res) ? res : (res && res.code === 200 ? res.data : null); if (!list || !Array.isArray(list)) { return; } const map = new Map(); list.forEach((item) => { if (!item) { return; } const locType = item.locType != null ? item.locType : item.loc_type; if (locType != null && locType !== '') { const normalizedType = this.normalizeLocTypeKey(locType); if (normalizedType && !map.has(normalizedType)) { map.set(normalizedType, item); } } }); this.locListMap = map; this.locListLoaded = true; if (this.shelfTooltip.visible) { this.shelfTooltip.text = this.getShelfArrangeInfo(this.shelfTooltip.item); } }, complete: () => { this.locListLoading = false; } }); }, showShelfTooltip(e, item) { if (!item) { return; } if (!this.isShelfTooltipAllowed()) { this.hideShelfTooltip(); return; } if (!this.locListLoaded && !this.locListLoading) { this.loadLocList(); } this.shelfTooltip.item = item; this.shelfTooltip.text = this.getShelfArrangeInfo(item); this.updateShelfTooltipPosition(e); this.shelfTooltip.visible = true; }, updateShelfTooltipPosition(e) { if (!e || !e.data || !e.data.global) { return; } this.updateShelfTooltipPositionByGlobal(e.data.global); }, updateShelfTooltipPositionByGlobal(globalPos) { if (!this.isShelfTooltipAllowed()) { this.hideShelfTooltip(); return; } if (!globalPos) { return; } this.shelfTooltip.x = globalPos.x + 12; this.shelfTooltip.y = globalPos.y + 12; }, hideShelfTooltip() { this.shelfTooltip.visible = false; this.shelfTooltip.item = null; }, isShelfTooltipAllowed() { return this.getStageAbsScale() >= this.shelfTooltipMinScale; }, getStageAbsScale() { if (!this.pixiApp || !this.pixiApp.stage) { return 1; } return Math.abs(this.pixiApp.stage.scale.x || 1); }, updateShelfTooltipVisibilityByScale() { if (this.shelfTooltip.visible && !this.isShelfTooltipAllowed()) { this.hideShelfTooltip(); this.hoveredShelfCell = null; } }, getShelfArrangeInfo(item) { const parts = []; const matchKey = this.getShelfMatchKey(item); if (matchKey != null) { parts.push('坐标:' + matchKey); } const locInfo = (matchKey != null) ? this.locListMap.get(matchKey) : null; if (locInfo) { const locNo = locInfo.locNo != null ? locInfo.locNo : locInfo.loc_no; const displayLocNo = this.stripLocLayer(locNo); if (displayLocNo != null) { parts.push('排列:' + displayLocNo); } } return parts.join(' '); }, getShelfMatchKey(item) { if (!item) { return null; } const direct = item.locType != null ? item.locType : (item.loc_type != null ? item.loc_type : null); const directKey = this.normalizeLocTypeKey(direct); if (directKey) { return directKey; } const rowIndex = item.rowIndex; const colIndex = item.colIndex; if (rowIndex == null || colIndex == null) { return null; } const key0 = rowIndex + '-' + colIndex; if (this.locListLoaded && this.locListMap && this.locListMap.size > 0) { if (this.locListMap.has(key0)) { return key0; } } return null; }, stripLocLayer(locNo) { if (locNo == null) { return null; } const str = String(locNo).trim(); if (!str) { return null; } const parts = str.split('-').filter(p => p !== ''); if (parts.length >= 3) { return parts.slice(0, parts.length - 1).join('-'); } return str; }, shelfTooltipStyle() { return { position: 'absolute', left: this.shelfTooltip.x + 'px', top: this.shelfTooltip.y + 'px', background: 'rgba(0,0,0,0.75)', color: '#ffffff', padding: '4px 8px', borderRadius: '4px', fontSize: '12px', pointerEvents: 'none', whiteSpace: 'nowrap', zIndex: 10 }; }, adjustLabelScale() { const s = this.pixiApp && this.pixiApp.stage ? Math.abs(this.pixiApp.stage.scale.x || 1) : 1; const minPx = 14; const vw = this.pixiApp.view.width; const vh = this.pixiApp.view.height; const margin = 50; const mirrorSign = this.mapMirrorX ? -1 : 1; const inverseRotation = -((this.mapRotation % 360) * Math.PI / 180); const tmpPoint = new PIXI.Point(); this.pixiStaMap && this.pixiStaMap.forEach((sprite) => { const textObj = sprite && sprite.textObj; if (!textObj) { return; } const base = (textObj.style && textObj.style.fontSize) ? textObj.style.fontSize : 10; let scale = minPx / (base * s); if (!isFinite(scale)) { scale = 1; } scale = Math.max(0.8, Math.min(scale, 3)); textObj.scale.set(scale * mirrorSign, scale); textObj.rotation = inverseRotation; textObj.position.set(sprite.width / 2, sprite.height / 2); sprite.getGlobalPosition(tmpPoint); const on = tmpPoint.x >= -margin && tmpPoint.y >= -margin && tmpPoint.x <= vw + margin && tmpPoint.y <= vh + margin; textObj.visible = (s >= 0.25) && on; }); this.pixiCrnMap && this.pixiCrnMap.forEach((sprite) => { const textObj = sprite && sprite.textObj; if (!textObj) { return; } const base = (textObj.style && textObj.style.fontSize) ? textObj.style.fontSize : 12; let scale = minPx / (base * s); if (!isFinite(scale)) { scale = 1; } scale = Math.max(0.8, Math.min(scale, 3)); textObj.scale.set(scale * mirrorSign, scale); textObj.rotation = inverseRotation; textObj.position.set(sprite.width / 2, sprite.height / 2); sprite.getGlobalPosition(tmpPoint); const on = tmpPoint.x >= -margin && tmpPoint.y >= -margin && tmpPoint.x <= vw + margin && tmpPoint.y <= vh + margin; textObj.visible = (s >= 0.25) && on; }); this.pixiDualCrnMap && this.pixiDualCrnMap.forEach((sprite) => { const textObj = sprite && sprite.textObj; if (!textObj) { return; } const base = (textObj.style && textObj.style.fontSize) ? textObj.style.fontSize : 12; let scale = minPx / (base * s); if (!isFinite(scale)) { scale = 1; } scale = Math.max(0.8, Math.min(scale, 3)); textObj.scale.set(scale * mirrorSign, scale); textObj.rotation = inverseRotation; textObj.position.set(sprite.width / 2, sprite.height / 2); sprite.getGlobalPosition(tmpPoint); const on = tmpPoint.x >= -margin && tmpPoint.y >= -margin && tmpPoint.x <= vw + margin && tmpPoint.y <= vh + margin; textObj.visible = (s >= 0.25) && on; }); this.pixiRgvMap && this.pixiRgvMap.forEach((sprite) => { const textObj = sprite && sprite.textObj; if (!textObj) { return; } const base = (textObj.style && textObj.style.fontSize) ? textObj.style.fontSize : 12; let scale = minPx / (base * s); if (!isFinite(scale)) { scale = 1; } scale = Math.max(0.8, Math.min(scale, 3)); textObj.scale.set(scale * mirrorSign, scale); textObj.rotation = inverseRotation; textObj.position.set(sprite.width / 2, sprite.height / 2); sprite.getGlobalPosition(tmpPoint); const on = tmpPoint.x >= -margin && tmpPoint.y >= -margin && tmpPoint.x <= vw + margin && tmpPoint.y <= vh + margin; textObj.visible = (s >= 0.25) && on; }); }, rotateMap() { this.mapRotation = (this.mapRotation + 90) % 360; this.applyMapTransform(true); this.saveMapTransformConfig(); }, toggleMirror() { this.mapMirrorX = !this.mapMirrorX; this.applyMapTransform(true); this.saveMapTransformConfig(); }, parseRotation(value) { const num = parseInt(value, 10); if (!isFinite(num)) { return 0; } const rot = ((num % 360) + 360) % 360; return (rot === 90 || rot === 180 || rot === 270) ? rot : 0; }, parseMirror(value) { if (value === true || value === false) { return value; } if (value == null) { return false; } const str = String(value).toLowerCase(); return str === '1' || str === 'true' || str === 'y'; }, loadMapTransformConfig() { if (!window.$ || typeof baseUrl === 'undefined') { return; } $.ajax({ url: baseUrl + "/config/listAll/auth", headers: { 'token': localStorage.getItem('token') }, dataType: 'json', method: 'GET', success: (res) => { if (!res || res.code !== 200 || !Array.isArray(res.data)) { if (res && res.code === 403) { parent.location.href = baseUrl + "/login"; } return; } const byCode = {}; res.data.forEach((item) => { if (item && item.code) { byCode[item.code] = item; } }); const rotateCfg = byCode[this.mapConfigCodes.rotate]; const mirrorCfg = byCode[this.mapConfigCodes.mirror]; if (rotateCfg && rotateCfg.value != null) { this.mapRotation = this.parseRotation(rotateCfg.value); } if (mirrorCfg && mirrorCfg.value != null) { this.mapMirrorX = this.parseMirror(mirrorCfg.value); } if (rotateCfg == null || mirrorCfg == null) { this.createMapTransformConfigIfMissing(rotateCfg, mirrorCfg); } if (this.mapContentSize && this.mapContentSize.width > 0) { this.applyMapTransform(true); } } }); }, createMapTransformConfigIfMissing(rotateCfg, mirrorCfg) { if (!window.$ || typeof baseUrl === 'undefined') { return; } const createList = []; if (!rotateCfg) { createList.push({ name: '地图旋转', code: this.mapConfigCodes.rotate, value: String(this.mapRotation || 0), type: 1, status: 1, selectType: 'map' }); } if (!mirrorCfg) { createList.push({ name: '地图镜像', code: this.mapConfigCodes.mirror, value: this.mapMirrorX ? '1' : '0', type: 1, status: 1, selectType: 'map' }); } createList.forEach((cfg) => { $.ajax({ url: baseUrl + "/config/add/auth", headers: { 'token': localStorage.getItem('token') }, method: 'POST', data: cfg }); }); }, saveMapTransformConfig() { if (!window.$ || typeof baseUrl === 'undefined') { return; } const updateList = [ { code: this.mapConfigCodes.rotate, value: String(this.mapRotation || 0) }, { code: this.mapConfigCodes.mirror, value: this.mapMirrorX ? '1' : '0' } ]; $.ajax({ url: baseUrl + "/config/updateBatch", headers: { 'token': localStorage.getItem('token') }, data: JSON.stringify(updateList), dataType: 'json', contentType: 'application/json;charset=UTF-8', method: 'POST' }); }, getTransformedContentSize() { const size = this.mapContentSize || { width: 0, height: 0 }; const w = size.width || 0; const h = size.height || 0; const rot = ((this.mapRotation % 360) + 360) % 360; const swap = rot === 90 || rot === 270; return { width: swap ? h : w, height: swap ? w : h }; }, fitStageToContent() { if (!this.pixiApp || !this.mapContentSize) { return; } const size = this.getTransformedContentSize(); const contentW = size.width || 0; const contentH = size.height || 0; if (contentW <= 0 || contentH <= 0) { return; } const vw = this.pixiApp.view.width; const vh = this.pixiApp.view.height; let scale = Math.min(vw / contentW, vh / contentH) * 0.95; if (!isFinite(scale) || scale <= 0) { scale = 1; } const baseW = this.mapContentSize.width || contentW; const baseH = this.mapContentSize.height || contentH; const mirrorX = this.mapMirrorX ? -1 : 1; const scaleX = scale * mirrorX; const scaleY = scale; const posX = (vw / 2) - (baseW / 2) * scaleX; const posY = (vh / 2) - (baseH / 2) * scaleY; this.pixiApp.stage.setTransform(posX, posY, scaleX, scaleY, 0, 0, 0, 0, 0); }, applyMapTransform(fitToView) { if (!this.mapRoot || !this.mapContentSize) { return; } const contentW = this.mapContentSize.width || 0; const contentH = this.mapContentSize.height || 0; if (contentW <= 0 || contentH <= 0) { return; } this.mapRoot.pivot.set(contentW / 2, contentH / 2); this.mapRoot.position.set(contentW / 2, contentH / 2); this.mapRoot.rotation = (this.mapRotation % 360) * Math.PI / 180; this.mapRoot.scale.set(1, 1); if (fitToView) { this.fitStageToContent(); } this.scheduleAdjustLabels(); }, scheduleAdjustLabels() { if (this.adjustLabelTimer) { clearTimeout(this.adjustLabelTimer); } this.adjustLabelTimer = setTimeout(() => { this.adjustLabelScale(); this.updateShelfTooltipVisibilityByScale(); this.adjustLabelTimer = null; }, 20); } } });