From f31d9efb6b5a5ab91ba59337d04d3983829486ad Mon Sep 17 00:00:00 2001 From: lumixraku Date: Thu, 28 Nov 2024 20:44:42 +0800 Subject: [PATCH] fix: add unitId for engine --- .../src/services/selection/text-range.ts | 2 +- packages/engine-render/src/base-object.ts | 10 - packages/engine-render/src/engine.ts | 106 ++++--- packages/engine-render/src/index.ts | 1 - packages/engine-render/src/layer.ts | 2 - .../render-manager/render-manager.service.ts | 2 +- .../engine-render/src/scene.input-manager.ts | 2 + packages/engine-render/src/scene.ts | 20 +- packages/engine-render/src/thin-scene.ts | 271 ------------------ .../src/facade/__tests__/create-test-bed.ts | 5 +- 10 files changed, 92 insertions(+), 329 deletions(-) delete mode 100644 packages/engine-render/src/thin-scene.ts diff --git a/packages/docs-ui/src/services/selection/text-range.ts b/packages/docs-ui/src/services/selection/text-range.ts index 7093f5e6ae12..4ff510ee8e3e 100644 --- a/packages/docs-ui/src/services/selection/text-range.ts +++ b/packages/docs-ui/src/services/selection/text-range.ts @@ -476,8 +476,8 @@ export class TextRange implements IDocRange { } this._anchorShape = anchor; - this._scene.addObject(anchor, TEXT_RANGE_LAYER_INDEX); + this.activeStatic(); } private _setCursorList(cursorList: ITextRange[]) { diff --git a/packages/engine-render/src/base-object.ts b/packages/engine-render/src/base-object.ts index 97a167e64841..f173aa9d2df1 100644 --- a/packages/engine-render/src/base-object.ts +++ b/packages/engine-render/src/base-object.ts @@ -676,16 +676,6 @@ export abstract class BaseObject extends Disposable { return true; } - // triggerKeyDown(evt: IKeyboardEvent) { - // // this.onKeyDownObservable.notifyObservers(evt); - // this._parent?.triggerKeyDown(evt); - // } - - // triggerKeyUp(evt: IKeyboardEvent) { - // // this.onKeyUpObservable.notifyObservers(evt); - // this._parent?.triggerKeyUp(evt); - // } - triggerPointerOut(evt: IPointerEvent | IMouseEvent) { if (!this.onPointerOut$.emitEvent(evt)?.stopPropagation) { this._parent?.triggerPointerOut(evt); diff --git a/packages/engine-render/src/engine.ts b/packages/engine-render/src/engine.ts index a62ede913e8d..a6bcc7e27d63 100644 --- a/packages/engine-render/src/engine.ts +++ b/packages/engine-render/src/engine.ts @@ -30,6 +30,13 @@ import { getPointerPrefix, getSizeForDom, IsSafari, requestNewFrame } from './ba import { Canvas, CanvasRenderMode } from './canvas'; import { observeClientRect } from './floating/util'; +export interface IEngineOption { + elementWidth: number; + elementHeight: number; + dpr?: number; + renderMode?: CanvasRenderMode; +} + export class Engine extends Disposable { renderEvenInBackground = true; @@ -41,6 +48,9 @@ export class Engine extends Disposable { readonly renderFrameTimeMetric$ = new Subject(); readonly renderFrameTags$ = new Subject<[string, any]>(); + /** + * Pass event to scene.input-manager + */ onInputChanged$ = new EventSubject(); onTransformChange$ = new EventSubject(); @@ -79,8 +89,6 @@ export class Engine extends Disposable { private _renderFrameTasks = new Array<() => void>(); - private _renderFunction = (_timestamp: number) => { /* empty */ }; - private _requestNewFrameHandler: number = -1; /** @@ -140,10 +148,37 @@ export class Engine extends Disposable { private _unitId: string = ''; // unitId - constructor(elemWidth: number = 1, elemHeight: number = 1, pixelRatio?: number, mode?: CanvasRenderMode) { + constructor(); + constructor(unitId: string, options?: IEngineOption); + constructor(elemW: number, elemH: number, dpr?: number, renderMode?: CanvasRenderMode); + constructor(...args: any[]) { super(); + let elemWidth = 1; + let elemHeight = 1; + let pixelRatio = 1; + let renderMode = CanvasRenderMode.Rendering; + + if (args[0] && typeof args[0] === 'string') { + this._unitId = args[0]; + const options = args[1] ?? { + elemWidth: 1, + elemHeight: 1, + pixelRatio: 1, + renderMode: CanvasRenderMode.Rendering, + }; + elemWidth = options.elementWidth; + elemHeight = options.elementHeight; + pixelRatio = options.pixelRatio ?? 1; + renderMode = options.renderMode ?? CanvasRenderMode.Rendering; + } else { + elemWidth = args[0] ?? 1; + elemHeight = args[1] ?? 1; + pixelRatio = args[2] ?? 1; + renderMode = args[3] ?? CanvasRenderMode.Rendering; + } + this._canvas = new Canvas({ - mode, + mode: renderMode, width: elemWidth, height: elemHeight, pixelRatio, @@ -153,7 +188,7 @@ export class Engine extends Disposable { this._handlePointerAction(); this._handleDragAction(); - if (mode !== CanvasRenderMode.Printing) { + if (renderMode !== CanvasRenderMode.Printing) { this._matchMediaHandler(); } } @@ -162,6 +197,10 @@ export class Engine extends Disposable { this._performanceMonitor = new PerformanceMonitor(); } + get unitId(): string { + return this._unitId; + } + get elapsedTime(): number { return Tools.now() - this._renderStartTime; } @@ -380,7 +419,7 @@ export class Engine extends Disposable { if (!this._renderingQueueLaunched) { this._renderStartTime = performance.now(); this._renderingQueueLaunched = true; - this._renderFunction = this._renderFunctionCore.bind(this); + // this._renderFunction = this._renderFunctionCore.bind(this); this._requestNewFrameHandler = requestNewFrame(this._renderFunction); } } @@ -394,6 +433,31 @@ export class Engine extends Disposable { this.startRenderLoop(); } + /** + * call itself by raf + * Exec all function in _renderFrameTasks in _renderFrame() + */ + private _renderFunction = (timestamp: number) => { + let shouldRender = true; + if (!this.renderEvenInBackground) { + shouldRender = false; + } + + if (shouldRender) { + // Start new frame + this._beginFrame(timestamp); + // exec functions in _renderFrameTasks + this._renderFrame(timestamp); + this._endFrame(timestamp); + } + + if (this._renderFrameTasks.length > 0) { + this._requestNewFrameHandler = requestNewFrame(this._renderFunction); + } else { + this._renderingQueueLaunched = false; + } + }; + /** * stop executing a render loop function and remove it from the execution array * @param renderFunction defines the function to be removed. If not provided all functions will be removed. @@ -492,38 +556,11 @@ export class Engine extends Disposable { return window; } - /** - * call itself by raf - * Exec all function in _renderFrameTasks in _renderFrame() - */ - private _renderFunctionCore(timestamp: number): void { - let shouldRender = true; - if (!this.renderEvenInBackground) { - shouldRender = false; - } - - if (shouldRender) { - // Start new frame - this._beginFrame(timestamp); - this._renderFrame(timestamp); - this._endFrame(timestamp); - } - - if (this._renderFrameTasks.length > 0) { - this._requestNewFrameHandler = requestNewFrame(this._renderFunction); - } else { - this._renderingQueueLaunched = false; - } - } - private _handleKeyboardAction() { const keyboardDownEvent = (evt: KeyboardEvent) => { const deviceEvent = evt as unknown as IKeyboardEvent; deviceEvent.deviceType = DeviceType.Keyboard; deviceEvent.inputIndex = evt.keyCode; - // deviceEvent.previousState = 0; - // deviceEvent.currentState = 1; - this.onInputChanged$.emitEvent(deviceEvent); }; @@ -531,9 +568,6 @@ export class Engine extends Disposable { const deviceEvent = evt as unknown as IKeyboardEvent; deviceEvent.deviceType = DeviceType.Keyboard; deviceEvent.inputIndex = evt.keyCode; - // deviceEvent.previousState = 1; - // deviceEvent.currentState = 0; - this.onInputChanged$.emitEvent(deviceEvent); }; diff --git a/packages/engine-render/src/index.ts b/packages/engine-render/src/index.ts index 31f9bf4fb00a..b0e1026aff33 100644 --- a/packages/engine-render/src/index.ts +++ b/packages/engine-render/src/index.ts @@ -49,5 +49,4 @@ export * from './scene-viewer'; export * from './scroll-timer'; export * from './shape'; export { ThinEngine } from './thin-engine'; -export { ThinScene } from './thin-scene'; export * from './viewport'; diff --git a/packages/engine-render/src/layer.ts b/packages/engine-render/src/layer.ts index e067faf13791..1462d3e4e580 100644 --- a/packages/engine-render/src/layer.ts +++ b/packages/engine-render/src/layer.ts @@ -125,7 +125,6 @@ export class Layer extends Disposable { this._objects.push(o); this.scene.setObjectBehavior(o); this._layerBehavior(o); - return this; } @@ -189,7 +188,6 @@ export class Layer extends Disposable { makeDirty(state: boolean = true) { this._dirty = state; - /** * parent is SceneViewer, make it dirty */ diff --git a/packages/engine-render/src/render-manager/render-manager.service.ts b/packages/engine-render/src/render-manager/render-manager.service.ts index 2b18d77430c8..78d95a80bba3 100644 --- a/packages/engine-render/src/render-manager/render-manager.service.ts +++ b/packages/engine-render/src/render-manager/render-manager.service.ts @@ -196,7 +196,7 @@ export class RenderManagerService extends Disposable implements IRenderManagerSe * @returns renderUnit:IRender */ createRender(unitId: string): IRender { - const renderer = this._createRender(unitId, new Engine()); + const renderer = this._createRender(unitId, new Engine(unitId)); this._renderCreated$.next(renderer); return renderer; } diff --git a/packages/engine-render/src/scene.input-manager.ts b/packages/engine-render/src/scene.input-manager.ts index 74609c2aad5d..59ba177aa9ea 100644 --- a/packages/engine-render/src/scene.input-manager.ts +++ b/packages/engine-render/src/scene.input-manager.ts @@ -210,10 +210,12 @@ export class InputManager extends Disposable { } _onKeyDown(evt: IKeyboardEvent) { + // currently nobody using this. use `fromGlobalEvent('keydown')` from rx.js instead. this._scene.onKeyDown$.emitEvent(evt); } _onKeyUp(evt: IKeyboardEvent) { + // currently nobody using this. use `fromGlobalEvent('keyup')` from rx.js instead. this._scene.onKeyUp$.emitEvent(evt); } diff --git a/packages/engine-render/src/scene.ts b/packages/engine-render/src/scene.ts index ee10effecf7b..256d41e57762 100644 --- a/packages/engine-render/src/scene.ts +++ b/packages/engine-render/src/scene.ts @@ -79,7 +79,15 @@ export class Scene extends Disposable { onDblclick$ = new EventSubject(); onTripleClick$ = new EventSubject(); onMouseWheel$ = new EventSubject(); + + /** + * @deprecated use `fromGlobalEvent('keydown')` from rx.js instead. + */ onKeyDown$ = new EventSubject(); + + /** + * @deprecated use `fromGlobalEvent('keyup')` from rx.js instead. + */ onKeyUp$ = new EventSubject(); private _beforeRender$ = new BehaviorSubject>(null); @@ -1004,19 +1012,19 @@ export class Scene extends Disposable { return isPickedObject; } - triggerKeyDown(evt: IKeyboardEvent) { - this.onKeyDown$.emitEvent(evt); + // triggerKeyDown(evt: IKeyboardEvent) { + // this.onKeyDown$.emitEvent(evt); // if (this._parent instanceof SceneViewer) { // this._parent?.triggerKeyDown(evt); // } - } + // } - triggerKeyUp(evt: IKeyboardEvent) { - this.onKeyUp$.emitEvent(evt); + // triggerKeyUp(evt: IKeyboardEvent) { + // this.onKeyUp$.emitEvent(evt); // if (this._parent instanceof SceneViewer) { // this._parent?.triggerKeyUp(evt); // } - } + // } triggerPointerUp(evt: IPointerEvent | IMouseEvent) { if ( diff --git a/packages/engine-render/src/thin-scene.ts b/packages/engine-render/src/thin-scene.ts deleted file mode 100644 index b31066863cd3..000000000000 --- a/packages/engine-render/src/thin-scene.ts +++ /dev/null @@ -1,271 +0,0 @@ -/** - * Copyright 2023-present DreamNum Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import type { Nullable } from '@univerjs/core'; -import type { BaseObject } from './base-object'; - -import type { CURSOR_TYPE } from './basics/const'; -import type { IDragEvent, IKeyboardEvent, IMouseEvent, IPointerEvent, IWheelEvent } from './basics/i-events'; -import type { ITransformChangeState } from './basics/interfaces'; -import type { Vector2 } from './basics/vector2'; -import type { UniverRenderingContext } from './context'; -import type { Engine } from './engine'; -import { Disposable, EventSubject } from '@univerjs/core'; -import { RENDER_CLASS_TYPE } from './basics/const'; -import { Transform } from './basics/transform'; - -export abstract class ThinScene extends Disposable { - onTransformChange$ = new EventSubject(); - - onFileLoaded$ = new EventSubject(); - - onPointerDown$ = new EventSubject(); - - onPointerMove$ = new EventSubject(); - - onPointerUp$ = new EventSubject(); - - onPointerEnter$ = new EventSubject(); - - onPointerOut$ = new EventSubject(); - - onPointerCancel$ = new EventSubject(); - - onPointerLeave$ = new EventSubject(); - - onDragEnter$ = new EventSubject(); - - onDragOver$ = new EventSubject(); - - onDragLeave$ = new EventSubject(); - - onDrop$ = new EventSubject(); - - onDblclick$ = new EventSubject(); - - onTripleClick$ = new EventSubject(); - - onMouseWheel$ = new EventSubject(); - - onKeyDown$ = new EventSubject(); - - onKeyUp$ = new EventSubject(); - - debounceParentTimeout: number = -1; - - private _sceneKey: string = ''; - - private _width: number = 100; - - private _height: number = 100; - - private _scaleX: number = 1; - - private _scaleY: number = 1; - - private _transform = new Transform(); - - private _evented = true; - - constructor(sceneKey: string) { - super(); - this._sceneKey = sceneKey; - } - - get classType() { - return RENDER_CLASS_TYPE.SCENE; - } - - get transform() { - return this._transform; - } - - get width() { - return this._width; - } - - get height() { - return this._height; - } - - get scaleX() { - return this._scaleX; - } - - get scaleY() { - return this._scaleY; - } - - get sceneKey() { - return this._sceneKey; - } - - get objectsEvented() { - return this._evented; - } - - set transform(trans: Transform) { - this._transform = trans; - } - - set width(num: number) { - this._width = num; - } - - set height(num: number) { - this._height = num; - } - - set scaleX(scaleX: number) { - this._scaleX = scaleX; - } - - set scaleY(scaleY: number) { - this._scaleY = scaleY; - } - - enableObjectsEvent() { - this._evented = true; - } - - /** - * If scene.event is disabled, scene.pick(curosrPos) return null. - * Then only scene itself can response to pointer event, all objects under the scene would not. - * see sceneInputManager@_onPointerMove - */ - disableObjectsEvent() { - this._evented = false; - } - - triggerKeyDown(evt: IKeyboardEvent) { - this.onKeyDown$.emitEvent(evt); - // if (this._parent instanceof SceneViewer) { - // this._parent?.triggerKeyDown(evt); - // } - } - - triggerKeyUp(evt: IKeyboardEvent) { - this.onKeyUp$.emitEvent(evt); - // if (this._parent instanceof SceneViewer) { - // this._parent?.triggerKeyUp(evt); - // } - } - - abstract triggerPointerUp(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerMouseWheel(evt: IWheelEvent): void; - - abstract triggerPointerMove(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerDblclick(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerTripleClick(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerPointerDown(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerPointerOut(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerPointerLeave(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerPointerOver(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerPointerEnter(evt: IPointerEvent | IMouseEvent): void; - - abstract triggerDragEnter(evt: IDragEvent | IMouseEvent): void; - - abstract triggerDragOver(evt: IDragEvent | IMouseEvent): void; - - abstract triggerDragLeave(evt: IDragEvent | IMouseEvent): void; - - abstract triggerDrop(evt: IDragEvent | IMouseEvent): void; - - abstract render(parentCtx?: UniverRenderingContext): void; - - abstract getParent(): any; - - override dispose(): void { - this.onTransformChange$.complete(); - this.onFileLoaded$.complete(); - this.onPointerDown$.complete(); - this.onPointerMove$.complete(); - this.onPointerUp$.complete(); - this.onPointerEnter$.complete(); - this.onPointerLeave$.complete(); - this.onDragEnter$.complete(); - this.onDragOver$.complete(); - this.onDragLeave$.complete(); - this.onDrop$.complete(); - this.onDblclick$.complete(); - this.onTripleClick$.complete(); - this.onMouseWheel$.complete(); - this.onKeyDown$.complete(); - this.onKeyUp$.complete(); - this.onPointerOut$.complete(); - this.onPointerCancel$.complete(); - - super.dispose(); - } - - abstract getObject(oKey: string): Nullable; - - abstract addObject(o: BaseObject, zIndex?: number): void; - - abstract addObjects(objects: BaseObject[], zIndex?: number): void; - - abstract getEngine(): Nullable; - - abstract setObjectBehavior(o: BaseObject): void; - - attachTransformerTo(o: BaseObject) { } - - detachTransformerFrom(o: BaseObject) { } - - makeDirtyNoParent(state: boolean = true): ThinScene { - return this; - } - - makeDirty(state: boolean = true) { - return this; - } - - abstract pick(coord: Vector2): Nullable; - - getViewports(): any[] { - return []; - } - - abstract addViewport(...viewport: any[]): void; - - abstract removeViewport(key: string): void; - - getAncestorScale() { - return { - scaleX: 1, - scaleY: 1, - }; - } - - getPrecisionScale() { - return { - scaleX: 1, - scaleY: 1, - }; - } - - abstract setCursor(val: CURSOR_TYPE): void; - - abstract resetCursor(): void; -} diff --git a/packages/sheets-ui/src/facade/__tests__/create-test-bed.ts b/packages/sheets-ui/src/facade/__tests__/create-test-bed.ts index 523cdab5190f..df0d22c4c704 100644 --- a/packages/sheets-ui/src/facade/__tests__/create-test-bed.ts +++ b/packages/sheets-ui/src/facade/__tests__/create-test-bed.ts @@ -95,7 +95,10 @@ export interface ITestBed { class RenderManagerServiceTestBed extends RenderManagerService { override createRender(unitId: string): IRender { - const renderer = this._createRender(unitId, new Engine(100, 100)); + const renderer = this._createRender(unitId, new Engine(unitId, { + elementWidth: 100, + elementHeight: 100, + })); return renderer; } }