Advertisement
Guest User

Untitled

a guest
Dec 16th, 2012
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. local Portrait = {}
  2. ShadowUF:RegisterModule(Portrait, "portrait", ShadowUF.L["Portrait"])
  3.  
  4. -- If the camera isn't reset OnShow, it'll show the entire character instead of just the head, odd I know
  5. local function resetCamera(self)
  6.     self:SetCamera(0)
  7. end
  8.  
  9. local function resetGUID(self)
  10.     self.guid = nil
  11. end
  12.  
  13. function Portrait:OnEnable(frame)
  14.     frame:RegisterUnitEvent("UNIT_PORTRAIT_UPDATE", self, "UpdateFunc")
  15.     frame:RegisterUnitEvent("UNIT_MODEL_CHANGED", self, "Update")
  16.    
  17.     frame:RegisterUpdateFunc(self, "UpdateFunc")
  18.     --frame:RegisterUpdateFunc(self, "Update")
  19.    
  20.    
  21. end
  22.  
  23. function Portrait:OnDisable(frame)
  24.     frame:UnregisterAll(self)
  25. end
  26.  
  27. function Portrait:OnPreLayoutApply(frame, config)
  28.     if( not frame.visibility.portrait ) then return end
  29.  
  30.     if( config.portrait.type == "3D" ) then
  31.         if( not frame.portraitModel ) then
  32.             frame.portraitModel = CreateFrame("PlayerModel", nil, frame)
  33.             frame.portraitModel:SetScript("OnShow", resetCamera)
  34.             frame.portraitModel:SetScript("OnHide", resetGUID)
  35.             frame.portraitModel.parent = frame
  36.         end
  37.        
  38.        
  39.         frame.portrait = frame.portraitModel
  40.         frame.portrait:Show()
  41.  
  42.         ShadowUF.Layout:ToggleVisibility(frame.portraitTexture, false)
  43.     else
  44.         frame.portraitTexture = frame.portraitTexture or frame:CreateTexture(nil, "ARTWORK")
  45.         frame.portrait = frame.portraitTexture
  46.         frame.portrait:Show()
  47.        
  48.        
  49.  
  50.         ShadowUF.Layout:ToggleVisibility(frame.portraitModel, false)
  51.     end
  52.    
  53.     portrait = frame.portrait
  54.    
  55.     if( not frame.health ) then
  56.         frame.health = frame.portrait:CreateTexture(nil, "OVERLAY")
  57.         frame.health:ClearAllPoints()
  58.         frame.health:SetPoint("TOPRIGHT", frame.portrait, "TOPRIGHT")
  59.         frame.health:SetPoint("BOTTOMRIGHT", frame.portrait, "BOTTOMRIGHT")
  60.         frame.health:SetWidth(1)
  61.         frame.health:SetTexture("Interface\\AddOns\\ShadowedUnitFrames\\Media\\Textures\\Striped")
  62.         frame.health:SetHorizTile(true)
  63.         frame.health:SetVertTile(false)
  64.         frame.health:SetVertexColor(0.75,0.75,0.75)
  65.         frame.health:SetBlendMode("ADD")
  66.        
  67.         frame.health.child = frame:CreateTexture(nil, "OVERLAY", nil, -8)
  68.         frame.health.child:ClearAllPoints()
  69.         frame.health.child:SetPoint("TOPLEFT", frame.health, "TOPLEFT")
  70.         frame.health.child:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT")
  71.         frame.health.child:SetTexture("Interface\\AddOns\\ShadowedUnitFrames\\Media\\Textures\\Striped")
  72.         frame.health.child:SetHorizTile(true)
  73.         frame.health.child:SetVertTile(false)
  74.         frame.health.child:SetBlendMode("ADD")
  75.         local t = 0
  76.         frame.timer = CreateFrame("Frame")
  77.         frame.timer:RegisterEvent("UNIT_NAME_UPDATE")
  78.         frame.timer:SetScript("OnEvent", function(self, unit)
  79.             Portrait:UpdateHealthBar(frame)
  80.         end)
  81.         frame.timer:SetScript("OnUpdate", function(self, elapsed, ...)
  82.             t = t + elapsed
  83.             if t > 0.1 then
  84.                 Portrait:UpdateHealthBar(frame)
  85.                 t = 0
  86.             end
  87.         end)
  88.        
  89.         frame.health:Hide()
  90.         frame.health.child:Hide()
  91.        
  92.         frame.health.parent = frame.portrait
  93.     end
  94. end
  95.  
  96. function Portrait:UpdateFunc(frame)
  97.     -- Portrait models can't be updated unless the GUID changed or else you have the animation jumping around
  98.     if( ShadowUF.db.profile.units[frame.unitType].portrait.type == "3D" ) then
  99.         local guid = UnitGUID(frame.unitOwner)
  100.         if( frame.portrait.guid ~= guid ) then
  101.             self:Update(frame)
  102.         end
  103.        
  104.         frame.portrait.guid = guid
  105.     else
  106.         self:Update(frame)
  107.     end
  108. end
  109.  
  110. function Portrait:UpdateHealthBar(frame)
  111. --Update the Health Overlay
  112.     local isOffline = not UnitIsConnected(frame.unit)
  113.     local isDead = UnitIsDeadOrGhost(frame.unit)
  114.     local currentHealth = UnitHealth(frame.unit)
  115.     local maxHealth = UnitHealthMax(frame.unit)
  116.    
  117.     local width = frame.portrait:GetWidth()
  118.     frame.health:SetWidth((isOffline or isDead) and width or (width - (width * (currentHealth / maxHealth))))
  119.     if currentHealth/maxHealth == 1 then
  120.         frame.health:Hide()
  121.         frame.health.child:Hide()
  122.     else
  123.         frame.health:Show()
  124.         frame.health.child:Show()
  125.     end
  126. end
  127. function Portrait:Update(frame, event)
  128.  
  129.     local type = ShadowUF.db.profile.units[frame.unitType].portrait.type
  130.    
  131.     -- Use class thingy
  132.     if( type == "class" ) then
  133.         local classToken = select(2, UnitClass(frame.unitOwner))
  134.         if( classToken ) then
  135.             frame.portrait:SetTexture("Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Classes")
  136.             frame.portrait:SetTexCoord(CLASS_ICON_TCOORDS[classToken][1], CLASS_ICON_TCOORDS[classToken][2], CLASS_ICON_TCOORDS[classToken][3], CLASS_ICON_TCOORDS[classToken][4])
  137.         else
  138.             frame.portrait:SetTexture("")
  139.         end
  140.     -- Use 2D character image
  141.     elseif( type == "2D" ) then
  142.         frame.portrait:SetTexCoord(0.10, 0.90, 0.10, 0.90)
  143.         SetPortraitTexture(frame.portrait, frame.unitOwner)
  144.     -- Using 3D portrait, but the players not in range so swap to question mark
  145.     elseif( not UnitIsVisible(frame.unitOwner) or not UnitIsConnected(frame.unitOwner) ) then
  146.         frame.portrait:SetModelScale(4.25)
  147.         frame.portrait:SetPosition(0, 0, -0.4)
  148.         frame.portrait:SetModel("Interface\\Buttons\\talktomequestionmark.mdx")
  149.     -- Use animated 3D portrait
  150.     else
  151.         frame.portrait:SetUnit(frame.unitOwner)
  152.         frame.portrait:SetCamera(0)
  153.         frame.portrait:Show()
  154.     end
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement