super-hero/assets/cc-common/cc-ui/UmButtonCustom.ts

103 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-05-08 04:03:33 -07:00
import { _decorator, Button, CCFloat, Color, Label, Sprite, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('UmButtonCustom')
export class UmButtonCustom extends Button {
@property(Label) public lbTitle: Label = null!;
@property(Sprite) public spIcon: Sprite = null!;
@property(CCFloat) pressedScale: Number = 0.95;
@property(CCFloat) textPosYChange: Number = 0;
@property(CCFloat) iconPosYChange: Number = 0;
@property(Color) public textColorNormal: Color = new Color(255, 255, 255, 255);
@property(Color) public textColorPressed: Color = new Color(255, 255, 255, 180);
private _state: string = "none";
private _textPosY = -1001;
private _iconPosY = -1001;
start() {
// if (this.lbTitle) {
// this._textOriginY = this.lbTitle.node.position.y;
// HTLog.error("BUTTON CUSTOM ===>>> ", this.lbTitle.string, this._textOriginY);
// }
// if (this.spIcon) {
// this._iconOriginY = this.spIcon.node.position.y;
// }
}
private _updateUINormal() {
if (this.lbTitle) {
this.lbTitle.setColor(this.textColorNormal);
this.lbTitle.node.setScale(Vec3.ONE);
if (this._textPosY < -1000) {
this._textPosY = this.lbTitle.node.position.y;
}
else {
this.lbTitle.node.setPositionY(this._textPosY);
}
}
if (this.spIcon) {
this.spIcon.setColor(this.textColorNormal);
this.spIcon.node.setScale(Vec3.ONE);
if (this._iconPosY < -1000) {
this._iconPosY = this.spIcon.node.position.y;
}
else {
this.spIcon.node.setPositionY(this._iconPosY);
}
}
}
private _updateUIPressed() {
if (this.lbTitle) {
this.lbTitle.setColor(this.textColorPressed);
this.lbTitle.node.setScale(new Vec3(Number(this.pressedScale), Number(this.pressedScale), 1));
// this.lbTitle.node.setPositionY(this.lbTitle.node.position.y - Number(this.textPosY));
if (this._textPosY < -1000) {
this._textPosY = this.lbTitle.node.position.y;
}
this.lbTitle.node.setPositionY(this._textPosY + Number(this.textPosYChange));
}
if (this.spIcon) {
this.spIcon.setColor(this.textColorPressed);
this.spIcon.node.setScale(new Vec3(Number(this.pressedScale), Number(this.pressedScale), 1));
if (this._iconPosY < -1000) {
this._iconPosY = this.spIcon.node.position.y;
}
this.spIcon.node.setPositionY(this._iconPosY + Number(this.iconPosYChange));
}
}
protected _applyTransition(state: string) {
super._applyTransition(state);
if (this._state !== state) {
// HTLog.log("==>ButtonCustom: _updateSpriteTransition = ", state);
if (state === "normal" || state === "hover" || state === "disabled") {
this._updateUINormal();
}
if (state === "pressed") {
this._updateUIPressed();
}
this._state = state;
}
}
}