import VectorType from "../enum/VectorType.js"; import SelectState from "../enum/SelectState.js"; import VectorCategory from "../enum/VectorCategory.js"; import { dataService } from "./DataService.js"; export default class StateService { constructor() { this.eventName = null; this.selectItem = null; //选中 this.focusItem = null; //点击 this.draggingItem = null; //拖拽 } getEventName() { return this.eventName; } setEventName(eventName) { this.eventName = eventName; } clearEventName() { this.eventName = null; } // type表示类型,state默认是select,但是有的元素需要知道选中的是哪个顶点 setSelectItem(vectorId, type, state, dir) { this.selectItem = {}; this.selectItem.vectorId = vectorId; this.selectItem.type = type; this.selectItem.state = state; this.selectItem.dir = dir; if (type == VectorType.Line) { const line = dataService.getLine(vectorId); this.selectItem.category = line.getCategory(); } else if (type == VectorType.Point) { const point = dataService.getPoint(vectorId); this.selectItem.category = point.getCategory(); } // console.log("选中的元素:" + JSON.stringify(this.selectItem)); } setSelectState(state) { if (!state || !this.selectItem) { return; } this.selectItem.state = state; } getSelectItem() { return this.selectItem; } clearSelectItem() { this.selectItem = null; } getDraggingItem() { return this.draggingItem; } setDraggingItem(draggingItem) { this.draggingItem = draggingItem; if (this.draggingItem == null) { return; } else if (this.draggingItem.type == VectorType.Line) { const line = dataService.getLine(this.draggingItem.vectorId); if (!line) { return; } this.draggingItem.category = line.getCategory(); } else if (this.draggingItem.type == VectorType.Point) { const point = dataService.getPoint(this.draggingItem.vectorId); this.draggingItem.category = point.getCategory(); } } clearDraggingItem() { this.draggingItem = null; } getFocusItem() { return this.focusItem; } setFocusItem(focusItem) { this.focusItem = {}; for (let key in focusItem) { this.focusItem[key] = focusItem[key]; } } clearFocusItem() { this.focusItem = null; } clearItems() { this.selectItem = null; this.focusItem = null; this.draggingItem = null; } clear() { this.clearItems(); this.clearEventName(); } } const stateService = new StateService(); window.stateService = stateService; export { stateService };