diff modules/VehicleExit.lua @ 159:799c6ea9da7b

added vehicle exit floater some fixes for vehicle exit button on main bars still some lingering settexcoords issues on the vehicle exit icon
author Flick <flickerstreak@gmail.com>
date Mon, 29 Jun 2009 17:43:16 +0000
parents
children 5257073138e8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/VehicleExit.lua	Mon Jun 29 17:43:16 2009 +0000
@@ -0,0 +1,173 @@
+--[[
+  ReAction Vehicle Exit button module
+
+  The button module implements a single button which you can use
+  to exit a vehicle that doesn't have controls (replacement for
+  MainMenuBarLeaveVehicleButton).
+
+--]]
+
+-- local imports
+local ReAction = ReAction
+local L = ReAction.L
+
+ReAction:UpdateRevision("$Revision: 200 $")
+
+-- module declaration
+local moduleID = "VehicleExit"
+local module = ReAction:NewModule( moduleID )
+
+-- Button class
+local Button = ReAction.Button.VehicleExit
+
+-- module methods
+function module:OnInitialize()
+  self.db = ReAction.db:RegisterNamespace( moduleID,
+    { 
+      profile = {
+        buttons = { }
+      }
+    }
+  )
+  self.registered = { }
+  self.buttons = { }
+
+  ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
+
+  ReAction.RegisterCallback(self, "OnCreateBar")
+  ReAction.RegisterCallback(self, "OnDestroyBar")
+  ReAction.RegisterCallback(self, "OnRefreshBar")
+  ReAction.RegisterCallback(self, "OnEraseBar")
+  ReAction.RegisterCallback(self, "OnRenameBar")
+end
+
+function module:OnEnable()
+  ReAction:RegisterBarType(L["Exit Vehicle Floater"], 
+    { 
+      type = moduleID ,
+      defaultButtonSize = 36,
+      defaultBarRows = 1,
+      defaultBarCols = 1,
+      defaultBarSpacing = 3
+    })
+end
+
+function module:OnDisable()
+  ReAction:UnregisterBarType(L["Exit Vehicle Floater"])
+end
+
+function module:OnCreateBar(event, bar, name)
+  if bar.config.type == moduleID then
+    self:OnRefreshBar(event, bar, name)
+  end
+end
+
+function module:OnRefreshBar(event, bar, name)
+  if bar.config.type == moduleID then
+    local profile = self.db.profile
+    if profile.buttons[name] == nil then
+      profile.buttons[name] = {}
+    end
+    local btnCfg = profile.buttons[name]
+
+    if profile.buttons[name] == nil then
+      profile.buttons[name] = { }
+    end
+    if self.buttons[bar] == nil then
+      local success, r = pcall(Button.New, Button, 1, profile.buttons[name], bar)
+      if success and r then
+        self.buttons[bar] = r
+        bar:AddButton(1,r)
+      end
+    else
+      self.buttons[bar]:Refresh()
+    end
+    bar:ClipNButtons(1)
+    self:UpdateRegistration(bar)
+  end
+end
+
+function module:OnDestroyBar(event, bar, name)
+  if self.buttons[bar] then
+    self.buttons[bar]:Destroy()
+    self.buttons[bar] = nil
+  end
+end
+
+function module:OnEraseBar(event, bar, name)
+  self.db.profile.buttons[name] = nil
+end
+
+function module:OnRenameBar(event, bar, oldname, newname)
+  local b = self.db.profile.buttons
+  b[newname], b[oldname] = b[oldname], nil
+end
+
+
+function module:UpdateRegistration(bar)
+  -- auto show/hide when on a vehicle
+  local config = self.db.profile.buttons[bar:GetName()]
+  local f = bar:GetFrame()
+  if config.withControls then
+    if bar.vehicleExitStateRegistered then
+      UnregisterStateDriver(f, "unitexists")
+      bar.vehicleExitStateRegistered = false
+    end
+    bar:RegisterUnitWatch("vehicle",true)
+  else
+    bar:RegisterUnitWatch("vehicle",false)
+    if not bar.vehicleExitStateRegistered then
+      f:SetAttribute("unit","vehicle")
+      RegisterStateDriver(f, "unitexists", "[target=vehicle,exists,nobonusbar:5] show; hide") -- spoof onstate-unitexists
+      bar.vehicleExitStateRegistered = true
+    end
+  end
+end
+
+---- Options ----
+local Handler = { }
+local meta = { __index = Handler }
+
+function Handler:New(bar)
+  return setmetatable(
+    {
+      bar = bar
+    }, meta)
+end
+
+function Handler:GetConfig()
+  return module.db.profile.buttons[self.bar:GetName()]
+end
+
+function Handler:GetPassengerOnly()
+  return not self:GetConfig().withControls
+end
+
+function Handler:SetPassengerOnly(info, value)
+  self:GetConfig().withControls = not value
+  module:UpdateRegistration(self.bar)
+end
+
+
+function module:GetBarOptions(bar)
+  if bar.config.type == moduleID then
+    return {
+      type = "group",
+      name = L["Exit Vehicle"],
+      handler = Handler:New(bar),
+      args = {
+        passengerOnly = {
+          name = L["Show only when passenger"],
+          desc = L["Only show the button when riding as a passenger in a vehicle (no vehicle controls)"],
+          order = 2,
+          width = "double",
+          type = "toggle",
+          get = "GetPassengerOnly",
+          set = "SetPassengerOnly",
+        },
+      }
+    }
+  end
+end
+
+