changeset 5:27aeec452e7f

(none)
author Flick <flickerstreak@gmail.com>
date Tue, 20 Mar 2007 21:20:05 +0000
parents dfd829db3ad0
children 2da5089ab7ff
files classes/ReBound.lua classes/ReBound.xml
diffstat 2 files changed, 378 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/classes/ReBound.lua	Tue Mar 20 21:20:05 2007 +0000
@@ -0,0 +1,136 @@
+-- ReBinder.lua
+-- 
+
+ReBinder = { }
+
+-- initial values
+ReBinder.active = false
+
+ReBinder.targets = { }
+
+function ReBinder:AddKeybindTarget( t )
+  if t then
+    self.targets[t] = CreateFrame("Button", nil, t, "ReBinderClickBindingTemplate")
+    self.targets[t].keybindTarget = t:GetName()
+  end
+end
+
+function ReBinder:RemoveKeybindTarget( t )
+  if t then
+    self.targets[t] = nil
+  end
+end
+
+function ReBinder:ShowClickBindingButtons()
+  for _, clickFrame in pairs(self.targets) do
+    clickFrame:Show()
+  end    
+end
+
+function ReBinder:HideClickBindingButtons()
+  for _, clickFrame in pairs(self.targets) do
+    clickFrame:Hide()
+  end    
+end
+
+function ReBinder:ClearSelectedKey()
+  self.selectedKey = nil
+end
+
+function ReBinder:ToggleEnabled()
+  if self:IsEnabled() then
+    self:Disable()
+  else
+    self:Enable()
+  end
+end
+
+function ReBinder:IsEnabled()
+  return ReBinderFrame:IsVisible()
+end
+
+function ReBinder:Enable()
+  ReBinderFrame:Show()
+end
+
+function ReBinder:Disable()
+  ReBinderFrame:Hide()
+end
+
+
+function ReBinder:HandleKeyPressed( key )
+  if key == nil or key == "UNKNOWN" or key == "SHIFT" or key == "CTRL" or key == "ALT" then 
+    return
+  end
+  if IsShiftKeyDown() then 
+    key = "SHIFT-"..key
+  end
+  if IsControlKeyDown() then
+    key = "CTRL-"..key
+  end
+  if IsAltKeyDown() then
+    key = "ALT-"..key
+  end
+  if key == "ESCAPE" or GetBindingAction(key) == "REBINDER_TOGGLEBINDINGMODE" then
+    ReBinderFrame:Hide()
+    return nil, nil
+  end
+
+  self.selectedKey = key
+
+  local keyTxt = GetBindingText(key, "KEY_")
+  local cmd    = GetBindingAction(key)
+  local cmdTxt
+  
+  if cmd then
+    cmdTxt = GetBindingText(cmd, "BINDING_NAME_")
+  end
+  
+  -- make click-bindings look prettier
+  local btnName
+  if cmdTxt then
+    btnName = string.match(cmdTxt,"CLICK (.+)\:LeftButton")
+    btnName = btnName or string.match(cmdTxt,"CLICK (.+)\:RightButton")
+  end
+  
+  return keyTxt, btnName or cmdTxt
+end
+
+-- TODO: move to override-binding model and store data in profile
+function ReBinder:BindSelectedKeyTo( btnName )
+  if self.selectedKey and btnName then
+    self:ClearBinding(btnName)
+    SetBindingClick(self.selectedKey, btnName, "LeftButton")
+    SaveBindings(2) -- 2 = character-specific
+    ReBinderFrame.statusMsg:SetText(GetBindingText(self.selectedKey, "KEY_") .. " is now bound to " .. btnName)
+    ReBinderFrame.selectedKey:SetText("(none)")
+    ReBinderFrame.currentAction:SetText("(none)")
+    self.selectedKey = nil
+  end
+end
+
+
+function ReBinder:ClearBinding( btnName )
+  if btnName then
+    local current = GetBindingKey("CLICK "..btnName..":LeftButton")
+    if current then
+      SetBinding(current, nil)
+      ReBinderFrame.statusMsg:SetText("|cFFFF3333"..btnName .. " is now unbound|r")
+    end
+  end
+end
+
+
+function ReBinder:UpdateCurrentTarget( btnName )
+  local msg = ""
+  if btnName then
+    msg = btnName.." is currently "
+    local current = GetBindingKey("CLICK "..btnName..":LeftButton")
+    if current then
+      msg = msg .. "bound to " .. GetBindingText(current, "KEY_")
+    else
+      msg = msg .. " not bound"
+    end
+  end
+  ReBinderFrame.statusMsg:SetText(msg)
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/classes/ReBound.xml	Tue Mar 20 21:20:05 2007 +0000
@@ -0,0 +1,242 @@
+<Ui xmlns="http://www.blizzard.com/wow/ui/" 
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.blizzard.com/wow/ui/..\FrameXML\UI.xsd">
+
+
+  <Button name="ReBinderClickBindingTemplate" virtual="true" hidden="true" toplevel="true" setAllPoints="true">
+		<HighlightTexture alphaMode="ADD" file="Interface\Buttons\ButtonHilight-Square"/>
+    <Layers>
+      <Layer level="BACKGROUND">
+        <Texture>
+          <Color r="0" g="0" b="0" a="0"/>
+        </Texture>
+      </Layer>
+    </Layers>
+    <Scripts>
+      <OnLoad>
+        this:RegisterForClicks("LeftButtonUp","RightButtonUp")
+      </OnLoad>
+      <OnClick>
+        local mouseBtn = arg1
+        if mouseBtn == "LeftButton" then
+          ReBinder:BindSelectedKeyTo(this.keybindTarget)
+        elseif mouseBtn == "RightButton" then
+          ReBinder:ClearBinding(this.keybindTarget)
+        end
+      </OnClick>
+      <PostClick>
+        this:SetButtonState("NORMAL")
+      </PostClick>
+      <OnEnter>
+        ReBinder:UpdateCurrentTarget(this.keybindTarget)
+      </OnEnter>
+      <OnLeave>
+        ReBinder:UpdateCurrentTarget(nil)
+      </OnLeave>
+    </Scripts>
+  </Button>
+
+
+  <!-- this frame covers the entire UIParent. It is visible but empty and in the background, so all it does is consume key presses and unhandled mouse clicks -->
+  <Button name="ReBinderFrame" frameStrata="BACKGROUND" movable="false" enableMouse="true" enableKeyboard="true" parent="UIParent" hidden="true" setAllPoints="true">
+    <Frames>
+      <!-- this is a dialog frame that appears to provide user feedback for the outer frame -->
+      <Button name="$parentDialog" frameStrata="DIALOG" movable="true" enableMouse="true">
+        <Size>
+          <AbsDimension x="330" y="350"/>
+        </Size>
+        <Anchors>
+          <Anchor point="CENTER"/>
+        </Anchors>
+        <TitleRegion>
+          <Size>
+            <AbsDimension x="350" y="30"/>
+          </Size>
+          <Anchors>
+            <Anchor point="TOP">
+              <Offset>
+                <AbsDimension x="0" y="-10"/>
+              </Offset>
+            </Anchor>
+          </Anchors>
+        </TitleRegion>
+        <Backdrop edgeFile="Interface\Tooltips\UI-Tooltip-Border" tile="true">
+          <EdgeSize>
+            <AbsValue val="16"/>
+          </EdgeSize>
+          <TileSize>
+            <AbsValue val="16"/>
+          </TileSize>
+          <BackgroundInsets>
+            <AbsInset left="4" right="4" top="4" bottom="4"/>
+          </BackgroundInsets>
+        </Backdrop>
+        <Layers>
+          <Layer level="BACKGROUND">
+            <Texture>
+              <Color r="0" g="0" b="0" a="0.7"/>
+            </Texture>
+          </Layer>
+          <Layer level="ARTWORK">
+            <FontString inherits="GameFontNormalLarge" text="Key Binding Setup" justifyH="CENTER">
+              <Size>
+                <AbsDimension x="200" y="30"/>
+              </Size>
+              <Anchors>
+                <Anchor point="TOP">
+                  <Offset>
+                    <AbsDimension x="0" y="-10"/>
+                  </Offset>
+                </Anchor>
+              </Anchors>
+            </FontString>
+            <FontString inherits="GameFontNormal" text="Press a key to ready it for assignment, then click a button to assign the key.|nRight-click a button to clear its binding." justifyH="CENTER">
+              <Size>
+                <AbsDimension x="240" y="70"/>
+              </Size>
+              <Anchors>
+                <Anchor point="TOP">
+                  <Offset>
+                    <AbsDimension x="0" y="-55"/>
+                  </Offset>
+                </Anchor>
+              </Anchors>
+              <Color r="1.0" g="1.0" b="1.0"/>
+            </FontString>
+            <FontString inherits="GameFontNormal" text="Selected Key:" justifyH="RIGHT">
+              <Size>
+                <AbsDimension x="175" y="25"/>
+              </Size>
+              <Anchors>
+                <Anchor point="RIGHT" relativePoint="TOP">
+                  <Offset>
+                    <AbsDimension x="-25" y="-160"/>
+                  </Offset>
+                </Anchor>
+              </Anchors>
+            </FontString>
+            <FontString inherits="GameFontNormal" text="Current Binding:" justifyH="RIGHT">
+              <Size>
+                <AbsDimension x="175" y="25"/>
+              </Size>
+              <Anchors>
+                <Anchor point="RIGHT" relativePoint="TOP">
+                  <Offset>
+                    <AbsDimension x="-25" y="-205"/>
+                  </Offset>
+                </Anchor>
+              </Anchors>
+            </FontString>
+            <FontString name="$parentCurrentActionText" inherits="GameFontNormal" text="(none)" justifyH="CENTER">
+              <Size>
+                <AbsDimension x="140" y="50"/>
+              </Size>
+              <Anchors>
+                <Anchor point="CENTER" relativePoint="TOP">
+                  <Offset>
+                    <AbsDimension x="75" y="-205"/>
+                  </Offset>
+                </Anchor>
+              </Anchors>
+              <Color r="0" g="1" b="0" a="1"/>
+            </FontString>
+            <FontString name="$parentStatusMsg" inherits="GameFontNormal" text="" justifyH="CENTER">
+              <Size>
+                <AbsDimension x="200" y="50"/>
+              </Size>
+              <Anchors>
+                <Anchor point="TOP">
+                  <Offset>
+                    <AbsDimension x="0" y="-230"/>
+                  </Offset>
+                </Anchor>
+              </Anchors>
+              <Color r="0.1" g="1.0" b="0.1"/>
+            </FontString>
+          </Layer>
+        </Layers>
+        <Frames>
+          <Button name="$parentSelectedKey" inherits="UIPanelButtonTemplate2" text="(none)">
+            <Size>
+              <AbsDimension x="140" y="28"/>
+            </Size>
+            <Anchors>
+              <Anchor point="CENTER" relativePoint="TOP">
+                <Offset>
+                  <AbsDimension x="75" y="-160"/>
+                </Offset>
+              </Anchor>
+            </Anchors>
+          </Button>
+          <Button inherits="GameMenuButtonTemplate" text="Done">
+            <Size>
+              <AbsDimension x="112" y="28"/>
+            </Size>
+            <Anchors>
+              <Anchor point="BOTTOM">
+                <Offset>
+                  <AbsDimension x="0" y="10"/>
+                </Offset>
+              </Anchor>
+            </Anchors>
+            <Scripts>
+              <OnClick>
+                this:GetParent():GetParent():Hide()
+              </OnClick>
+            </Scripts>
+          </Button>
+        </Frames>
+        <Scripts>
+          <!-- the dialog frame needs to handle clicks (close button, drag title) so we have to
+            re-implement the behavior of capturing alternate mouse buttons -->
+          <OnLoad>
+            this.selectedKey   = getglobal(this:GetName().."SelectedKeyText")
+            this.currentAction = getglobal(this:GetName().."CurrentActionText")
+            this:RegisterForClicks("MiddleButtonUp","Button4Up","Button5Up")
+          </OnLoad>
+          <OnClick>
+            local k, a = ReBinder:HandleKeyPressed(arg1)
+            if k then
+              this.selectedKey:SetText(k)
+              this.currentAction:SetText(a or "(none)")
+            end
+          </OnClick>
+        </Scripts>
+      </Button>
+    </Frames>
+    <Scripts>
+      <OnLoad>
+        this.selectedKey   = getglobal(this:GetName().."DialogSelectedKeyText")
+        this.currentAction = getglobal(this:GetName().."DialogCurrentActionText")
+        this.statusMsg     = getglobal(this:GetName().."DialogStatusMsg")
+        tinsert(UISpecialFrames,this:GetName())
+        this:RegisterForClicks("MiddleButtonUp","Button4Up","Button5Up")
+      </OnLoad>
+      <OnShow>
+        this.selectedKey:SetText("(none)")
+        this.currentAction:SetText("(none)")
+        this.statusMsg:SetText("")
+        ReBinder:ShowClickBindingButtons()
+      </OnShow>
+      <OnHide>
+        ReBinder:HideClickBindingButtons()
+        ReBinder:ClearSelectedKey()
+      </OnHide>
+      <OnKeyDown>
+        local k, a = ReBinder:HandleKeyPressed(arg1)
+        if k then
+          this.selectedKey:SetText(k)
+          this.currentAction:SetText(a or "(none)")
+        end
+      </OnKeyDown>
+      <OnClick>
+        local k, a = ReBinder:HandleKeyPressed(arg1)
+        if k then
+          this.selectedKey:SetText(k)
+          this.currentAction:SetText(a or "(none)")
+        end
+      </OnClick>
+    </Scripts>
+  </Button>
+
+</Ui>