flickerstreak@159: --[[ flickerstreak@159: ReAction Vehicle Exit button module flickerstreak@159: flickerstreak@159: The button module implements a single button which you can use flickerstreak@159: to exit a vehicle that doesn't have controls (replacement for flickerstreak@159: MainMenuBarLeaveVehicleButton). flickerstreak@159: flickerstreak@159: --]] flickerstreak@159: flickerstreak@159: -- local imports flickerstreak@175: local addonName, addonTable = ... flickerstreak@175: local ReAction = addonTable.ReAction flickerstreak@159: local L = ReAction.L flickerstreak@159: flickerstreak@159: -- module declaration flickerstreak@159: local moduleID = "VehicleExit" flickerstreak@159: local module = ReAction:NewModule( moduleID ) flickerstreak@159: flickerstreak@159: -- Button class flickerstreak@159: local Button = ReAction.Button.VehicleExit flickerstreak@159: flickerstreak@159: -- module methods flickerstreak@159: function module:OnInitialize() flickerstreak@159: self.registered = { } flickerstreak@159: self.buttons = { } flickerstreak@159: flickerstreak@159: ReAction:RegisterBarOptionGenerator(self, "GetBarOptions") flickerstreak@159: flickerstreak@159: ReAction.RegisterCallback(self, "OnCreateBar") flickerstreak@159: ReAction.RegisterCallback(self, "OnDestroyBar") flickerstreak@159: ReAction.RegisterCallback(self, "OnRefreshBar") flickerstreak@159: end flickerstreak@159: flickerstreak@159: function module:OnEnable() flickerstreak@218: ReAction:RegisterBarType(Button) flickerstreak@159: end flickerstreak@159: flickerstreak@159: function module:OnDisable() flickerstreak@218: ReAction:UnregisterBarType(Button) flickerstreak@159: end flickerstreak@159: flickerstreak@159: function module:OnCreateBar(event, bar, name) flickerstreak@159: if bar.config.type == moduleID then flickerstreak@159: self:OnRefreshBar(event, bar, name) flickerstreak@159: end flickerstreak@159: end flickerstreak@159: flickerstreak@159: function module:OnRefreshBar(event, bar, name) flickerstreak@222: local config = bar:GetConfig() flickerstreak@222: if config.type == moduleID then flickerstreak@222: if not config.buttons then flickerstreak@222: config.buttons = { } flickerstreak@159: end flickerstreak@222: local btnCfg = config.buttons flickerstreak@222: btnCfg[1] = btnCfg[1] or { } flickerstreak@159: flickerstreak@159: if self.buttons[bar] == nil then flickerstreak@222: local success, r = pcall(Button.New, Button, 1, btnCfg[1], bar) flickerstreak@159: if success and r then flickerstreak@159: self.buttons[bar] = r flickerstreak@159: bar:AddButton(1,r) flickerstreak@159: end flickerstreak@159: else flickerstreak@159: self.buttons[bar]:Refresh() flickerstreak@159: end flickerstreak@159: bar:ClipNButtons(1) flickerstreak@159: self:UpdateRegistration(bar) flickerstreak@159: end flickerstreak@159: end flickerstreak@159: flickerstreak@159: function module:OnDestroyBar(event, bar, name) flickerstreak@159: if self.buttons[bar] then flickerstreak@159: self.buttons[bar]:Destroy() flickerstreak@159: self.buttons[bar] = nil flickerstreak@159: end flickerstreak@159: end flickerstreak@159: flickerstreak@159: flickerstreak@159: function module:UpdateRegistration(bar) flickerstreak@159: -- auto show/hide when on a vehicle flickerstreak@222: local config = bar:GetConfig() flickerstreak@159: local f = bar:GetFrame() flickerstreak@159: if config.withControls then flickerstreak@159: if bar.vehicleExitStateRegistered then flickerstreak@159: UnregisterStateDriver(f, "unitexists") flickerstreak@159: bar.vehicleExitStateRegistered = false flickerstreak@159: end flickerstreak@159: bar:RegisterUnitWatch("vehicle",true) flickerstreak@159: else flickerstreak@159: bar:RegisterUnitWatch("vehicle",false) flickerstreak@159: if not bar.vehicleExitStateRegistered then flickerstreak@159: f:SetAttribute("unit","vehicle") flickerstreak@165: RegisterStateDriver(f, "unitexists", "[target=vehicle,exists,novehicleui] show; hide") -- spoof onstate-unitexists flickerstreak@159: bar.vehicleExitStateRegistered = true flickerstreak@159: end flickerstreak@159: end flickerstreak@159: end flickerstreak@159: flickerstreak@159: ---- Options ---- flickerstreak@159: local Handler = { } flickerstreak@159: local meta = { __index = Handler } flickerstreak@159: flickerstreak@159: function Handler:New(bar) flickerstreak@159: return setmetatable( flickerstreak@159: { flickerstreak@159: bar = bar flickerstreak@159: }, meta) flickerstreak@159: end flickerstreak@159: flickerstreak@159: function Handler:GetConfig() flickerstreak@222: return self.bar:GetConfig() flickerstreak@159: end flickerstreak@159: flickerstreak@159: function Handler:GetPassengerOnly() flickerstreak@159: return not self:GetConfig().withControls flickerstreak@159: end flickerstreak@159: flickerstreak@159: function Handler:SetPassengerOnly(info, value) flickerstreak@159: self:GetConfig().withControls = not value flickerstreak@159: module:UpdateRegistration(self.bar) flickerstreak@159: end flickerstreak@159: flickerstreak@159: flickerstreak@159: function module:GetBarOptions(bar) flickerstreak@159: if bar.config.type == moduleID then flickerstreak@159: return { flickerstreak@159: type = "group", flickerstreak@159: name = L["Exit Vehicle"], flickerstreak@159: handler = Handler:New(bar), flickerstreak@159: args = { flickerstreak@159: passengerOnly = { flickerstreak@159: name = L["Show only when passenger"], flickerstreak@159: desc = L["Only show the button when riding as a passenger in a vehicle (no vehicle controls)"], flickerstreak@159: order = 2, flickerstreak@159: width = "double", flickerstreak@159: type = "toggle", flickerstreak@159: get = "GetPassengerOnly", flickerstreak@159: set = "SetPassengerOnly", flickerstreak@159: }, flickerstreak@159: } flickerstreak@159: } flickerstreak@159: end flickerstreak@159: end flickerstreak@159: flickerstreak@159: