Quantcast

Implemented target unit/show menu actions and binding captures

James Whitehead II [10-04-10 - 21:27]
Implemented target unit/show menu actions and binding captures
Filename
BindConfig.lua
Clique.xml
Utils.lua
diff --git a/BindConfig.lua b/BindConfig.lua
index 5b7aad2..a35cbc1 100755
--- a/BindConfig.lua
+++ b/BindConfig.lua
@@ -37,9 +37,8 @@ function CliqueConfig:SetupGUI()
     self.dialog = _G["CliqueDialog"]
     self.dialog.title = _G["CliqueDialogTitleText"]

-    self.dialog.title:SetText(L["Clique: Set binding"])
+    self.dialog.title:SetText(L["Set binding"])
     self.dialog.button_accept:SetText(L["Accept"])
-    self.dialog.bindText:SetText(L["Alt-Control-Shift-F"])

     self.dialog.button_binding:SetText(L["Set binding"])
     local desc = L["In order to specify a binding, move your mouse over the button labelled 'Set binding' and either click with your mouse or press a key on your keyboard. You can modify the binding by holding down a combination of the alt, control and shift keys on your keyboard."]
@@ -124,48 +123,21 @@ function CliqueConfig:Spellbook_DisableKeyboard(button, motion)
     button:EnableKeyboard(false)
 end

-invalidKeys = {
-    ["UNKNOWN"] = true,
-    ["LSHIFT"] = true,
-    ["RSHIFT"] = true,
-    ["LCTRL"] = true,
-    ["RCTRL"] = true,
-    ["LALT"] = true,
-    ["RALT"] = true,
-}
-
 function CliqueConfig:Spellbook_OnBinding(button, key)
-    -- We can't bind modifiers or invalid keys
-    if invalidKeys[key] then
-        return
-    elseif key == "ESCAPE" then
+    if key == "ESCAPE" then
         HideUIPanel(CliqueConfig)
         return
     end

-    -- Remap any mouse buttons
-    if key == "LeftButton" then
-        key = "BUTTON1"
-    elseif key == "RightButton" then
-        key = "BUTTON2"
-    elseif key == "MiddleButton" then
-        key = "BUTTON3"
-    else
-        buttonNum = key:match("Button(%d+)")
-        if buttonNum and tonumber(buttonNum) <= 31 then
-            key = "BUTTON" .. buttonNum
-        end
-    end
-
-    -- TODO: Support NOT splitting the modifier keys
-    local prefix = addon:GetPrefixString(true)
-
     local slot = SpellBook_GetSpellBookSlot(button:GetParent());
     local name, subtype = GetSpellBookItemName(slot, SpellBookFrame.bookType)
     local texture = GetSpellBookItemTexture(slot, SpellBookFrame.bookType)
