Files
esengine/packages/ui/src/components/UITextComponent.ts

345 lines
7.8 KiB
TypeScript
Raw Normal View History

import { Component, ECSComponent, Property, Serializable, Serialize } from '@esengine/esengine';
/**
*
* Text alignment options
*/
export type UITextAlign = 'left' | 'center' | 'right';
/**
*
* Text vertical alignment options
*/
export type UITextVerticalAlign = 'top' | 'middle' | 'bottom';
/**
*
* Text overflow handling
*/
export type UITextOverflow = 'visible' | 'hidden' | 'ellipsis' | 'clip';
/**
*
* Font weight options
*/
export type UIFontWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
/**
* UI
* UI Text Component - Handles text rendering
*
*
* Defines text content and style
*/
@ECSComponent('UIText')
@Serializable({ version: 1, typeId: 'UIText' })
export class UITextComponent extends Component {
/**
*
* Text content
*/
@Serialize()
@Property({ type: 'string', label: 'Text' })
public text: string = '';
// ===== 字体 Font =====
/**
*
* Font size in pixels
*/
@Serialize()
@Property({ type: 'number', label: 'Font Size', min: 1 })
public fontSize: number = 14;
/**
*
* Font family
*/
@Serialize()
@Property({ type: 'string', label: 'Font Family' })
public fontFamily: string = 'Arial, sans-serif';
/**
*
* Font weight
*/
@Serialize()
@Property({
type: 'enum',
label: 'Font Weight',
options: [
{ value: 'normal', label: 'Normal' },
{ value: 'bold', label: 'Bold' },
{ value: 'lighter', label: 'Lighter' },
{ value: 'bolder', label: 'Bolder' }
]
})
public fontWeight: UIFontWeight = 'normal';
/**
*
* Whether italic
*/
@Serialize()
@Property({ type: 'boolean', label: 'Italic' })
public italic: boolean = false;
// ===== 颜色 Color =====
/**
* (0xRRGGBB)
* Text color
*/
@Serialize()
@Property({ type: 'color', label: 'Color' })
public color: number = 0x000000;
/**
*
* Text alpha
*/
@Serialize()
@Property({ type: 'number', label: 'Alpha', min: 0, max: 1, step: 0.01 })
public alpha: number = 1;
// ===== 对齐 Alignment =====
/**
*
* Horizontal alignment
*/
@Serialize()
@Property({
type: 'enum',
label: 'Align',
options: [
{ value: 'left', label: 'Left' },
{ value: 'center', label: 'Center' },
{ value: 'right', label: 'Right' }
]
})
public align: UITextAlign = 'left';
/**
*
* Vertical alignment
*/
@Serialize()
@Property({
type: 'enum',
label: 'Vertical Align',
options: [
{ value: 'top', label: 'Top' },
{ value: 'middle', label: 'Middle' },
{ value: 'bottom', label: 'Bottom' }
]
})
public verticalAlign: UITextVerticalAlign = 'middle';
// ===== 换行 Wrapping =====
/**
*
* Whether to wrap text
*/
@Serialize()
@Property({ type: 'boolean', label: 'Word Wrap' })
public wordWrap: boolean = false;
/**
* 0 = 使
* Wrap width (0 = use parent width)
*/
@Serialize()
@Property({ type: 'number', label: 'Wrap Width', min: 0 })
public wrapWidth: number = 0;
/**
* 1 =
* Line height multiplier
*/
@Serialize()
@Property({ type: 'number', label: 'Line Height', min: 0.1, step: 0.1 })
public lineHeight: number = 1.2;
/**
*
* Letter spacing
*/
@Serialize()
@Property({ type: 'number', label: 'Letter Spacing' })
public letterSpacing: number = 0;
// ===== 溢出 Overflow =====
/**
*
* Text overflow handling
*/
@Property({
type: 'enum',
label: 'Overflow',
options: [
{ value: 'visible', label: 'Visible' },
{ value: 'hidden', label: 'Hidden' },
{ value: 'ellipsis', label: 'Ellipsis' },
{ value: 'clip', label: 'Clip' }
]
})
public overflow: UITextOverflow = 'visible';
/**
* 0 =
* Maximum number of lines (0 = unlimited)
*/
@Property({ type: 'integer', label: 'Max Lines', min: 0 })
public maxLines: number = 0;
// ===== 装饰 Decoration =====
/**
* 线
* Underline
*/
@Property({ type: 'boolean', label: 'Underline' })
public underline: boolean = false;
/**
* 线
* Strikethrough
*/
@Property({ type: 'boolean', label: 'Strikethrough' })
public strikethrough: boolean = false;
// ===== 描边 Stroke =====
/**
*
* Stroke width
*/
@Property({ type: 'number', label: 'Stroke Width', min: 0 })
public strokeWidth: number = 0;
/**
*
* Stroke color
*/
@Property({ type: 'color', label: 'Stroke Color' })
public strokeColor: number = 0x000000;
// ===== 阴影 Shadow =====
/**
*
* Text shadow enabled
*/
@Property({ type: 'boolean', label: 'Shadow' })
public shadowEnabled: boolean = false;
/**
* X
* Shadow X offset
*/
public shadowOffsetX: number = 1;
/**
* Y
* Shadow Y offset
*/
public shadowOffsetY: number = 1;
/**
*
* Shadow color
*/
public shadowColor: number = 0x000000;
/**
*
* Shadow alpha
*/
public shadowAlpha: number = 0.5;
// ===== 计算属性 Computed =====
/**
*
* Computed text lines (filled by render system)
*/
public computedLines: string[] = [];
/**
*
* Computed text width
*/
public computedWidth: number = 0;
/**
*
* Computed text height
*/
public computedHeight: number = 0;
/**
*
* Whether text needs recomputation
*/
public dirty: boolean = true;
/**
*
* Set text content
*/
public setText(text: string): this {
if (this.text !== text) {
this.text = text;
this.dirty = true;
}
return this;
}
/**
*
* Set font properties
*/
public setFont(size: number, family?: string, weight?: UIFontWeight): this {
this.fontSize = size;
if (family !== undefined) this.fontFamily = family;
if (weight !== undefined) this.fontWeight = weight;
this.dirty = true;
return this;
}
/**
*
* Set text color
*/
public setColor(color: number, alpha: number = 1): this {
this.color = color;
this.alpha = alpha;
return this;
}
/**
* CSS
* Get CSS font string
*/
public getCSSFont(): string {
const style = this.italic ? 'italic ' : '';
const weight = typeof this.fontWeight === 'number' ? this.fontWeight : this.fontWeight;
return `${style}${weight} ${this.fontSize}px ${this.fontFamily}`;
}
/**
* CSS
* Get color as CSS string
*/
public getCSSColor(): string {
const r = (this.color >> 16) & 0xFF;
const g = (this.color >> 8) & 0xFF;
const b = this.color & 0xFF;
return `rgba(${r}, ${g}, ${b}, ${this.alpha})`;
}
}