182 lines
4.7 KiB
Plaintext
182 lines
4.7 KiB
Plaintext
/*
|
|
文件名:UI_Widget.nut
|
|
路径:Core/UI_Class/UI_Widget.nut
|
|
创建日期:2024-11-08 14:24
|
|
文件用途: 控件基类
|
|
*/
|
|
//基础UI
|
|
class Yosin_CommonUi extends Yosin_BaseWindow {
|
|
|
|
ObjectId = null;
|
|
|
|
Localtion_X = 0;
|
|
Localtion_Y = 0;
|
|
Width = null;
|
|
Height = null;
|
|
|
|
isLBDown = false;
|
|
//是否悬停
|
|
isInRect = false;
|
|
|
|
OnClick = null;
|
|
OnClickSound = null;
|
|
|
|
Data = null;
|
|
|
|
constructor(x, y, width, height) {
|
|
this.Localtion_X = x;
|
|
this.Localtion_Y = y;
|
|
this.Width = width;
|
|
this.Height = height;
|
|
|
|
ObjectId = clock();
|
|
base.constructor();
|
|
//构造时第一次同步坐标
|
|
SyncPos(x, y);
|
|
}
|
|
|
|
//override
|
|
//鼠标事件回调
|
|
function OnMouseProc(MousePos_X, MousePos_Y) {
|
|
local Pos = GetWorldPosition();
|
|
if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, Pos.x, Pos.y, Width, Height)) isInRect = true;
|
|
else isInRect = false;
|
|
base.OnMouseProc(MousePos_X, MousePos_Y);
|
|
}
|
|
//鼠标左键按下回调
|
|
function OnMouseLbDown(MousePos_X, MousePos_Y) {
|
|
local Pos = GetWorldPosition();
|
|
if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, Pos.x, Pos.y, Width, Height)) {
|
|
isLBDown = true;
|
|
//如果有配置按键音效
|
|
if (OnClickSound) {
|
|
Sq_PlaySoundEffect(OnClickSound);
|
|
}
|
|
}
|
|
base.OnMouseLbDown(MousePos_X, MousePos_Y);
|
|
}
|
|
//鼠标左键弹起回调
|
|
function OnMouseLbUp(MousePos_X, MousePos_Y) {
|
|
isLBDown = false;
|
|
base.OnMouseLbUp(MousePos_X, MousePos_Y);
|
|
}
|
|
|
|
//鼠标左键单击回调
|
|
function OnMouseLbClick(MousePos_X, MousePos_Y) {
|
|
local Pos = GetWorldPosition();
|
|
if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, Pos.x, Pos.y, Width, Height)) {
|
|
if (OnClick) OnClick(this);
|
|
}
|
|
base.OnMouseLbClick(MousePos_X, MousePos_Y);
|
|
}
|
|
|
|
}
|
|
|
|
//基础按钮
|
|
class Yosin_BaseButton extends Yosin_CommonUi {
|
|
//按钮状态
|
|
State = 0;
|
|
Path = null;
|
|
Idx = null;
|
|
|
|
Sprite = null;
|
|
SpriteState = -1;
|
|
FrameList = null;
|
|
|
|
|
|
constructor(X, Y, W, H, Path, Idx) {
|
|
this.Path = Path;
|
|
this.Idx = Idx;
|
|
base.constructor(X, Y, W, H);
|
|
|
|
FrameList = [];
|
|
Sprite = CL_SpriteObject();
|
|
// Sprite.ShowBorder(true);
|
|
Addchild(Sprite);
|
|
|
|
for (local i = 0; i< 4; i++) {
|
|
local Sf = CL_SpriteFrameObject(this.Path, this.Idx + i);
|
|
FrameList.push(Sf);
|
|
}
|
|
}
|
|
|
|
function ChangeFrame() {
|
|
//状态更改 刷新精灵帧
|
|
if (State != SpriteState) {
|
|
//如果按下 调整Y坐标向下一个单位
|
|
if (State == 2) {
|
|
Y += 1;
|
|
SyncPos(X, Y);
|
|
} else if (SpriteState == 2) {
|
|
Y -= 1;
|
|
SyncPos(X, Y);
|
|
}
|
|
SpriteState = State;
|
|
Sprite.SetFrame(FrameList[SpriteState]);
|
|
Sprite.SetPosition(0, 0);
|
|
}
|
|
}
|
|
|
|
function Proc(Dt) {
|
|
//不可用
|
|
if (State == 3) {
|
|
|
|
} else {
|
|
//按下
|
|
if (isLBDown) {
|
|
State = 2;
|
|
}
|
|
//悬停
|
|
else if (isInRect) {
|
|
State = 1;
|
|
}
|
|
//普通
|
|
else {
|
|
State = 0;
|
|
}
|
|
}
|
|
ChangeFrame();
|
|
}
|
|
}
|
|
|
|
//三分法拉伸
|
|
class Yosin_EmeStretch extends Yosin_CommonUi {
|
|
|
|
Path = null;
|
|
Idx = null;
|
|
//按钮状态
|
|
SpriteList = null;
|
|
|
|
constructor(X, Y, W, H, Path, Idx, Direction = true) {
|
|
this.Path = Path;
|
|
this.Idx = Idx;
|
|
base.constructor(X, Y, W, H);
|
|
|
|
SpriteList = [];
|
|
SpriteList.push(CL_SpriteObject(Path, Idx));
|
|
SpriteList.push(CL_SpriteObject(Path, Idx + 1));
|
|
SpriteList.push(CL_SpriteObject(Path, Idx + 2));
|
|
|
|
//横向
|
|
if (Direction) {
|
|
local ScaleW = (W - SpriteList[0].GetSize().w - SpriteList[2].GetSize().w);
|
|
local ScaleRate = ScaleW / SpriteList[1].GetSize().w;
|
|
SpriteList[1].SetPosition(SpriteList[0].GetSize().w, 0);
|
|
SpriteList[1].SetScale(ScaleRate, 1.0);
|
|
SpriteList[2].SetPosition(SpriteList[0].GetSize().w + ScaleW, 0);
|
|
}
|
|
//纵向
|
|
else {
|
|
local ScaleH = (H - SpriteList[0].GetSize().h - SpriteList[2].GetSize().h);
|
|
local ScaleRate = ScaleH / SpriteList[1].GetSize().h;
|
|
SpriteList[1].SetPosition(0, SpriteList[0].GetSize().h);
|
|
SpriteList[1].SetScale(1.0, ScaleRate);
|
|
SpriteList[2].SetPosition(0, SpriteList[0].GetSize().h + ScaleH);
|
|
}
|
|
|
|
foreach(Child in SpriteList) {
|
|
Addchild(Child);
|
|
}
|
|
|
|
}
|
|
} |