375 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			375 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| /*
 | |
| 文件名:7_Npc_Shop.nut
 | |
| 路径:User/UI/Window/7_Npc_Shop.nut
 | |
| 创建日期:2025-01-26	18:30
 | |
| 文件用途:NPC商店
 | |
| */
 | |
| 
 | |
| //NPC商店命名空间
 | |
| UISpace_NpcShop <- {};
 | |
| 
 | |
| 
 | |
| class UISpace_NpcShop.Item extends Yosin_CommonUi {
 | |
|     //槽位
 | |
|     Index = null;
 | |
|     //物品
 | |
|     Item = null;
 | |
|     //物品图标
 | |
|     ItemIcon = null;
 | |
|     //物品对象的详细信息窗口
 | |
|     ItemInfo = null;
 | |
|     //详细信息窗口显示Flag
 | |
|     ItemInfoShowFlag = false;
 | |
|     //物品需求图标
 | |
|     ItemRequireIcon = null;
 | |
|     //物品需求数量
 | |
|     ItemRequireCount = null;
 | |
|     //物品需求名称
 | |
|     ItemRequireName = null;
 | |
|     //信息
 | |
|     Info = null;
 | |
|     //画布对象
 | |
|     Canvas = null;
 | |
|     //悬停框
 | |
|     HoverBox = null;
 | |
| 
 | |
|     constructor(gIndex, gInfo) {
 | |
|         base.constructor(0, 0, 165, 57);
 | |
|         this.Index = gIndex;
 | |
|         this.Info = gInfo;
 | |
| 
 | |
| 
 | |
|         InitItem();
 | |
|         InitDraw();
 | |
|     }
 | |
| 
 | |
|     function InitItem() {
 | |
|         //构造道具
 | |
|         this.Item = GameItem.Item.ConstructionItemById(Info.Id);
 | |
|         //构造图标
 | |
|         this.ItemIcon = this.Item.GetIconSprite();
 | |
|         //构造详细信息窗口
 | |
|         this.ItemInfo = this.Item.GetInfoWindow();
 | |
|         this.ItemInfo.SetZOrder(1);
 | |
|         //构造需求图标
 | |
|         //物品
 | |
|         if (this.Info.Material) {
 | |
| 
 | |
|         }
 | |
|         //金币
 | |
|         else if (this.Info.Gold) {
 | |
|             ItemRequireCount = this.Info.Gold;
 | |
|             ItemRequireIcon = CL_SpriteObject("sprite/interface/lenheartwindowcommon.img", 299);
 | |
|             ItemRequireName = "金币";
 | |
|         }
 | |
|         //点卷
 | |
|         else if (this.Info.Cera) {
 | |
|             ItemRequireName = "点卷";
 | |
|         }
 | |
|         //代币券
 | |
|         else if (this.Info.CeraPoint) {
 | |
|             ItemRequireName = "代币券";
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
|     function InitDraw() {
 | |
|         Canvas = CL_CanvasObject();
 | |
|         Addchild(Canvas);
 | |
|         Canvas.ResizeAndClear(321, 5000);
 | |
|         Canvas.BeginDraw();
 | |
|         //绘制背景
 | |
|         Canvas.DrawSpriteFrame(CL_SpriteFrameObject("sprite/interface2/ui/shop/shop_slot.img", 0), 0, 0);
 | |
|         //绘制图标
 | |
|         Canvas.DrawSprite(ItemIcon, 5, 7);
 | |
|         //绘制名称
 | |
|         local Name = FontAssetManager.GenerateNormal(Item.Name, false, {
 | |
|             color = AssetManager.EtcConfig.ItemInfoTag.rarity_color[Item.Rarity]
 | |
|         });
 | |
|         Canvas.DrawActor(Name, 44, 7);
 | |
|         //绘制价格槽
 | |
|         local SlotBg = CL_SpriteObject("sprite/interface/lenheartwindowcommon.img", 391);
 | |
|         SlotBg.SetScale(5.2, 1.0);
 | |
|         SlotBg.SetPosition(84, 38);
 | |
|         Canvas.DrawSprite(SlotBg);
 | |
|         //绘制需求道具图标
 | |
|         if (ItemRequireIcon) {
 | |
|             ItemRequireIcon.SetScale(0.5, 0.5);
 | |
|             ItemRequireIcon.SetPosition(142, 38);
 | |
|             Canvas.DrawSprite(ItemRequireIcon);
 | |
|         }
 | |
|         //绘制需求数量
 | |
|         if (ItemRequireCount) {
 | |
|             local RequireCount = FontAssetManager.GenerateNormal(ItemRequireCount.tostring(), false, {
 | |
|                 color = sq_RGBA(255, 255, 255, 255) //TODO 这里要计算我是否有足够的道具去购买他 选择是否标红
 | |
|             });
 | |
|             Canvas.DrawActor(RequireCount, 142 - (RequireCount.GetSize().w), 37);
 | |
|         }
 | |
| 
 | |
| 
 | |
|         Canvas.EndDraw();
 | |
| 
 | |
|         //构造悬停框精灵
 | |
|         HoverBox = CL_SpriteObject("sprite/interface2/ui/shop/shop_slot.img", 1);
 | |
|         HoverBox.SetMode(0);
 | |
|         HoverBox.SetVisible(false);
 | |
|         Addchild(HoverBox);
 | |
|     }
 | |
| 
 | |
|     //override
 | |
|     //鼠标事件回调
 | |
|     function OnMouseProc(MousePos_X, MousePos_Y, WindowInteractiveFlag) {
 | |
|         if (!Visible) return;
 | |
|         base.OnMouseProc(MousePos_X, MousePos_Y, WindowInteractiveFlag);
 | |
|         if (isInRect) {
 | |
|             //没有输入框的时候才会显示悬停框
 | |
|             if (!Parent.QuantityInput || Parent.QuantityInput.Visible == false) HoverBox.SetVisible(true);
 | |
|             //判断是否有悬停到道具图标
 | |
|             local Pos = GetWorldPosition();
 | |
|             if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, Pos.x + 5, Pos.y + 7, 32, 32)) {
 | |
|                 if (!ItemInfoShowFlag) {
 | |
|                     ItemInfoShowFlag = true;
 | |
|                     ItemInfo.SetPosition(MousePos_X + 16, MousePos_Y + 16);
 | |
|                     ItemInfo.ResetFocus();
 | |
|                 }
 | |
|             }
 | |
|         } else {
 | |
|             HoverBox.SetVisible(false);
 | |
|             if (ItemInfoShowFlag) {
 | |
|                 ItemInfoShowFlag = false;
 | |
|                 ItemInfo.CloseWindow();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         //输入框出来了关闭道具显示信息
 | |
|         if (Parent.QuantityInput && Parent.QuantityInput.Visible) {
 | |
|             HoverBox.SetVisible(false);
 | |
|             if (ItemInfoShowFlag) {
 | |
|                 ItemInfoShowFlag = false;
 | |
|                 ItemInfo.CloseWindow();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //override
 | |
|     //鼠标左键按下回调
 | |
|     function OnMouseLbDown(MousePos_X, MousePos_Y, WindowInteractiveFlag) {
 | |
|         if (!Visible) return;
 | |
|         base.OnMouseLbDown(MousePos_X, MousePos_Y, WindowInteractiveFlag);
 | |
|         //关闭显示的对象
 | |
|         if (ItemInfoShowFlag) {
 | |
|             ItemInfoShowFlag = false;
 | |
|             ItemInfo.CloseWindow();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //鼠标左键单击回调
 | |
|     function OnMouseLbClick(MousePos_X, MousePos_Y, WindowInteractiveFlag) {
 | |
|         if (!Visible) return;
 | |
|         base.OnMouseLbDown(MousePos_X, MousePos_Y, WindowInteractiveFlag);
 | |
|         //购买逻辑
 | |
|         if (isInRect && !WindowInteractiveFlag) {
 | |
|             if (!Parent.QuantityInput) {
 | |
|                 Parent.QuantityInput = _QuantityInput(MousePos_X - 35, MousePos_Y - 35);
 | |
|             } else {
 | |
|                 Parent.QuantityInput.SetPosition(MousePos_X - 35, MousePos_Y - 35);
 | |
|                 Parent.QuantityInput.ResetFocus();
 | |
|             }
 | |
|             Parent.QuantityInput.SetOnConfirmFunc(function(Count) {
 | |
|                 //设置购买信息
 | |
|                 Parent.Parent.SetBuyInfo({
 | |
|                     npcshopId = Parent.Parent.ShopId,
 | |
|                     shopId = Parent.Type,
 | |
|                     pos = Index,
 | |
|                     num = Count
 | |
|                 });
 | |
|                 local NoticeBox = _Yosin_MessageBox(format("购买[%s]数量 %d 个\n总购买费用为 %d %s\n您确定要购买吗?", Item.Name, Count, Count * ItemRequireCount, ItemRequireName));
 | |
|                 NoticeBox.SetOnConfirmFunc(function() {
 | |
|                     Parent.Parent.SendBuyInfo();
 | |
|                 }.bindenv(this));
 | |
|             }.bindenv(this));
 | |
|         }
 | |
|     }
 | |
| 
 | |
| }
 | |
| 
 | |
| // 物品栏
 | |
| class UISpace_NpcShop.ItemCollection extends Yosin_Window {
 | |
| 
 | |
|     //是否为独立窗口
 | |
|     IsIndependent = false;
 | |
|     //是否为图层窗口
 | |
|     IsLayer = true;
 | |
| 
 | |
|     //栏位类型
 | |
|     Type = null;
 | |
| 
 | |
|     //信息
 | |
|     Info = null;
 | |
|     //物品列表
 | |
|     ItemList = null;
 | |
|     //数量输入框
 | |
|     QuantityInput = null;
 | |
| 
 | |
| 
 | |
|     constructor(x, y, type, gInfo) {
 | |
|         this.Type = type;
 | |
|         this.Info = gInfo;
 | |
|         //计算实际需要的高度
 | |
|         base.constructor("商店页面" + type + clock(), x, y, 336, 400, 0);
 | |
|         SetClipRect(0, 0, 336, 400);
 | |
|         // OpenDeBug();
 | |
|         Init();
 | |
|     }
 | |
| 
 | |
|     function Init() {
 | |
|         ItemList = [];
 | |
|         foreach(Index, ItemObject in Info) {
 | |
|             local Buffer = UISpace_NpcShop.Item(Index, ItemObject);
 | |
|             Buffer.SetPosition(2 + (168 * (Index % 2)), 2 + (61 * (Index / 2)));
 | |
|             AddUIChild(Buffer);
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| class UISpace_NpcShop.Shop extends Yosin_Window {
 | |
|     //商店信息
 | |
|     Info = null;
 | |
|     //商店ID
 | |
|     ShopId = null;
 | |
|     //栏
 | |
|     ItemCollection = null;
 | |
|     //滚动条
 | |
|     ScrollObject = null;
 | |
|     //购买信息包
 | |
|     BuyInfo = null;
 | |
| 
 | |
|     function _typeof() {
 | |
|         return "Game_Window";
 | |
|     }
 | |
| 
 | |
|     constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH) {
 | |
|         ItemCollection = [];
 | |
|         base.constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH);
 | |
|     }
 | |
| 
 | |
|     function Init(gShopId) {
 | |
|         ShopId = gShopId;
 | |
|         //判断NPC是否有商店
 | |
|         Info = AssetManager.GetNpcShop(ShopId);
 | |
|         if (Info) {
 | |
|             local title = Yosin_TopTitle(Width, Height, Info.name);
 | |
|             AddUIChild(title);
 | |
|             //注册控件
 | |
|         } else throw "NPC没有商店";
 | |
| 
 | |
|         //注册控件
 | |
|         RegisterWidget();
 | |
|     }
 | |
| 
 | |
|     function RegisterWidget() {
 | |
|         //关闭按钮
 | |
|         local closeBtn = Yosin_BaseButton(Width - 20, 4, 12, 12, "sprite/interface/lenheartwindowcommon.img", 544);
 | |
|         closeBtn.DownSimulateOffset = false;
 | |
|         closeBtn.SetZOrder(1);
 | |
|         closeBtn.OnClick = function(btn) {
 | |
|             DestroyWindow();
 | |
|         }.bindenv(this);
 | |
|         AddUIChild(closeBtn);
 | |
| 
 | |
|         //分页按钮
 | |
|         local titlesBtn = Yosin_RowMoreTitleBtn(5, 30, 354, Info.PageNameList, "sprite/interface/lenheartwindowcommon.img", 160);
 | |
|         AddUIChild(titlesBtn);
 | |
|         titlesBtn.LBDownOnClick = function(btns, index) {
 | |
|             // ChangPage(index);
 | |
|         }.bindenv(this);
 | |
|         titlesBtn.LBDownOnClick = function(btns, index) {
 | |
|             foreach(Iindex, ItemCollectionBuffer in ItemCollection) {
 | |
|                 if (Iindex == index) {
 | |
|                     ItemCollectionBuffer.SetVisible(true);
 | |
|                 } else {
 | |
|                     ItemCollectionBuffer.SetVisible(false);
 | |
|                 }
 | |
|             }
 | |
|         }.bindenv(this);
 | |
| 
 | |
| 
 | |
|         //底框
 | |
|         local Bg = Yosin_NineBoxStretch(353, 407, "sprite/interface/lenheartwindowcommon.img", 97);
 | |
|         Bg.SetPosition(8, 51);
 | |
|         Addchild(Bg);
 | |
| 
 | |
|         //滚动条
 | |
|         ScrollObject = Yosin_ScrollBar(347, 55, 400, 20);
 | |
|         ScrollObject.SetScrollBarState(false);
 | |
|         ScrollObject.SetChangeCallBack(function(Value) {
 | |
|             MsgTextWindow.SetScrollPos(Value);
 | |
|         }.bindenv(this));
 | |
|         AddUIChild(ScrollObject);
 | |
| 
 | |
|         //创建栏位
 | |
|         foreach(Index, LInfo in Info.PageList) {
 | |
|             local Buffer = UISpace_NpcShop.ItemCollection(10, 53, Index, LInfo);
 | |
|             ItemCollection.push(Buffer);
 | |
|             AddUIChild(Buffer);
 | |
|             if (Index != 0) Buffer.SetVisible(false);
 | |
|         }
 | |
| 
 | |
|         //购买按钮
 | |
|         local BuyButton = Yosin_SplicingButton(115, 456, 59, 24 "sprite/interface/lenheartwindowcommon.img", 172, true, false);
 | |
|         //购买按钮文本
 | |
|         local BuyTextActor = FontAssetManager.GenerateNormal("购买", false, {
 | |
|             color = sq_RGBA(186, 147, 97, 255)
 | |
|         });
 | |
|         BuyTextActor.SetZOrder(1);
 | |
|         BuyTextActor.SetPosition(18, 3);
 | |
|         BuyButton.OnClick = function(Button) {
 | |
| 
 | |
|         }.bindenv(this);
 | |
|         BuyButton.Addchild(BuyTextActor);
 | |
|         AddUIChild(BuyButton);
 | |
| 
 | |
|         //出售按钮
 | |
|         local SellButton = Yosin_SplicingButton(184, 456, 59, 24 "sprite/interface/lenheartwindowcommon.img", 172, true, false);
 | |
|         //出售按钮文本
 | |
|         local SellTextActor = FontAssetManager.GenerateNormal("出售", false, {
 | |
|             color = sq_RGBA(186, 147, 97, 255)
 | |
|         });
 | |
|         SellTextActor.SetZOrder(1);
 | |
|         SellTextActor.SetPosition(18, 3);
 | |
|         SellButton.OnClick = function(Button) {
 | |
| 
 | |
|         }.bindenv(this);
 | |
|         SellButton.Addchild(SellTextActor);
 | |
|         AddUIChild(SellButton);
 | |
|     }
 | |
| 
 | |
|     //设置购买信息
 | |
|     function SetBuyInfo(gTable) {
 | |
|         BuyInfo = gTable;
 | |
|     }
 | |
| 
 | |
|     //发送购买信息
 | |
|     function SendBuyInfo() {
 | |
|         if (BuyInfo) {
 | |
|             //发送购买信息
 | |
|             MySocket.Send(PACKET_ID.BUY_ITEM, BuyInfo);
 | |
|             //清空购买信息
 | |
|             BuyInfo = null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //逻辑入口
 | |
|     function Proc(Dt) {
 | |
|         SyncPos(X, Y);
 | |
|         base.Proc(Dt);
 | |
|     }
 | |
| 
 | |
|     //在Esc按下时
 | |
|     function OnEsc() {
 | |
|         //下帧执行
 | |
|         Timer.SetNextFrame(function(Window) {
 | |
|             Window.DestroyWindow();
 | |
|         }, this);
 | |
|     }
 | |
| } |