+
+    local key = addon:GetCapturedKey(key)
+    assert(key, "Unable to get binding information: " .. tostring(key))

     local succ, err = addon:AddBinding{
-        key = prefix .. key,
+        key = key,
         type = "spell",
         spell = name,
         icon = texture
@@ -196,17 +168,13 @@ function CliqueConfig:Button_OnClick(button)
             {
                 text = L["Target clicked unit"],
                 func = function()
-                    config.page1:Hide()
-                    config.page2.bindType = "target"
-                    config.page2:Show()
+                    self:SetupCaptureDialog("target")
                 end,
             },
             {
                 text = L["Open unit menu"],
                 func = function()
-                    config.page1:Hide()
-                    config.page2.bindType = "menu"
-                    config.page2:Show()
+                    self:SetupCaptureDialog("menu")
                 end,
             },
             {
@@ -340,12 +308,41 @@ function CliqueConfig:ShowEditPage()
     self.page3:Show()
 end

-function CliqueConfig:Save_OnClick(button, button, down)
+function CliqueConfig:Save_OnClick(button, down)
 end

-function CliqueConfig:Cancel_OnClick(button, button, down)
+function CliqueConfig:Cancel_OnClick(button, down)
     self:ClearEditPage()
     self.page3:Hide()
     self.page1:Show()
 end

+function CliqueConfig:SetupCaptureDialog(type)
+    self.dialog.bindType = type
+    self.dialog.bindText:SetText("")
+
+    local actionText = addon:GetBindingActionText(type)
+    self.dialog.title:SetText(L["Set binding: %s"]:format(actionText))
+    self.dialog:Show()
+end
+
+function CliqueConfig:BindingButton_OnClick(button, key)
+    local dialog = CliqueDialog
+    dialog.key = addon:GetCapturedKey(key)
+    if dialog.key then
+        CliqueDialog.bindText:SetText(addon:GetBindingKeyComboText(dialog.key))
+    end
+end
+
+function CliqueConfig:AcceptSetBinding()
+    local dialog = CliqueDialog
+    local key = dialog.key
+    local succ, err = addon:AddBinding{
+        key = key,
+        type = dialog.bindType,
+    }
+    if succ then
+        CliqueConfig:UpdateList()
+    end
+    dialog:Hide()
+end
diff --git a/Clique.xml b/Clique.xml
index 9493d27..8de5bbe 100755
--- a/Clique.xml
+++ b/Clique.xml
@@ -115,21 +115,21 @@
         <HighlightTexture file="Interface\BUTTONS\ButtonHilight-Square" alphaMode="ADD"/>
     </Button>

-    <Frame name="CliqueDialog" toplevel="true" inherits="BasicFrameTemplate" parent="UIParent" frameStrata="DIALOG" hidden="true">
+    <Frame name="CliqueDialog" toplevel="true" movable="true" inherits="BasicFrameTemplate" parent="UIParent" frameStrata="DIALOG" hidden="true">
         <Size x="350" y="160"/>
         <Anchors>
-            <Anchor point="CENTER"/>
+            <Anchor point="CENTER" relativeTo="CliqueConfig" x="30" y="0"/>
         </Anchors>
         <Layers>
-            <Layer level="OVERLAY">
+            <Layer level="OVERLAY">
                 <FontString name="$parentDesc" parentKey="desc" inherits="GameFontHighlightSmallLeftTop">
                     <Size x="340" y="60"/>
                     <Anchors>
                         <Anchor point="TOPLEFT" x="5" y="-25"/>
                     </Anchors>
                 </FontString>
-                <FontString name="$parentBindText" parentKey="bindText" inherits="GameFontHighlightSmallLeftTop">
-                    <Size x="100" y="12"/>
+                <FontString name="$parentBindText" parentKey="bindText" JustifyH="CENTER" inherits="GameFontHighlightSmallLeftTop">
+                    <Size x="200" y="12"/>
                     <Anchors>
                         <Anchor point="BOTTOM" x="0" y="35"/>
                     </Anchors>
@@ -137,19 +137,71 @@
             </Layer>
         </Layers>
         <Frames>
+            <Button>
+                <Size x="0" y="20"/>
+                <Anchors>
+                    <Anchor point="TOPLEFT"/>
+                    <Anchor point="TOPRIGHT"/>
+                </Anchors>
+                <Scripts>
+                    <OnLoad>
+                        self:RegisterForClicks("LeftButton")
+                    </OnLoad>
+                    <OnMouseDown>
+                        self:GetParent():StartMoving()
+                    </OnMouseDown>
+                    <OnMouseUp>
+                        self:GetParent():StopMovingOrSizing()
+                    </OnMouseUp>
+                    <OnHide>
+                        self:GetParent():StopMovingOrSizing()
+                    </OnHide>
+                </Scripts>
+            </Button>
             <Button name="$parentButtonBinding" parentKey="button_binding" inherits="UIPanelButtonTemplate2">
                 <Size x="160" y="22"/>
                 <Anchors>
                     <Anchor point="TOP" relativeTo="$parentDesc" relativePoint="BOTTOM" x="0" y="0"/>
                 </Anchors>
+                <Scripts>
+                    <OnLoad>
+                        self:RegisterForClicks("AnyUp")
+                    </OnLoad>
+                    <OnEnter>
+                        self:EnableKeyboard(true)
+                    </OnEnter>
+                    <OnLeave>
+                        self:EnableKeyboard(false)
+                    </OnLeave>
+                    <OnClick>
+                        CliqueConfig:BindingButton_OnClick(self, button)
+                    </OnClick>
+                    <OnKeyDown>
+                        CliqueConfig:BindingButton_OnClick(self, key)
+                    </OnKeyDown>
+                    <OnMouseWheel>
+                        local button = (delta > 0) and "MOUSEWHEELUP" or "MOUSEWHEELDOWN"
+                        CliqueConfig:BindingButton_OnClick(self, button)
+                    </OnMouseWheel>
+                </Scripts>
             </Button>
             <Button name="$parentButtonAccept" parentKey="button_accept" inherits="UIPanelButtonTemplate">
                 <Size x="80" y="22"/>
                 <Anchors>
                     <Anchor point="BOTTOMRIGHT" x="-3" y="3"/>
                 </Anchors>
+                <Scripts>
+                    <OnClick>
+                        CliqueConfig:AcceptSetBinding()
+                    </OnClick>
+                </Scripts>
             </Button>
         </Frames>
+        <Scripts>
+            <OnLoad>
+                table.insert(UISpecialFrames, "CliqueDialog")
+            </OnLoad>
+        </Scripts>
     </Frame>

     <!-- Main configuration frame -->
diff --git a/Utils.lua b/Utils.lua
index e7b29ac..d455da9 100644
--- a/Utils.lua
+++ b/Utils.lua
@@ -48,6 +48,8 @@ local convertMap = setmetatable({
     BUTTON1 = L["LeftButton"],
     BUTTON2 = L["RightButton"],
     BUTTON3 = L["MiddleButton"],
+    MOUSEWHEELUP = L["MousewheelUp"],
+    MOUSEWHEELDOWN = L["MousewheelDown"],
 }, {
     __index = function(t, k, v)
         if k:match("^BUTTON(%d+)$") then
@@ -87,11 +89,16 @@ function addon:GetBindingIcon(binding)
 end

 function addon:GetBindingKeyComboText(binding)
-    return strconcat(convert(strsplit("-", binding.key)))
+    if type(binding) == "table" then
+        return strconcat(convert(strsplit("-", binding.key)))
+    elseif type(binding) == "string" then
+        return strconcat(convert(strsplit("-", binding)))
+    end
 end

 function addon:GetBindingActionText(binding)
-    local btype = binding.type
+    local btype = type(binding) == "table" and binding.type or binding
+
     if btype == "menu" then
         return L["Show unit menu"]
     elseif btype == "target" then
@@ -102,10 +109,6 @@ function addon:GetBindingActionText(binding)
         end
         return L["Cast %s"]:format(binding.spell)
     else
-        for k,v in pairs(binding) do
-            print("binding", k, v)
-        end
-
         return L["Unknown binding type '%s'"]:format(tostring(btype))
     end
 end
@@ -140,3 +143,40 @@ function addon:GetBinaryBindingKey(binding)
     end
     return table.concat(ret)
 end
+
+invalidKeys = {
+    ["UNKNOWN"] = true,
+    ["LSHIFT"] = true,
+    ["RSHIFT"] = true,
+    ["LCTRL"] = true,
+    ["RCTRL"] = true,
+    ["LALT"] = true,
+    ["RALT"] = true,
+    ["ESCAPE"] = true,
+}
+
+function addon:GetCapturedKey(key)
+    -- We can't bind modifiers or invalid keys
+    if invalidKeys[key] then
+        return
+    end
+
+    -- Remap any mouse buttons
+    if key == "LeftButton" then
+        key = "BUTTON1"
+    elseif key == "RightButton" then
+        key = "BUTTON2"
+    elseif key == "MiddleButton" then
+        key = "BUTTON3"
+    else
+        buttonNum = key:match("Button(%d+)")
+        if buttonNum and tonumber(buttonNum) <= 31 then
+            key = "BUTTON" .. buttonNum
+        end
+    end
+
+    -- TODO: Support NOT splitting the modifier keys
+    local prefix = addon:GetPrefixString(true)
+    return tostring(prefix) .. tostring(key)
+end
+