feat: Textbox & fix: hide & show

This commit is contained in:
unanmed 2024-12-22 23:06:35 +08:00
parent b018a5fd9a
commit 297b67da5b
5 changed files with 459 additions and 329 deletions

View File

@ -1,9 +1,12 @@
import { MotaOffscreenCanvas2D } from '@/core/fx/canvas2d'; import { MotaOffscreenCanvas2D } from '@/core/fx/canvas2d';
import { import {
computed,
defineComponent, defineComponent,
onMounted, onMounted,
onUnmounted,
onUpdated, onUpdated,
ref, ref,
shallowReactive,
shallowRef, shallowRef,
SlotsType, SlotsType,
VNode, VNode,
@ -16,6 +19,8 @@ import { Sprite } from '../sprite';
import { onTick } from '../renderer'; import { onTick } from '../renderer';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
import { SetupComponentOptions } from './types'; import { SetupComponentOptions } from './types';
import EventEmitter from 'eventemitter3';
import { Container } from '../container';
export const enum WordBreak { export const enum WordBreak {
/** 不换行 */ /** 不换行 */
@ -128,13 +133,25 @@ const textContentOptions = {
'width', 'width',
'wordBreak', 'wordBreak',
'x', 'x',
'y' 'y',
'fill',
'fillStyle',
'strokeStyle',
'strokeWidth',
'stroke'
], ],
emits: ['typeEnd', 'typeStart'] emits: ['typeEnd', 'typeStart']
} satisfies SetupComponentOptions<TextContentProps, TextContentEmits>; } satisfies SetupComponentOptions<
TextContentProps,
TextContentEmits,
keyof TextContentEmits
>;
export const TextContent = defineComponent<TextContentProps, TextContentEmits>( export const TextContent = defineComponent<
(props, { emit }) => { TextContentProps,
TextContentEmits,
keyof TextContentEmits
>((props, { emit }) => {
if (props.width && props.width <= 0) { if (props.width && props.width <= 0) {
logger.warn(41, String(props.width)); logger.warn(41, String(props.width));
} }
@ -145,10 +162,7 @@ export const TextContent = defineComponent<TextContentProps, TextContentEmits>(
y: props.y ?? 0, y: props.y ?? 0,
width: !props.width || props.width <= 0 ? 200 : props.width, width: !props.width || props.width <= 0 ? 200 : props.width,
height: props.height ?? 200, height: props.height ?? 200,
font: font: props.font ?? core.status.globalAttribute?.font ?? '16px Verdana',
props.font ??
core.status.globalAttribute?.font ??
'16px Verdana',
ignoreLineEnd: props.ignoreLineEnd ?? new Set(), ignoreLineEnd: props.ignoreLineEnd ?? new Set(),
ignoreLineStart: props.ignoreLineStart ?? new Set(), ignoreLineStart: props.ignoreLineStart ?? new Set(),
keepLast: props.keepLast ?? false, keepLast: props.keepLast ?? false,
@ -227,8 +241,7 @@ export const TextContent = defineComponent<TextContentProps, TextContentEmits>(
spriteElement.value?.update(); spriteElement.value?.update();
const time = Date.now(); const time = Date.now();
const char = const char =
Math.floor((time - startTime) / renderData.interval!) + Math.floor((time - startTime) / renderData.interval!) + fromChar;
fromChar;
if (!isFinite(char)) { if (!isFinite(char)) {
renderable.forEach(v => (v.pointer = v.text.length)); renderable.forEach(v => (v.pointer = v.text.length));
needUpdate = false; needUpdate = false;
@ -272,8 +285,18 @@ export const TextContent = defineComponent<TextContentProps, TextContentEmits>(
renderable.forEach(v => { renderable.forEach(v => {
if (v.pointer === 0) return; if (v.pointer === 0) return;
const text = v.text.slice(0, v.pointer); const text = v.text.slice(0, v.pointer);
if (renderData.textAlign === TextAlign.Left) {
if (renderData.stroke) ctx.strokeText(text, v.x, v.y); if (renderData.stroke) ctx.strokeText(text, v.x, v.y);
if (renderData.fill) ctx.fillText(text, v.x, v.y); if (renderData.fill) ctx.fillText(text, v.x, v.y);
} else if (renderData.textAlign === TextAlign.Center) {
const x = (renderData.width - v.x) / 2 + v.x;
if (renderData.stroke) ctx.strokeText(text, x, v.y);
if (renderData.fill) ctx.fillText(text, x, v.y);
} else {
const x = renderData.width;
if (renderData.stroke) ctx.strokeText(text, x, v.y);
if (renderData.fill) ctx.fillText(text, x, v.y);
}
}); });
}; };
@ -406,12 +429,7 @@ export const TextContent = defineComponent<TextContentProps, TextContentEmits>(
rawRender(data.value.text, value); rawRender(data.value.text, value);
} else { } else {
const index = old.length - 1; const index = old.length - 1;
continueRender( continueRender(data.value.text, oldLast, value, index);
data.value.text,
oldLast,
value,
index
);
} }
} }
} else { } else {
@ -427,7 +445,7 @@ export const TextContent = defineComponent<TextContentProps, TextContentEmits>(
<sprite <sprite
ref={spriteElement} ref={spriteElement}
hd hd
antiAliasing={false} antiAliasing={true}
x={renderData.x} x={renderData.x}
y={renderData.y} y={renderData.y}
width={renderData.width} width={renderData.width}
@ -436,35 +454,66 @@ export const TextContent = defineComponent<TextContentProps, TextContentEmits>(
></sprite> ></sprite>
); );
}; };
}, }, textContentOptions);
textContentOptions
);
export interface TextboxProps extends TextContentProps { export interface TextboxProps extends TextContentProps {
id?: string;
/** 背景颜色 */ /** 背景颜色 */
backColor?: CanvasStyle; backColor?: CanvasStyle;
/** 背景 winskin */ /** 背景 winskin */
winskin?: string; winskin?: string;
/** 边框与文字间的距离默认为8 */
padding?: number;
} }
interface TextboxSlots extends SlotsType { type TextboxEmits = TextContentEmits;
default: () => VNode; type TextboxSlots = SlotsType<{ default: (data: TextboxProps) => VNode[] }>;
}
const textboxOptions = { const textboxOptions = {
props: (textContentOptions.props as (keyof TextboxProps)[]).concat([ props: (textContentOptions.props as (keyof TextboxProps)[]).concat([
'backColor', 'backColor',
'winskin' 'winskin',
]) 'id'
]),
emits: textContentOptions.emits
} satisfies SetupComponentOptions<TextboxProps, {}, string, TextboxSlots>; } satisfies SetupComponentOptions<TextboxProps, {}, string, TextboxSlots>;
export const Textbox = defineComponent<TextboxProps, {}, string, TextboxSlots>( let id = 0;
(props, { slots }) => { function getNextTextboxId() {
return `@default-textbox-${id++}`;
}
export const Textbox = defineComponent<
TextboxProps,
TextboxEmits,
keyof TextboxEmits,
TextboxSlots
>((props, { slots }) => {
const data = shallowReactive({ ...props });
data.padding ??= 8;
data.width ??= 200;
data.height ??= 200;
data.id ??= '';
const store = TextboxStore.use(props.id ?? getNextTextboxId(), data);
const hidden = ref(false);
store.on('hide', () => (hidden.value = true));
store.on('show', () => (hidden.value = false));
onUpdated(() => {
for (const [key, value] of Object.entries(props)) {
// @ts-ignore
if (!isNil(value)) data[key] = value;
}
});
const contentWidth = computed(() => data.width! - data.padding! * 2);
const contentHeight = computed(() => data.height! - data.padding! * 2);
return () => { return () => {
return ( return (
<container> <container hidden={hidden.value} id="11111">
{slots.default ? ( {slots.default ? (
slots.default() slots.default(data)
) : props.winskin ? ( ) : props.winskin ? (
// todo // todo
<winskin></winskin> <winskin></winskin>
@ -473,19 +522,23 @@ export const Textbox = defineComponent<TextboxProps, {}, string, TextboxSlots>(
<g-rect <g-rect
x={0} x={0}
y={0} y={0}
width={props.width ?? 200} width={data.width ?? 200}
height={props.height ?? 200} height={data.height ?? 200}
fill fill
fillStyle={props.backColor} fillStyle={data.backColor}
></g-rect> ></g-rect>
)} )}
<TextContent {...props}></TextContent> <TextContent
{...data}
x={data.padding}
y={data.padding}
width={contentWidth.value}
height={contentHeight.value}
></TextContent>
</container> </container>
); );
}; };
}, }, textboxOptions);
textboxOptions
);
const fontSizeGuessScale = new Map<string, number>([ const fontSizeGuessScale = new Map<string, number>([
['px', 1], ['px', 1],
@ -531,6 +584,7 @@ function splitLines(data: TextContentData) {
let mid = 0; let mid = 0;
let guessCount = 1; let guessCount = 1;
let splitProgress = false; let splitProgress = false;
let last = 0;
while (1) { while (1) {
if (!splitProgress) { if (!splitProgress) {
@ -548,8 +602,15 @@ function splitLines(data: TextContentData) {
const diff = end - start; const diff = end - start;
if (diff === 1) { if (diff === 1) {
if (start === last) {
res.push(words[last + 1]);
start = words[last + 1];
end = start + 1;
} else {
res.push(words[start]); res.push(words[start]);
}
if (end >= words.length) break; if (end >= words.length) break;
last = resolved;
resolved = start; resolved = start;
end = Math.ceil(start + guess); end = Math.ceil(start + guess);
if (end > words.length) end = words.length; if (end > words.length) end = words.length;
@ -659,3 +720,67 @@ function testHeight(text: string, font: string) {
ctx.font = font; ctx.font = font;
return ctx.measureText(text).fontBoundingBoxAscent; return ctx.measureText(text).fontBoundingBoxAscent;
} }
interface TextboxStoreEvent {
update: [value: TextboxProps];
show: [];
hide: [];
}
export class TextboxStore extends EventEmitter<TextboxStoreEvent> {
static list: Map<string, TextboxStore> = new Map();
private constructor(private readonly data: TextboxProps) {
super();
}
/**
*
*/
modify(data: Partial<TextboxProps>) {
for (const [key, value] of Object.entries(data)) {
// @ts-ignore
if (!isNil(value)) this.data[key] = value;
}
this.emit('update', this.data);
}
/**
*
*/
show() {
this.emit('show');
}
/**
*
*/
hide() {
this.emit('hide');
}
/**
*
* @param id id
*/
static get(id: string): TextboxStore | undefined {
return this.list.get(id);
}
/**
*
* @param id id
* @param props
*/
static use(id: string, props: TextboxProps) {
const store = new TextboxStore(props);
if (this.list.has(id)) {
logger.warn(42, id);
}
this.list.set(id, store);
onUnmounted(() => {
this.list.delete(id);
});
return store;
}
}

View File

@ -159,6 +159,7 @@ const beforeFrame: (() => void)[] = [];
const afterFrame: (() => void)[] = []; const afterFrame: (() => void)[] = [];
const renderFrame: (() => void)[] = []; const renderFrame: (() => void)[] = [];
let count = 0;
export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent> export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
extends EventEmitter<ERenderItemEvent | E> extends EventEmitter<ERenderItemEvent | E>
implements implements
@ -182,6 +183,8 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
/** id到渲染元素的映射 */ /** id到渲染元素的映射 */
static itemMap: Map<string, RenderItem> = new Map(); static itemMap: Map<string, RenderItem> = new Map();
readonly uid: number = count++;
private _id: string = ''; private _id: string = '';
get id(): string { get id(): string {
@ -399,11 +402,11 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
this.anchorY = y; this.anchorY = y;
} }
update(item: RenderItem<any> = this): void { update(item: RenderItem<any> = this, force: boolean = false): void {
if (this.needUpdate || this.hidden) return; if ((this.needUpdate || this.hidden) && !force) return;
this.needUpdate = true; this.needUpdate = true;
this.cacheDirty = true; this.cacheDirty = true;
this.parent?.update(item); this.parent?.update(item, force);
} }
setHD(hd: boolean): void { setHD(hd: boolean): void {
@ -473,7 +476,7 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
hide() { hide() {
if (this.hidden) return; if (this.hidden) return;
this.hidden = true; this.hidden = true;
this.update(this); this.update(this, true);
} }
/** /**
@ -482,7 +485,23 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
show() { show() {
if (!this.hidden) return; if (!this.hidden) return;
this.hidden = false; this.hidden = false;
this.update(this); this.refreshAllChildren(true);
}
/**
*
*/
refreshAllChildren(force: boolean = false) {
if (this.children.size > 0) {
const stack: RenderItem[] = [this];
while (stack.length > 0) {
const item = stack.pop();
if (!item) continue;
item.cacheDirty = true;
item.children.forEach(v => stack.push(v));
}
}
this.update(this, force);
} }
/** /**
@ -555,7 +574,7 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
if (typeof expected === 'string') { if (typeof expected === 'string') {
const type = typeof value; const type = typeof value;
if (type !== expected) { if (type !== expected) {
logger.warn(21, key, expected, type); logger.error(21, key, expected, type);
return false; return false;
} else { } else {
return true; return true;
@ -564,7 +583,7 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
if (value instanceof expected) { if (value instanceof expected) {
return true; return true;
} else { } else {
logger.warn( logger.error(
21, 21,
key, key,
expected.name, expected.name,

View File

@ -9,7 +9,6 @@ export class MotaRenderer extends Container {
target!: MotaOffscreenCanvas2D; target!: MotaOffscreenCanvas2D;
protected needUpdate: boolean = false;
readonly isRoot: boolean = true; readonly isRoot: boolean = true;
constructor(id: string = 'render-main') { constructor(id: string = 'render-main') {
@ -84,27 +83,13 @@ export class MotaRenderer extends Container {
this.target.delete(); this.target.delete();
} }
/**
*
*/
refreshAll() {
const stack: RenderItem[] = [this];
while (stack.length > 0) {
const item = stack.pop();
if (!item) break;
if (item.children.size === 0) {
item.update();
} else {
item.children.forEach(v => stack.push(v));
}
}
}
static get(id: string) { static get(id: string) {
return this.list.get(id); return this.list.get(id);
} }
} }
window.addEventListener('resize', () => { window.addEventListener('resize', () => {
MotaRenderer.list.forEach(v => v.requestAfterFrame(() => v.refreshAll())); MotaRenderer.list.forEach(v =>
v.requestAfterFrame(() => v.refreshAllChildren())
);
}); });

View File

@ -49,7 +49,7 @@ export const { createApp, render } = createRenderer<RenderItem, RenderItem>({
}, },
createText: function (text: string): RenderItem<ERenderItemEvent> { createText: function (text: string): RenderItem<ERenderItemEvent> {
logger.warn(38); if (!/^\s*$/.test(text)) logger.warn(38);
return new Text(text); return new Text(text);
}, },

View File

@ -69,6 +69,7 @@
"39": "Plain text is not supported outside Text element.", "39": "Plain text is not supported outside Text element.",
"40": "Cannot return canvas that is not provided by this pool.", "40": "Cannot return canvas that is not provided by this pool.",
"41": "Width of text content components must be positive. receive: $1", "41": "Width of text content components must be positive. receive: $1",
"42": "Repeat Textbox id: '$1'.",
"1001": "Item-detail extension needs 'floor-binder' and 'floor-damage' extension as dependency.", "1001": "Item-detail extension needs 'floor-binder' and 'floor-damage' extension as dependency.",
"1101": "Cannot add new effect to point effect instance, for there's no more reserve space for it. Please increase the max count of the instance." "1101": "Cannot add new effect to point effect instance, for there's no more reserve space for it. Please increase the max count of the instance."
} }