--[[ 按键设置界面 ]] local UISettingPCKeyMapEditing = UIViewClass() local private = {} local JSON = require("cjson") local EventSystem = require('Event/EventSystem') local DescriptionList = {0,1,2,3,4,5,6,11,12,13,20,21,22,23,1002,1003,32,33,41} local DescriptionListIndex = {11,12,13,14,15,16,21,22,23,24,31,32,33,34,35,36,41,42,43} function UISettingPCKeyMapEditing:OnOpened(context) self.keyDescriptionArray = {} self:RegistSystemEvent(EventID.PCPackage_SetKeyMapInput, self.HandleKeyboardInput) self:InitKeyMapDisplay() end function UISettingPCKeyMapEditing:InitKeyMapDisplay() self.dataDic = MDL.Setting.GetPCKeyMap() for i, keyMapEnum in ipairs(DescriptionList) do local keyDescriptionItem = self.keyDescriptionArray[i] if not keyDescriptionItem then --克隆预制体 keyDescriptionItem = self:InstantiateCollection(self.collection_key, self.cell.gameObject) UIUtils.SetActive(keyDescriptionItem.gameObject, true) --设置本地坐标位置 keyDescriptionItem.keyMapEnum = keyMapEnum local x,y = self:GetPosByKeyMapEnum(DescriptionListIndex[i]) keyDescriptionItem.gameObject.transform:SetLocalPosition(x, y, 0) --键位名 local idsData = DataSheetManager.SetKeyPosition:GetDataByID(keyMapEnum).Describe UIUtils.SetLabelText(keyDescriptionItem.txt_Name, Const_IDS[idsData]) --键位颜色 keyDescriptionItem.state_Bg:SetState(self:GetStateByKeyMapEnum(keyMapEnum)) --键位选中状态 UIUtils.SetActive(keyDescriptionItem.img_Selected.gameObject,false) --监听选中事件 self:RegistTriggerEvent(keyDescriptionItem.btn_set, UIEventTriggerType.PointerDown) --显示设置的按键 local keycode = self.dataDic[keyMapEnum] if keycode then keyDescriptionItem.txt_key.text = MDL.Setting.GetKeyStrByKeyCode(keycode) end self.keyDescriptionArray[i] = keyDescriptionItem end end end function UISettingPCKeyMapEditing:GetStateByKeyMapEnum(keyMapEnum) if keyMapEnum < 20 then return 2 elseif keyMapEnum < 30 or keyMapEnum == 1002 or keyMapEnum == 1003 then return 1 elseif keyMapEnum < 40 then return 3 else return 0 end end function UISettingPCKeyMapEditing:GetPosByKeyMapEnum(keyMapEnum) local columnIndex = math.floor(keyMapEnum/10) local rowIndex = keyMapEnum%10 return -560+(columnIndex-1)*350, 350-rowIndex*100 end function UISettingPCKeyMapEditing:OnButtonClicked(comp) if comp == self.btn_Customization then MDL.Setting.SetPCKeyMap(self.dataDic) MDL.Setting.ApplyPCKeyMapToCSharp() EventSystem.Dispatch(EventID.Setting_ShowPCKeyMapEditing, false) return; end end function UISettingPCKeyMapEditing:OnClosed() MDL.Setting.isEditing = false end function UISettingPCKeyMapEditing:OnPointerDown(comp, eventData) for i, cellComp in ipairs(self.keyDescriptionArray) do if cellComp.btn_set == comp then UIUtils.SetActive(cellComp.img_Selected.gameObject,true) if self.curSelectComp == nil then self.curSelectComp = cellComp self.curKeyMapEnum = DescriptionList[i] AuxUtil.StartChangeKeyMap() cellComp.txt_key.text = "" else self:SetDisplayText(self.curKeyMapEnum,self.dataDic[self.curKeyMapEnum]) self.curSelectComp = cellComp self.curKeyMapEnum = DescriptionList[i] cellComp.txt_key.text = "" end else UIUtils.SetActive(cellComp.img_Selected.gameObject,false) end end end function UISettingPCKeyMapEditing:HandleKeyboardInput(keycode) if self.curSelectComp ~= nil then local isRepeat,oldKeyMapEnum = self:CheckKeySettingRepeat(keycode,self.curKeyMapEnum) if isRepeat then self.dataDic[oldKeyMapEnum] = 0 self:SetDisplayText(oldKeyMapEnum,0) local idsData = DataSheetManager.SetKeyPosition:GetDataByID(oldKeyMapEnum).Describe CTL.Tips.AddTipsMsg(string.FormatByIdsKey("IDS_PCPackage_KeyRepeat", UIUtils.GetLocalizedTxt(Const_IDS[idsData]))) end self.curSelectComp.txt_key.text = MDL.Setting.GetKeyStrByKeyCode(keycode) UIUtils.SetActive(self.curSelectComp.img_Selected.gameObject,false) self.dataDic[self.curKeyMapEnum] = keycode self.curSelectComp = nil self.curKeyMapEnum = nil end end function UISettingPCKeyMapEditing:CheckKeySettingRepeat(keycode,curKeyMapEnum) for keyMapEnum,keyCodeValue in pairs(self.dataDic) do if keycode == keyCodeValue and curKeyMapEnum ~= keyMapEnum then return true,keyMapEnum end end return false,0 end function UISettingPCKeyMapEditing:SetDisplayText(oldKeyMapEnum,keycode) for i, keyComp in ipairs(self.keyDescriptionArray) do if keyComp.keyMapEnum == oldKeyMapEnum then keyComp.txt_key.text = MDL.Setting.GetKeyStrByKeyCode(keycode) end end end return UISettingPCKeyMapEditing