diff State.lua @ 72:aa88aed52124

Fixed bugs with state keybinds. Simplified state driver API
author Flick <flickerstreak@gmail.com>
date Thu, 05 Jun 2008 18:34:36 +0000
parents 3d2cef5dc459
children 06cd74bdc7da
line wrap: on
line diff
--- a/State.lua	Wed Jun 04 21:46:51 2008 +0000
+++ b/State.lua	Thu Jun 05 18:34:36 2008 +0000
@@ -106,87 +106,58 @@
     return r
   end
 
-  local function BuildRuleString(states)
-    local s = ""
+  local function BuildRules(states)
+    local rules = { }
+    local keybinds = { }
     local default
-    local sorted = fieldsort(states, "rule", "order")
-    for idx, name in ipairs(sorted) do
-      local state = states[name]
-      local semi = #s > 0 and "; " or ""
-      local mode = tfetch(state,"rule","type")
-      if mode == "default" then
-        default = name
-      elseif mode == "custom" then
-        if state.rule.custom then
+    local fmt = "%s %s"
+    for idx, state in ipairs(fieldsort(states, "rule", "order")) do
+      local c = states[state].rule
+      local type = tfetch(c,"type")
+      if type == "default" then
+        default = default or state
+      elseif type == "keybind" then
+        keybinds[state] = c.keybind or false
+      elseif type == "custom" then
+        if c.custom then
           -- strip out all spaces from the custom rule
-          s = ("%s%s%s %s"):format(s, semi, state.rule.custom:gsub("%s",""), name)
+          table.insert(rules, fmt:format(c.custom:gsub("%s",""), state))
         end
-      elseif mode == "any" then
-        if state.rule.values then
-          local clause = ""
-          for key, value in pairs(state.rule.values) do
-            clause = ("%s[%s]"):format(clause,ruleformats[key])
+      elseif type == "any" then
+        if c.values then
+          local clauses = { }
+          for key, value in pairs(c.values) do
+            table.insert(clauses, ("[%s]"):format(ruleformats[key]))
           end
-          if #clause > 0 then
-            s = ("%s%s%s %s"):format(s, semi, clause, name)
+          if #clauses > 0 then
+            table.insert(rules, fmt:format(table.concat(clauses), state))
           end
         end
-      elseif mode == "all" then
-        if state.rule.values then
-          local clause = ""
-          for key, value in pairs(state.rule.values) do
-            clause = ("%s%s%s"):format(clause,#clause > 0 and "," or "", ruleformats[key])
+      elseif type == "all" then
+        if c.values then
+          local clauses = { }
+          for key, value in pairs(c.values) do
+            table.insert(clauses, ruleformats[key])
           end
-          if #clause > 0 then
-            s = ("%s%s[%s] %s"):format(s, semi, clause, name)
+          if #clauses > 0 then
+            table.insert(rules, fmt:format(("[%s]"):format(table.concat(clauses, ",")), state))
           end
         end
       end
     end
     if default then
-      s = ("%s%s%s"):format(s, #s > 0 and "; " or "", default)
+      table.insert(rules, default)
     end
-    return s, default
+    return rules, keybinds
   end
 
-  local drivers = setmetatable({},{__mode="k"})
   local propertyFuncs = { }
 
   function ApplyStates( bar )
     local states = tfetch(module.db.profile.bars, bar:GetName(), "states")
     if states then
-      local frame = bar:GetFrame()
-      local string, default = BuildRuleString(states)
-      if string and #string > 0 then
-        drivers[bar] = true
-        -- register a map for each "statemap-reaction-XXX" to set 'state' to 'XXX'
-        -- UNLESS we're in a keybound state AND there's a default state, in which case
-        -- all keybound states go back to themselves.
-        local keybindprefix
-        if default then
-          local tmp = { }
-          for state, config in pairs(states) do
-            if tfetch(config, "rule", "type") == "keybind" then
-              bar:SetStateKeybind(tfetch(config,"rule","keybind"), state, tfetch(config,"rule","keybindreturn") or default or 0)
-              table.insert(tmp, ("%s:%s"):format(state,state))
-            end
-          end
-          if #tmp > 0 then
-            table.insert(tmp,"") -- to get a final ';'
-          end
-          keybindprefix = table.concat(tmp,";")
-        end
-        for state in pairs(states) do
-          frame:SetAttribute(("statemap-reaction-%s"):format(state), ("%s%s"):format(keybindprefix or "",state))
-        end
-        -- register a handler to set the value of attribute "state-reaction" 
-        -- in response to events as per the rule string
-        RegisterStateDriver(frame, "reaction", string)
-        SecureStateHeader_Refresh(frame)
-      elseif drivers[bar] then
-        UnregisterStateDriver(frame, "reaction")
-        drivers[bar] = nil
-      end
+      local rules, keybinds = BuildRules(states)
+      bar:SetStateDriver(table.concat(rules,";"), states, keybinds)
       for k, f in pairs(propertyFuncs) do
         f(bar, states)
       end
@@ -816,7 +787,7 @@
             type = "group",
             args = {
               desc = {
-                name = L["Invoking a state keybind overrides all other transition rules. Toggle the keybind again to remove the override and return to the specified toggle-off state."],
+                name = L["Invoking a state keybind toggles an override of all other transition rules."],
                 order = 1,
                 type = "description",
               },
@@ -834,26 +805,6 @@
                        end,
                 get  = function() return getrule("keybind") end,
               },
-              default = {
-                name = L["Toggle Off State"],
-                desc = L["Select a state to return to when the keybind override is toggled off"],
-                order = 3,
-                type = "select",
-                values = function()
-                           local t = { }
-                           for k in pairs(states) do
-                             if k ~= opts.name then
-                               t[k] = k
-                             end
-                           end
-                           return t
-                         end,
-                set = function(info, value)
-                        setrule("keybindreturn",value)
-                        update()
-                      end,
-                get = function() return getrule("keybindreturn") end,
-              },
             },
           },
         },