WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Changing hotkey text (https://www.wowinterface.com/forums/showthread.php?t=39329)

stako 03-18-11 12:29 PM

Changing hotkey text
 
So, here's what I have:


And here's what I want:


Notice the mousewheel hotkey text. Looks much prettier in the second image, right?

So I've secure hooked ActionButton_Update and am running a series of if statements to check if the hotkey text equals, for example, "Mouse Wheel Down" and changing it to what I want.

My problem is that as soon as I enter combat, the hotkey text switches back to it's original form.

I don't want to disable the SetText function (ActionButton1HotKey.SetText = function() end) because then the text won't change if I change my keybinds.

Wat do?

Crissa 03-18-11 03:43 PM

I think you'll need to have it create a second list of names, which is truncated. I'm not sure the button style includes a specific truncation method.

-Crissa

Akkorian 03-18-11 04:03 PM

Hi Stako,

The easiest thing to do would probably be to hook the GetBindingText function:

Lua Code:
  1. local old_GetBindingText = GetBindingText
  2. function GetBindingText( name, prefix, returnAbbr )
  3.     local text = old_GetBindingText( name, prefix, returnAbbr )
  4.     if returnAbbr then
  5.         -- Remove dashes
  6.         text = text:replace( "-", "" )
  7.         -- Replace “Mouse Wheel Down”
  8.         text = text:replace( "Mouse Wheel Down", "WD" )
  9.         -- Replace “Mouse Wheel Up”
  10.         text = text:replace( "Mouse Wheel Up", "WU" )
  11.     end
  12.     return text
  13. end

I’m not sure if “Mouse Wheel Down” and “Mouse Wheel Up” are exactly what it’s showing since the text is cut off in your screenshot, so change those if they’re not right, and add anything else that you want to change.

stako 05-01-11 06:22 PM

Bumping because GetBindingText is protected now. Can't figure out what to do.

Edit: Secure hook ActionButton_UpdateHotkeys and do something like this:
Code:

local function styleHotKeys(self, actionButtonType)
        local ho = _G[self:GetName().."HotKey"]
        local text = ho:GetText()
        text = text:replace( "-", "" )
        text = text:replace( "Mouse Wheel Down", "WD" )
        text = text:replace( "Mouse Wheel Up", "WU" )
        ho:SetText(text)
end


jasje 06-18-11 11:39 AM

Taken from Tukui so you can see whats going on
Lua Code:
  1. local function updatehotkey(self, actionButtonType)
  2.     local hotkey = _G[self:GetName() .. 'HotKey']
  3.     local text = hotkey:GetText()
  4.    
  5.     text = replace(text, '(s%-)', 'S')
  6.     text = replace(text, '(a%-)', 'A')
  7.     text = replace(text, '(c%-)', 'C')
  8.     text = replace(text, '(Mouse Button )', 'M')
  9.     text = replace(text, '(Mouse Wheel Up)', 'MU')
  10.     text = replace(text, '(Mouse Wheel Down)', 'MD')
  11.     text = replace(text, '(Middle Mouse)', 'M3')
  12.     text = replace(text, '(Num Pad )', 'N')
  13.     text = replace(text, '(Page Up)', 'PU')
  14.     text = replace(text, '(Page Down)', 'PD')
  15.     text = replace(text, '(Spacebar)', 'SpB')
  16.     text = replace(text, '(Insert)', 'Ins')
  17.     text = replace(text, '(Home)', 'Hm')
  18.     text = replace(text, '(Delete)', 'Del')
  19.    
  20.     if hotkey:GetText() == _G['RANGE_INDICATOR'] then
  21.         hotkey:SetText('')
  22.     else
  23.         hotkey:SetText(text)
  24.     end
  25. end

sorrykari 01-01-12 11:43 PM

sorry to super necro, but i was looking to do exactly this just now and found the above code to be incomplete. replace is the local so you also need:
Code:

local replace = string.gsub

laukond 01-30-12 09:22 AM

So I copy pasted the above code and added
Code:

local replace = string.gsub
below
Code:

local text = hotkey:GetText()
but nothing happens.. at all. :-)
help! :D

Phanx 01-30-12 07:22 PM

The code posted does nothing by itself. As per stako's post, you need to run that function using a secure hook on the ActionButton_UpdateHotkeys function:

Lua Code:
  1. local replace = string.gsub
  2.  
  3. local function updatehotkey(self, actionButtonType)
  4.     local hotkey = _G[self:GetName() .. 'HotKey']
  5.     local text = hotkey:GetText()
  6.    
  7.     text = replace(text, 's%-', 'S')
  8.     text = replace(text, 'a%-', 'A')
  9.     text = replace(text, 'c%-', 'C')
  10.     text = replace(text, 'Mouse Button ', 'M')
  11.     text = replace(text, 'Mouse Wheel Up', 'MU')
  12.     text = replace(text, 'Mouse Wheel Down', 'MD')
  13.     text = replace(text, 'Middle Mouse', 'M3')
  14.     text = replace(text, 'Num Pad ', 'N')
  15.     text = replace(text, 'Page Up', 'PU')
  16.     text = replace(text, 'Page Down', 'PD')
  17.     text = replace(text, 'Spacebar', 'SpB')
  18.     text = replace(text, 'Insert', 'Ins')
  19.     text = replace(text, 'Home', 'Hm')
  20.     text = replace(text, 'Delete', 'Del')
  21.    
  22.     if hotkey:GetText() == RANGE_INDICATOR then
  23.         hotkey:SetText('')
  24.     else
  25.         hotkey:SetText(text)
  26.     end
  27. end
  28.  
  29. hooksecurefunc("ActionButton_UpdateHotkeys", updatehotkey)

This will cause your function to be attached to the end of the default UI's hotkey update function, so every time that runs, your function is run immediately afterward. The "secure" part means that your function doesn't interact with the default UI's function, so it doesn't break anything or cause taint.

A few more technical observations:

- You only need to upvalue string.gsub once, not every time the function runs, so I moved that local declaration outside of the function.

- It's not necessary to wrap the search patterns in parentheses; those are only needed (or useful) if you only wish to match a subset of the given pattern. Since that's not the case here, they're not needed.

- It's not necessary to look up RANGE_INDICATOR in the _G table explicitly; this is assumed, and specifying _G adds another global lookup for no reason. Just compare to RANGE_INDICATOR directly. I also changed this in the above code.

veoj 11-11-16 02:57 AM

How does this work in 7.1?
 
Sorry for the ancient threadomancy but this mechanism has been solid all the way through 7.0.3 so no one has had to ask any new questions.

Unfortunately it's broken in 7.1

my code:
Code:

        local hotkey = _G[button:GetName()..'HotKey']

        local hotkeyText = nil
        if hotkey then
                hotkeyText = hotkey:GetText()
        end
        if hotkeyText then
                hotkeyText = hotkeyText:gsub("(s%-)", "S")
                hotkeyText = hotkeyText:gsub("(a%-)",  "A")
                hotkeyText = hotkeyText:gsub("(c%-)",  "C")
                hotkeyText = hotkeyText:gsub('BUTTON',  "B")
                hotkeyText = hotkeyText:gsub('MOUSEWHEELUP', "WU")
                hotkeyText = hotkeyText:gsub('MOUSEWHEELDOWN', "WD")
                hotkeyText = hotkeyText:gsub('NUMPAD',  "N")
                hotkeyText = hotkeyText:gsub('PAGEUP', "PgU")
                hotkeyText = hotkeyText:gsub('PAGEDOWN', "PgD")
                hotkeyText = hotkeyText:gsub('SPACE', "SP")
                hotkeyText = hotkeyText:gsub('INSERT', "INS")
                hotkeyText = hotkeyText:gsub('HOME', "HM")
                hotkeyText = hotkeyText:gsub('DELETE', "DEL")
                hotkeyText = hotkeyText:gsub('NMULTIPLY', "N*")
                hotkeyText = hotkeyText:gsub('NMINUS', "N-")
                hotkeyText = hotkeyText:gsub('NPLUS', "N+")
                hotkey:SetText(hotkeyText)
        end

Problem with this code is that the hotkeyText always comes back with "1","2" etc. but not the modifiers on the text.

Note: This mechanism is used by several actionbar modifying addons including Moncai Hotkeys and others which I've seen are all getting reports of being broken in 7.1

I've been looking at the FrameXML code for ActionButton but using the methods in there to resolve the text also does not yield the modifier text for me...

