WoWInterface SVN KeyboardAccess

[/] [trunk/] [core.lua] - Rev 11

Go to most recent revision | Compare with Previous | Blame | View Log

--[[
        ##AddOn   : KeyboardAccess 
        ##Subfile : core.lua
        ##Author  : Hati-EK
--]]
--[[Logic Rules:
FIFO Principe
Example:
1. Ready-Check comes in.
2. Rogue wants to swap poison on MH.
3. !No Rule Conflict!
=> Rule#1:
What shows up first -> Gets handled first


informations
>>readycheck module
        ReadyCheckFrameYesButton --yes
        ReadyCheckFrameNoButton --no


>>npc/enchant module
        StaticPopup1Button1 --yes
        StaticPopup1Button2 --no
        up to StaticPopup4

>>LFD tool (role)
        LFDRoleCheckPopupAcceptButton
        LFDRoleCheckPopupDeclineButton
        
>>LFD tool (join)
        LFDDungeonReadyDialogEnterDungeonButton --Enter
        LFDDungeonReadyDialogLeaveQueueButton --Leave
]]
KeyboardAccess = { } --just for personal debugging issues
local KA = KeyboardAccess  -- 4b6579626f617264416363657373 = Hex-CODE for KeyboarAccess
KA.name = 'KeyboardAccess'
KA.ver = GetAddOnMetadata( KA.name, 'Version')
if KA.ver=='rwowi:revision' then KA.ver='SVN' end
KA.MODShift=false
KA.handleT = {}
local events = {}


local function toboolean(x)
        if x==1 then
                return true
        else
                return false
        end
end

local function inTable(tbl,value)
  local b = false
  for i,k in ipairs(tbl) do
    if k==value then
      return i
    end
  end
  return b
end

function events:MODIFIER_STATE_CHANGED(modi,state) --required for still opening chat or config 
        if modi=='LSHIFT' or modi=='RSHIFT' then
                KA.MODShift=toboolean(state)
                if KA.MODShift then
                        KA.handler:EnableKeyboard(0)
                else
                        if #KA.handleT>0 then
                                KA.handler:EnableKeyboard(1)
                        else
                                KA.handler:EnableKeyboard(0)
                        end
                end
        end
end

function events:READY_CHECK(initiator,timer) --ReadyCheck PopUp
        --print(UnitName('player')==initiator)
        if not (UnitName('player')==initiator) then
                --print(initiator)
                tinsert(KA.handleT,'ReadyCheckFrame')
                KA.handler:EnableKeyboard(1)
                local RCFYB_OnClick = ReadyCheckFrameYesButton:GetScript('OnClick')
                ReadyCheckFrameYesButton:SetScript('OnClick',function(self, button, down)
                        if button=='LeftButton' and not down then
                                RCFYB_OnClick(button,down)
                                local num = inTable(KA.handleT,'ReadyCheckFrame')
                                print(num)
                                if num then
                                        print('removing', 'READY_CHECK')
                                        tremove(KA.handleT,num)
                                        if #KA.handleT==0 then
                                                KA.handler:EnableKeyboard(0)
                                        end
                                end
                        end
                end)
                local RCFNB_OnClick = ReadyCheckFrameNoButton:GetScript('OnClick')
                ReadyCheckFrameNoButton:SetScript('OnClick',function(self, button, down)
                        if button=='LeftButton' and not down then
                                RCFNB_OnClick(button,down)
                                local num = inTable(KA.handleT,'ReadyCheckFrame')
                                print(num)
                                if num then
                                        print('removing', 'READY_CHECK')
                                        tremove(KA.handleT,num)
                                        if #KA.handleT==0 then
                                                KA.handler:EnableKeyboard(0)
                                        end
                                end
                        end
                end)
        end
end

function events:LFG_PROPOSAL_SHOW(...) --LFD Tool PopUp
        --print('LFG_PROPOSAL_SHOW')
        tinsert(KA.handleT,'LFDDungeonReadyDialog')
        KA.handler:EnableKeyboard(1)
end

KA.handler = CreateFrame('Frame',KA.name.."_Handler")
KA.handler:SetScript('OnEvent',function(self,event,...)
        events[event](self, ...)
end)
for k, v in pairs(events) do
        print('Registering',k)
        KA.handler:RegisterEvent(k); -- Register all events for which handlers have been defined
