DOF/sqr/User/UI/Widget/BaseWidget.nut

600 lines
15 KiB
Plaintext
Raw Normal View History

2025-02-20 13:53:36 +08:00
/*
文件名:BaseWidget.nut
路径:User/UI/Widget/BaseWidget.nut
创建日期:2025-02-07 21:37
文件用途:
*/
//基础按钮
class Yosin_BaseButton extends Yosin_CommonUi {
//按钮状态
State = 0;
Path = null;
Idx = null;
Sprite = null;
SpriteState = -1;
FrameList = null;
//按下时的模拟偏移
DownSimulateOffset = true;
OnClickSound = SOUND.CLICK_BUTTON2;
OnClickMoveSound = SOUND.CLICK_MOVE;
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) {
//按下时模拟偏移的Flag 如果按下 调整Y坐标向下一个单位
if (DownSimulateOffset) {
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 CL_CanvasObject {
Path = null;
Idx = null;
//按钮状态
SpriteList = null;
constructor(W, H, Path, Idx, Direction = true) {
this.Path = Path;
this.Idx = Idx;
base.constructor();
// 创建画布
CL_CanvasObject();
// 重设大小并清空
ResizeAndClear(W, H);
// 开始绘制
BeginDraw();
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.0);
SpriteList[1].SetScale(ScaleRate, 1.0);
SpriteList[2].SetPosition(SpriteList[0].GetSize().w + ScaleW, 0.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);
DrawSprite(Child);
}
// 结束绘制
EndDraw();
// 添加画布
// Addchild(Canvas);
}
}
//九宫格拉伸
function Yosin_NineBoxStretch(width, height, path, imgId) {
// 创建画布
local Canvas = CL_CanvasObject();
// 重设大小并清空
Canvas.ResizeAndClear(width, height);
// 开始绘制
Canvas.BeginDraw();
// 左上角
// local backgroundTopLeft = CL_SpriteObject(path, imgId);
local backgroundTopLeft = CL_SpriteObject(path, imgId);
// 上边
local backgroundTop = CL_SpriteObject(path, imgId + 1);
// 右上角
local backgroundTopRight = CL_SpriteObject(path, imgId + 2);
// 左边
local backgroundLeft = CL_SpriteObject(path, imgId + 3);
// 中间
local backgroundCenter = CL_SpriteObject(path, imgId + 4);
// 右边
local backgroundRight = CL_SpriteObject(path, imgId + 5);
// 左下角
local backgroundBottomLeft = CL_SpriteObject(path, imgId + 6);
// 下边
local backgroundBottom = CL_SpriteObject(path, imgId + 7);
// 右下角
local backgroundBottomRight = CL_SpriteObject(path, imgId + 8);
// 左上角
Canvas.DrawSprite(backgroundTopLeft);
local TopLeftSize = backgroundTopLeft.GetSize();
local TopLeftBottom = TopLeftSize.h;
local TopLeftRight = TopLeftSize.w;
// 中间图片大小
local centerImgSize = backgroundCenter.GetSize();
local centerImgWidth = centerImgSize.w;
local centerImgHeight = centerImgSize.h;
local centerWidth = width - backgroundTopLeft.GetSize().w - backgroundTopRight.GetSize().w;
local centerHeight = height - backgroundTopLeft.GetSize().h - backgroundBottomLeft.GetSize().h;
local scaleW = (centerWidth - 1).tofloat() / centerImgWidth.tofloat();
local scaleH = (centerHeight - 1).tofloat() / centerImgHeight.tofloat();
// 上边
backgroundTop.SetScale(scaleW, 1);
backgroundTop.SetPosition(TopLeftRight, 0);
Canvas.DrawSprite(backgroundTop);
// 右上角
backgroundTopRight.SetPosition(width - backgroundTopRight.GetSize().w - 1, 0);
Canvas.DrawSprite(backgroundTopRight);
// 左边
backgroundLeft.SetScale(1, scaleH);
backgroundLeft.SetPosition(0, TopLeftBottom);
Canvas.DrawSprite(backgroundLeft);
// 中间
backgroundCenter.SetScale(scaleW, scaleH);
// Addchild(backgroundCenter);
backgroundCenter.SetPosition(TopLeftRight, backgroundLeft.Y);
Canvas.DrawSprite(backgroundCenter);
// 右边
backgroundRight.SetScale(1, scaleH);
backgroundRight.SetPosition(width - backgroundRight.GetSize().w - 1, backgroundCenter.Y);
Canvas.DrawSprite(backgroundRight);
// 左下角
backgroundBottomLeft.SetPosition(0, height - backgroundBottomLeft.GetSize().h - 1);
Canvas.DrawSprite(backgroundBottomLeft);
// 下边
backgroundBottom.SetScale(scaleW, 1);
backgroundBottom.SetPosition(TopLeftRight, backgroundBottomLeft.Y);
Canvas.DrawSprite(backgroundBottom);
// 右下角
backgroundBottomRight.SetPosition(width - backgroundBottomRight.GetSize().w - 1, backgroundBottomLeft.Y);
Canvas.DrawSprite(backgroundBottomRight);
// 结束绘制
Canvas.EndDraw();
// 添加画布
// Addchild(Canvas);
local Sp = CL_SpriteObject();
Sp.SetFrame(Canvas.ExportSpriteFrame());
return Sp;
}
//拼接按钮
class Yosin_SplicingButton extends Yosin_CommonUi {
//按钮状态
State = 0;
Path = null;
Idx = null;
SpriteList = null;
SpriteState = -1;
FrameList = null;
//按下时的模拟偏移
DownSimulateOffset = true;
constructor(X, Y, W, H, Path, Idx, Direction = true, UnavailableFlag = true, IsTwoImg = false) {
this.Path = Path;
this.Idx = Idx;
base.constructor(X, Y, W, H);
SpriteList = array(4);
//普通态
SpriteList[0] = Yosin_EmeStretch(W, H, Path, Idx, Direction);
//悬停态
SpriteList[1] = Yosin_EmeStretch(W, H, Path, Idx + (UnavailableFlag ? 4 : 3), Direction);
//按下态
SpriteList[2] = Yosin_EmeStretch(W, H, Path, Idx + (IsTwoImg ? 3 : (UnavailableFlag ? 8 : 6)), Direction);
if (UnavailableFlag) {
//不可用态
SpriteList[3] = Yosin_EmeStretch(W, H, Path, Idx + 12, Direction);
}
}
function ChangeFrame() {
//状态更改 刷新精灵帧
if (State != SpriteState) {
//按下时模拟偏移的Flag 如果按下 调整Y坐标向下一个单位
if (DownSimulateOffset) {
if (State == 2) {
Y += 1;
SyncPos(X, Y);
} else if (SpriteState == 2) {
Y -= 1;
SyncPos(X, Y);
}
}
if (SpriteState != -1) {
RemoveUIChild(SpriteList[SpriteState]);
}
SpriteState = State;
Addchild(SpriteList[SpriteState]);
}
}
function Proc(Dt) {
//不可用
if (State == 3) {
} else {
//按下
if (isLBDown) {
State = 2;
}
//悬停
else if (isInRect) {
State = 1;
}
//普通
else {
State = 0;
}
}
ChangeFrame();
}
}
// 标题按钮
class titleButton extends Yosin_BaseButton {
index = null;
select = false;
cacheSelect = false;
cacheY = null;
LBDownOnClick = null;
constructor(X, Y, W, H, Path, Idx, title) {
base.constructor(X, Y, W, H, Path, Idx);
cacheY = Y;
DownSimulateOffset = false;
local backText = FontAssetManager.GenerateNormal(title, true, {
color = sq_RGBA(130, 114, 84, 255)
});
backText.SetUpdateFunc(function(Text, Dt) {
if (select == cacheSelect) return;
if (select) {
Text.SetFillColor(sq_RGBA(187, 176, 149, 255));
} else {
Text.SetFillColor(sq_RGBA(130, 114, 84, 255));
}
cacheSelect = select;
})
backText.SetPosition(9, 2);
Addchild(backText);
}
function ChangeFrame() {
//状态更改 刷新精灵帧
if (State != SpriteState) {
if (State == 2) {
Y = cacheY - 1;
SyncPos(X, Y);
} else if (SpriteState == 2) {
Y = cacheY;
SyncPos(X, Y);
}
SpriteState = State;
Sprite.SetFrame(FrameList[SpriteState]);
Sprite.SetPosition(0, 0);
}
}
function Proc(Dt) {
if (select) return;
//不可用
if (State == 3) {
} else {
//按下
if (isLBDown) {
State = 2;
select = true;
if (LBDownOnClick != null) {
LBDownOnClick(this);
}
}
//悬停
else if (isInRect) {
State = 1;
}
//普通
else {
State = 0;
}
}
ChangeFrame();
}
}
// 拉伸标题按钮
class Yosin_StretchTitleButton extends Yosin_CommonUi {
index = null;
//按钮状态
State = 0;
//ui的当前状态
uiState = 0;
cecheY = null;
titleText = null;
SpriteList = null;
// 鼠标左键按下时的回调
LBDownOnClick = null;
constructor(X, Y, W, H, Path, Idx, title) {
base.constructor(X, Y, W, H)
cecheY = Y;
SpriteList = array(3);
//普通态
SpriteList[0] = Yosin_EmeStretch(W, H, Path, Idx);
Addchild(SpriteList[0]);
//悬停态
SpriteList[1] = Yosin_EmeStretch(W, H, Path, Idx + 3);
SpriteList[1].SetVisible(false)
Addchild(SpriteList[1]);
//按下态
SpriteList[2] = Yosin_EmeStretch(W, H, Path, Idx + 6);
SpriteList[2].SetVisible(false)
Addchild(SpriteList[2]);
// 文字
titleText = FontAssetManager.GenerateNormal(title, true, {
color = sq_RGBA(130, 114, 84, 255)
});
titleText.SetUpdateFunc(function(Text, Dt) {
if (select == cacheSelect) return;
if (select) {
Text.SetFillColor(sq_RGBA(187, 176, 149, 255));
} else {
Text.SetFillColor(sq_RGBA(130, 114, 84, 255));
}
cacheSelect = select;
})
titleText.SetPosition(W / 2 - titleText.GetSize().w / 2, 2);
Addchild(titleText);
}
// 设置为选中状态
function SetSelect(select) {
if (select) {
State = 2;
ChangeFrame();
} else {
State = 0;
ChangeFrame();
}
}
function ChangeFrame() {
//状态更改 刷新精灵帧
if (State != uiState) {
if (State == 2) {
SyncPos(X, cecheY - 1);
} else {
SyncPos(X, cecheY);
}
uiState = State;
for (local i = 0; i< SpriteList.len(); i++) {
SpriteList[i].SetVisible(i == uiState);
}
}
}
function Proc(Dt) {
if (State == 2) return;
//按下
if (isLBDown) {
if (LBDownOnClick != null) {
LBDownOnClick(this);
}
State = 2;
}
//悬停
else if (isInRect) {
State = 1;
}
//普通
else {
State = 0;
}
ChangeFrame();
}
}
// 横向多个标题单选按钮
class Yosin_RowMoreTitleBtn extends Yosin_CommonUi {
LBDownOnClick = null;
btns = null;
tests = null;
constructor(X, Y, W, titles, path, idx, baseWidth = 48) {
this.tests = titles;
btns = [];
local btnX = 5;
for (local i = 0; i< titles.len(); i++) {
local textW = FontAssetManager.GenerateNormal(titles[i], true).GetSize().w + 10;
local btnW = baseWidth;
btnW = textW > btnW ? textW : btnW;
local titleBtn = Yosin_StretchTitleButton(btnX, 1, btnW, 19, path, idx, titles[i]);
titleBtn.index = i;
titleBtn.LBDownOnClick = function(btn) {
LBDownOnClick(btn.Parent, btn.index);
for (local i = 0; i< btns.len(); i++) {
btns[i].SetSelect(false);
btns[i].titleText.SetFillColor(sq_RGBA(130, 114, 84, 255));
}
btn.SetSelect(true);
btns[btn.index].titleText.SetFillColor(sq_RGBA(187, 176, 149, 255));
}.bindenv(this);
btns.push(titleBtn);
btnX += btnW;
}
base.constructor(X, Y, btnX, 21);
for (local i = 0; i< btns.len(); i++) {
AddUIChild(btns[i]);
}
btns[0].SetSelect(true);
// 创建画布
local Canvas = CL_CanvasObject();
Canvas.SetPosition(0, 19);
// 重设大小并清空
Canvas.ResizeAndClear(W, 1);
// 设置填充画刷 用于绘制边框和线条
Canvas.SetFillBrush(sq_RGBA(66, 61, 59, 250));
// 设置轮廓画刷 用于绘制边框和线条
Canvas.SetStrokeBrush(sq_RGBA(66, 61, 59, 250));
// 开始绘制
Canvas.BeginDraw();
// 画线段
Canvas.DrawLine(0, 1, W, 1);
// 结束绘制
Canvas.EndDraw();
// 添加画布
Addchild(Canvas);
}
}
// 进度显示
class Yosin_Schedule extends Yosin_CommonUi {
//背景
BgSprite = null;
//条
BarSprite = null;
// schedule 进度比例0-1
constructor(X, Y, W, H, path, idx) {
base.constructor(X, Y, W, H);
BgSprite = CL_SpriteObject(path, idx + 1);
Addchild(BgSprite);
BarSprite = CL_SpriteObject(path, idx);
Addchild(BarSprite);
}
function SetPercentage(Rate) {
local barSize = BarSprite.GetSize();
local barW = barSize.w * Rate;
BarSprite.SetCropRect(BarSprite.X, BarSprite.Y, barW, barSize.h);
BarSprite.SetSize(barW, barSize.h);
}
}