Skip to content

Commit

Permalink
fix: add unitId for engine
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Nov 30, 2024
1 parent 4b14e93 commit 2fb4444
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 328 deletions.
2 changes: 1 addition & 1 deletion packages/docs-ui/src/services/selection/text-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down
10 changes: 0 additions & 10 deletions packages/engine-render/src/base-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
106 changes: 70 additions & 36 deletions packages/engine-render/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,6 +48,9 @@ export class Engine extends Disposable {
readonly renderFrameTimeMetric$ = new Subject<ITimeMetric>();
readonly renderFrameTags$ = new Subject<[string, any]>();

/**
* Pass event to scene.input-manager
*/
onInputChanged$ = new EventSubject<IEvent>();

onTransformChange$ = new EventSubject<ITransformChangeState>();
Expand Down Expand Up @@ -79,8 +89,6 @@ export class Engine extends Disposable {

private _renderFrameTasks = new Array<() => void>();

private _renderFunction = (_timestamp: number) => { /* empty */ };

private _requestNewFrameHandler: number = -1;

/**
Expand Down Expand Up @@ -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,
Expand All @@ -153,7 +188,7 @@ export class Engine extends Disposable {
this._handlePointerAction();
this._handleDragAction();

if (mode !== CanvasRenderMode.Printing) {
if (renderMode !== CanvasRenderMode.Printing) {
this._matchMediaHandler();
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -492,48 +556,18 @@ 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);
};

const keyboardUpEvent = (evt: KeyboardEvent) => {
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);
};

Expand Down
1 change: 0 additions & 1 deletion packages/engine-render/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 0 additions & 1 deletion packages/engine-render/src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export class Layer extends Disposable {

makeDirty(state: boolean = true) {
this._dirty = state;

/**
* parent is SceneViewer, make it dirty
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/engine-render/src/scene.input-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 14 additions & 6 deletions packages/engine-render/src/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ export class Scene extends Disposable {
onDblclick$ = new EventSubject<IPointerEvent | IMouseEvent>();
onTripleClick$ = new EventSubject<IPointerEvent | IMouseEvent>();
onMouseWheel$ = new EventSubject<IWheelEvent>();

/**
* @deprecated use `fromGlobalEvent('keydown')` from rx.js instead.
*/
onKeyDown$ = new EventSubject<IKeyboardEvent>();

/**
* @deprecated use `fromGlobalEvent('keyup')` from rx.js instead.
*/
onKeyUp$ = new EventSubject<IKeyboardEvent>();

private _beforeRender$ = new BehaviorSubject<Nullable<Canvas>>(null);
Expand Down Expand Up @@ -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 (
Expand Down
Loading

0 comments on commit 2fb4444

Please sign in to comment.