end


--overwriting all possible frames
local popups = {
        LFDRoleCheckPopup, --rolecheck
        StaticPopup1, --default
        StaticPopup2, --popup frames 
        StaticPopup3, --for enchants
        StaticPopup4 --buying items
}
local buttonT = { 
        ["LFDRoleCheckPopup"] = { 
                "AcceptButton",
                "DeclineButton",
        },
        ["StaticPopup1"] = {
                "Button1",
                "Button2",
        },
        ["StaticPopup2"] = {
                "Button1",
                "Button2",
        },
        ["StaticPopup3"] = {
                "Button1",
                "Button2",
        },
        ["StaticPopup4"] = {
                "Button1",
                "Button2",
        },
        ["LFDDungeonReadyDialog"] = {
                "EnterDungeonButton",
                "LeaveQueueButton",
        },
        ["ReadyCheckFrame"] = { 
                "YesButton",
                "NoButton",
        }
}
KA.oldShow = { }
KA.oldClick = { }
--storing old shows & creating new ones
for _,k in ipairs(popups) do
        local name = k:GetName()
        KA.oldShow[name] = k:GetScript('OnShow')
        KA.oldClick[name]={}
        
        KA.oldClick[name][1] = _G[name..buttonT[name][1]]:GetScript('OnClick') --accept button
        KA.oldClick[name][2] = _G[name..buttonT[name][2]]:GetScript('OnClick') --decline button
        
        k:SetScript('OnShow',function(...)
                if not ( k.which =='REPLACE_ENCHANT' or k.which=='DEATH' or k.which=='RECOVER_CORPSE_INSTANCE' or k.which=='BUYOUT_AUCTION') then
                        tinsert(KA.handleT,name)
                        KA.handler:EnableKeyboard(1)
                end
                KA.oldShow[name](...)
        end)
        _G[name..buttonT[name][1]]:SetScript('OnClick',function(self, button, down)
                if button=='LeftButton' and not down then
                        KA.oldClick[name][1](self,button,down)
                        local num = inTable(KA.handleT,name)
                        print(num)
                        if num then
                                print('removing', name)
                                tremove(KA.handleT,num)
                                if #KA.handleT==0 then
                                        KA.handler:EnableKeyboard(0)
                                end
                        end
                end
        end)
        _G[name..buttonT[name][2]]:SetScript('OnClick',function(self, button, down)
                if button=='LeftButton' and not down then
                        KA.oldClick[name][2](self,button,down)
                        local num = inTable(KA.handleT,name)
                        print(num)
                        if num then
                                print('removing', name)
                                tremove(KA.handleT,num)
                                if #KA.handleT==0 then
                                        KA.handler:EnableKeyboard(0)
                                end
                        end
                end
        end)
end

--fix clicking on buttons

--KeyPressing handling
KA.handler:SetScript('OnKeyDown',function(self,key)
        if key=='LSHIFT' or key=='RSHIFT' then
                self:EnableKeyboard(0)
                KA.MODShift=true
        end
end)
KA.handler:SetScript('OnKeyUp',function(self,key)
        if key=='ENTER' then
                local frame = _G[KA.handleT[1]]
                if frame.which == "REPLACE_ENCHANT" or frame.which=='DEATH' or frame.which=='RECOVER_CORPSE_INSTANCE' or frame.which=='BUYOUT_AUCTION' then
                        tremove(KA.handleT,1)
                        if #KA.handleT==0 then
                                self:EnableKeyboard(0)
                        end
                else
                        _G[KA.handleT[1]..buttonT[KA.handleT[1]][1]]:Click()
                        tremove(KA.handleT,1)
                end
        elseif key=='ESCAPE' then
                _G[KA.handleT[1]..buttonT[KA.handleT[1]][2]]:Click()
                tremove(KA.handleT,1)
        end
        if key=='LSHIFT' or key=='RSHIFT' then
                self:EnableKeyboard(1)
                KA.MODShift=false
        end
        if #KA.handleT==0 then
                self:EnableKeyboard(0)
        end
end)


Go to most recent revision | Compare with Previous | Blame