import * as PIXI from 'pixi.js'; import * as TWEEDLE from 'tweedle.js'; import * as Utils from './utils' export default class Player { constructor(dom, dark, didClickSprite) { this.darkModel = dark; this.didClickSprite = didClickSprite; // init this.app = generatePixiApp(dark); dom.appendChild(this.app.view); globalThis.__PIXI_APP__ = this.app; this.mapContainer = generatePixiContainer('mapContainer'); this.app.stage.addChild(this.mapContainer); this.app.view.addEventListener('contextmenu', (event) => { event.preventDefault(); }); this.pan = false; // 平移 this.activateMapEvent(null, Utils.MapEvent.PAN); this.activateMapScale(); this.showCoordinates(); this.appTicker(); } activateMapEvent = (leftEvent, rightEvent) => { if (this.mapEvent) { this.app.view.removeEventListener('mousedown', this.mapEvent); } this.mapEvent = (event) => { // left if (event.button === 0 && leftEvent) { switch (leftEvent) { case Utils.MapEvent.SELECTION_BOX: this.mapSelect(event); break default: break } } // right if (event.button === 2 && rightEvent) { switch (rightEvent) { case Utils.MapEvent.PAN: this.mapPan(event); break default: break } } } this.app.view.addEventListener('mousedown', this.mapEvent) } mapSelect = (event) => { let isSelecting = false; const selectionBox = new PIXI.Graphics(); this.app.stage.addChild(selectionBox); // start const startPoint = new PIXI.Point(); this.app.renderer.events.mapPositionToPoint(startPoint, event.clientX, event.clientY); let selectionStart = { x: startPoint.x, y: startPoint.y }; isSelecting = true; const handleMouseMove = (event) => { if (isSelecting && !this.didClickSprite) { // end const endPoint = new PIXI.Point(); this.app.renderer.events.mapPositionToPoint(endPoint, event.clientX, event.clientY); const selectionEnd = { x: endPoint.x, y: endPoint.y } const width = Math.abs(selectionEnd.x - selectionStart.x); const height = Math.abs(selectionEnd.y - selectionStart.y); selectionBox.clear(); selectionBox.lineStyle(2, 0xCCCCCC, 1); selectionBox.beginFill(0xCCCCCC, 0.2); selectionBox.drawRect(Math.min(selectionStart.x, selectionEnd.x), Math.min(selectionStart.y, selectionEnd.y), width, height); selectionBox.endFill(); } } this.app.view.addEventListener('mousemove', handleMouseMove); this.app.view.addEventListener('mouseup', (event) => { if (isSelecting) { isSelecting = false; selectionBox.clear(); // const selectedSprites = this.mapContainer.children.filter(sprite => { // const spriteBounds = sprite.getBounds(); // const boxBounds = new PIXI.Rectangle(Math.min(selectionStart.x, event.clientX), Math.min(selectionStart.y, event.clientY), Math.abs(event.clientX - selectionStart.x), Math.abs(event.clientY - selectionStart.y)); // return spriteBounds.contains(boxBounds.x, boxBounds.y) && spriteBounds.contains(boxBounds.x + boxBounds.width, boxBounds.y + boxBounds.height); // }); // console.log('Selected Sprites:', selectedSprites); } this.app.view.removeEventListener('mousemove', handleMouseMove); }); } mapPan = (event) => { this.pan = true; let previousPosition = { x: event.clientX, y: event.clientY }; const mouseMoveHandler = (event) => { if (this.pan) { const dx = event.clientX - previousPosition.x; const dy = event.clientY - previousPosition.y; this.mapContainer.position.x += dx; this.mapContainer.position.y += dy; previousPosition = { x: event.clientX, y: event.clientY }; } }; this.app.view.addEventListener('mousemove', mouseMoveHandler); this.app.view.addEventListener('mouseup', () => { this.app.view.removeEventListener('mousemove', mouseMoveHandler); this.pan = false; }); } activateMapScale = () => { this.scale = 1; // 缩放 this.app.view.addEventListener('wheel', (event) => { event.preventDefault(); const delta = Math.sign(event.deltaY); if (delta === 1) { this.scale *= 0.9; } else if (delta === -1) { this.scale *= 1.1; } this.mapContainer.scale.set(this.scale); this.mapContainer.children.forEach(child => { // child.scale.set(1 / this.scale); // 防止图标变小 }) }); } showCoordinates = () => { const coordinatesText = new PIXI.Text('{ x: 0, y: 0 }', { fill: 0x000000, fontSize: 13, fontFamily: 'MicrosoftYaHei', fontWeight: 'bold', }); coordinatesText.name = 'xyStr' coordinatesText.position.set(10, 10); this.app.stage.addChild(coordinatesText); const mouseMoveInfoTextHandler = (event) => { const mouseX = (event.clientX - this.mapContainer.position.x) / this.scale; const mouseY = (event.clientY - this.mapContainer.position.y) / this.scale; coordinatesText.text = `{ x: ${mouseX.toFixed(2)}, y: ${mouseY.toFixed(2)} }`; }; this.app.view.addEventListener('mousemove', mouseMoveInfoTextHandler); } showGridlines = () => { if (!this.gridLineContainer) { this.gridLineContainer = generatePixiContainer('gridLineContainer'); this.app.stage.addChild(this.gridLineContainer); } const inte = 30; const lineDefaultAlpha = .5;; const lineDefaultColor = 0x000000; for (let i = 0; i < this.app.view.width / inte; i++) { const graphics = new PIXI.Graphics(); graphics.lineStyle(.3, lineDefaultColor, lineDefaultAlpha); graphics.beginFill(lineDefaultColor); graphics.moveTo(i * inte, 0); graphics.lineTo(i * inte, this.app.view.height); graphics.endFill(); this.gridLineContainer.addChild(graphics); } for (let i = 0; i < this.app.view.height / inte; i++) { const graphics = new PIXI.Graphics(); graphics.lineStyle(.3, lineDefaultColor, lineDefaultAlpha); graphics.beginFill(lineDefaultColor); graphics.moveTo(0, i * inte); graphics.lineTo(this.app.view.width, i * inte); graphics.endFill(); this.gridLineContainer.addChild(graphics); } } hideGridlines = () => { if (this.gridLineContainer) { this.app.stage.removeChild(this.gridLineContainer); this.gridLineContainer = null; } } updateDidClickSprite = (value) => { this.didClickSprite = value; } appTicker = () => { TWEEDLE.Group.shared.update(); } } function generatePixiApp(dark) { const app = new PIXI.Application({ background: dark ? '#f1f2f6' : '#f1f2f6', antialias: true, }) app.stage.eventMode = 'static'; app.stage.hitArea = app.screen; return app; } function generatePixiContainer(name) { const mapContainer = new PIXI.Container(); mapContainer.name = name; mapContainer.data = {}; return mapContainer; }