/* 文件名:ItemCollect.nut 路径:User/UI/Window/5_Inventory/ItemCollect.nut 创建日期:2025-01-06 13:50 文件用途: 物品栏 */ //物品槽 class UISpace_Inventory.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.GetIconSprite(); } //生成详细信息 function GenerateInfo() { this.ItemInfo = this.Item.GetInfoWindow(); } //显示详细信息 function ShowInfo(x, y) { if (!this.ItemInfo) GenerateInfo(); this.ItemInfoShowFlag = true; // 获取信息框尺寸 local infoW = 211; local infoH = this.ItemInfo.RealCanvasHeight; // X轴边界修正 if (x< 0) { x = 0; } else if (x + infoW > 1067) { x = 1067 - infoW; } // Y轴边界修正 if (y< 0) { y = 0; } else if (y + infoH > 600) { y = 600 - infoH; } //设置位置 this.ItemInfo.SetPosition(x, y); this.ItemInfo.ResetFocus(); } //关闭显示详细信息 function CloseInfo() { if (this.ItemInfo) { this.ItemInfoShowFlag = false; this.ItemInfo.CloseWindow(); } } } // 物品栏 class UISpace_Inventory.ItemCollection extends Yosin_CommonUi { //栏位类型 Type = null; // 悬浮时显示的框 HoverEffect = null; //行数 RowNum = null; //底层画布对象 BottomCanvas = null; //顶层画布对象 TopCanvas = null; //物品对象List ItemList = null; //当前显示详细信息的物品对象 CurrentShowItem = null; //当前鼠标指向的位置 ItemPos = null; //拖拽物品原来的位置 DragItemPos = null; constructor(x, y, rowNum, type) { this.Type = type; 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) { ItemList[Index] = UISpace_Inventory.ItemSlot(); ItemList[Index].SetItem(ItemObject); } RefreshItemList(); } //设置道具格子 function SetItemSlot(Idx, ItemObject) { if (ItemObject == null) { ItemList[Idx] = null; } else { ItemList[Idx] = UISpace_Inventory.ItemSlot(); ItemList[Idx].SetItem(ItemObject); SetAddEffect(Idx); } RefreshItemList(); } //设置添加特效 function SetAddEffect(Idx) { local AddEffect = Animation("ui/inventory/slot.ani"); AddEffect.SetPosition((Idx % 8) * 30, (Idx / 8) * 30); AddEffect.SetZOrder(100); AddEffect.SetUpdateFunc(function(Ani, Dt) { if (Ani.IsUsability == false) Ani.RemoveSelf(); }); Addchild(AddEffect); } //刷新道具列表 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, WindowInteractiveFlag) { if (!Visible) return; base.OnMouseProc(MousePos_X, MousePos_Y, WindowInteractiveFlag); if (isInRect) { local WorldPosition = this.GetWorldPosition(); local xx = MousePos_X - WorldPosition.x; local yy = MousePos_Y - WorldPosition.y; local column = (yy / 30).tointeger(); //悬停的时候有可能悬停到最下面的最后一条缝 会导致判定行数多了一行 所以这里做限制 column = (column <= (RowNum - 1) ? column : (RowNum - 1)); 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 (!IMouse.DragObj) { //显示详细信息 ItemList[Idx].ShowInfo(MousePos_X - 50, MousePos_Y - ((ItemList[Idx].ItemInfo.RealCanvasHeight + 10) / 2)); //记录当前显示的对象 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, WindowInteractiveFlag) { if (!Visible) return; base.OnMouseLbDown(MousePos_X, MousePos_Y, WindowInteractiveFlag); //关闭显示的对象 if (CurrentShowItem) { CurrentShowItem.CloseInfo(); } if (ItemPos != null && ItemList[ItemPos]) { //记录拖拽物品 IMouse.AddDragObject(ItemList[ItemPos]); //记录拖拽物品原来的位置 DragItemPos = ItemPos; //从槽中移除 ItemList[ItemPos] = null; //刷新物品列表 RefreshItemList(); } } //override //鼠标左键弹起回调 function OnMouseLbUp(MousePos_X, MousePos_Y, WindowInteractiveFlag) { if (!Visible) return; base.OnMouseLbUp(MousePos_X, MousePos_Y, WindowInteractiveFlag); if (IMouse.DragObj) { //如果拖动到窗口内部 发包交换位置 local WorldPosition = this.GetWorldPosition(); if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, WorldPosition.x, WorldPosition.y, Width, Height)) { MySocket.Send(PACKET_ID.INVENTORY_SWAP_ITEM, { oldBaid = this.Type + 2, newBaid = this.Type + 2, oldPos = this.DragItemPos, newPos = this.ItemPos, }); } //移除鼠标上的拖动道具 local DragItem = IMouse.RemoveDragObject(); //发包完成后先把道具放回去 ItemList[DragItemPos] = DragItem; DragItemPos = null; RefreshItemList(); } } //override //鼠标右键按下回调 function OnMouseRbDown(MousePos_X, MousePos_Y, WindowInteractiveFlag) { if (!Visible) return; base.OnMouseRbDown(MousePos_X, MousePos_Y, WindowInteractiveFlag); //关闭显示的对象 if (CurrentShowItem) { CurrentShowItem.CloseInfo(); } if (ItemPos != null && ItemList[ItemPos]) { //穿戴装备 if (this.Type == 0) { MySocket.Send(PACKET_ID.WEAR_EQUIPMENT, { backpackId = 2, oldPos = ItemPos }); } //使用道具 else if (this.Type == 1) { } } } //移动Item逻辑 function MoveItem(oldPos, newPos) { //如果这个格子不是空的 要把他放回交换的位置 if (ItemList[newPos] != null) { local Temp = ItemList[newPos]; ItemList[oldPos] = Temp; } ItemList[newPos] = DragItem; RefreshItemList(); } }