DOF/sqr/User/UI/Window/5_Inventory/ItemCollect.nut

275 lines
7.8 KiB
Plaintext

/*
文件名:ItemCollect.nut
路径:User/UI/Window/5_Inventory/ItemCollect.nut
创建日期:2025-01-06 13:50
文件用途: 物品栏
*/
//物品槽
class ItemSlot {
X = null;
Y = null;
//物品对象
Item = null;
//物品对象的图标
ItemIcon = null;
//物品对象的详细信息窗口
ItemInfo = null;
//详细信息窗口显示Flag
ItemInfoShowFlag = false;
constructor() {
}
function SyncPos(x, y) {
this.X = x;
this.Y = y;
}
function SetItem(Item) {
this.Item = Item;
this.ItemIcon = this.Item.GetEquipmentIconSprite();
}
//生成详细信息
function GenerateInfo() {
this.ItemInfo = this.Item.GetEquipmentInfoWindow();
}
//显示详细信息
function ShowInfo(x, y) {
if (!this.ItemInfo) GenerateInfo();
this.ItemInfoShowFlag = true;
//设置位置
this.ItemInfo.SetPosition(x, y);
this.ItemInfo.ResetFocus();
}
//关闭显示详细信息
function CloseInfo() {
if (this.ItemInfo) {
this.ItemInfoShowFlag = false;
this.ItemInfo.CloseWindow();
}
}
}
// 物品栏
class _ItemCollection extends Yosin_CommonUi {
// 悬浮时显示的框
HoverEffect = null;
//行数
RowNum = null;
//底层画布对象
BottomCanvas = null;
//顶层画布对象
TopCanvas = null;
//物品对象List
ItemList = null;
//当前显示详细信息的物品对象
CurrentShowItem = null;
//当前鼠标指向的位置
ItemPos = null;
//拖拽中的物品对象
DragItem = null;
//拖拽物品原来的位置
DragItemPos = null;
constructor(x, y, rowNum) {
this.RowNum = rowNum;
//计算实际需要的高度
local RealH = 30 * rowNum;
base.constructor(x, y, 239, RealH);
//构造相应数量的槽
ItemList = array(8 * rowNum, null);
//整体底板
InitOverallBasePlate();
HoverEffect = CL_SpriteObject("sprite/interface/newstyle/windows/inventory/inventory.img", 131);
HoverEffect.SetZOrder(1);
HoverEffect.SetVisible(false);
Addchild(HoverEffect);
}
//根据高度绘制整体底板
function InitOverallBasePlate() {
// 创建画布
BottomCanvas = CL_CanvasObject();
// 重设大小并清空
BottomCanvas.ResizeAndClear(239, this.RowNum * 30);
// 开始绘制
BottomCanvas.BeginDraw();
foreach(pos, ItemObj in ItemList) {
local XPos = (pos % 8) * 30;
local YPos = (pos / 8) * 30;
local bg = CL_SpriteFrameObject("sprite/interface/newstyle/windows/inventory/inventory.img", 49);
BottomCanvas.DrawSpriteFrame(bg, XPos, YPos);
}
// 结束绘制
BottomCanvas.EndDraw();
// 添加画布
Addchild(BottomCanvas);
}
//设置道具列表
function SetItemList(gItemList) {
//创建道具
foreach(Index, ItemObject in gItemList) {
local ItemId = ItemObject.ItemId;
//TODO
local Item = GameItem.Equipment(ItemId);
ItemList[Index] = ItemSlot();
ItemList[Index].SetItem(Item);
}
RefreshItemList();
}
//刷新道具列表
function RefreshItemList() {
//如果不存在则构造画布
if (!TopCanvas) {
// 创建画布
TopCanvas = CL_CanvasObject();
// 添加画布
Addchild(TopCanvas);
}
// 重设大小并清空
TopCanvas.ResizeAndClear(239, this.RowNum * 30);
// 开始绘制
TopCanvas.BeginDraw();
foreach(pos, ItemObj in ItemList) {
if (ItemObj) {
local XPos = (pos % 8) * 30;
local YPos = (pos / 8) * 30;
ItemObj.ItemIcon.SetPosition(XPos, YPos);
TopCanvas.DrawSprite(ItemObj.ItemIcon, XPos, YPos);
}
}
// 结束绘制
TopCanvas.EndDraw();
}
//override
//鼠标事件回调
function OnMouseProc(MousePos_X, MousePos_Y) {
base.OnMouseProc(MousePos_X, MousePos_Y);
if (isInRect) {
local WorldPosition = this.GetWorldPosition();
local xx = MousePos_X - WorldPosition.x;
local yy = MousePos_Y - WorldPosition.y;
local column = (yy / 30).tointeger();
local row = (xx / 30).tointeger();
//指向的项目位置
local Idx = column * 8 + row;
ItemPos = Idx;
//如果有道具
if (ItemList[Idx]) {
//设置透明度
HoverEffect.SetOpacity(0.4);
//如果没有物品信息窗口
if (!ItemList[Idx].ItemInfo) {
ItemList[Idx].GenerateInfo();
}
if (!ItemList[Idx].ItemInfoShowFlag) {
//关闭上一个显示的对象
if (CurrentShowItem) {
CurrentShowItem.CloseInfo();
}
//如果当前没有拖拽物品时才打开详细信息窗口
if (!DragItem) {
//显示详细信息
ItemList[Idx].ShowInfo(MousePos_X - 50, MousePos_Y - 150);
//记录当前显示的对象
CurrentShowItem = ItemList[Idx];
}
}
} else {
//关闭上一个显示的对象
if (CurrentShowItem) {
CurrentShowItem.CloseInfo();
}
HoverEffect.SetOpacity(1);
}
//设置悬停槽
HoverEffect.SetVisible(true);
HoverEffect.SetPosition(row * 30, column * 30);
} else {
ItemPos = null;
HoverEffect.SetVisible(false);
//关闭所有详细信息显示
foreach(ItemObj in ItemList) {
if (ItemObj && ItemObj.ItemInfoShowFlag) {
ItemObj.CloseInfo();
}
}
}
}
//override
//鼠标左键按下回调
function OnMouseLbDown(MousePos_X, MousePos_Y) {
if (!Visible) return;
base.OnMouseLbDown(MousePos_X, MousePos_Y);
//关闭显示的对象
if (CurrentShowItem) {
CurrentShowItem.CloseInfo();
}
if (ItemPos != null && ItemList[ItemPos]) {
DragItem = ItemList[ItemPos];
DragItemPos = ItemPos;
ItemList[ItemPos] = null;
local IconBuffer = DragItem.Item.GetEquipmentIconSprite();
IconBuffer.SetPosition(-15, -15);
IMouse.AttachObjectBottom("ItemCollectDragItem", IconBuffer);
RefreshItemList();
}
}
//override
//鼠标左键弹起回调
function OnMouseLbUp(MousePos_X, MousePos_Y) {
if (!Visible) return;
base.OnMouseLbUp(MousePos_X, MousePos_Y);
if (DragItem) {
//如果这个格子不是空的 要把他放回交换的位置
if (ItemList[ItemPos] != null) {
local Temp = ItemList[ItemPos];
ItemList[DragItemPos] = Temp;
DragItemPos = null;
}
ItemList[ItemPos] = DragItem;
DragItem = null;
IMouse.RemoveObject("ItemCollectDragItem");
RefreshItemList();
}
}
}
// if (!getroottable().rawin("chongzaiflag")) {
// getroottable()["chongzaiflag"] <- true;
// } else {
// //遍历窗口队列 如果可见则调用Show
// for (local i = 0; i< _SYS_WINDOW_LIST_.len(); i++) {
// local Window = _SYS_WINDOW_LIST_[i];
// Window.Visible = false;
// Window.RemoveSelf();
// }
// TestStage();
// }