mirror of
https://github.com/motajs/template.git
synced 2026-12-01 19:34:31 +08:00
refactor: 将渲染 core 的 misc 拆成多个文件,调整监听事件写法
This commit is contained in:
1 parent
375d12632e
commit
69109d7372
15 files changed
+614
-392
No files matched your search
@@ -17,7 +17,12 @@ import {
|
||||
ShaderProps,
|
||||
TextProps
|
||||
} from './props';
|
||||
import { IRenderItemBaseHooks } from '@motajs/render';
|
||||
import {
|
||||
IRenderAnimatedHooks,
|
||||
IRenderImageHooks,
|
||||
IRenderItemBaseHooks,
|
||||
IRenderTextHooks
|
||||
} from '@motajs/render';
|
||||
|
||||
export type TagDefine<T extends object, H extends IRenderItemBaseHooks> = T & {
|
||||
[K in keyof H]?: H[K];
|
||||
@@ -32,9 +37,9 @@ declare module 'vue/jsx-runtime' {
|
||||
IRenderItemBaseHooks
|
||||
>;
|
||||
shader: TagDefine<ShaderProps, IRenderItemBaseHooks>;
|
||||
text: TagDefine<TextProps, IRenderItemBaseHooks>;
|
||||
image: TagDefine<ImageProps, IRenderItemBaseHooks>;
|
||||
animated: TagDefine<AnimatedProps, IRenderItemBaseHooks>;
|
||||
text: TagDefine<TextProps, IRenderTextHooks>;
|
||||
image: TagDefine<ImageProps, IRenderImageHooks>;
|
||||
animated: TagDefine<AnimatedProps, IRenderAnimatedHooks>;
|
||||
comment: TagDefine<CommentProps, IRenderItemBaseHooks>;
|
||||
custom: TagDefine<CustomProps, IRenderItemBaseHooks>;
|
||||
'g-rect': TagDefine<RectProps, IRenderItemBaseHooks>;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import {
|
||||
RenderItem,
|
||||
Text,
|
||||
RenderText,
|
||||
Comment,
|
||||
IRenderItem,
|
||||
IRenderTreeRoot
|
||||
@@ -47,7 +47,7 @@ export function createRendererFor(renderer: IRenderTreeRoot) {
|
||||
} else {
|
||||
logger.warn(38);
|
||||
}
|
||||
return new Text();
|
||||
return new RenderText();
|
||||
},
|
||||
|
||||
createComment: function (): IRenderItem {
|
||||
@@ -55,7 +55,7 @@ export function createRendererFor(renderer: IRenderTreeRoot) {
|
||||
},
|
||||
|
||||
setText: function (node: IRenderItem, text: string): void {
|
||||
if (node instanceof Text) {
|
||||
if (node instanceof RenderText) {
|
||||
node.setText(text);
|
||||
} else {
|
||||
logger.warn(39);
|
||||
@@ -63,7 +63,7 @@ export function createRendererFor(renderer: IRenderTreeRoot) {
|
||||
},
|
||||
|
||||
setElementText: function (node: IRenderItem, text: string): void {
|
||||
if (node instanceof Text) {
|
||||
if (node instanceof RenderText) {
|
||||
node.setText(text);
|
||||
} else {
|
||||
logger.warn(39);
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
import {
|
||||
IRenderAnimated,
|
||||
IRenderAnimatedHooks,
|
||||
IRenderTreeRoot
|
||||
} from './types';
|
||||
import { ITexture, ITextureAnimater, ITextureRenderable } from '../assets';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
|
||||
export class Animated<T>
|
||||
extends RenderItem<IRenderAnimatedHooks>
|
||||
implements IRenderAnimated<T>
|
||||
{
|
||||
texture: ITexture | null = null;
|
||||
animater: ITextureAnimater<T> | null = null;
|
||||
interval: number = 50;
|
||||
cycled: boolean = false;
|
||||
paused: boolean = false;
|
||||
param: T | undefined = void 0;
|
||||
frame: number = 0;
|
||||
|
||||
/** 当前时刻 */
|
||||
private currentTime: number = -1;
|
||||
/** 上一帧的时刻 */
|
||||
private lastFrameTime: number = -1;
|
||||
/** 当前的可渲染对象 */
|
||||
private current: ITextureRenderable | null = null;
|
||||
|
||||
/** 动画生成器 */
|
||||
private gen: Generator<ITextureRenderable, void> | null = null;
|
||||
/** 委托 Excitation 的 id */
|
||||
private delegation: number = -1;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.eventNames
|
||||
.add('onSetTexture')
|
||||
.add('onSetAnimater')
|
||||
.add('onAnimationEnd')
|
||||
.add('onFrame');
|
||||
}
|
||||
|
||||
protected onConnected(root: IRenderTreeRoot): void {
|
||||
this.delegation = root.delegateExcitable(time => {
|
||||
this.currentTime = time;
|
||||
if (this.lastFrameTime === -1) {
|
||||
this.lastFrameTime = time;
|
||||
}
|
||||
if (this.paused || !this.gen) return;
|
||||
const dt = time - this.lastFrameTime;
|
||||
const nextFrames = Math.floor(dt / this.interval);
|
||||
if (!this.current || nextFrames >= 1) {
|
||||
let pass = this.current ? nextFrames : 1;
|
||||
for (let i = 0; i < pass; i++) {
|
||||
const next = this.gen.next();
|
||||
if (next.done) {
|
||||
pass = i;
|
||||
this.forEachHook(hook => hook.onAnimationEnd?.());
|
||||
break;
|
||||
}
|
||||
this.current = next.value;
|
||||
}
|
||||
if (pass !== 0) {
|
||||
this.frame += pass;
|
||||
this.lastFrameTime = time;
|
||||
this.forEachHook(hook => hook.onFrame?.(this.frame));
|
||||
this.update();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected onDisconnected(prevRoot: IRenderTreeRoot): void {
|
||||
prevRoot.removeExcitable(this.delegation);
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderAnimatedHooks {
|
||||
return new AnimatedDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected render(canvas: MotaOffscreenCanvas2D): void {
|
||||
const ctx = canvas.ctx;
|
||||
if (!this.current) return;
|
||||
const { source, rect: r } = this.current;
|
||||
const { width, height } = this;
|
||||
ctx.drawImage(source, r.x, r.y, r.w, r.h, 0, 0, width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行动画执行器,获取动画序列生成器
|
||||
*/
|
||||
private generate() {
|
||||
if (!this.animater || !this.texture) {
|
||||
this.gen = null;
|
||||
this.current = null;
|
||||
this.frame = 0;
|
||||
this.forEachHook(hook => hook.onFrame?.(this.frame));
|
||||
this.update();
|
||||
return;
|
||||
}
|
||||
if (this.cycled) {
|
||||
this.gen = this.animater.cycled(this.texture, this.param as T);
|
||||
this.lastFrameTime = this.currentTime;
|
||||
} else {
|
||||
this.gen = this.animater.once(this.texture, this.param as T);
|
||||
this.lastFrameTime = this.currentTime;
|
||||
}
|
||||
this.frame = 0;
|
||||
this.forEachHook(hook => hook.onFrame?.(this.frame));
|
||||
this.update();
|
||||
}
|
||||
|
||||
setTexture(texture: ITexture | null): void {
|
||||
this.texture = texture;
|
||||
this.generate();
|
||||
this.forEachHook(hook => hook.onSetTexture?.(texture));
|
||||
}
|
||||
|
||||
setInterval(interval: number): void {
|
||||
if (interval <= 0) {
|
||||
this.interval = 50;
|
||||
} else {
|
||||
this.interval = interval;
|
||||
}
|
||||
}
|
||||
|
||||
setAnimater(animater: ITextureAnimater<T> | null): void {
|
||||
this.animater = animater;
|
||||
this.generate();
|
||||
this.forEachHook(hook => hook.onSetAnimater?.(animater));
|
||||
}
|
||||
|
||||
setParam(param: T | undefined): void {
|
||||
this.param = param;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
pause(): void {
|
||||
this.paused = true;
|
||||
}
|
||||
|
||||
resume(): void {
|
||||
this.paused = false;
|
||||
this.lastFrameTime = this.currentTime;
|
||||
}
|
||||
|
||||
setCycled(cycled: boolean): void {
|
||||
this.cycled = cycled;
|
||||
this.generate();
|
||||
}
|
||||
}
|
||||
|
||||
export class AnimatedDefaultHooks
|
||||
extends RenderItemDefaultHooks
|
||||
implements IRenderAnimatedHooks
|
||||
{
|
||||
onSetTexture(texture: ITexture | null): void {
|
||||
this.list.get('onSetTexture')?.(texture);
|
||||
}
|
||||
|
||||
onSetAnimater(animater: ITextureAnimater<unknown> | null): void {
|
||||
this.list.get('onSetAnimater')?.(animater);
|
||||
}
|
||||
|
||||
onAnimationEnd(): void {
|
||||
this.list.get('onAnimationEnd')?.();
|
||||
}
|
||||
|
||||
onFrame(frame: number): void {
|
||||
this.list.get('onFrame')?.(frame);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { IRenderItemBaseHooks } from './types';
|
||||
|
||||
export class Comment extends RenderItem {
|
||||
readonly isComment: boolean = true;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.hide();
|
||||
this.size(1, 1);
|
||||
this.pos(-1, -1);
|
||||
this.setZIndex(-Infinity);
|
||||
this.ignoreEvent(true);
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
getBoundingRect(): DOMRectReadOnly {
|
||||
return new DOMRectReadOnly(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
protected render(): void {}
|
||||
|
||||
show(): void {
|
||||
this.hide();
|
||||
}
|
||||
|
||||
patchProp(): void {
|
||||
this.hide();
|
||||
this.setCacheEnabled(false);
|
||||
this.size(1, 1);
|
||||
this.pos(-1, -1);
|
||||
this.ignoreEvent(true);
|
||||
this.setZIndex(-Infinity);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,23 @@
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
import { ActionType, EventProgress, ActionEventMap } from './event';
|
||||
import { RenderItem } from './item';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { Transform } from './transform';
|
||||
import { CustomContainerPropagateFn, CustomContainerRenderFn } from './types';
|
||||
import {
|
||||
CustomContainerPropagateFn,
|
||||
CustomContainerRenderFn,
|
||||
IRenderItemBaseHooks
|
||||
} from './types';
|
||||
|
||||
export class Container extends RenderItem {
|
||||
protected getDefaultCache(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected render(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
_transform: Transform
|
||||
): void {
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected render(canvas: MotaOffscreenCanvas2D): void {
|
||||
const sorted = this.getSortedChildren() as RenderItem[];
|
||||
sorted.forEach(v => {
|
||||
if (v.hidden) return;
|
||||
@@ -47,22 +52,59 @@ export class Container extends RenderItem {
|
||||
}
|
||||
}
|
||||
|
||||
export class CustomContainer extends Container {
|
||||
export class CustomContainer extends RenderItem {
|
||||
private renderFn?: CustomContainerRenderFn;
|
||||
private propagateFn?: CustomContainerPropagateFn;
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected render(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
transform: Transform
|
||||
): void {
|
||||
const sorted = this.getSortedChildren() as RenderItem[];
|
||||
if (!this.renderFn) {
|
||||
super.render(canvas, transform);
|
||||
sorted.forEach(v => {
|
||||
if (v.hidden) return;
|
||||
v.renderContent(canvas);
|
||||
});
|
||||
} else {
|
||||
const sorted = this.getSortedChildren() as RenderItem[];
|
||||
this.renderFn(canvas, sorted, transform);
|
||||
}
|
||||
}
|
||||
|
||||
private originPropagateEvent<T extends ActionType>(
|
||||
type: T,
|
||||
progress: EventProgress,
|
||||
event: ActionEventMap[T]
|
||||
): void {
|
||||
const sorted = this.getSortedChildren() as RenderItem[];
|
||||
const len = sorted.length;
|
||||
if (progress === EventProgress.Capture) {
|
||||
let success = false;
|
||||
for (let i = len - 1; i >= 0; i--) {
|
||||
const child = sorted[i];
|
||||
if (child.hidden) continue;
|
||||
if (child.captureEvent(type, event)) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 如果没有子元素能够触发,那么自身触发冒泡
|
||||
if (!success) {
|
||||
this.bubbleEvent(type, event);
|
||||
}
|
||||
} else {
|
||||
this.parent?.bubbleEvent(type, event);
|
||||
}
|
||||
}
|
||||
|
||||
protected propagateEvent<T extends ActionType>(
|
||||
type: T,
|
||||
progress: EventProgress,
|
||||
@@ -70,10 +112,10 @@ export class CustomContainer extends Container {
|
||||
): void {
|
||||
if (this.propagateFn) {
|
||||
this.propagateFn(type, progress, event, this, () => {
|
||||
super.propagateEvent(type, progress, event);
|
||||
this.originPropagateEvent(type, progress, event);
|
||||
});
|
||||
} else {
|
||||
super.propagateEvent(type, progress, event);
|
||||
this.originPropagateEvent(type, progress, event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import { RenderItem } from './item';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
import { CustomRenderFunction } from './types';
|
||||
import { CustomRenderFunction, IRenderItemBaseHooks } from './types';
|
||||
|
||||
const EMPTY = () => {};
|
||||
|
||||
export class CustomRenderItem extends RenderItem {
|
||||
renderFn: CustomRenderFunction = EMPTY;
|
||||
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
import { RenderItem } from './item';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { Transform } from './transform';
|
||||
import { isWebGL2Supported } from './utils';
|
||||
import { SizedCanvasImageSource } from '../types';
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
DrawParamsMap,
|
||||
IGL2Program,
|
||||
IGL2ProgramPrefix,
|
||||
IRenderItemBaseHooks,
|
||||
IShaderAttrib,
|
||||
IShaderAttribArray,
|
||||
IShaderIndices,
|
||||
@@ -138,6 +139,10 @@ export abstract class GL2 extends RenderItem {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
private init() {
|
||||
const gl = this.gl;
|
||||
if (!gl) return;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Transform, RenderItem, MotaOffscreenCanvas2D } from '.';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { CanvasStyle } from '../types';
|
||||
import { logger } from '@motajs/common';
|
||||
import { clamp, isEqual, isNil } from 'lodash-es';
|
||||
@@ -17,8 +17,11 @@ import {
|
||||
EllipseParams,
|
||||
RectRCircleParams,
|
||||
RectREllipseParams,
|
||||
IGraphicRectR
|
||||
IGraphicRectR,
|
||||
IRenderItemBaseHooks
|
||||
} from './types';
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
import { Transform } from './transform';
|
||||
|
||||
const enum GraphicModeProp {
|
||||
Fill,
|
||||
@@ -70,6 +73,10 @@ export abstract class GraphicItemBase
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected render(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
_transform: Transform
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { ITexture } from '../assets';
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { IRenderImage, IRenderImageHooks } from './types';
|
||||
|
||||
export class RenderImage
|
||||
extends RenderItem<IRenderImageHooks>
|
||||
implements IRenderImage
|
||||
{
|
||||
image: ITexture | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.eventNames.add('onSetImage');
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderImageHooks {
|
||||
return new RenderImageDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected render(canvas: MotaOffscreenCanvas2D): void {
|
||||
if (!this.image) return;
|
||||
const ctx = canvas.ctx;
|
||||
const {
|
||||
source,
|
||||
rect: { x, y, w, h }
|
||||
} = this.image.render();
|
||||
ctx.drawImage(source, x, y, w, h, 0, 0, this.width, this.height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图片资源
|
||||
* @param image 图片资源
|
||||
*/
|
||||
setImage(image: ITexture | null) {
|
||||
this.image = image;
|
||||
this.forEachHook(hook => hook.onSetImage?.(image));
|
||||
this.update();
|
||||
}
|
||||
|
||||
protected handleProps(
|
||||
key: string,
|
||||
_prevValue: any,
|
||||
nextValue: any
|
||||
): boolean {
|
||||
switch (key) {
|
||||
case 'image':
|
||||
if (!nextValue) this.setImage(null);
|
||||
else this.setImage(nextValue);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class RenderImageDefaultHooks
|
||||
extends RenderItemDefaultHooks
|
||||
implements IRenderImageHooks
|
||||
{
|
||||
onSetImage(image: ITexture | null): void {
|
||||
this.list.get('onSetImage')?.(image);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
export * from './animated';
|
||||
export * from './canvas2d';
|
||||
export * from './comment';
|
||||
export * from './container';
|
||||
export * from './custom';
|
||||
export * from './event';
|
||||
export * from './gl2';
|
||||
export * from './graphics';
|
||||
export * from './image';
|
||||
export * from './item';
|
||||
export * from './misc';
|
||||
export * from './render';
|
||||
export * from './shader';
|
||||
export * from './text';
|
||||
export * from './transform';
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
@@ -29,9 +29,11 @@ interface RenderItemCanvasData {
|
||||
autoScale: boolean;
|
||||
}
|
||||
|
||||
export abstract class RenderItem
|
||||
extends Hookable<IRenderItemBaseHooks>
|
||||
implements IRenderItem
|
||||
export abstract class RenderItem<
|
||||
H extends IRenderItemBaseHooks = IRenderItemBaseHooks
|
||||
>
|
||||
extends Hookable<H>
|
||||
implements IRenderItem<H>
|
||||
{
|
||||
//#region 元素属性
|
||||
|
||||
@@ -129,9 +131,10 @@ export abstract class RenderItem
|
||||
//#region 交互事件
|
||||
|
||||
/** 当前元素包含的监听事件名称列表 */
|
||||
private readonly eventNames: Set<string> = new Set();
|
||||
protected readonly eventNames: Set<string> = new Set();
|
||||
/** 当前元素包含的监听事件,这是为了能够使用 `prop` 传入时不需要为每个内容都添加一个 `hook` 对象 */
|
||||
private readonly events: Map<string, (...params: any[]) => any> = new Map();
|
||||
protected readonly events: Map<string, (...params: any[]) => any> =
|
||||
new Map();
|
||||
|
||||
/** 是否调用了 `ev.stopPropagation` */
|
||||
protected propagationStoped: Map<ActionType, boolean> = new Map();
|
||||
@@ -176,7 +179,8 @@ export abstract class RenderItem
|
||||
.add('onUpdatedChildren')
|
||||
.add('onAppendTo')
|
||||
.add('onRemove')
|
||||
.add('onDestroyed');
|
||||
.add('onDestroyed')
|
||||
.add('onUpdated');
|
||||
|
||||
this.addHook(this.getDefaultHook());
|
||||
|
||||
@@ -188,13 +192,9 @@ export abstract class RenderItem
|
||||
/**
|
||||
* 获取默认钩子对象,子类可以拓展以实现自己默认钩子的 `prop` 支持
|
||||
*/
|
||||
protected getDefaultHook(): IRenderItemBaseHooks {
|
||||
return new RenderItemDefaultHooks(this.events);
|
||||
}
|
||||
protected abstract getDefaultHook(): H;
|
||||
|
||||
protected createController(
|
||||
hook: Partial<IRenderItemBaseHooks>
|
||||
): IHookController<IRenderItemBaseHooks> {
|
||||
protected createController(hook: Partial<H>): IHookController<H> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
@@ -274,6 +274,7 @@ export abstract class RenderItem
|
||||
if (this.cacheDirty) return;
|
||||
this.cacheDirty = true;
|
||||
}
|
||||
this.forEachHook(hook => hook.onUpdated?.());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,345 +0,0 @@
|
||||
import { RenderItem, Transform, MotaOffscreenCanvas2D } from '.';
|
||||
import { CanvasStyle } from '../types';
|
||||
import { Font } from '../style';
|
||||
import {
|
||||
IRenderAnimated,
|
||||
IRenderImage,
|
||||
IRenderText,
|
||||
IRenderTreeRoot
|
||||
} from './types';
|
||||
import { ITexture, ITextureAnimater, ITextureRenderable } from '../assets';
|
||||
|
||||
/** 文字的安全填充,会填充在文字的上侧和下侧,防止削顶和削底 */
|
||||
const SAFE_PAD = 1;
|
||||
|
||||
/** 文本测量画布 */
|
||||
const MEASURE = new MotaOffscreenCanvas2D();
|
||||
MEASURE.setHD(false);
|
||||
MEASURE.setScale(1);
|
||||
MEASURE.size(1, 1);
|
||||
|
||||
export class Text extends RenderItem implements IRenderText {
|
||||
text: string = '';
|
||||
|
||||
fillStyle?: CanvasStyle = '#fff';
|
||||
strokeStyle?: CanvasStyle;
|
||||
font: Font = new Font();
|
||||
strokeWidth: number = 1;
|
||||
|
||||
/** 文本的下边缘位置 */
|
||||
private descent: number = 0;
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected render(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
_transform: Transform
|
||||
): void {
|
||||
const ctx = canvas.ctx;
|
||||
const stroke = this.strokeWidth;
|
||||
ctx.textBaseline = 'bottom';
|
||||
ctx.fillStyle = this.fillStyle ?? 'transparent';
|
||||
ctx.strokeStyle = this.strokeStyle ?? 'transparent';
|
||||
ctx.font = this.font.string();
|
||||
ctx.lineWidth = this.strokeWidth;
|
||||
ctx.lineJoin = 'round';
|
||||
|
||||
if (this.strokeStyle) {
|
||||
ctx.strokeText(this.text, stroke, this.descent + stroke + SAFE_PAD);
|
||||
}
|
||||
if (this.fillStyle) {
|
||||
ctx.fillText(this.text, stroke, this.descent + stroke + SAFE_PAD);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文字的长度
|
||||
*/
|
||||
measure() {
|
||||
const ctx = MEASURE.ctx;
|
||||
ctx.textBaseline = 'bottom';
|
||||
ctx.font = this.font.string();
|
||||
const res = ctx.measureText(this.text);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置显示文字
|
||||
* @param text 显示的文字
|
||||
*/
|
||||
setText(text: string) {
|
||||
this.text = text;
|
||||
this.calBox();
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置使用的字体
|
||||
* @param font 字体
|
||||
*/
|
||||
setFont(font: Font) {
|
||||
this.font = font;
|
||||
this.calBox();
|
||||
this.update(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字体样式
|
||||
* @param fill 填充样式
|
||||
* @param stroke 描边样式
|
||||
*/
|
||||
setStyle(fill?: CanvasStyle, stroke?: CanvasStyle) {
|
||||
this.fillStyle = fill;
|
||||
this.strokeStyle = stroke;
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置描边宽度
|
||||
* @param width 宽度
|
||||
*/
|
||||
setStrokeWidth(width: number) {
|
||||
const before = this.strokeWidth;
|
||||
this.strokeWidth = width;
|
||||
const dw = width - before;
|
||||
this.size(this.width + dw * 2, this.height + dw * 2);
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算字体所占空间,从而确定这个元素的大小
|
||||
*/
|
||||
calBox() {
|
||||
const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } =
|
||||
this.measure();
|
||||
this.descent = actualBoundingBoxAscent;
|
||||
const height = actualBoundingBoxAscent + actualBoundingBoxDescent;
|
||||
const stroke = this.strokeWidth * 2;
|
||||
this.size(width + stroke, height + stroke + SAFE_PAD * 2);
|
||||
}
|
||||
|
||||
protected handleProps(
|
||||
key: string,
|
||||
_prevValue: any,
|
||||
nextValue: any
|
||||
): boolean {
|
||||
switch (key) {
|
||||
case 'text':
|
||||
if (!this.assertType(nextValue, 'string', key)) return false;
|
||||
this.setText(nextValue);
|
||||
return true;
|
||||
case 'fillStyle':
|
||||
this.setStyle(nextValue, this.strokeStyle);
|
||||
return true;
|
||||
case 'strokeStyle':
|
||||
this.setStyle(this.fillStyle, nextValue);
|
||||
return true;
|
||||
case 'font':
|
||||
if (!this.assertType(nextValue, Font, key)) return false;
|
||||
this.setFont(nextValue);
|
||||
return true;
|
||||
case 'strokeWidth':
|
||||
this.setStrokeWidth(nextValue);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class Image extends RenderItem implements IRenderImage {
|
||||
image: ITexture | null = null;
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected render(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
_transform: Transform
|
||||
): void {
|
||||
if (!this.image) return;
|
||||
const ctx = canvas.ctx;
|
||||
const {
|
||||
source,
|
||||
rect: { x, y, w, h }
|
||||
} = this.image.render();
|
||||
ctx.drawImage(source, x, y, w, h, 0, 0, this.width, this.height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图片资源
|
||||
* @param image 图片资源
|
||||
*/
|
||||
setImage(image: ITexture | null) {
|
||||
this.image = image;
|
||||
this.update();
|
||||
}
|
||||
|
||||
protected handleProps(
|
||||
key: string,
|
||||
_prevValue: any,
|
||||
nextValue: any
|
||||
): boolean {
|
||||
switch (key) {
|
||||
case 'image':
|
||||
if (!nextValue) this.setImage(null);
|
||||
else this.setImage(nextValue);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class Animated<T> extends RenderItem implements IRenderAnimated<T> {
|
||||
texture: ITexture | null = null;
|
||||
animater: ITextureAnimater<T> | null = null;
|
||||
interval: number = 50;
|
||||
cycled: boolean = false;
|
||||
paused: boolean = false;
|
||||
param: T | undefined = void 0;
|
||||
|
||||
/** 当前时刻 */
|
||||
private currentTime: number = -1;
|
||||
/** 上一帧的时刻 */
|
||||
private lastFrameTime: number = -1;
|
||||
/** 当前的可渲染对象 */
|
||||
private current: ITextureRenderable | null = null;
|
||||
|
||||
/** 动画生成器 */
|
||||
private gen: Generator<ITextureRenderable, void> | null = null;
|
||||
/** 委托 Excitation 的 id */
|
||||
private delegation: number = -1;
|
||||
|
||||
protected onConnected(root: IRenderTreeRoot): void {
|
||||
this.delegation = root.delegateExcitable(time => {
|
||||
this.currentTime = time;
|
||||
if (this.lastFrameTime === -1) {
|
||||
this.lastFrameTime = time;
|
||||
}
|
||||
if (this.paused || !this.gen) return;
|
||||
const dt = time - this.lastFrameTime;
|
||||
const nextFrames = Math.floor(dt / this.interval);
|
||||
if (!this.current || nextFrames >= 1) {
|
||||
const pass = this.current ? nextFrames : 1;
|
||||
for (let i = 0; i < pass; i++) {
|
||||
const next = this.gen.next();
|
||||
if (next.done) break;
|
||||
this.current = next.value;
|
||||
}
|
||||
this.lastFrameTime = time;
|
||||
this.update();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected onDisconnected(prevRoot: IRenderTreeRoot): void {
|
||||
prevRoot.removeExcitable(this.delegation);
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected render(canvas: MotaOffscreenCanvas2D): void {
|
||||
const ctx = canvas.ctx;
|
||||
if (!this.current) return;
|
||||
const { source, rect: r } = this.current;
|
||||
const { width, height } = this;
|
||||
ctx.drawImage(source, r.x, r.y, r.w, r.h, 0, 0, width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行动画执行器,获取动画序列生成器
|
||||
*/
|
||||
private generate() {
|
||||
if (!this.animater || !this.texture) {
|
||||
this.gen = null;
|
||||
this.current = null;
|
||||
this.update();
|
||||
return;
|
||||
}
|
||||
if (this.cycled) {
|
||||
this.gen = this.animater.cycled(this.texture, this.param as T);
|
||||
this.lastFrameTime = this.currentTime;
|
||||
} else {
|
||||
this.gen = this.animater.once(this.texture, this.param as T);
|
||||
this.lastFrameTime = this.currentTime;
|
||||
}
|
||||
this.update();
|
||||
}
|
||||
|
||||
setTexture(texture: ITexture | null): void {
|
||||
this.texture = texture;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
setInterval(interval: number): void {
|
||||
if (interval <= 0) {
|
||||
this.interval = 50;
|
||||
} else {
|
||||
this.interval = interval;
|
||||
}
|
||||
}
|
||||
|
||||
setAnimater(animater: ITextureAnimater<T> | null): void {
|
||||
this.animater = animater;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
setParam(param: T | undefined): void {
|
||||
this.param = param;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
pause(): void {
|
||||
this.paused = true;
|
||||
}
|
||||
|
||||
resume(): void {
|
||||
this.paused = false;
|
||||
this.lastFrameTime = this.currentTime;
|
||||
}
|
||||
|
||||
setCycled(cycled: boolean): void {
|
||||
this.cycled = cycled;
|
||||
this.generate();
|
||||
}
|
||||
}
|
||||
|
||||
export class Comment extends RenderItem {
|
||||
readonly isComment: boolean = true;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.hide();
|
||||
this.size(1, 1);
|
||||
this.pos(-1, -1);
|
||||
this.setZIndex(-Infinity);
|
||||
this.ignoreEvent(true);
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
getBoundingRect(): DOMRectReadOnly {
|
||||
return new DOMRectReadOnly(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
protected render(): void {}
|
||||
|
||||
show(): void {
|
||||
this.hide();
|
||||
}
|
||||
|
||||
patchProp(): void {
|
||||
this.hide();
|
||||
this.setCacheEnabled(false);
|
||||
this.size(1, 1);
|
||||
this.pos(-1, -1);
|
||||
this.ignoreEvent(true);
|
||||
this.setZIndex(-Infinity);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
} from './types';
|
||||
import { IExcitable, IExcitation, RafExcitation } from '@motajs/animate';
|
||||
import { CustomRenderItem } from './custom';
|
||||
import { Animated, Comment, Image, Text } from './misc';
|
||||
import { Animated } from './animated';
|
||||
import { Shader } from './shader';
|
||||
import {
|
||||
BezierCurve,
|
||||
@@ -31,6 +31,9 @@ import {
|
||||
Rect,
|
||||
RectR
|
||||
} from './graphics';
|
||||
import { RenderText } from './text';
|
||||
import { RenderImage } from './image';
|
||||
import { Comment } from './comment';
|
||||
|
||||
interface TouchInfo {
|
||||
/** 这次触摸在渲染系统的标识符 */
|
||||
@@ -240,8 +243,8 @@ export class MotaRenderer
|
||||
private registerIntrinsicTags() {
|
||||
this.registerElement('container', () => new Container());
|
||||
this.registerElement('custom', () => new CustomRenderItem());
|
||||
this.registerElement('text', () => new Text());
|
||||
this.registerElement('image', () => new Image());
|
||||
this.registerElement('text', () => new RenderText());
|
||||
this.registerElement('image', () => new RenderImage());
|
||||
this.registerElement('animated', () => new Animated<any>());
|
||||
this.registerElement('shader', () => new Shader());
|
||||
this.registerElement('comment', () => new Comment());
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
import { Font } from '../style';
|
||||
import { MotaOffscreenCanvas2D } from './canvas2d';
|
||||
import { RenderItem, RenderItemDefaultHooks } from './item';
|
||||
import { IRenderText, IRenderTextHooks } from './types';
|
||||
|
||||
/** 文字的安全填充,会填充在文字的上侧和下侧,防止削顶和削底 */
|
||||
const SAFE_PAD = 1;
|
||||
|
||||
/** 文本测量画布 */
|
||||
const MEASURE = new MotaOffscreenCanvas2D();
|
||||
MEASURE.setHD(false);
|
||||
MEASURE.setScale(1);
|
||||
MEASURE.size(1, 1);
|
||||
|
||||
export class RenderText
|
||||
extends RenderItem<IRenderTextHooks>
|
||||
implements IRenderText
|
||||
{
|
||||
text: string = '';
|
||||
|
||||
fillStyle?: CanvasStyle = '#fff';
|
||||
strokeStyle?: CanvasStyle;
|
||||
font: Font = new Font();
|
||||
strokeWidth: number = 1;
|
||||
|
||||
/** 文本的下边缘位置 */
|
||||
private descent: number = 0;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.eventNames.add('onSetText');
|
||||
}
|
||||
|
||||
protected getDefaultCache(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getDefaultHook(): IRenderTextHooks {
|
||||
return new RenderTextDefaultHooks(this.events);
|
||||
}
|
||||
|
||||
protected render(canvas: MotaOffscreenCanvas2D): void {
|
||||
const ctx = canvas.ctx;
|
||||
const stroke = this.strokeWidth;
|
||||
ctx.textBaseline = 'bottom';
|
||||
ctx.fillStyle = this.fillStyle ?? 'transparent';
|
||||
ctx.strokeStyle = this.strokeStyle ?? 'transparent';
|
||||
ctx.font = this.font.string();
|
||||
ctx.lineWidth = this.strokeWidth;
|
||||
ctx.lineJoin = 'round';
|
||||
|
||||
if (this.strokeStyle) {
|
||||
ctx.strokeText(this.text, stroke, this.descent + stroke + SAFE_PAD);
|
||||
}
|
||||
if (this.fillStyle) {
|
||||
ctx.fillText(this.text, stroke, this.descent + stroke + SAFE_PAD);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文字的长度
|
||||
*/
|
||||
measure() {
|
||||
const ctx = MEASURE.ctx;
|
||||
ctx.textBaseline = 'bottom';
|
||||
ctx.font = this.font.string();
|
||||
const res = ctx.measureText(this.text);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置显示文字
|
||||
* @param text 显示的文字
|
||||
*/
|
||||
setText(text: string) {
|
||||
this.text = text;
|
||||
this.calBox();
|
||||
this.forEachHook(hook => hook.onSetText?.(text));
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置使用的字体
|
||||
* @param font 字体
|
||||
*/
|
||||
setFont(font: Font) {
|
||||
this.font = font;
|
||||
this.calBox();
|
||||
this.update(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字体样式
|
||||
* @param fill 填充样式
|
||||
* @param stroke 描边样式
|
||||
*/
|
||||
setStyle(fill?: CanvasStyle, stroke?: CanvasStyle) {
|
||||
this.fillStyle = fill;
|
||||
this.strokeStyle = stroke;
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置描边宽度
|
||||
* @param width 宽度
|
||||
*/
|
||||
setStrokeWidth(width: number) {
|
||||
const before = this.strokeWidth;
|
||||
this.strokeWidth = width;
|
||||
const dw = width - before;
|
||||
this.size(this.width + dw * 2, this.height + dw * 2);
|
||||
this.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算字体所占空间,从而确定这个元素的大小
|
||||
*/
|
||||
calBox() {
|
||||
const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } =
|
||||
this.measure();
|
||||
this.descent = actualBoundingBoxAscent;
|
||||
const height = actualBoundingBoxAscent + actualBoundingBoxDescent;
|
||||
const stroke = this.strokeWidth * 2;
|
||||
this.size(width + stroke, height + stroke + SAFE_PAD * 2);
|
||||
}
|
||||
|
||||
protected handleProps(
|
||||
key: string,
|
||||
_prevValue: any,
|
||||
nextValue: any
|
||||
): boolean {
|
||||
switch (key) {
|
||||
case 'text':
|
||||
if (!this.assertType(nextValue, 'string', key)) return false;
|
||||
this.setText(nextValue);
|
||||
return true;
|
||||
case 'fillStyle':
|
||||
this.setStyle(nextValue, this.strokeStyle);
|
||||
return true;
|
||||
case 'strokeStyle':
|
||||
this.setStyle(this.fillStyle, nextValue);
|
||||
return true;
|
||||
case 'font':
|
||||
if (!this.assertType(nextValue, Font, key)) return false;
|
||||
this.setFont(nextValue);
|
||||
return true;
|
||||
case 'strokeWidth':
|
||||
this.setStrokeWidth(nextValue);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class RenderTextDefaultHooks
|
||||
extends RenderItemDefaultHooks
|
||||
implements IRenderTextHooks
|
||||
{
|
||||
onSetText(text: string): void {
|
||||
this.list.get('onSetText')?.(text);
|
||||
}
|
||||
}
|
||||
@@ -162,7 +162,7 @@ export interface IRenderItemBaseHooks extends IHookBase {
|
||||
onDisconnected?(prevRoot: IRenderTreeRoot): void;
|
||||
|
||||
/**
|
||||
* 当更新子元素列表时触发
|
||||
* 当更新子元素列表时触发,其子元素的子元素更新时不会触发
|
||||
* @param children 当前的子元素列表
|
||||
*/
|
||||
onUpdateChildren?(children: readonly IRenderItem[]): void;
|
||||
@@ -183,10 +183,17 @@ export interface IRenderItemBaseHooks extends IHookBase {
|
||||
* 当此元素被销毁时触发
|
||||
*/
|
||||
onDestroyed?(): void;
|
||||
|
||||
/**
|
||||
* 当触发 `update` 更新后触发,会优先触发父元素的 `onUpdated`,再触发子元素的 `onUpdated`
|
||||
*/
|
||||
onUpdated?(): void;
|
||||
}
|
||||
|
||||
export interface IRenderItem
|
||||
extends ITransformUpdatable<Transform>, IHookable<IRenderItemBaseHooks> {
|
||||
export interface IRenderItem<
|
||||
H extends IRenderItemBaseHooks = IRenderItemBaseHooks
|
||||
>
|
||||
extends ITransformUpdatable<Transform>, IHookable<H> {
|
||||
/** 是否为根元素 */
|
||||
readonly isRoot: boolean;
|
||||
/** 当前元素的父元素 */
|
||||
@@ -645,7 +652,15 @@ export interface IRenderTreeRoot extends IRenderItem {
|
||||
|
||||
//#region 核心元素
|
||||
|
||||
export interface IRenderText extends IRenderItem {
|
||||
export interface IRenderTextHooks extends IRenderItemBaseHooks {
|
||||
/**
|
||||
* 当设置文字内容时触发
|
||||
* @param text 设置的文字内容
|
||||
*/
|
||||
onSetText?(text: string): void;
|
||||
}
|
||||
|
||||
export interface IRenderText extends IRenderItem<IRenderTextHooks> {
|
||||
/** 文字内容 */
|
||||
readonly text: string;
|
||||
/** 填充样式 */
|
||||
@@ -688,7 +703,15 @@ export interface IRenderText extends IRenderItem {
|
||||
setStrokeWidth(width: number): void;
|
||||
}
|
||||
|
||||
export interface IRenderImage extends IRenderItem {
|
||||
export interface IRenderImageHooks extends IRenderItemBaseHooks {
|
||||
/**
|
||||
* 当设置图片资源时触发
|
||||
* @param text 设置的图片资源
|
||||
*/
|
||||
onSetImage?(image: ITexture | null): void;
|
||||
}
|
||||
|
||||
export interface IRenderImage extends IRenderItem<IRenderImageHooks> {
|
||||
/** 当前元素的图片内容 */
|
||||
readonly image: ITexture | null;
|
||||
|
||||
@@ -696,10 +719,35 @@ export interface IRenderImage extends IRenderItem {
|
||||
* 设置图片资源
|
||||
* @param image 图片资源
|
||||
*/
|
||||
setImage(image: ITexture): void;
|
||||
setImage(image: ITexture | null): void;
|
||||
}
|
||||
|
||||
export interface IRenderAnimated<T> extends IRenderItem {
|
||||
export interface IRenderAnimatedHooks extends IRenderItemBaseHooks {
|
||||
/**
|
||||
* 当设置动画的纹理对象时触发
|
||||
* @param text 设置为的纹理对象
|
||||
*/
|
||||
onSetTexture?(texture: ITexture | null): void;
|
||||
|
||||
/**
|
||||
* 当设置贴图动画执行器时触发
|
||||
* @param animater 设置为的贴图动画执行器
|
||||
*/
|
||||
onSetAnimater?(animater: ITextureAnimater<unknown> | null): void;
|
||||
|
||||
/**
|
||||
* 在非循环模式下,当动画执行完毕后触发
|
||||
*/
|
||||
onAnimationEnd?(): void;
|
||||
|
||||
/**
|
||||
* 当帧数向前推进时触发,如果播放速度过快或因为某些原因卡顿时,可能产生跳帧
|
||||
* @param frame 当前帧数
|
||||
*/
|
||||
onFrame?(frame: number): void;
|
||||
}
|
||||
|
||||
export interface IRenderAnimated<T> extends IRenderItem<IRenderAnimatedHooks> {
|
||||
/** 贴图的动画执行器 */
|
||||
readonly animater: ITextureAnimater<T> | null;
|
||||
/** 播放动画的纹理对象 */
|
||||
@@ -712,6 +760,8 @@ export interface IRenderAnimated<T> extends IRenderItem {
|
||||
readonly paused: boolean;
|
||||
/** 传递给动画执行器的参数 */
|
||||
readonly param: T | undefined;
|
||||
/** 当前的播放帧数,当纹理、动画器及其参数修改后会从零重新计数 */
|
||||
readonly frame: number;
|
||||
|
||||
/**
|
||||
* 设置播放动画的纹理对象
|
||||
|
||||
Reference in new issue
Block a user