Files
esengine/packages/ui/src/components/widgets/UIButtonComponent.ts

312 lines
8.0 KiB
TypeScript
Raw Normal View History

import { Component, ECSComponent, Property, Serializable, Serialize } from '@esengine/ecs-framework';
/**
*
* Button state style configuration
*/
export interface UIButtonStyle {
backgroundColor: number;
backgroundAlpha: number;
textColor: number;
borderColor: number;
borderWidth: number;
texture?: string;
}
/**
*
* Button display mode
*/
export type UIButtonDisplayMode = 'color' | 'texture' | 'both';
/**
* UI
* UI Button Component - Button-specific state and callbacks
*/
@ECSComponent('UIButton')
@Serializable({ version: 1, typeId: 'UIButton' })
export class UIButtonComponent extends Component {
/**
*
* Button label text
*/
@Serialize()
@Property({ type: 'string', label: 'Label' })
public label: string = 'Button';
// ===== 显示模式 Display Mode =====
/**
*
* Display mode: color only, texture only, or both
*/
@Serialize()
@Property({
type: 'enum',
label: 'Display Mode',
options: ['color', 'texture', 'both']
})
public displayMode: UIButtonDisplayMode = 'color';
// ===== 状态纹理 State Textures =====
/**
*
* Normal state texture
*/
@Serialize()
@Property({ type: 'asset', label: 'Normal Texture', assetType: 'texture' })
public normalTexture: string = '';
/**
*
* Hover state texture
*/
@Serialize()
@Property({ type: 'asset', label: 'Hover Texture', assetType: 'texture' })
public hoverTexture: string = '';
/**
*
* Pressed state texture
*/
@Serialize()
@Property({ type: 'asset', label: 'Pressed Texture', assetType: 'texture' })
public pressedTexture: string = '';
/**
*
* Disabled state texture
*/
@Serialize()
@Property({ type: 'asset', label: 'Disabled Texture', assetType: 'texture' })
public disabledTexture: string = '';
// ===== 状态样式 State Styles =====
/**
*
* Normal state background color
*/
@Serialize()
@Property({ type: 'color', label: 'Normal Color' })
public normalColor: number = 0x4A90D9;
/**
*
* Hover state background color
*/
@Serialize()
@Property({ type: 'color', label: 'Hover Color' })
public hoverColor: number = 0x5BA0E9;
/**
*
* Pressed state background color
*/
@Serialize()
@Property({ type: 'color', label: 'Pressed Color' })
public pressedColor: number = 0x3A80C9;
/**
*
* Disabled state background color
*/
@Serialize()
@Property({ type: 'color', label: 'Disabled Color' })
public disabledColor: number = 0x888888;
/**
*
* Focused state background color
*/
@Serialize()
@Property({ type: 'color', label: 'Focused Color' })
public focusedColor: number = 0x4A90D9;
/**
*
* Text color
*/
@Serialize()
@Property({ type: 'color', label: 'Text Color' })
public textColor: number = 0xFFFFFF;
/**
*
* Disabled text color
*/
@Serialize()
@Property({ type: 'color', label: 'Disabled Text Color' })
public disabledTextColor: number = 0xCCCCCC;
// ===== 动画 Animation =====
/**
*
* Color transition duration in seconds
*/
@Serialize()
@Property({ type: 'number', label: 'Transition Duration', min: 0, step: 0.01 })
public transitionDuration: number = 0.1;
/**
*
* Current display color (for animation)
*/
public currentColor: number = 0x4A90D9;
/**
*
* Target color
*/
public targetColor: number = 0x4A90D9;
// ===== 回调 Callbacks =====
/**
*
* Click callback
*/
public onClick?: () => void;
/**
*
* Long press callback
*/
public onLongPress?: () => void;
/**
*
* Long press threshold in milliseconds
*/
@Serialize()
@Property({ type: 'number', label: 'Long Press Threshold', min: 0 })
public longPressThreshold: number = 500;
/**
*
* Long press timer
*/
public pressTimer: number = 0;
/**
*
* Whether long press has been triggered
*/
public longPressTriggered: boolean = false;
// ===== 配置 Configuration =====
/**
*
* Whether button is disabled
*/
@Serialize()
@Property({ type: 'boolean', label: 'Disabled' })
public disabled: boolean = false;
/**
*
* Whether to show ripple effect
*/
@Serialize()
@Property({ type: 'boolean', label: 'Show Ripple' })
public showRipple: boolean = false;
/**
*
* Ripple color
*/
@Serialize()
@Property({ type: 'color', label: 'Ripple Color' })
public rippleColor: number = 0xFFFFFF;
/**
*
* Ripple alpha
*/
@Serialize()
@Property({ type: 'number', label: 'Ripple Alpha', min: 0, max: 1, step: 0.01 })
public rippleAlpha: number = 0.3;
/**
*
* Get the background color that should be displayed based on state
*/
public getStateColor(state: 'disabled' | 'pressed' | 'hovered' | 'focused' | 'normal'): number {
if (this.disabled) return this.disabledColor;
switch (state) {
case 'pressed': return this.pressedColor;
case 'hovered': return this.hoverColor;
case 'focused': return this.focusedColor;
default: return this.normalColor;
}
}
/**
*
* Get the texture that should be displayed based on state
*/
public getStateTexture(state: 'disabled' | 'pressed' | 'hovered' | 'focused' | 'normal'): string {
if (this.disabled && this.disabledTexture) return this.disabledTexture;
switch (state) {
case 'pressed': return this.pressedTexture || this.normalTexture;
case 'hovered': return this.hoverTexture || this.normalTexture;
case 'focused': return this.normalTexture;
default: return this.normalTexture;
}
}
/**
* 使
* Whether to use texture for rendering
*/
public useTexture(): boolean {
return (this.displayMode === 'texture' || this.displayMode === 'both') && !!this.normalTexture;
}
/**
* 使
* Whether to use color for rendering
*/
public useColor(): boolean {
return this.displayMode === 'color' || this.displayMode === 'both';
}
/**
*
* Get the text color that should be displayed based on state
*/
public getTextColor(): number {
return this.disabled ? this.disabledTextColor : this.textColor;
}
/**
*
* Set color theme
*/
public setColors(normal: number, hover: number, pressed: number, disabled?: number): this {
this.normalColor = normal;
this.hoverColor = hover;
this.pressedColor = pressed;
if (disabled !== undefined) this.disabledColor = disabled;
this.currentColor = normal;
this.targetColor = normal;
return this;
}
/**
*
* Set textures for different states
*/
public setTextures(normal: string, hover?: string, pressed?: string, disabled?: string): this {
this.normalTexture = normal;
if (hover) this.hoverTexture = hover;
if (pressed) this.pressedTexture = pressed;
if (disabled) this.disabledTexture = disabled;
this.displayMode = 'texture';
return this;
}
}