105 lines
2.8 KiB
TypeScript
105 lines
2.8 KiB
TypeScript
import { _decorator, Color, Component, EventMouse, Node, Sprite, UITransform, v2 } from 'cc';
|
|
import { NpkImage } from '../Tool/NPKImage';
|
|
import { MapTileDirection } from './MapTileData';
|
|
import { BaseSprite } from '../GlobalScript/CommonComponent/BaseSprite';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
enum buttonState{
|
|
// 普通
|
|
Normal,
|
|
// 悬停
|
|
Hover,
|
|
// // 按下
|
|
// Down,
|
|
}
|
|
|
|
|
|
@ccclass('MapTileButtonComponent')
|
|
export class MapTileButtonComponent extends Component {
|
|
|
|
direction: MapTileDirection;
|
|
|
|
/// 状态
|
|
state: buttonState = buttonState.Normal;
|
|
|
|
//精灵对象
|
|
private SpriteObj: Sprite;
|
|
|
|
start() {
|
|
|
|
|
|
// //判断是否有精灵 如果没有 就给他搞一个
|
|
// if (this.node.getComponent(Sprite))
|
|
// this.SpriteObj = this.node.getComponent(Sprite);
|
|
// else
|
|
// this.SpriteObj = this.node.addComponent(Sprite);
|
|
|
|
// //设置节点锚点为左上角
|
|
// this.node.getComponent(UITransform).anchorPoint = v2(0, 1);
|
|
// //设置类型
|
|
// this.SpriteObj.sizeMode = Sprite.SizeMode.RAW;
|
|
// //设置
|
|
// this.SpriteObj.trim = false;
|
|
|
|
const bs = this.node.addComponent( BaseSprite );
|
|
this.SpriteObj = bs.SpriteObj;
|
|
|
|
let index = 24;
|
|
switch (this.direction) {
|
|
case MapTileDirection.horizontal:
|
|
index = 24;
|
|
case MapTileDirection.vertical:
|
|
index = 25;
|
|
case MapTileDirection.nook:
|
|
index = 26;
|
|
}
|
|
|
|
bs.updateSpriteFrame(NpkImage.board,index);
|
|
|
|
|
|
this.node.on(Node.EventType.MOUSE_ENTER, this.OnHover, this);
|
|
this.node.on(Node.EventType.MOUSE_LEAVE, this.OffHover, this);
|
|
this.node.on(Node.EventType.MOUSE_DOWN, this.OnDown, this);
|
|
// this.node.on(Node.EventType.MOUSE_UP, this.OnUp, this);
|
|
|
|
}
|
|
|
|
update(deltaTime: number) {
|
|
if (this.SpriteObj){
|
|
switch (this.state) {
|
|
case buttonState.Normal:
|
|
this.SpriteObj.color = Color.WHITE;
|
|
break;
|
|
case buttonState.Hover:
|
|
this.SpriteObj.color = Color.YELLOW;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
OnHover(event: EventMouse) {
|
|
this.state = buttonState.Normal;
|
|
}
|
|
OffHover(event: EventMouse) {
|
|
this.state = buttonState.Hover;
|
|
}
|
|
/// 按下就选择
|
|
OnDown(event: EventMouse) {
|
|
// if (event.getButton() != EventMouse.BUTTON_LEFT) return;
|
|
// const np = this.node.position;
|
|
// this.node.setPosition(np.x,np.y -1);
|
|
}
|
|
|
|
OnUp(event: EventMouse) {
|
|
//必须是鼠标左键
|
|
if (event.getButton() != EventMouse.BUTTON_LEFT) return;
|
|
this.state = buttonState.Normal;
|
|
const np = this.node.position;
|
|
this.node.setPosition(np.x,np.y +1);
|
|
}
|
|
|
|
}
|
|
|
|
|