Mercurial > wow > reaction
view modules/VehicleExit.lua @ 228:ad40cd3fc7e9
pull states config out of namespace
author | Flick |
---|---|
date | Mon, 21 Mar 2011 11:33:15 -0700 |
parents | c4b134512c50 |
children | 0e20f65375d5 |
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.registered = { } self.buttons = { } ReAction:RegisterBarOptionGenerator(self, "GetBarOptions") ReAction.RegisterCallback(self, "OnCreateBar") ReAction.RegisterCallback(self, "OnDestroyBar") ReAction.RegisterCallback(self, "OnRefreshBar") 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) local config = bar:GetConfig() if config.type == moduleID then if not config.buttons then config.buttons = { } end local btnCfg = config.buttons btnCfg[1] = btnCfg[1] or { } if self.buttons[bar] == nil then local success, r = pcall(Button.New, Button, 1, btnCfg[1], 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:UpdateRegistration(bar) -- auto show/hide when on a vehicle local config = bar:GetConfig() 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 self.bar:GetConfig() 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