Code:

        local hotkey = _G[button:GetName()..'HotKey']

        local hotkeyText = nil
        if hotkey then
                -- Take a leaf from the FrameXML/ActionButton.lua book.
                local id = button.buttonIndex or button:GetID()
                local key = GetBindingKey("ACTIONBUTTON"..id) or
                                        GetBindingKey("MULTICASTACTIONBUTTON"..id) or
                                        GetBindingKey("CLICK "..button:GetName()..":LeftButton");
                if key then
                        hotkeyText = GetBindingText(key, 1);
                end
                --hotkeyText = hotkey:GetText()
        end
        if hotkeyText then
                hotkeyText = hotkeyText:gsub("(s%-)", "S")
                hotkeyText = hotkeyText:gsub("(a%-)",  "A")
                hotkeyText = hotkeyText:gsub("(c%-)",  "C")
                hotkeyText = hotkeyText:gsub('BUTTON',  "B")
                hotkeyText = hotkeyText:gsub('MOUSEWHEELUP', "WU")
                hotkeyText = hotkeyText:gsub('MOUSEWHEELDOWN', "WD")
                hotkeyText = hotkeyText:gsub('NUMPAD',  "N")
                hotkeyText = hotkeyText:gsub('PAGEUP', "PgU")
                hotkeyText = hotkeyText:gsub('PAGEDOWN', "PgD")
                hotkeyText = hotkeyText:gsub('SPACE', "SP")
                hotkeyText = hotkeyText:gsub('INSERT', "INS")
                hotkeyText = hotkeyText:gsub('HOME', "HM")
                hotkeyText = hotkeyText:gsub('DELETE', "DEL")
                hotkeyText = hotkeyText:gsub('NMULTIPLY', "N*")
                hotkeyText = hotkeyText:gsub('NMINUS', "N-")
                hotkeyText = hotkeyText:gsub('NPLUS', "N+")
                hotkey:SetText(hotkeyText)
        end

Same result... hotkeyText comes back as 1,2,3 etc. but not ALT-1, ALT-2 etc as I would expect given that I can see that text on my action buttons.

Confused as heck and scratching my head...

Anyone have any insight?

Many thanks in advance.

joev

lightspark 11-11-16 04:38 AM

Don't use hardcoded strings like "SPACE", "INSERT", "(s%-)", use global variables instead, e.g. SHIFT_KEY_TEXT, KEY_SPACE, KEY_INSERT, cuz different languages use different names for said keys :p

I do something like this and it works fine...

Lua Code:
  1. local name = button:GetName()
  2. local bType = button.buttonType
  3.  
  4. if not bType then
  5.     if name and not string.match(name, "Stance") then
  6.         if string.match(name, "PetAction") then
  7.             bType = "BONUSACTIONBUTTON"
  8.         else
  9.             bType = "ACTIONBUTTON"
  10.         end
  11.     end
  12. end
  13.  
  14. local text = bType and GetBindingText(GetBindingKey(bType..button:GetID())) or ""
  15.  
  16. if text and text ~= "" then
  17.     text = string.gsub(text, SHIFT_KEY_TEXT, "S")
  18.     text = string.gsub(text, CTRL_KEY_TEXT, "C")
  19.     text = string.gsub(text, ALT_KEY_TEXT, "A")
  20.     text = string.gsub(text, KEY_BUTTON1, "LM")
  21.     text = string.gsub(text, KEY_BUTTON2, "RM")
  22.     text = string.gsub(text, KEY_BUTTON3, "MM")
  23.     text = string.gsub(text, KEY_MOUSEWHEELDOWN, "MWD")
  24.     text = string.gsub(text, KEY_MOUSEWHEELUP, "MWU")
  25.     text = string.gsub(text, KEY_SPACE, "SP")
  26.     text = string.gsub(text, "%-", "")
  27. end

P.S. high lvl necromancy :p

veoj 11-11-16 07:28 AM

Quote:

Originally Posted by lightspark (Post 320754)
Don't use hardcoded strings like "SPACE", "INSERT", "(s%-)", use global variables instead, e.g. SHIFT_KEY_TEXT, KEY_SPACE, KEY_INSERT, cuz different languages use different names for said keys :p

Agreed I was in the process of refactoring those :)

I have tried your approach and it's still not yielding the expected results.

My ABs are set up by LibActionButton which hasn't had a 7.1 update so I'll have a root around in the code of that because I suspect it's borked by something changed in 7.1 (though looking at the FrameXML/ActionButton code I'm still stumped as to why because it doesn't look manifestly different to 7.0.3)

Thanks for the snippet though. I've used it :)

Quote:

Originally Posted by lightspark (Post 320754)
P.S. high lvl necromancy :p

Ayuh. I did call it out though :)

Phanx 11-13-16 10:20 AM

Can you post your entire, actual code please? If you're hooking ActionButton_UpdateHotkeys, you do not need to manually look up the hotkey for the button -- just look at and modify the text the original Blizzard function set on the button. hooksecurefunc means that the original function runs first, and then your function runs immediately afterward.

If you're not hooking ActionButton_UpdateHotkeys (for example, if you're trying to display a hotkey on a custom button) then your problem isn't related to this thread, and should be moved to a new thread.


All times are GMT -6. The time now is 08:25 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI