template/packages-user/data-base/src/map/eventView.ts
unanmed a76137f2a0 fix(08-05): correct map layer region/event logic (D-10 authorized)
- getMapData(x,y,w,h): end index lineStart+nw and buffer width*height (keep out-of-range zero padding)

- toStatic(keepEvent=false): reset reused static tile to default events via loadState({})

- cropPointEvents: reindex by new width; resize/resize2 sync indexer width; resize2 clears point events on same-size reset

- eventView.reset(): clear dirty counter so dirty() returns false after restoring reference
2026-10-05 15:19:37 +08:00

87 lines
2.5 KiB
TypeScript

import { logger } from '@motajs/common';
import { ILayerEventView } from './types';
import { shouldReplay } from '@user/data-common';
export class LayerEventView implements ILayerEventView {
/** 当前绑定的事件 */
private readonly store: Map<number, string> = new Map();
/** 用于判断事件是否变化的参考基准 */
private reference: Map<number, string> = new Map();
/** 当前存储与参考基准不一致的条目数量 */
private dirtyEntries: number = 0;
[Symbol.iterator]() {
return this.store.entries();
}
get(): ReadonlyMap<number, string> {
return this.store;
}
ref(): ReadonlyMap<number, string> {
return this.reference;
}
/**
* 判断指定优先级当前是否偏离参考基准
* @param priority 事件优先级
*/
private isEntryDirty(priority: number): boolean {
const stored = this.store.get(priority);
const ref = this.reference.get(priority);
return stored !== ref;
}
/**
* 根据指定条目的变更更新脏条目数量
* @param priority 事件优先级
* @param before 条目变更前是否脏
*/
private updateDirtyEntry(priority: number, before: boolean): void {
const after = this.isEntryDirty(priority);
if (before !== after) {
this.dirtyEntries += after ? 1 : -1;
}
}
@shouldReplay('Setting layer event view should be replayed.')
set(priority: number, event: string): void {
if (this.store.has(priority)) {
logger.warn(136, priority.toString());
}
const before = this.isEntryDirty(priority);
this.store.set(priority, event);
this.updateDirtyEntry(priority, before);
}
@shouldReplay('Deleting layer event view should be replayed.')
delete(priority: number): void {
const before = this.isEntryDirty(priority);
this.store.delete(priority);
this.updateDirtyEntry(priority, before);
}
@shouldReplay('Reseting layer event view should be replayed.')
reset(): void {
this.store.clear();
for (const [priority, id] of this.reference) {
this.store.set(priority, id);
}
this.dirtyEntries = 0;
}
clear(): void {
this.store.clear();
this.dirtyEntries = this.reference.size;
}
markPure(): void {
this.reference = new Map(this.store);
this.dirtyEntries = 0;
}
dirty(): boolean {
return this.dirtyEntries > 0;
}
}