local UIStratagemEquip = UIViewClass() local SLOTCOUNT = 6 local normalColor = Color.New(1,1, 1, 1) local tranparentColor = Color.New(1,1, 1, 0) function UIStratagemEquip:OnOpened(context) CTL.Stratagem.Init2DShowScene(self.ShowModel,"tlzl_bg_01.png") UIUtils.SetLabelText(self.UITemplateTitlePanel.lab_Title, Const_IDS.IDS_Stratagem_FunctionName) UIUtils.SetActive(self.group_Bottom, false) self.memberCells = {} self.scrollToSlotDict = {} self.slotToIds = {} self:InitSlotToIds() self:InitSlots() self:ExitEditor() self:RegistSystemEvent(EventID.UI_Stratagem_EquipRefresh, self.ExitEditor) self:SetMainSlotCanvas() end function UIStratagemEquip:InitDropEvent() self.dropSlots = {} table.insert(self.dropSlots, self.loop_drop.gameObject) self:UnregistTriggerEvent(self.group_SpotMain.img_Lihui, UIEventTriggerType.PointerClick) self:RegistTriggerEvent(self.group_SpotMain.img_Lihui, UIEventTriggerType.BeginDrag) self:RegistTriggerEvent(self.group_SpotMain.img_Lihui, UIEventTriggerType.Drag) self:RegistTriggerEvent(self.group_SpotMain.img_Lihui, UIEventTriggerType.EndDrag) table.insert(self.dropSlots, self.group_SpotMain.drop_img.gameObject) table.insert(self.dropSlots, self.group_SpotMain.img_Lihui.gameObject) for i, v in ipairs(self.slots) do self:UnregistTriggerEvent(v.img_Lihui, UIEventTriggerType.PointerClick) self:RegistTriggerEvent(v.img_Lihui, UIEventTriggerType.BeginDrag) self:RegistTriggerEvent(v.img_Lihui, UIEventTriggerType.Drag) self:RegistTriggerEvent(v.img_Lihui, UIEventTriggerType.EndDrag) table.insert(self.dropSlots, v.drop_img.gameObject) table.insert(self.dropSlots, v.img_Lihui.gameObject) --self:RegistTriggerEvent(v.img_PlayerHead, UIEventTriggerType.PointerClick) end for i, v in ipairs(self.slots) do self:RegistDropEvent(v.img_Lihui, self.dropSlots) end self:RegistDropEvent(self.group_SpotMain.img_Lihui, self.dropSlots) end local function SortData(a, b) return a.SortId > b.SortId end function UIStratagemEquip:RefreshLoopScroll() self.loopList = {} for i, v in ipairs(MDL.Stratagem.GetSortList()) do local stratagemInfo = MDL.Stratagem.GetStratagemInfo(v.ID) if stratagemInfo and not table.containValue(self.slotToIds, v.ID) then table.insert(self.loopList, v) end end table.sort(self.loopList, SortData) self.loop_StratagemPanel:ReloadAll(self.loopList) end function UIStratagemEquip:RemoveLoopScroll(id, addId) for i, v in ipairs(self.loopList) do if v.ID == id then table.remove(self.loopList, i) break end end self.loop_StratagemPanel:ReloadAll(self.loopList) end function UIStratagemEquip:InitSlots() self.slotToIds = {} self:SetSlotInfo(1, self.group_SpotMain) for i, v in ipairs(self.slots) do self:SetSlotInfo(i + 1, v) end self:CalculateEquipGs() --超宽屏适配,这里的参数参考的是 UIRender_Utility.SceneBackgroundFitScreen local w, h = UIUtils.GetScreenSize(1,1) local screenDelta = w / h if screenDelta > 2340/1080 then local scale = 0.6 * screenDelta - 0.27; self.slotsParent:SetLocalScale(scale,scale,1) end end function UIStratagemEquip:SetSlotInfo(slotId, collection) local data = DataSheetManager.StratagemEquip:GetDataByID(slotId) if MDL.Unlock.IsTriggerIdComplete(data.TriggerID) then local id = MDL.Stratagem.GetStratagemIdBySlotId(slotId) if id and id > 0 then --collection.img_Lihui self:SetEquipSlot(id, collection, slotId == 1) self.slotToIds[slotId] = id else UIUtils.SetActive(collection.img_Lihui, false) UIUtils.SetActive(collection.img_Rank, false) UIUtils.SetActive(collection.img_Bg, true) if self.isEditorState then UIUtils.SetActive(collection.img_Plus, false) else UIUtils.SetActive(collection.img_Plus, true) end self.slotToIds[slotId] = nil collection.img_Plus:SetRedPoint(self:IsCanUp()) end UIUtils.SetActive(collection.img_BgLocked, false) UIUtils.SetActive(collection.drop_img, true) else UIUtils.SetActive(collection.img_Lihui, false) UIUtils.SetActive(collection.img_Rank, false) UIUtils.SetActive(collection.img_Plus, false) UIUtils.SetActive(collection.img_Bg, true) UIUtils.SetActive(collection.img_BgLocked, true) UIUtils.SetActive(collection.drop_img, false) self.slotToIds[slotId] = nil local triggerData = DataSheetManager.TriggerID:GetDataByID(data.TriggerID) collection.txt_UNlockTip.text = MDL.Unlock.GetDesByTriggerID(triggerData.Unlock_Table[1], false) end if collection.txt_effect then collection.txt_effect.text = string.FormatByIdsId(Const_IDS.IDS_Stratagem_AttrRatio, (data.AttrRatio * 100) .. "%") end end function UIStratagemEquip:SetEquipSlot(id, collection, isMain) local excelData = DataSheetManager.StratagemData:GetDataByID(id) local stratagemInfo = MDL.Stratagem.GetStratagemInfo(id) local leveId = MDL.Stratagem.GetLearnNextId(excelData.ID) if leveId then UIUtils.SetLabelText(collection.txt_Rank, Const_IDS["IDS_Type_EquipShow_" .. leveId]) else UIUtils.SetLabelText(collection.txt_Rank, Const_IDS["IDS_Type_EquipShow_" .. stratagemInfo.Learn.CurrLvID]) end UIUtils.SetActive(collection.img_Lihui, true) UIUtils.SetActive(collection.img_Rank, true) collection.txt_Name.text = UTL.Color.FormatQualityText(UIUtils.GetLocalizedTxt(Const_IDS[excelData.StratagemName]), excelData.StratagemQuality) UIUtils.SetActive(collection.img_Bg, false) UIUtils.SetImageByAssetBundle(collection.img_Lihui, excelData.PicturePath) if isMain then CTL.Stratagem.RefreshModelShow(excelData,self.ShowModel,0,0.4,excelData.Scale * 0.65) self:ToggleMainImageVisible(false) CTL.Stratagem.SetActiveModelShow(true) end end function UIStratagemEquip:OnClosed() CTL.Stratagem.DisposeAll(self.ShowModel) end function UIStratagemEquip:OnButtonClicked(comp) if comp == self.UITemplateTitlePanel.btn_close then self:CloseView(true) elseif comp == self.UITemplateTitlePanel.btn_Home then CTL.HomeMain.CloseViewBackHomeMain() elseif comp == self.btn_Save then if MDL.Stratagem.StratagemEquipReq(self.slotToIds) then self:ExitEditor() end elseif comp == self.btn_Set then self:EnterEditor() elseif comp == self.btn_Enlarge then UIManager.OpenView(Const_UI_Lua.UIEnum.UIStratagemAttributesInfoView, self.slotToIds) elseif comp == self.btn_View then UIManager.OpenAndPushAll(Const_UI_Lua.UIEnum.UIStratagemMainPanel) elseif comp == self.UITemplateTitlePanel.btn_Info then UIManager.OpenView(Const_UI_Lua.UIEnum.UICommonNoteView, {id = LuaConst.NoteID.Stratagem }) elseif comp == self.group_SpotMain.img_Plus then self:EnterEditor() else for i, v in ipairs(self.slots) do if comp == v.img_Plus then self:EnterEditor() end end end end function UIStratagemEquip:OnBackButtonPressed() self:OnButtonClicked(self.UITemplateTitlePanel.btn_close) end function UIStratagemEquip:OnPointerClick(comp, eventData) local slot = nil if comp == self.group_SpotMain.img_Lihui then slot = 1 else for i, v in ipairs(self.slots) do if comp == v.img_Lihui then slot = i + 1 break end end end if slot then local id = self:GetStratagemIdBySlotId(slot) local data = DataSheetManager.StratagemData:GetDataByID(id) CTL.Stratagem.OpenInfoView(data) end end function UIStratagemEquip:EnterEditor() self.isEditorState = true UIUtils.SetActive(self.group_Bottom, true) self:InitDropEvent() self:RefreshLoopScroll() self:InitSlots() UIUtils.SetActive(self.btn_Set, false) UIUtils.SetActive(self.btn_Save, true) UIUtils.SetActive(self.btn_View, false) end function UIStratagemEquip:ExitEditor() self:UnregistTriggerEvent(self.group_SpotMain.img_Lihui) for i, v in ipairs(self.slots) do self:UnregistTriggerEvent(v.img_Lihui) end self.isEditorState = false UIUtils.SetActive(self.group_Bottom, true) UIUtils.SetActive(self.btn_Set, true) UIUtils.SetActive(self.btn_Save, false) self:InitSlots() UIUtils.SetActive(self.btn_View, true) self.loop_StratagemPanel:ClearCells() end function UIStratagemEquip:OnCellInit(scrollRect, index, cellTable, data) self.memberCells[index + 1] = { cellTable, data } UIUtils.SetImageByAssetBundle(cellTable.img_Item, data.PicturePath) UTL.Color.SetQualityImageByType(cellTable.bg_img, data.StratagemQuality, UTL.Color.QualityType.Square) local stratagemInfo = MDL.Stratagem.GetStratagemInfo(data.ID) UIUtils.SetLabelText(cellTable.txt_Tier, Const_IDS["IDS_Type_EquipShow_" .. stratagemInfo.Learn.CurrLvID]) if self.isEditorState then self.loop_StratagemPanel:RegistTriggerEvent(cellTable.img_Item, UIEventTriggerType.BeginDrag, index) self.loop_StratagemPanel:RegistTriggerEvent(cellTable.img_Item, UIEventTriggerType.Drag, index) self.loop_StratagemPanel:RegistTriggerEvent(cellTable.img_Item, UIEventTriggerType.EndDrag, index) self.loop_StratagemPanel:RegistTriggerEvent(cellTable.img_Item, UIEventTriggerType.PointerDown, index) self.loop_StratagemPanel:RegistTriggerEvent(cellTable.img_Item, UIEventTriggerType.PointerUp, index) self:RegistDropEvent(cellTable.img_Item, self.dropSlots) end end function UIStratagemEquip:Cell_OnPointerUp(scrollRect, index, target, eventData) if not self.curDragObj and self.curPointerDownTime and Time.realtimeSinceStartup - self.curPointerDownTime <= 0.5 then local data = self.memberCells[index + 1][2] for i = 1, SLOTCOUNT do local equipData = DataSheetManager.StratagemEquip:GetDataByID(i) if MDL.Unlock.IsTriggerIdComplete(equipData.TriggerID) then if not self:GetStratagemIdBySlotId(i) then local collection = self:GetCollectionBySlot(i) self:SetEquipSlot(data.ID, collection, i == 1) self.slotToIds[i] = data.ID self:RemoveLoopScroll(data.ID) self:CalculateEquipGs() break end end end end end function UIStratagemEquip:Cell_OnPointerDown(scrollRect, index, target, eventData) self.curPointerDownTime = Time.realtimeSinceStartup end function UIStratagemEquip:Cell_OnBeginDrag(scrollRect, index, target, eventData) if math.abs(eventData.delta.y) > 3 then local cell = self.memberCells[index + 1] if cell then self.drag_Cell = cell[1].cell.gameObject self.curDragData = { index = index + 1, data = cell[2], siblingIndex = self.drag_Cell.transform:GetSiblingIndex() } self.drag_Cell.transform:SetParent(self.dragParent.transform, false) self.curPointerDownTime = nil else logging.error(LTAG.XuQian, "重大错误 拖拽对象 越界 ", index, #self.memberCells) end else self.curDragObj = self.loop_StratagemPanel.gameObject UIUtils.ExecuteBeginDrag(self.curDragObj, eventData) end end function UIStratagemEquip:Cell_OnEndDrag(scrollRect, index, comp, eventData) if self.curDragObj then UIUtils.ExecuteEndDrag(self.curDragObj, eventData) self.curDragObj = nil end end function UIStratagemEquip:Cell_OnDrop(scrollRect, index, target, eventData) if self.drag_Cell then self.drag_Cell.transform:SetParent(self.loop_StratagemPanel.content, false) self.drag_Cell.transform:SetSiblingIndex(self.curDragData.siblingIndex) self.drag_Cell = nil end end function UIStratagemEquip:Cell_OnDrag(scrollRect, index, target, eventData) if self.curDragObj then UIUtils.ExecuteDrag(self.curDragObj, eventData) else UIUtils.ScreenPointToWorldPointInRectangle(self.drag_Cell, eventData) end end ---comp 槽位对象 eventdata 拖拽对象 function UIStratagemEquip:OnDrop(comp, eventData) if comp == nil then self:RecoverSlot(eventData.pointerDrag) if self.group_SpotMain.img_Lihui.transform == self.curDragItem.transform then CTL.Stratagem.SetActiveModelShow(true) end elseif self.drag_Cell then local isSlot, slotIndex, collection = self:GetSlotByDropComp(comp) if isSlot then self:ScrollToSlot(slotIndex, collection) end else local isSlot, slotIndex, collection = self:GetSlotByDropComp(comp) if isSlot == nil then self:RecoverSlot(eventData.pointerDrag) elseif not isSlot then self:SlotToScroll(slotIndex, eventData.pointerDrag) else self:SlotExchange(slotIndex, collection, eventData.pointerDrag) end end UIUtils.SetActive(self.loop_drop, false) if self.curDragItem and self.group_SpotMain.img_Lihui.transform == self.curDragItem.transform then self:ToggleMainImageVisible(false) end end function UIStratagemEquip:OnBeginDrag(comp, eventData) UIUtils.SetActive(self.loop_drop, true) self.curDragItem = comp.gameObject self.curDragItem.transform:SetParent(self.dragParent.transform, false) if self.group_SpotMain.img_Lihui.transform == comp.transform then self:ToggleMainImageVisible(true) CTL.Stratagem.SetActiveModelShow(false) end end function UIStratagemEquip:OnDrag(comp, eventData) UIUtils.ScreenPointToWorldPointInRectangle(comp.gameObject, eventData) end function UIStratagemEquip:OnEndDrag() end function UIStratagemEquip:GetSlotByDropComp(comp) for i, v in ipairs(self.slots) do if comp.gameObject == v.img_Lihui.gameObject or comp.gameObject == v.drop_img.gameObject then return true, i + 1, v end end if comp.gameObject == self.group_SpotMain.img_Lihui.gameObject or comp.gameObject == self.group_SpotMain.drop_img.gameObject then return true, 1, self.group_SpotMain end if comp.gameObject == self.loop_drop.gameObject then return false end end --拖拽目标没有槽位 恢复原位置 function UIStratagemEquip:RecoverSlot(obj) local isSlot, slotIndex, collection = self:GetSlotByDropComp(obj) if isSlot then obj.transform:SetParent(collection.gameObject.transform, false) obj.transform:SetAnchoredPosition(0, 0, 0) obj.transform:SetSiblingIndex(3) end end --槽位上的节点拖拽到loopcroll上 function UIStratagemEquip:SlotToScroll(slotIndex, obj) local _, sourceSlotIndex, sourceCollection = self:GetSlotByDropComp(obj) local id = self:GetStratagemIdBySlotId(sourceSlotIndex) self.scrollToSlotDict[id] = true self.slotToIds[sourceSlotIndex] = nil self:RefreshLoopScroll() UIUtils.SetActive(sourceCollection.img_Lihui, false) UIUtils.SetActive(sourceCollection.img_Rank, false) UIUtils.SetActive(sourceCollection.img_Bg, true) UIUtils.SetActive(sourceCollection.img_Plus, false) sourceCollection.txt_Name.text = nil obj.transform:SetParent(sourceCollection.gameObject.transform, false) obj.transform:SetAnchoredPosition(0, 0, 0) obj.transform:SetSiblingIndex(3) self:CalculateEquipGs() end function UIStratagemEquip:SlotExchange(slotIndex, collection, obj) local targetId = self:GetStratagemIdBySlotId(slotIndex) local _, sourceSlotIndex, sourceCollection = self:GetSlotByDropComp(obj) local sourceId = self:GetStratagemIdBySlotId(sourceSlotIndex) self.slotToIds[slotIndex] = sourceId if sourceSlotIndex then self.slotToIds[sourceSlotIndex] = targetId self:SetEquipSlot(sourceId, collection, slotIndex == 1) end if sourceCollection then if targetId and targetId > 0 then self:SetEquipSlot(targetId, sourceCollection, sourceSlotIndex == 1) else UIUtils.SetActive(sourceCollection.img_Lihui, false) UIUtils.SetActive(sourceCollection.img_Rank, false) UIUtils.SetActive(sourceCollection.img_Bg, true) UIUtils.SetActive(sourceCollection.img_Plus, false) sourceCollection.txt_Name.text = nil end obj.transform:SetParent(sourceCollection.gameObject.transform, false) obj.transform:SetAnchoredPosition(0, 0, 0) obj.transform:SetSiblingIndex(3) end self:CalculateEquipGs() end function UIStratagemEquip:GetStratagemIdBySlotId(slotIndex) return self.slotToIds[slotIndex] end function UIStratagemEquip:ScrollToSlot(slotIndex, collection) self.drag_Cell.transform:SetParent(self.loop_StratagemPanel.content, false) self.drag_Cell.transform:SetSiblingIndex(self.curDragData.siblingIndex) local targetId = self:GetStratagemIdBySlotId(slotIndex) self:SetEquipSlot(self.curDragData.data.ID, collection, slotIndex == 1) self.slotToIds[slotIndex] = self.curDragData.data.ID if targetId and targetId > 0 then self:RefreshLoopScroll() self.scrollToSlotDict[targetId] = true end self.scrollToSlotDict[ self.curDragData.data.ID] = false self:RemoveLoopScroll(self.curDragData.data.ID, targetId) self.drag_Cell = nil self.curDragData = nil self:CalculateEquipGs() end function UIStratagemEquip:GetCollectionBySlot(slotId) if slotId == 1 then return self.group_SpotMain else return self.slots[slotId - 1] end end function UIStratagemEquip:CalculateEquipGs() local value = CTL.Stratagem.CalculateEquipGs(self.slotToIds) self.txt_TotalBR.text = string.FormatByIdsId(Const_IDS.IDS_Stratagem_TotalBR, value) end function UIStratagemEquip:SetMainSlotCanvas() local canvas = self.gameObject:GetComponent(typeof(UnityEngine.Canvas)) self.group_SpotMain.mainSlotCanvas.sortingOrder = canvas.sortingOrder + 30 end function UIStratagemEquip:IsCanUp() local upCount = 0 for k,v in pairs(self.slotToIds) do upCount = upCount + 1 end local activeCount = MDL.Stratagem.GetActiveCount() return upCount < activeCount end function UIStratagemEquip:InitSlotToIds() for slotId = 1,SLOTCOUNT do local data = DataSheetManager.StratagemEquip:GetDataByID(slotId) local id = MDL.Stratagem.GetStratagemIdBySlotId(slotId) self.slotToIds[slotId] = id end end function UIStratagemEquip:ToggleMainImageVisible(isShow) UIUtils.SetImageColor(self.group_SpotMain.img_Lihui,isShow and normalColor or tranparentColor) end return UIStratagemEquip