diff 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
line wrap: on
line diff
--- a/libs/ReBound-1.0/ReBound-1.0.lua	Thu Mar 22 21:08:43 2007 +0000
+++ b/libs/ReBound-1.0/ReBound-1.0.lua	Fri Mar 23 19:28:30 2007 +0000
@@ -1,27 +1,50 @@
 --[[
 Name: ReBound-1.0
-Revision: $Rev: 2 $
+Revision: $Rev: 3 $
 Author: Flick
 Website: 
 Documentation: 
 SVN: 
 Description: A library to assist with click-binding
 License: MIT
-Dependencies: AceLibrary, AceEvent-2.0, AceLocale-2.2, AceOO-2.0
+Dependencies: AceLibrary, AceEvent-2.0, AceLocale-2.2, AceOO-2.0, AceConsole-2.0
 ]]
 
 
-local version_major, version_minor = "ReBound-1.0", "$Rev: 2 $"
+local version_major, version_minor = "ReBound-1.0", "$Rev: 3 $"
 
 if not AceLibrary then error(version_major .. " requires AceLibrary.") end
 if not AceLibrary:IsNewVersion(version_major, version_minor) then return end
-if not AceLibrary:HasInstance("AceEvent-2.0") then error(version_major .. " requires AceEvent-2.0.") end
-if not AceLibrary:HasInstance("AceLocale-2.2") then error(version_major .. " requires AceLocale-2.2.") end
-if not AceLibrary:HasInstance("AceOO-2.0") then error(version_major .. " requires AceOO-2.0.") end
+for _, lib in pairs({ "AceEvent-2.0", "AceLocale-2.2", "AceOO-2.0", "AceConsole-2.0" }) do
+  if not AceLibrary:HasInstance(lib) then error(version_major .. " requires "..lib) end
+end
 
 local L = AceLibrary("AceLocale-2.2"):new("ReBound")
 
+-- localization
+L:RegisterTranslations( "enUS", function()
+  return {
+    ["none"] = true,
+    ["Click"] = true,
+    ["Right-click"] = true,
+    ["Shift-click"] = true,
+    ["Shift-right-click"] = true,
+    ["Right Click"] = true,
+    ["to select for binding"] = true,
+    ["to select for alternate (right-click) binding"] = true,
+    ["to clear binding"] = true,
+    ["to clear alternate (right-click) binding"] = true,
+    ["Press a key to assign binding"] = true,
+    ["is now unbound"] = true,
+  }
+end )
+
+
+
+local kbValidate = AceLibrary("AceConsole-2.0").keybindingValidateFunc
+
 local colorGreen = "|cff00ff00"
+local colorOrange = "|cffffcc00"
 local colorOff = "|r"
 
 local mouseButtonConvert = {
@@ -32,20 +55,42 @@
   Button5 = "BUTTON5"
 }
 
---local BINDING_PENDING = { }  -- a constant
-
--- localization
-L:RegisterTranslations( "enUS", function()
-  return {
-    ["none"] = true,
-    ["Right-click"] = true,
-    ["Right Click"] = true,
-    ["Click to select for binding"] = true,
-    ["Shift-click to clear binding"] = true,
-    ["Press a key to assign binding"] = true,
-    ["is now unbound"] = true,
-  }
-end )
+-- TODO: localize
+local keybindAbbreviations = {
+  [KEY_BACKSPACE]      = "BkSp",
+  [KEY_BUTTON3]        = "M-3",
+  [KEY_BUTTON4]        = "M-4",
+  [KEY_BUTTON5]        = "M-5",
+  [KEY_DOWN]           = "Down",
+  [KEY_ESCAPE]         = "Esc",
+  [KEY_INSERT]         = "Ins",
+  [KEY_LEFT]           = "Left",
+  [KEY_MOUSEWHEELDOWN] = "M-Down",
+  [KEY_MOUSEWHEELUP]   = "M-Up",
+  [KEY_NUMLOCK]        = "NumLk",
+  [KEY_NUMPAD0]        = "Np0",
+  [KEY_NUMPAD1]        = "Np1",
+  [KEY_NUMPAD2]        = "Np2",
+  [KEY_NUMPAD3]        = "Np3",
+  [KEY_NUMPAD4]        = "Np4",
+  [KEY_NUMPAD5]        = "Np5",
+  [KEY_NUMPAD6]        = "Np6",
+  [KEY_NUMPAD7]        = "Np7",
+  [KEY_NUMPAD8]        = "Np8",
+  [KEY_NUMPAD9]        = "Np9",
+  [KEY_NUMPADDECIMAL]  = "Np.",
+  [KEY_NUMPADDIVIDE]   = "Np/",
+  [KEY_NUMPADMINUS]    = "Np-",
+  [KEY_NUMPADMULTIPLY] = "Np*",
+  [KEY_NUMPADPLUS]     = "Np+",
+  [KEY_PAGEDOWN]       = "PgDn",
+  [KEY_PAGEUP]         = "PgUp",
+  [KEY_PRINTSCREEN]    = "PrScr",
+  [KEY_RIGHT]          = "Right",
+  [KEY_SCROLLLOCK]     = "ScrLk",
+  [KEY_SPACE]          = "Sp",
+  [KEY_UP]             = "Up",
+}
 
 
 local ReBound = AceLibrary("AceOO-2.0").Class("AceEvent-2.0")
