view modules/VehicleExit.lua @ 183:1696ff2c80cc

minor library rename/storage/cleanup
author Flick <flickerstreak@gmail.com>
date Fri, 22 Oct 2010 15:56:08 +0000
parents df68b5a40490
children e63aefb8a555
line wrap: on
line source
--[[
  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 addonName, addonTable = ...
local ReAction = addonTable.ReAction
local L = ReAction.L

-- 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,novehicleui] 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