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

309 lines
7.8 KiB
TypeScript
Raw Normal View History

import { Component, ECSComponent, Property, Serializable, Serialize } from '@esengine/ecs-framework';
/**
*
* Render types for different visual elements
*/
export enum UIRenderType {
/** 纯色矩形 Solid color rectangle */
Rect = 'rect',
/** 图片 Image/Texture */
Image = 'image',
/** 九宫格图片 Nine-patch/Nine-slice image */
NinePatch = 'ninepatch',
/** 圆形 Circle */
Circle = 'circle',
/** 圆角矩形 Rounded rectangle */
RoundedRect = 'rounded-rect'
}
/**
*
* Border style configuration
*/
export interface UIBorderStyle {
width: number;
color: number;
alpha: number;
}
/**
*
* Shadow style configuration
*/
export interface UIShadowStyle {
offsetX: number;
offsetY: number;
blur: number;
color: number;
alpha: number;
}
/**
* UI
* UI Render Component - Handles visual appearance of UI elements
*
*
* Defines visual properties like color, texture, border, etc.
*/
@ECSComponent('UIRender')
@Serializable({ version: 1, typeId: 'UIRender' })
export class UIRenderComponent extends Component {
/**
*
* Type of rendering
*/
@Serialize()
@Property({
type: 'enum',
label: 'Type',
options: [
{ value: 'rect', label: 'Rectangle' },
{ value: 'image', label: 'Image' },
{ value: 'ninepatch', label: 'Nine Patch' },
{ value: 'circle', label: 'Circle' },
{ value: 'rounded-rect', label: 'Rounded Rect' }
]
})
public type: UIRenderType = UIRenderType.Rect;
// ===== 颜色 Colors =====
/**
* (0xRRGGBB)
* Background color in hex format
*/
@Serialize()
@Property({ type: 'color', label: 'Background Color' })
public backgroundColor: number = 0xFFFFFF;
/**
* (0-1)
* Background alpha
*/
@Serialize()
@Property({ type: 'number', label: 'Background Alpha', min: 0, max: 1, step: 0.01 })
public backgroundAlpha: number = 1;
/**
*
* Whether to fill background
*/
@Serialize()
@Property({ type: 'boolean', label: 'Fill Background' })
public fillBackground: boolean = true;
// ===== 纹理 Texture =====
/**
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
* GUID ID
* Texture asset GUID or runtime ID
*/
@Serialize()
@Property({ type: 'asset', label: 'Texture', assetType: 'texture' })
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
public textureGuid: string | number | null = null;
/**
* UV ()
* Texture UV coordinates (for atlas)
*/
public textureUV: { u0: number; v0: number; u1: number; v1: number } | null = null;
/**
* (0xRRGGBB)
* Texture tint color
*/
public textureTint: number = 0xFFFFFF;
// ===== 九宫格 Nine-Patch =====
/**
* [top, right, bottom, left]
* Nine-patch margins
*/
public ninePatchMargins: [number, number, number, number] = [0, 0, 0, 0];
// ===== 边框 Border =====
/**
*
* Border width
*/
@Property({ type: 'number', label: 'Border Width', min: 0 })
public borderWidth: number = 0;
/**
*
* Border color
*/
@Property({ type: 'color', label: 'Border Color' })
public borderColor: number = 0x000000;
/**
*
* Border alpha
*/
@Property({ type: 'number', label: 'Border Alpha', min: 0, max: 1, step: 0.01 })
public borderAlpha: number = 1;
/**
* [topLeft, topRight, bottomRight, bottomLeft]
* Corner radius for each corner
*/
public borderRadius: [number, number, number, number] = [0, 0, 0, 0];
// ===== 阴影 Shadow =====
/**
*
* Whether shadow is enabled
*/
@Property({ type: 'boolean', label: 'Shadow Enabled' })
public shadowEnabled: boolean = false;
/**
* X
* Shadow X offset
*/
@Property({ type: 'number', label: 'Shadow Offset X' })
public shadowOffsetX: number = 0;
/**
* Y
* Shadow Y offset
*/
@Property({ type: 'number', label: 'Shadow Offset Y' })
public shadowOffsetY: number = 2;
/**
*
* Shadow blur radius
*/
@Property({ type: 'number', label: 'Shadow Blur', min: 0 })
public shadowBlur: number = 4;
/**
*
* Shadow color
*/
@Property({ type: 'color', label: 'Shadow Color' })
public shadowColor: number = 0x000000;
/**
*
* Shadow alpha
*/
@Property({ type: 'number', label: 'Shadow Alpha', min: 0, max: 1, step: 0.01 })
public shadowAlpha: number = 0.3;
// ===== 渐变 Gradient =====
/**
*
* Gradient type
*/
public gradientType: 'none' | 'linear' | 'radial' = 'none';
/**
* 线
* Gradient angle for linear gradient
*/
public gradientAngle: number = 0;
/**
* [[position, color, alpha], ...]
* Gradient color stops
*/
public gradientStops: Array<[number, number, number]> = [];
/**
*
* Set solid color background
*/
public setColor(color: number, alpha: number = 1): this {
this.backgroundColor = color;
this.backgroundAlpha = alpha;
this.fillBackground = true;
return this;
}
/**
*
* Set image texture
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
*
* @param textureGuid - GUID | Texture asset GUID
*/
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
public setImage(textureGuid: string | number): this {
this.type = UIRenderType.Image;
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
this.textureGuid = textureGuid;
return this;
}
/**
*
* Set nine-patch image
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
*
* @param textureGuid - GUID | Texture asset GUID
* @param margins - | Nine-patch margins
*/
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
public setNinePatch(textureGuid: string | number, margins: [number, number, number, number]): this {
this.type = UIRenderType.NinePatch;
feat(asset): 统一资产引用使用 GUID 替代路径 (#287) * feat(world-streaming): 添加世界流式加载系统 实现基于区块的世界流式加载系统,支持开放世界游戏: 运行时包 (@esengine/world-streaming): - ChunkComponent: 区块实体组件,包含坐标、边界、状态 - StreamingAnchorComponent: 流式锚点组件(玩家/摄像机) - ChunkLoaderComponent: 流式加载配置组件 - ChunkStreamingSystem: 区块加载/卸载调度系统 - ChunkCullingSystem: 区块可见性剔除系统 - ChunkManager: 区块生命周期管理服务 - SpatialHashGrid: 空间哈希网格 - ChunkSerializer: 区块序列化 编辑器包 (@esengine/world-streaming-editor): - ChunkVisualizer: 区块可视化覆盖层 - ChunkLoaderInspectorProvider: 区块加载器检视器 - StreamingAnchorInspectorProvider: 流式锚点检视器 - WorldStreamingPlugin: 完整插件导出 * feat(asset): 统一资产引用使用 GUID 替代路径 将所有组件的资产引用字段从路径改为 GUID: - SpriteComponent: texture -> textureGuid, material -> materialGuid - SpriteAnimatorComponent: AnimationFrame.texture -> textureGuid - UIRenderComponent: texture -> textureGuid - UIButtonComponent: normalTexture -> normalTextureGuid 等 - AudioSourceComponent: clip -> clipGuid - ParticleSystemComponent: 已使用 textureGuid 修复 AssetRegistryService 注册问题和路径规范化, 添加渲染系统的 GUID 解析支持。 * fix(sprite-editor): 更新 material 为 materialGuid * fix(editor-app): 更新 AnimationFrame.texture 为 textureGuid
2025-12-06 14:08:48 +08:00
this.textureGuid = textureGuid;
this.ninePatchMargins = margins;
return this;
}
/**
*
* Set border style
*/
public setBorder(width: number, color: number, alpha: number = 1): this {
this.borderWidth = width;
this.borderColor = color;
this.borderAlpha = alpha;
return this;
}
/**
*
* Set corner radius (uniform or per-corner)
*/
public setCornerRadius(radius: number | [number, number, number, number]): this {
if (typeof radius === 'number') {
this.borderRadius = [radius, radius, radius, radius];
} else {
this.borderRadius = radius;
}
const hasRadius = typeof radius === 'number' ? radius > 0 : radius.some(r => r > 0);
if (hasRadius) {
this.type = UIRenderType.RoundedRect;
}
return this;
}
/**
*
* Set shadow style
*/
public setShadow(offsetX: number, offsetY: number, blur: number, color: number, alpha: number = 0.3): this {
this.shadowEnabled = true;
this.shadowOffsetX = offsetX;
this.shadowOffsetY = offsetY;
this.shadowBlur = blur;
this.shadowColor = color;
this.shadowAlpha = alpha;
return this;
}
/**
* 线
* Set linear gradient
*/
public setLinearGradient(angle: number, stops: Array<[number, number, number]>): this {
this.gradientType = 'linear';
this.gradientAngle = angle;
this.gradientStops = stops;
return this;
}
}