@@ -71,16 +116,12 @@
   Calls to new() which share ids will return an existing object with that id, similar
   to AceLocale-2.2.
 ]]
+ReBound.instances = { }
 local super_new = ReBound.new
 function ReBound:new( id )
-  self.instances = self.instances or { }
-  if self.instances[id] then
-    return self.instances[id]
-  else
-    local i = super_new(self,id)
-    self.instances[id] = i
-    return i
-  end
+  local instances = self.instances
+  instances[id] = instances[id] or super_new(self,id)
+  return instances[id]
 end
 
 
@@ -91,7 +132,7 @@
     id : the ID that will be provided in events. This can be absolutely 
          anything, but a string is recommended.
 ]]
-local super_init = ReBound.prototype.init
+local super_init = ReBound.super.prototype.init
 function ReBound.prototype:init( id )
   super_init(self)
 
@@ -109,6 +150,7 @@
   key: A string representation of a key, suitable for passing to SetBinding.
   target: The frame with an OnClick handler to which the click-binding should be attached
   [button]: The mouse button to emulate. Default is "LeftButton".
+  [silent]: boolean - whether to suppress messages about keys being unbound.
 
   Returns:
   nothing.
@@ -119,6 +161,8 @@
 function ReBound.prototype:SetBinding( key, target, button )
   if not key then error("ReBound:SetBinding() requires a key argument.") end
   if not target then error("ReBound:SetBinding() requires a binding target argument") end
+  if key and not kbValidate(key) then error("ReBound:SetBinding(): invalid key code "..tostring(key)) end
+
   button = button or "LeftButton"
 
   -- prevent setting a binding that's already set
@@ -131,7 +175,7 @@
 
   -- clear the old binding for the key. This isn't strictly necessary, but it allows us to collect
   -- notification of the unbinding in one place (ClearBinding).
-  self:ClearBinding( key )
+  self:ClearBinding( key, nil, nil, silent )
 
   -- clear the old binding for the target and button (silently)
   self:ClearBinding( nil, target, button, true )
@@ -214,6 +258,33 @@
 
 
 --[[
+  Gets the localized, abbreviated key cap text for the primary binding on a target. 
+  Abbreviations are more aggressive than the standard Blizzard abbreviation
+  (which just shortens modifier keys)
+
+  Arguments:
+  target: target frame to query
+  [abbrev]: boolean flag to abbreviate the result
+  [button]: mouse button to emulate ("LeftButton", "RightButton")
+
+  Returns:
+  abbreviated, localized key label
+]]
+function ReBound.prototype:GetBindingText( target, abbrev, button )
+  local key = self:GetBinding(target,button)
+  local txt = key and GetBindingText(key, "KEY_", abbrev and 1) or ""
+
+  if txt and abbrev then
+    -- further abbreviate some key names
+    txt = string.gsub(txt, "[^%-]+$", keybindAbbreviations)
+    -- the above does not handle "num pad -"
+    txt = string.gsub(txt, KEY_NUMPADMINUS, keybindAbbreviations)
+  end
+  return txt
+end
+
+
+--[[
   Publishes permanent binding notification events.
 ]]
 local function PublishBindings(self)
@@ -308,7 +379,7 @@
 --[[
   Shows all the registered click binding frames.
 ]]
-function ReBound.prototype:ShowFrames()
+function ReBound.prototype:ShowRegisteredFrames()
   if InCombatLockdown() then
     -- can't set bindings while in combat, so don't bother showing them
     UIErrorsFrame:AddMessage(ERR_NOT_IN_COMBAT)
@@ -323,7 +394,7 @@
 --[[
   Hides all the registered click binding frames.
 ]]
-function ReBound.prototype:HideFrames()
+function ReBound.prototype:HideRegisteredFrames()
   -- because these frames aren't protected, there's no restriction 
   -- on hiding them while in combat.
   for _, f in pairs(self.frames) do
@@ -345,8 +416,11 @@
     GameTooltip:AddDoubleLine(L["Right-click"]..":", colorGreen.."("..binding2..")"..colorOff)
   end
   -- line 3: instructions
-  GameTooltip:AddLine(L["Click to select for binding"])
-  GameTooltip:AddLine(L["Shift-click to clear binding"])
+  GameTooltip:AddLine(colorGreen..L["Click"]..colorOff.." "..L["to select for binding"])
+  GameTooltip:AddLine(colorGreen..L["Shift-click"]..colorOff.." "..L["to clear binding"])
+  GameTooltip:AddLine("")
+  GameTooltip:AddLine(colorOrange..L["Right-click"]..colorOff.." "..L["to select for alternate (right-click) binding"])
+  GameTooltip:AddLine(colorOrange..L["Shift-right-click"]..colorOff.." "..L["to clear alternate (right-click) binding"])
   GameTooltip:Show()
 end