diff VehicleExitButton.lua @ 245:65f2805957a0

No real reason to store some of the code in a subdirectory.
author Flick
date Sat, 26 Mar 2011 12:35:08 -0700
parents classes/VehicleExitButton.lua@0e20f65375d5
children 46b59a9ded76
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VehicleExitButton.lua	Sat Mar 26 12:35:08 2011 -0700
@@ -0,0 +1,108 @@
+local addonName, addonTable = ...
+local ReAction = addonTable.ReAction
+local L = ReAction.L
+local format = string.format
+
+--
+-- VExitButton Button class
+--
+local buttonTypeID = "VehicleExit"
+local Super = ReAction.Button
+local VExitButton = setmetatable(
+  { 
+    defaultBarConfig = { 
+      type = buttonTypeID ,
+      btnWidth = 36,
+      btnHeight = 36,
+      btnRows = 1,
+      btnColumns = 1,
+      spacing = 3,
+      buttons = { }
+    },
+
+    barType = L["Exit Vehicle Floater"], 
+    buttonTypeID = buttonTypeID
+  }, 
+  { __index = Super } )
+
+ReAction.Button.VehicleExit = VExitButton
+ReAction:RegisterBarType(VExitButton)
+
+function VExitButton:New( config, bar, idx )
+  local name = format("ReAction_%s_VehicleExit_%d",bar:GetName(),idx)
+ 
+  self = Super.New(self, name, config, bar, idx, "SecureFrameTemplate, ActionButtonTemplate", "Button")
+
+  -- frame setup
+  local f = self:GetFrame()
+  self.frames.icon:SetTexture("Interface\\Vehicles\\UI-Vehicles-Button-Exit-Up")
+  self.frames.icon:SetTexCoord(0.140625, 0.859375, 0.140625, 0.859375)
+
+  -- attribute setup
+  -- (none)
+
+  -- non secure scripts
+  f:SetScript("OnClick", VehicleExit)
+  f:SetScript("OnEnter", function(frame) GameTooltip_AddNewbieTip(frame, LEAVE_VEHICLE, 1.0, 1.0, 1.0, nil) end)
+  f:SetScript("OnLeave", GameTooltip_Hide)
+  f:SetScript("OnEvent", function(frame, evt, ...) self:OnEvent(evt,...) end)
+
+  -- event registration
+  f:EnableMouse(true)
+  f:RegisterForClicks("AnyUp")
+  f:RegisterEvent("UPDATE_BINDINGS")
+
+  -- attach to skinner
+  bar:SkinButton(self)
+
+  self:Refresh()
+  self:UpdateHotkey()
+
+  return self
+end
+
+function VExitButton:SetupBar(bar)
+  Super.SetupBar(self,bar)
+  self:UpdateRegistration(bar)
+end
+
+function VExitButton:GetActionID()
+  return 1
+end
+
+function VExitButton:Refresh()
+  Super.Refresh(self)
+  -- it seems that setscale kills the texcoord, have to refresh it
+  self.frames.icon:SetTexCoord(0.140625, 0.859375, 0.140625, 0.859375) 
+end
+
+function VExitButton:OnEvent(event, ...)
+  if self[event] then
+    self[event](self, event, ...)
+  end
+end
+
+function VExitButton:UPDATE_BINDINGS()
+  self:UpdateHotkey()
+end
+
+function VExitButton: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
+