comparison libs/ReBound-1.0/ReBound-1.0.lua @ 17:639282f3a0e0

More cleanup of main.lua, ReBound-1.0.lua
author Flick <flickerstreak@gmail.com>
date Fri, 23 Mar 2007 19:28:30 +0000
parents 2735edcf9ab7
children
comparison
equal deleted inserted replaced
16:832aab6fed49 17:639282f3a0e0
1 --[[ 1 --[[
2 Name: ReBound-1.0 2 Name: ReBound-1.0
3 Revision: $Rev: 2 $ 3 Revision: $Rev: 3 $
4 Author: Flick 4 Author: Flick
5 Website: 5 Website:
6 Documentation: 6 Documentation:
7 SVN: 7 SVN:
8 Description: A library to assist with click-binding 8 Description: A library to assist with click-binding
9 License: MIT 9 License: MIT
10 Dependencies: AceLibrary, AceEvent-2.0, AceLocale-2.2, AceOO-2.0 10 Dependencies: AceLibrary, AceEvent-2.0, AceLocale-2.2, AceOO-2.0, AceConsole-2.0
11 ]] 11 ]]
12 12
13 13
14 local version_major, version_minor = "ReBound-1.0", "$Rev: 2 $" 14 local version_major, version_minor = "ReBound-1.0", "$Rev: 3 $"
15 15
16 if not AceLibrary then error(version_major .. " requires AceLibrary.") end 16 if not AceLibrary then error(version_major .. " requires AceLibrary.") end
17 if not AceLibrary:IsNewVersion(version_major, version_minor) then return end 17 if not AceLibrary:IsNewVersion(version_major, version_minor) then return end
18 if not AceLibrary:HasInstance("AceEvent-2.0") then error(version_major .. " requires AceEvent-2.0.") end 18 for _, lib in pairs({ "AceEvent-2.0", "AceLocale-2.2", "AceOO-2.0", "AceConsole-2.0" }) do
19 if not AceLibrary:HasInstance("AceLocale-2.2") then error(version_major .. " requires AceLocale-2.2.") end 19 if not AceLibrary:HasInstance(lib) then error(version_major .. " requires "..lib) end
20 if not AceLibrary:HasInstance("AceOO-2.0") then error(version_major .. " requires AceOO-2.0.") end 20 end
21 21
22 local L = AceLibrary("AceLocale-2.2"):new("ReBound") 22 local L = AceLibrary("AceLocale-2.2"):new("ReBound")
23 23
24 -- localization
25 L:RegisterTranslations( "enUS", function()
26 return {
27 ["none"] = true,
28 ["Click"] = true,
29 ["Right-click"] = true,
30 ["Shift-click"] = true,
31 ["Shift-right-click"] = true,
32 ["Right Click"] = true,
33 ["to select for binding"] = true,
34 ["to select for alternate (right-click) binding"] = true,
35 ["to clear binding"] = true,
36 ["to clear alternate (right-click) binding"] = true,
37 ["Press a key to assign binding"] = true,
38 ["is now unbound"] = true,
39 }
40 end )
41
42
43
44 local kbValidate = AceLibrary("AceConsole-2.0").keybindingValidateFunc
45
24 local colorGreen = "|cff00ff00" 46 local colorGreen = "|cff00ff00"
47 local colorOrange = "|cffffcc00"
25 local colorOff = "|r" 48 local colorOff = "|r"
26 49
27 local mouseButtonConvert = { 50 local mouseButtonConvert = {
28 LeftButton = "BUTTON1", 51 LeftButton = "BUTTON1",
29 RightButton = "BUTTON2", 52 RightButton = "BUTTON2",
30 MiddleButton = "BUTTON3", 53 MiddleButton = "BUTTON3",
31 Button4 = "BUTTON4", 54 Button4 = "BUTTON4",
32 Button5 = "BUTTON5" 55 Button5 = "BUTTON5"
33 } 56 }
34 57
35 --local BINDING_PENDING = { } -- a constant 58 -- TODO: localize
36 59 local keybindAbbreviations = {
37 -- localization 60 [KEY_BACKSPACE] = "BkSp",
38 L:RegisterTranslations( "enUS", function() 61 [KEY_BUTTON3] = "M-3",
39 return { 62 [KEY_BUTTON4] = "M-4",
40 ["none"] = true, 63 [KEY_BUTTON5] = "M-5",
41 ["Right-click"] = true, 64 [KEY_DOWN] = "Down",
42 ["Right Click"] = true, 65 [KEY_ESCAPE] = "Esc",
43 ["Click to select for binding"] = true, 66 [KEY_INSERT] = "Ins",
44 ["Shift-click to clear binding"] = true, 67 [KEY_LEFT] = "Left",
45 ["Press a key to assign binding"] = true, 68 [KEY_MOUSEWHEELDOWN] = "M-Down",
46 ["is now unbound"] = true, 69 [KEY_MOUSEWHEELUP] = "M-Up",
47 } 70 [KEY_NUMLOCK] = "NumLk",
48 end ) 71 [KEY_NUMPAD0] = "Np0",
72 [KEY_NUMPAD1] = "Np1",
73 [KEY_NUMPAD2] = "Np2",
74 [KEY_NUMPAD3] = "Np3",
75 [KEY_NUMPAD4] = "Np4",
76 [KEY_NUMPAD5] = "Np5",
77 [KEY_NUMPAD6] = "Np6",
78 [KEY_NUMPAD7] = "Np7",
79 [KEY_NUMPAD8] = "Np8",
80 [KEY_NUMPAD9] = "Np9",
81 [KEY_NUMPADDECIMAL] = "Np.",
82 [KEY_NUMPADDIVIDE] = "Np/",
83 [KEY_NUMPADMINUS] = "Np-",
84 [KEY_NUMPADMULTIPLY] = "Np*",
85 [KEY_NUMPADPLUS] = "Np+",
86 [KEY_PAGEDOWN] = "PgDn",
87 [KEY_PAGEUP] = "PgUp",
88 [KEY_PRINTSCREEN] = "PrScr",
89 [KEY_RIGHT] = "Right",
90 [KEY_SCROLLLOCK] = "ScrLk",
91 [KEY_SPACE] = "Sp",
92 [KEY_UP] = "Up",
93 }
49 94
50 95
51 local ReBound = AceLibrary("AceOO-2.0").Class("AceEvent-2.0") 96 local ReBound = AceLibrary("AceOO-2.0").Class("AceEvent-2.0")
52 97
53 --[[ 98 --[[
69 114
70 --[[ 115 --[[
71 Calls to new() which share ids will return an existing object with that id, similar 116 Calls to new() which share ids will return an existing object with that id, similar
72 to AceLocale-2.2. 117 to AceLocale-2.2.
73 ]] 118 ]]
119 ReBound.instances = { }
74 local super_new = ReBound.new 120 local super_new = ReBound.new
75 function ReBound:new( id ) 121 function ReBound:new( id )
76 self.instances = self.instances or { } 122 local instances = self.instances
77 if self.instances[id] then 123 instances[id] = instances[id] or super_new(self,id)
78 return self.instances[id] 124 return instances[id]
79 else
80 local i = super_new(self,id)
81 self.instances[id] = i
82 return i
83 end
84 end 125 end
85 126
86 127
87 --[[ 128 --[[
88 Class object constructor 129 Class object constructor
89 130
90 arguments: 131 arguments:
91 id : the ID that will be provided in events. This can be absolutely 132 id : the ID that will be provided in events. This can be absolutely
92 anything, but a string is recommended. 133 anything, but a string is recommended.
93 ]] 134 ]]
94 local super_init = ReBound.prototype.init 135 local super_init = ReBound.super.prototype.init
95 function ReBound.prototype:init( id ) 136 function ReBound.prototype:init( id )
96 super_init(self) 137 super_init(self)
97 138
98 self.id = id 139 self.id = id
99 self.frames = { } 140 self.frames = { }
107 --[[ 148 --[[
108 Arguments: 149 Arguments:
109 key: A string representation of a key, suitable for passing to SetBinding. 150 key: A string representation of a key, suitable for passing to SetBinding.
110 target: The frame with an OnClick handler to which the click-binding should be attached 151 target: The frame with an OnClick handler to which the click-binding should be attached
111 [button]: The mouse button to emulate. Default is "LeftButton". 152 [button]: The mouse button to emulate. Default is "LeftButton".
153 [silent]: boolean - whether to suppress messages about keys being unbound.
112 154
113 Returns: 155 Returns:
114 nothing. 156 nothing.
115 157
116 Notes: 158 Notes:
117 This does not save the bindings. 159 This does not save the bindings.
118 ]] 160 ]]
119 function ReBound.prototype:SetBinding( key, target, button ) 161 function ReBound.prototype:SetBinding( key, target, button )
120 if not key then error("ReBound:SetBinding() requires a key argument.") end 162 if not key then error("ReBound:SetBinding() requires a key argument.") end
121 if not target then error("ReBound:SetBinding() requires a binding target argument") end 163 if not target then error("ReBound:SetBinding() requires a binding target argument") end
164 if key and not kbValidate(key) then error("ReBound:SetBinding(): invalid key code "..tostring(key)) end
165
122 button = button or "LeftButton" 166 button = button or "LeftButton"
123 167
124 -- prevent setting a binding that's already set 168 -- prevent setting a binding that's already set
125 local current = { self:GetBinding(target,button) } 169 local current = { self:GetBinding(target,button) }
126 for _, b in pairs(current) do 170 for _, b in pairs(current) do
129 end 173 end
130 end 174 end
131 175
132 -- clear the old binding for the key. This isn't strictly necessary, but it allows us to collect 176 -- clear the old binding for the key. This isn't strictly necessary, but it allows us to collect
133 -- notification of the unbinding in one place (ClearBinding). 177 -- notification of the unbinding in one place (ClearBinding).
134 self:ClearBinding( key ) 178 self:ClearBinding( key, nil, nil, silent )
135 179
136 -- clear the old binding for the target and button (silently) 180 -- clear the old binding for the target and button (silently)
137 self:ClearBinding( nil, target, button, true ) 181 self:ClearBinding( nil, target, button, true )
138 182
139 -- set the new binding 183 -- set the new binding
212 return GetBindingKey("CLICK "..target:GetName()..":"..button) 256 return GetBindingKey("CLICK "..target:GetName()..":"..button)
213 end 257 end
214 258
215 259
216 --[[ 260 --[[
261 Gets the localized, abbreviated key cap text for the primary binding on a target.
262 Abbreviations are more aggressive than the standard Blizzard abbreviation
263 (which just shortens modifier keys)
264
265 Arguments:
266 target: target frame to query
267 [abbrev]: boolean flag to abbreviate the result
268 [button]: mouse button to emulate ("LeftButton", "RightButton")
269
270 Returns:
271 abbreviated, localized key label
272 ]]
273 function ReBound.prototype:GetBindingText( target, abbrev, button )
274 local key = self:GetBinding(target,button)
275 local txt = key and GetBindingText(key, "KEY_", abbrev and 1) or ""
276
277 if txt and abbrev then
278 -- further abbreviate some key names
279 txt = string.gsub(txt, "[^%-]+$", keybindAbbreviations)
280 -- the above does not handle "num pad -"
281 txt = string.gsub(txt, KEY_NUMPADMINUS, keybindAbbreviations)
282 end
283 return txt
284 end
285
286
287 --[[
217 Publishes permanent binding notification events. 288 Publishes permanent binding notification events.
218 ]] 289 ]]
219 local function PublishBindings(self) 290 local function PublishBindings(self)
220 for _, key in ipairs(self.pending) do 291 for _, key in ipairs(self.pending) do
221 local action = GetBindingAction(key) 292 local action = GetBindingAction(key)
306 377
307 378
308 --[[ 379 --[[
309 Shows all the registered click binding frames. 380 Shows all the registered click binding frames.
310 ]] 381 ]]
311 function ReBound.prototype:ShowFrames() 382 function ReBound.prototype:ShowRegisteredFrames()
312 if InCombatLockdown() then 383 if InCombatLockdown() then
313 -- can't set bindings while in combat, so don't bother showing them 384 -- can't set bindings while in combat, so don't bother showing them
314 UIErrorsFrame:AddMessage(ERR_NOT_IN_COMBAT) 385 UIErrorsFrame:AddMessage(ERR_NOT_IN_COMBAT)
315 else 386 else
316 for _, f in pairs(self.frames) do 387 for _, f in pairs(self.frames) do
321 392
322 393
323 --[[ 394 --[[
324 Hides all the registered click binding frames. 395 Hides all the registered click binding frames.
325 ]] 396 ]]
326 function ReBound.prototype:HideFrames() 397 function ReBound.prototype:HideRegisteredFrames()
327 -- because these frames aren't protected, there's no restriction 398 -- because these frames aren't protected, there's no restriction
328 -- on hiding them while in combat. 399 -- on hiding them while in combat.
329 for _, f in pairs(self.frames) do 400 for _, f in pairs(self.frames) do
330 f:Hide() 401 f:Hide()
331 end 402 end
343 local binding2 = self.ReBound:GetBinding(target,"RightButton") 414 local binding2 = self.ReBound:GetBinding(target,"RightButton")
344 if binding2 then 415 if binding2 then
345 GameTooltip:AddDoubleLine(L["Right-click"]..":", colorGreen.."("..binding2..")"..colorOff) 416 GameTooltip:AddDoubleLine(L["Right-click"]..":", colorGreen.."("..binding2..")"..colorOff)
346 end 417 end
347 -- line 3: instructions 418 -- line 3: instructions
348 GameTooltip:AddLine(L["Click to select for binding"]) 419 GameTooltip:AddLine(colorGreen..L["Click"]..colorOff.." "..L["to select for binding"])
349 GameTooltip:AddLine(L["Shift-click to clear binding"]) 420 GameTooltip:AddLine(colorGreen..L["Shift-click"]..colorOff.." "..L["to clear binding"])
421 GameTooltip:AddLine("")
422 GameTooltip:AddLine(colorOrange..L["Right-click"]..colorOff.." "..L["to select for alternate (right-click) binding"])
423 GameTooltip:AddLine(colorOrange..L["Shift-right-click"]..colorOff.." "..L["to clear alternate (right-click) binding"])
350 GameTooltip:Show() 424 GameTooltip:Show()
351 end 425 end
352 426
353 local function ShowTooltip2( self ) 427 local function ShowTooltip2( self )
354 if GameTooltip:IsOwned(self) then 428 if GameTooltip:IsOwned(self) then