View Single Post
04/02/15, 11:38 AM   #11
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,001
I got a new problem now. The addon is working so far. Mouse wheel and keyboard keys 1 to 5 will switch the combobox at the guild bank selection popup to the according guild.

The mousewheel is working directly after the 1st time you open the guild bank selector popup.
But the keyboard keys 1 to 5 won#t work at this time?
You'll have to close the dialog and reopen it again. After this the keys are working too.

Currently I hooked the OnShow method of the dialog, prehooked the OnKeyUp function of the dialog and enabled the keyboard then. But it seems that the dialog won't register the OnKeyUp handler's PreHook until you "reload" the dialog (by closing and opening it again)?

Any ideas how to fix this?

Here is the current source code:
Lua Code:
  1. ------------------------------------------------------------------
  2. --FCOGuildBankQuickSelect.lua
  3. --Author: Baertram
  4. --v0.0.2b
  5. --[[
  6. Quickly select the guild bank by numbers 1-5 (at an opened guild bank selection)
  7. ]]
  8. ------------------------------------------------------------------
  9. --Array for all the variables
  10. local locVars = {}
  11.  
  12. --Uncolored "FCOIS" pre chat text for the chat output
  13. locVars.preChatText = "FCOGuildBankQuickSelect"
  14. --Green colored "FCOIS" pre text for the chat output
  15. locVars.preChatTextGreen = "|c22DD22"..locVars.preChatText.."|r "
  16. --Red colored "FCOIS" pre text for the chat output
  17. locVars.preChatTextRed = "|cDD2222"..locVars.preChatText.."|r "
  18. --Blue colored "FCOIS" pre text for the chat output
  19. locVars.preChatTextBlue = "|c2222DD"..locVars.preChatText.."|r "
  20.  
  21. --Addon variables
  22. local addonVars = {}
  23. addonVars.gAddonName                 = "FCOGuildBankQuickSelect"
  24. addonVars.addonNameMenu              = "FCO GuildBankQuickSelect"
  25. addonVars.addonNameMenuDisplay       = "|c00FF00FCO |cFFFF00GuildBankQuickSelect|r"
  26. addonVars.addonAuthor                = '|cFFFF00Baertram|r'
  27. addonVars.addonVersion               = 0.01 -- Changing this will reset SavedVariables!
  28. addonVars.addonVersionOptions        = '0.0.2b' -- version shown in the settings panel
  29. addonVars.addonSavedVariablesName    = "FCOGuildBankQuickSelect_Settings"
  30. addonVars.gAddonLoaded               = false
  31. addonVars.hookedGuildSelectDialog    = false
  32. addonVars.currentSelectedGuildNumber = -1
  33.  
  34. --Original/Backup avriables
  35. local origVars = {}
  36. --origVars.GuildBankSelectorOnKeyDown         = nil
  37. origVars.GuildBankSelectorOnKeyUp           = nil
  38. origVars.GuildBankSelectorOnMouseWheelMU    = nil
  39. origVars.GuildBankSelectorOnMouseWheelG     = nil
  40.  
  41. --Control names of ZO* standard controls etc.
  42. local GUILDBANK_SELECT_POPUP                = ZO_SelectGuildBankDialog
  43. local GUILDBANK_SELECT_POPUP_GUILD          = ZO_SelectGuildBankDialogGuild
  44. local GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX = GUILDBANK_SELECT_POPUP_GUILD.m_comboBox
  45. local GUILDBANK_SELECT_POPUP_ACCEPT         = ZO_SelectGuildBankDialogAccept
  46. local GUILDBANK_SELECT_POPUP_ABORT          = ZO_SelectGuildBankDialogCancel
  47. local GUILDBANK_SELECT_POPUP_MODAL_UNDERLAY = ZO_SelectGuildBankDialogModalUnderlay
  48.  
  49. --===================== FUNCTIONS ==============================================
  50.  
  51. --Get the current selected item's index
  52. local function FCOGuildBankQuickSelect_GetCurrentSelectedIndex(comboBoxCtrl)
  53. d("blubb")
  54.     --Check the combobox if the last item is possible in there, otherwise select "All"
  55.     local comboboxItems = comboBoxCtrl.m_sortedItems
  56.     local currentItem   = comboBoxCtrl.m_selectedItemText:GetText()
  57.     if currentItem == nil then return -1 end
  58.     local foundIndex = -1
  59.     if comboboxItems ~= nil then
  60.         for idx, itemTable in pairs(comboboxItems) do
  61.             if itemTable.name == currentItem then
  62.                 --item was found, abort here
  63.                 foundIndex = idx
  64.                 break
  65.             end
  66.         end
  67.     end
  68.     return foundIndex
  69. end
  70.  
  71. --Select the next entry in the guild selection combobox
  72. local function FCOGuildBankQuickSelect_SelectGuildEntry(upDown, newIndex)
  73.     local maxIndex = #GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX.m_sortedItems
  74.     if maxIndex == 0 or maxIndex == nil then return end
  75.     newIndex = newIndex or -1
  76.     if newIndex ~= nil and newIndex ~= -1 and newIndex >= 1 and newIndex <= maxIndex then
  77.         --Set the given new index
  78.         if GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX then
  79.             GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX:SelectItemByIndex(newIndex)
  80.         end
  81.     else
  82.         if upDown == nil then return end
  83.         --Get the next index by help of the upDown variable coming from the mouse wheel capture
  84.         --Get the current selected item index first
  85.         local currentIndex = addonVars.currentSelectedGuildNumber
  86.         if currentIndex == -1 then
  87.             currentIndex = FCOGuildBankQuickSelect_GetCurrentSelectedIndex(GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX)
  88.         end
  89.         if currentIndex == nil or currentIndex == -1 or currentIndex < 1 or currentIndex > maxIndex then return end
  90.         --Scrolled up?
  91.         local newIndexToUse = -1
  92.         if upDown then
  93.             newIndexToUse = currentIndex - 1
  94.         else
  95.         --Scrolled down?
  96.             newIndexToUse = currentIndex + 1
  97.         end
  98.         --Did we scroll to the first index and below, then use maximum index
  99.         if newIndexToUse < 1 then
  100.             newIndexToUse = maxIndex
  101.         end
  102.         --Did we scroll to the last index and above, then use minimum index
  103.         if newIndexToUse > maxIndex then
  104.             newIndexToUse = 1
  105.         end
  106.         if newIndexToUse ~= -1 then
  107.             GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX:SelectItemByIndex(newIndexToUse)
  108.             addonVars.currentSelectedGuildNumber = newIndexToUse
  109.         end
  110.     end
  111. end
  112.  
  113. --Get the information about the pressed key and map it to a keybind
  114. local function FCOGuildBankQuickSelect_GetKeybindByPressedKey(key)
  115.     local actionNameAccept = "DIALOG_PRIMARY"
  116.     local actionNameDecline = "DIALOG_NEGATIVE"
  117.  
  118.     --KEYBIND_TEXT_OPTIONS_ABBREVIATED_NAME = 1
  119.     --KEYBIND_TEXT_OPTIONS_FULL_NAME = 2
  120.     --KEYBIND_TEXT_OPTIONS_FULL_NAME_SEPARATE_MODS = 3
  121.     local textOptions = KEYBIND_TEXT_OPTIONS_ABBREVIATED_NAME
  122.     --KEYBIND_TEXTURE_OPTIONS_NONE = 1
  123.     --KEYBIND_TEXTURE_OPTIONS_EMBED_MARKUP = 2
  124.     local textureOptions = KEYBIND_TEXTURE_OPTIONS_NONE
  125.     local bindingIndexAccept = 1  --1 or 2, primary or secondary binding
  126.     local bindingIndexDecline = 1  --1 or 2, primary or secondary binding
  127.     local bindingStringAccept = ZO_Keybindings_GetBindingStringFromAction(actionNameAccept, textOptions, textureOptions, bindingIndexAccept)
  128.     local bindingStringDecline = ZO_Keybindings_GetBindingStringFromAction(actionNameDecline, textOptions, textureOptions, bindingIndexDecline)
  129.  
  130. --d("bindingStringAccept: " .. tostring(bindingStringAccept))
  131. --d("bindingStringDecline: " .. tostring(bindingStringDecline))
  132. --d("Key: " .. tostring(key) .. "(".. GetKeyName(key) .. ")")
  133.     if GetKeyName(key) == bindingStringAccept then
  134.         return 1
  135.     elseif GetKeyName(key) == bindingStringDecline then
  136.         return 2
  137.     else
  138.         return -1
  139.     end
  140. end
  141.  
  142. -- set handlers for the mouse wheel
  143. local function FCOGuildBankQuickSelect_HandleMouseWheelEvents(doRegister)
  144.     doRegister = doRegister or false
  145.     if doRegister then
  146.         GUILDBANK_SELECT_POPUP_MODAL_UNDERLAY:SetMouseEnabled(true)
  147.         GUILDBANK_SELECT_POPUP_MODAL_UNDERLAY:SetHandler("OnMouseWheel", function(self, delta, ctrl, alt, shift)
  148.             if delta < 0 then
  149.                 -- Mouse wheel down
  150.                 FCOGuildBankQuickSelect_SelectGuildEntry(false, nil)
  151.             else
  152.                 -- Mouse wheel up
  153.                 FCOGuildBankQuickSelect_SelectGuildEntry(true, nil)
  154.             end
  155.         end)
  156.         GUILDBANK_SELECT_POPUP_GUILD:SetMouseEnabled(true)
  157.         GUILDBANK_SELECT_POPUP_GUILD:SetHandler("OnMouseWheel", function(self, delta, ctrl, alt, shift)
  158.             if delta < 0 then
  159.                 -- Mouse wheel down
  160.                 FCOGuildBankQuickSelect_SelectGuildEntry(false, nil)
  161.             else
  162.                 -- Mouse wheel up
  163.                 FCOGuildBankQuickSelect_SelectGuildEntry(true, nil)
  164.             end
  165.         end)
  166.     else
  167.         if origVars.GuildBankSelectorOnMouseWheelMU and origVars.GuildBankSelectorOnMouseWheelMU ~= nil then
  168.             --Use original handler for the mousewheel again
  169.             GUILDBANK_SELECT_POPUP_MODAL_UNDERLAY:SetHandler("OnMouseWheel", function(self, delta, ctrl, alt, shift)
  170.                 origVars.GuildBankSelectorOnMouseWheelMU(self, delta, ctrl, alt, shift)
  171.             end)
  172.         end
  173.         if origVars.GuildBankSelectorOnMouseWheelG and origVars.GuildBankSelectorOnMouseWheelG ~= nil then
  174.             GUILDBANK_SELECT_POPUP_GUILD:SetHandler("OnMouseWheel", function(self, delta, ctrl, alt, shift)
  175.                 origVars.GuildBankSelectorOnMouseWheelG(self, delta, ctrl, alt, shift)
  176.             end)
  177.         end
  178.     end
  179. end
  180.  
  181. -- set handlers for keyboard events:
  182. local function FCOGuildBankQuickSelect_HandleKeyboardEvents(self, key, ctrl, alt, shift, command)
  183.     --Check if the keybinding for accept or decline (or key ESCAPE) were pressed
  184.     local pressedKeyMappedToKeybindKey = -1
  185.     if key ~= KEY_ESCAPE then
  186.         pressedKeyMappedToKeybindKey = FCOGuildBankQuickSelect_GetKeybindByPressedKey(key)
  187.     end
  188.     if key == KEY_ESCAPE or pressedKeyMappedToKeybindKey ~= -1 then
  189.         if pressedKeyMappedToKeybindKey == 1 then
  190.             if GUILDBANK_SELECT_POPUP_ACCEPT.clickSound ~= nil then
  191.                 PlaySound(GUILDBANK_SELECT_POPUP_ACCEPT.clickSound)
  192.             else
  193.                 PlaySound(SOUNDS.DIALOG_ACCEPT)
  194.             end
  195.             GUILDBANK_SELECT_POPUP_ACCEPT:callback()
  196.         elseif key == KEY_ESCAPE or pressedKeyMappedToKeybindKey == 2 then
  197.             if GUILDBANK_SELECT_POPUP_ABORT.clickSound ~= nil then
  198.                 PlaySound(GUILDBANK_SELECT_POPUP_ABORT.clickSound)
  199.             else
  200.                 PlaySound(SOUNDS.DIALOG_DECLINE)
  201.             end
  202.             GUILDBANK_SELECT_POPUP_ABORT:callback()
  203.         end
  204.     else
  205.         --Check for the pressed key 1 to 5 or numpad 1 to 5 and change the guild accordingly
  206.         if key == KEY_1 or key == KEY_NUMPAD1 then
  207.             FCOGuildBankQuickSelect_SelectGuildEntry(nil, 1)
  208.         elseif key == KEY_2 or key == KEY_NUMPAD2 then
  209.             FCOGuildBankQuickSelect_SelectGuildEntry(nil, 2)
  210.         elseif key == KEY_3 or key == KEY_NUMPAD3 then
  211.             FCOGuildBankQuickSelect_SelectGuildEntry(nil, 3)
  212.         elseif key == KEY_4 or key == KEY_NUMPAD4 then
  213.             FCOGuildBankQuickSelect_SelectGuildEntry(nil, 4)
  214.         elseif key == KEY_5 or key == KEY_NUMPAD5 then
  215.             FCOGuildBankQuickSelect_SelectGuildEntry(nil, 5)
  216.         end
  217.     end
  218.     return false
  219. end
  220.  
  221. --
  222. local function FCOGuildBankQuickSelect_ChangeTweaks(doRegister)
  223.     if doRegister then
  224.         if GUILDBANK_SELECT_POPUP ~= nil then
  225.             --Register the OnKeyUp function for the guild bank select dialog
  226.             ZO_PreHookHandler(GUILDBANK_SELECT_POPUP, 'OnKeyUp', function(self, key, ctrl, alt, shift, command) FCOGuildBankQuickSelect_HandleKeyboardEvents(self, key, ctrl, alt, shift, command) end)
  227.             --GUILDBANK_SELECT_POPUP:SetHandler("OnKeyUp", function(self, key, ctrl, alt, shift, command) FCOGuildBankQuickSelect_HandleKeyboardEvents(self, key, ctrl, alt, shift, command) end)
  228.             FCOGuildBankQuickSelect_HandleMouseWheelEvents(true)
  229.             GUILDBANK_SELECT_POPUP:SetKeyboardEnabled(true)
  230.             addonVars.hookedGuildSelectDialog = true
  231.         end
  232.     else
  233.     if GUILDBANK_SELECT_POPUP ~= nil then
  234.         --Unregister OnKeyUp callback
  235.         if origVars.GuildBankSelectorOnKeyUp and origVars.GuildBankSelectorOnKeyUp ~= nil then
  236.             GUILDBANK_SELECT_POPUP:SetHandler("OnKeyUp", function(self, key, ctrl, alt, shift, command) origVars.GuildBankSelectorOnKeyUp(self, key, ctrl, alt, shift, command) end)
  237.         end
  238.         FCOGuildBankQuickSelect_HandleMouseWheelEvents(false)
  239.         GUILDBANK_SELECT_POPUP:SetKeyboardEnabled(false)
  240.         addonVars.hookedGuildSelectDialog = false
  241.         end
  242.     end
  243. end
  244.  
  245. --un/register guild bank selection popup tweaks
  246. local function registerGuildBankSelectionTweaks(doRegister, doOverride)
  247.     doOverride = doOverride or false
  248.     --If no guilds are given the functions make no sense here, so abort everything
  249.     local maxIndex = -1
  250.     if not doOverride then
  251.         if GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX ~= nil then
  252.             maxIndex = #GUILDBANK_SELECT_POPUP_GUILD_COMBOBOX.m_sortedItems
  253.         end
  254.         if maxIndex == nil or maxIndex < 1 then
  255.             --Unregister existing tweaks now
  256.             FCOGuildBankQuickSelect_ChangeTweaks(false)
  257.             --Abort here as no guilds are enabled
  258.             return false
  259.         end
  260.     end
  261.     --Enable or disable the tweaks now
  262.     FCOGuildBankQuickSelect_ChangeTweaks(doRegister)
  263. end
  264.  
  265. --==============================================================================
  266. --==================== START EVENT CALLBACK FUNCTIONS===========================
  267. --==============================================================================
  268.  
  269. --[[
  270. --Event function if guild bank is opened
  271. local function FCOGuildBankQuickSelect_Open_Guild_Bank()
  272.     if not addonVars.hookedGuildSelectDialog then
  273.         registerGuildBankSelectionTweaks(true, false)
  274.     end
  275. end
  276. ]]
  277.  
  278. --Event function if guild bank is closed
  279. local function FCOGuildBankQuickSelect_Close_Guild_Bank()
  280.     if addonVars.hookedGuildSelectDialog then
  281.         registerGuildBankSelectionTweaks(false, false)
  282.     end
  283. end
  284.  
  285. --PreHook the OnShow callback function of the guildbank select dialog
  286. local function FCOGuildBankQuickSelect_PreHookGuildSelectDialogOnShow(self)
  287.     --Reset the variable for the currently selected guild index
  288.     addonVars.currentSelectedGuildNumber = -1
  289.     if not addonVars.hookedGuildSelectDialog then
  290.         registerGuildBankSelectionTweaks(true, false)
  291.     end
  292. end
  293.  
  294. -- Fires each time after addons were loaded and player is ready to move (after each zone change too)
  295. local function FCOGuildBankQuickSelect_Player_Activated(...)
  296.     --Prevent this event to be fired again and again upon each zone change
  297.     EVENT_MANAGER:UnregisterForEvent(addonVars.gAddonName, EVENT_PLAYER_ACTIVATED)
  298.  
  299.     --Backup the original handlers
  300.     --origVars.GuildBankSelectorOnKeyDown      = GUILDBANK_SELECT_POPUP:GetHandler("OnKeyDown")
  301.     origVars.GuildBankSelectorOnKeyUp        = GUILDBANK_SELECT_POPUP:GetHandler("OnKeyUp")
  302.     origVars.GuildBankSelectorOnMouseWheelMU = GUILDBANK_SELECT_POPUP_MODAL_UNDERLAY:GetHandler("OnMouseWheel")
  303.     origVars.GuildBankSelectorOnMouseWheelG  = GUILDBANK_SELECT_POPUP_GUILD:GetHandler("OnMouseWheel")
  304.  
  305.     --PreHook the OnShow handler for the guild bank select dialog
  306.     ZO_PreHookHandler(GUILDBANK_SELECT_POPUP, 'OnShow', function(self) FCOGuildBankQuickSelect_PreHookGuildSelectDialogOnShow(self) end)
  307.  
  308.     --Set addon loaded = false
  309.     addonVars.gAddonLoaded = false
  310. end
  311.  
  312. --==============================================================================
  313. --===== HOOKS BEGIN ============================================================
  314. --==============================================================================
  315. --Create the hooks & pre-hooks
  316. local function CreateHooks()
  317. --nothing here atm
  318. end
  319.  
  320. --Addon loads up
  321. local function FCOGuildBankQuickSelect_Loaded(eventCode, addOnName)
  322.     --Is this addon found?
  323.     if(addOnName ~= addonVars.gAddonName) then
  324.         return
  325.     end
  326.     --Unregister this event again so it isn't fired again after this addon has beend reckognized
  327.     EVENT_MANAGER:UnregisterForEvent(addonVars.gAddonName, EVENT_ADD_ON_LOADED)
  328.  
  329.     --Register for Guild Bank opened & closed
  330.     --EVENT_MANAGER:RegisterForEvent(addonVars.gAddonName, EVENT_OPEN_GUILD_BANK, FCOGuildBankQuickSelect_Open_Guild_Bank)
  331.     EVENT_MANAGER:RegisterForEvent(addonVars.gAddonName, EVENT_CLOSE_GUILD_BANK, FCOGuildBankQuickSelect_Close_Guild_Bank)
  332.  
  333.     addonVars.gAddonLoaded = true
  334. end
  335.  
  336. -- Register the event "addon loaded" for this addon
  337. local function FCOGuildBankQuickSelect_Initialized()
  338.     EVENT_MANAGER:RegisterForEvent(addonVars.gAddonName, EVENT_ADD_ON_LOADED, FCOGuildBankQuickSelect_Loaded)
  339.     --Register for the zone change/player ready event
  340.     EVENT_MANAGER:RegisterForEvent(addonVars.gAddonName, EVENT_PLAYER_ACTIVATED, FCOGuildBankQuickSelect_Player_Activated)
  341. end
  342.  
  343.  
  344. --------------------------------------------------------------------------------
  345. --- Call the start function for this addon to register events etc.
  346. --------------------------------------------------------------------------------
  347. FCOGuildBankQuickSelect_Initialized()

Last edited by Baertram : 04/02/15 at 11:41 AM.
  Reply With Quote