-- <* -- @Author: Saber -- @Description: 圣物兑换界面商品节点 -- *> PsionicExchangeItem = PsionicExchangeItem or BaseClass(BaseItem) local PsionicExchangeItem = PsionicExchangeItem PsionicExchangeItem.ColorStr = { [1] = "#58e8c4", [2] = "#69d0ff", [3] = "#efafff", [4] = "#ffc076", [5] = "#ff96a1", [6] = "#ffec6b", } function PsionicExchangeItem:__init(parent_wnd,prefab_asset,layer_name) self.base_file = "psionic" self.layout_file = "PsionicExchangeItem" self.parent_wnd = parent_wnd self.layer_name = layer_name self.have_num = 0 self.is_enough = true -- 材料足够兑换 self.model = PsionicModel:getInstance() self.goods_model = GoodsModel:getInstance() self:Load() end function PsionicExchangeItem:Load_callback() local nodes = { "bg:img", "add_btn:obj", "price_icon:img", "item_con", "need_num:tmp", "name:tmp", "exchange_lb:obj" } self:GetChildren(nodes) self.award_item = UIObjPool:getInstance():PopItem(UIObjPool.UIType.AwardItem, self.item_con) self.award_item:SetItemSize(78, 78) self.award_item:SetAnchoredPosition(0, 0) self.award_item:SetVisible(false) self:AddEvents() if self.need_refreshData then self:UpdateView() end end function PsionicExchangeItem:AddEvents( ) local function click_event(target, x, y) if target == self.exchange_lb_obj then -- 兑换按钮 self:OnExchangeBtnClick(x, y) elseif target == self.add_btn_obj then -- 添加按钮 if self.cost_cfg then local cost_goods_id = self.cost_cfg[2] UIToolTipMgr:getInstance():AppendGoodsTips(cost_goods_id, x, y, nil, nil, nil, nil, true) end end end AddClickEvent(self.exchange_lb_obj, click_event) AddClickEvent(self.add_btn_obj, click_event) -- 背包更新或者兑换成功时刷新节点扣费数据 local function update_goods_num(tab_id) if not tab_id or tab_id == PsionicConst.TabId.PExchange then self:UpdateCostData() end end self:BindEvent(self.model, PsionicConst.UPDATE_RED_BY_TABID, update_goods_num) -- self:BindEvent(self.model, PsionicConst.UPDATE_PSIONIC_ADVANCE_GOODSNUM, update_goods_num) -- self:BindEvent(self.model, PsionicConst.EXCHANGE_SUCCEED, update_goods_num) end function PsionicExchangeItem:SetData( cfg_data ) self.cfg_data = cfg_data self.cost_cfg = cfg_data and self.cfg_data.cost and stringtotable(self.cfg_data.cost)[1] or nil if self.is_loaded then self.need_refreshData = false self:UpdateView() else self.need_refreshData = true end end function PsionicExchangeItem:UpdateView( ) self:UpdateBasicData() self:UpdateCostData() end -- 加载基础数据 function PsionicExchangeItem:UpdateBasicData( ) if not self.cfg_data or not self.cost_cfg then return end -- 加载节点样式 local goodsVo = GoodsModel:getInstance():GetGoodsBasicByTypeId(self.cfg_data.goodsid) -- lua_resM:setImageSprite(self, self.name_bg_img, "common_asset", "shop_item_color"..goodsVo.color, false) self.award_item:SetData(self.cfg_data.goodsid) self.award_item:SetVisible(true) self.name_tmp.text = HtmlColorTxt(Trim(goodsVo.goods_name), ShopItem.ColorStr[goodsVo.color]) lua_resM:setImageSprite(self, self.bg_img, "shopOut_asset", "shop_item_color"..goodsVo.color, false) lua_resM:setOutsideImageSprite(self, self.price_icon_img, GameResPath.GetGoodsIcon(self.cost_cfg[2]), false) end function PsionicExchangeItem:UpdateCostData( ) if not self.cfg_data then return end self.have_num = self.goods_model:GetTypeGoodsNum(self.cost_cfg[2]) self.is_enough = self.have_num >= self.cost_cfg[3] self.need_num_tmp.text = string.format("%s/%s", self.is_enough and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, self.have_num, self.cost_cfg[3]) -- self.ex_btn_imgex.gray = not is_enough -- self.ex_btn_lb_tmp.color = ColorUtil:ConvertHexToRGBColor(is_enough and "8F350D" or "4e4e4e") -- self.ex_btn_lb_outline.effectColor = ColorUtil:ConvertHexToRGBColor(is_enough and "FFF799" or "DBDBDB") end function PsionicExchangeItem:OnExchangeBtnClick(x, y) if self.cost_cfg and self.is_enough then local data = { shop_data = { price = self.cost_cfg[3], money_id = self.cost_cfg[2], goods_id = self.cfg_data.goodsid, quota_type = 0, discount = 100, is_psionic_shop = true, sort = self.cfg_data.sort, }, } UIToolTipMgr:getInstance():AppendGoodsTips(self.cfg_data.goodsid, x, y, nil, nil, data, true) else Message.show("兑换所需的材料不足", "fault") end end function PsionicExchangeItem:__delete( ) end