---SLG编辑器 ---编辑器主要有两部分,一个是编辑模板,一个是编辑阻碍 ---编辑模板:通过多个地形单位组合成一个模板,单个地形单位可能是多个地块组合,可以理解为一个模板是多个单位的组合,一个单位是多个地块的组合 ---编辑模板需要先选中中心点,再在合适位置选中地形单位,导出时会保存单位与中心点的偏移数据 ---编辑阻碍:多个模板配合组成 ---编辑阻碍选中相应模板,再在合适位置生成模板,会将模板中心生成在所选位置,并同时生成模板中的各个单位。最终导出模板和位置索引的组合数据 local UISlgGvEEditorPanel = UIViewClass() local SlgGvEBlockData = require("Modules/SlgGvE/SlgGvEBlockData") local SlgGvEBlockDataPath = ConstAssetPathConfig.SlgGvEBlockDataPath --格子UI尺寸 local ITEM_WIDTH = CTL.SlgGvE.GRID_UI_WIDTH local ITEM_HEIGHT = CTL.SlgGvE.GRID_UI_HEIGHT local GRID_COUNT = MDL.SlgGvE.GRID_SIZE local DRAG_LIMIT_HORIZONTAL_NUM = CTL.SlgGvE.DRAG_LIMIT_HORIZONTAL_NUM local DRAG_LIMIT_VERTICAL_NUM = CTL.SlgGvE.DRAG_LIMIT_VERTICAL_NUM local DRAG_LIMIT_MIDDLE_POS_X = MDL.SlgGvE.GRID_SIZE / 2 local DRAG_LIMIT_MIDDLE_POS_Y = MDL.SlgGvE.GRID_SIZE / 2 local Type_Edit_Template = 1 local Type_Edit_BlockGrids = 2 function UISlgGvEEditorPanel:OnOpened(context) self:DecodeBlockData() self:RegistTriggerEvent(self.dragRoot, UIEventTriggerType.Drag) self.editType = 0 self.saveBtn:SetText("保存数据") self.editTemplateBtn:SetText("编辑模板") self.editMapBtn:SetText("编辑阻碍") self.deleteBtn:SetText("选中删除") self.editorContainer = {} self.editTemplateMiddleIndex = nil self:UpdateBlockUnit() UIUtils.SetActive(self.toggle_Group.gameObject, false) self:UpdateTemplateTabs() UIUtils.SetActive(self.loop_Tab, false) self.blockIndexDict = {} self.deleteIndexDict = {} UTL.SLG.InitSlgMap(GRID_COUNT, ITEM_WIDTH, ITEM_HEIGHT) self.limitLeft, self.limitRight, self.limitTop, self.limitBottom = CTL.SlgGvE.GetLimitRectPosition() self.collectionList = {} self.btnToGridId = {} local x, y = UTL.SLG.PositionToUIPosition(DRAG_LIMIT_MIDDLE_POS_X, DRAG_LIMIT_MIDDLE_POS_Y) self.dragRoot:SetLocalPosition(-x, -y) end function UISlgGvEEditorPanel:InitAll(isInit) for i, v in pairs(self.collectionList) do UIUtils.SetActive(v.gameObject, false) end for row = 1, GRID_COUNT do for slot = 1, GRID_COUNT do local index = UTL.SLG.PositionToIndex(row - 1, slot - 1) self:InitItem(index) end end self:ResetAll() for i, v in ipairs(self.staticBlockCfg[1]) do local index = v local templateId = self.staticBlockCfg[2][i] self.editorContainer[index] = templateId self.blockIndexDict[index] = index self:SetItemByTemplateId(templateId, v) end for index, v in pairs(self.blockIndexDict) do local item = self.collectionList[index] if item then UIUtils.SetActive(item.select, true) UIUtils.SetActive(item.delete, false) end end if isInit then local x, y = UTL.SLG.PositionToUIPosition(DRAG_LIMIT_MIDDLE_POS_X, DRAG_LIMIT_MIDDLE_POS_X) self.dragRoot:SetLocalPosition(-x, -y) self.loop_Tab:SelectIndex(0, true) end end function UISlgGvEEditorPanel:InitTemplate() for i, v in pairs(self.collectionList) do UIUtils.SetActive(v.gameObject, false) end for row = 1, 15 do for slot = 1, 15 do local index = UTL.SLG.PositionToIndex(row - 1, slot - 1) self:InitItem(index) end end self:ResetAll() local x, y = UTL.SLG.PositionToUIPosition(5, 5) self.dragRoot:SetLocalPosition(-x, -y) end function UISlgGvEEditorPanel:ResetAll() self.editorContainer = {} self.editTemplateMiddleIndex = nil self.blockIndexDict = {} self.deleteIndexDict = {} for i, v in pairs(self.collectionList) do UIUtils.SetActive(v.select, false) UIUtils.SetActive(v.image, false) UIUtils.SetActive(v.delete, false) end end function UISlgGvEEditorPanel:OnDrag(comp, eventData) local x = eventData.delta.x local y = eventData.delta.y local oriPosX, oriPosY = self.dragRoot:GetLocalPosition() if x == 0 and y == 0 then return end local tarUIPosX, tarUIPosY = oriPosX + x, oriPosY + y self.dragRoot:SetLocalPosition(tarUIPosX, tarUIPosY) end function UISlgGvEEditorPanel:InitItem(slotIndex) local posX, posY = UTL.SLG.IndexToPosition(slotIndex) local x, y = UTL.SLG.PositionToUIPosition(posX, posY) if self.editType == Type_Edit_Template or (x >= self.limitLeft and x <= self.limitRight and y >= self.limitBottom and y <= self.limitTop) then local item = self.collectionList[slotIndex] or self:InstantiateCollection(self.layer, self.item.gameObject) UIUtils.SetActive(item.gameObject, true) UIUtils.SetActive(item.select, false) UIUtils.SetActive(item.image, false) UIUtils.SetActive(item.delete, false) item.txt.text = slotIndex .. "|" .. posX .. "," .. posY UIUtils.SetLocalPosition(item.gameObject, x, y, 0) self.collectionList[slotIndex] = item self.btnToGridId[item.button] = slotIndex return item end end function UISlgGvEEditorPanel:OnButtonClicked(comp) if self.btnToGridId[comp] then local slotIndex = self.btnToGridId[comp] self:HandleSelect(slotIndex) elseif comp == self.editTemplateBtn then self.editType = Type_Edit_Template UIUtils.SetActive(self.toggle_Group.gameObject, true) UIUtils.SetActive(self.loop_Tab, false) self.toggle_Group:SelectIndex(0) self:InitTemplate() elseif comp == self.editMapBtn then self.editType = Type_Edit_BlockGrids UIUtils.SetActive(self.toggle_Group.gameObject, false) UIUtils.SetActive(self.loop_Tab, true) self:UpdateTemplateTabs() self:InitAll(true) elseif comp == self.saveBtn then self:SaveData() elseif comp == self.deleteBtn then self:HandleDelete() elseif comp == self.resetBtn then self:ResetAll() end end function UISlgGvEEditorPanel:HandleDelete() if table.getDictCount(self.deleteIndexDict) > 0 then for index, _ in pairs(self.deleteIndexDict) do local item = self.collectionList[index] if item then UIUtils.SetActive(item.image, false) UIUtils.SetActive(item.select, false) UIUtils.SetActive(item.delete, false) end if self.editorContainer[index] then local templateCfg = self.staticBlockTemplateCfg[self.editorContainer[index]] if templateCfg then local x, y = UTL.SLG.IndexToPosition(index) local blockUnits = templateCfg[1] local xOffsets = templateCfg[2] local yOffsets = templateCfg[3] for i, v in ipairs(blockUnits) do local xOffset = xOffsets[i] local yOffset = yOffsets[i] local _index = UTL.SLG.PositionToIndex(x + xOffset, y + yOffset) local item = self.collectionList[_index] if item then UIUtils.SetActive(item.image, false) UIUtils.SetActive(item.select, false) UIUtils.SetActive(item.delete, false) end end end end if self.editTemplateMiddleIndex and index == self.editTemplateMiddleIndex then self.editTemplateMiddleIndex = nil end self.editorContainer[index] = nil self.blockIndexDict[index] = nil end --可能会误删重叠的部分,将中心点还在的部分补回来 if self.editType == Type_Edit_BlockGrids then for index, v in pairs(self.editorContainer) do self:SetItemByTemplateId(v, index) end else for index, v in pairs(self.editorContainer) do self:SetUnitItem(index, v) end end for index, v in pairs(self.blockIndexDict) do local item = self.collectionList[index] if self.editorContainer[v] then if item then UIUtils.SetActive(item.select, true) UIUtils.SetActive(item.delete, false) end else if item then UIUtils.SetActive(item.select, false) UIUtils.SetActive(item.delete, false) end self.blockIndexDict[index] = nil end end if self.editType == Type_Edit_Template then if self.editTemplateMiddleIndex then local item = self.collectionList[self.editTemplateMiddleIndex] if item then UIUtils.SetActive(item.select, true) end else self:ResetAll() end end end end function UISlgGvEEditorPanel:SetUnitItem(slotIndex, unitIndex) local item = self.collectionList[slotIndex] if item then UIUtils.SetActive(item.select, false) local unitCfg = self.staticBlockUnitCfg[unitIndex] local path = unitCfg[3] self:SetItemImage(item, path) self:CollectAllBlock(unitCfg, slotIndex, slotIndex) end end function UISlgGvEEditorPanel:HandleSelect(slotIndex) if self.editType == Type_Edit_Template and self.editTemplateMiddleIndex == slotIndex and self.editorContainer[slotIndex] == true then --中心点可以直接配置模板,不走到删除分支 else if self.editorContainer[slotIndex] or self.blockIndexDict[slotIndex] then self.deleteIndexDict[slotIndex] = true local index = self.blockIndexDict[slotIndex] self.deleteIndexDict[index] = true for i, v in pairs(self.blockIndexDict) do if v == slotIndex or v == index then local item = self.collectionList[i] if item then UIUtils.SetActive(item.delete, true) end self.deleteIndexDict[i] = true end end return end end local item = self.collectionList[slotIndex] if item == nil then return end if self.editType == Type_Edit_Template then if self.editTemplateMiddleIndex == nil then self.editorContainer[slotIndex] = true self.blockIndexDict[slotIndex] = slotIndex UIUtils.SetActive(item.select, true) UIUtils.SetActive(item.image, false) self.editTemplateMiddleIndex = slotIndex else self.editorContainer[slotIndex] = self.selectUnitIndex self:SetUnitItem(slotIndex, self.selectUnitIndex) end else if self.selectTemplateId then self:SetItemByTemplateId(self.selectTemplateId, slotIndex) self.blockIndexDict[slotIndex] = slotIndex self.editorContainer[slotIndex] = self.selectTemplateId end end self.deleteIndexDict = {} for index, v in pairs(self.blockIndexDict) do local item = self.collectionList[index] if item then UIUtils.SetActive(item.select, true) UIUtils.SetActive(item.delete, false) end end end function UISlgGvEEditorPanel:OnValueChanged(comp, index) self.selectUnitIndex = index + 1 end function UISlgGvEEditorPanel:OnCellInit(scrollRect, index, cellTable, data) cellTable.txt.text = "模板" .. index + 1 end function UISlgGvEEditorPanel:OnCellClicked(scrollRect, index, cellTable, target, data) if self.selectCell then UIUtils.SetActive(self.selectCell.select, false) end self.selectCell = cellTable UIUtils.SetActive(self.selectCell.select, true) self.selectTemplateId = index + 1 end function UISlgGvEEditorPanel:SetItemByTemplateId(selectTemplateId, slotIndex) local templateCfg = self.staticBlockTemplateCfg[selectTemplateId] if templateCfg then local x, y = UTL.SLG.IndexToPosition(slotIndex) local blockUnits = templateCfg[1] local xOffsets = templateCfg[2] local yOffsets = templateCfg[3] for i, v in ipairs(blockUnits) do local unitCfg = self.staticBlockUnitCfg[v] local xOffset = xOffsets[i] local yOffset = yOffsets[i] local path = unitCfg[3] local index = UTL.SLG.PositionToIndex(x + xOffset, y + yOffset) local item = self.collectionList[index] if item then UIUtils.SetActive(item.select, true) self:SetItemImage(item, path) end self:CollectAllBlock(unitCfg, index, slotIndex) end end end function UISlgGvEEditorPanel:SetItemImage(item, path) UIUtils.SetActive(item.image, true) local image = item.image:GetComponent(typeof(UnityEngine.UI.Image)) UIUtils.SetImageByAssetBundle(image, path) image:SetNativeSize() end function UISlgGvEEditorPanel:CollectAllBlock(unitCfg, slotIndex, middleIndex) for i, v in ipairs(unitCfg[1]) do local x, y = UTL.SLG.IndexToPosition(slotIndex) local index = UTL.SLG.PositionToIndex(x + v, y + unitCfg[2][i]) self.blockIndexDict[index] = middleIndex end end function UISlgGvEEditorPanel:DecodeBlockData() self.staticBlockUnitCfg = SlgGvEBlockData.BlockUnitCfg self.staticBlockTemplateCfg = SlgGvEBlockData.BlockTemplateCfg self.staticBlockCfg = SlgGvEBlockData.BlockCfg end function UISlgGvEEditorPanel:SaveData() if self.editType == Type_Edit_Template then local addTemplateCfg = { [1] = {}, [2] = {}, [3] = {} } for index, blockUnitId in pairs(self.editorContainer) do if index ~= self.editTemplateMiddleIndex or (index == self.editTemplateMiddleIndex and (type(blockUnitId) ~= 'boolean')) then local x, y = UTL.SLG.IndexToPosition(index) local middleX, middleY = UTL.SLG.IndexToPosition(self.editTemplateMiddleIndex) local offsetX = middleX - x local offsetY = middleY - y table.insert(addTemplateCfg[1], blockUnitId) table.insert(addTemplateCfg[2], offsetX) table.insert(addTemplateCfg[3], offsetY) end end table.insert(self.staticBlockTemplateCfg, addTemplateCfg) else self.staticBlockCfg = { [1] = {}, [2] = {} } for index, templateId in pairs(self.editorContainer) do table.insert(self.staticBlockCfg[1], index) table.insert(self.staticBlockCfg[2], templateId) end end self:ExportStaticData() if self.editType == Type_Edit_BlockGrids then self:InitAll(false) else self:InitTemplate() end end function UISlgGvEEditorPanel:UpdateBlockUnit() local nameList = {} for i, v in ipairs(self.staticBlockUnitCfg) do table.insert(nameList, "阻挡" .. i) end UTL.Toggle.InitToggleGroupByText(self.toggle_Group, "lab", nameList) end function UISlgGvEEditorPanel:UpdateTemplateTabs() self.loop_Tab:ReloadAll(self.staticBlockTemplateCfg) end function UISlgGvEEditorPanel:ExportStaticData() local content = "local SlgGvEBlockData={}\nSlgGvEBlockData.BlockUnitCfg={\n" for i, v in ipairs(self.staticBlockUnitCfg) do content = content .. " [" .. i .. "]={{" .. self:EncodeTableData(v[1]) .. "}," .. "{" .. self:EncodeTableData(v[2]) .. "},\"" .. v[3] .. "\"},\n" end content = content .. "}\nSlgGvEBlockData.BlockTemplateCfg={\n" for i, v in ipairs(self.staticBlockTemplateCfg) do content = content .. " [" .. i .. "]={{" .. self:EncodeTableData(v[1]) .. "}," .. "{" .. self:EncodeTableData(v[2]) .. "}," .. "{" .. self:EncodeTableData(v[3]) .. "}},\n" end content = content .. "}\nSlgGvEBlockData.BlockCfg={\n" content = content .. " [1]={" .. self:EncodeTableData(self.staticBlockCfg[1]) .. "},\n" content = content .. " [2]={" .. self:EncodeTableData(self.staticBlockCfg[2]) .. "}\n}\n" content = content .. "return SlgGvEBlockData" LuaExt.FileWriteText(SlgGvEBlockDataPath, content) end function UISlgGvEEditorPanel:EncodeTableData(array) if #array == 0 then return "" end local content = nil for i, v in ipairs(array) do if content == nil then content = v else content = content .. "," .. v end end return content end return UISlgGvEEditorPanel