Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.58 KB | None | 0 0
  1. local cooline = CreateFrame('Button', nil, UIParent)
  2. cooline:SetScript('OnEvent', function()
  3.     this[event]()
  4. end)
  5. cooline:RegisterEvent('VARIABLES_LOADED')
  6.  
  7. cooline_settings = { x = 0, y = -240 }
  8.  
  9. local frame_pool = {}
  10. local cooldowns = {}
  11.  
  12. function cooline.hyperlink_name(hyperlink)
  13.     local _, _, name = strfind(hyperlink, '|Hitem:%d+:%d+:%d+:%d+|h[[]([^]]+)[]]|h')
  14.     return name
  15. end
  16.  
  17. function cooline.detect_cooldowns()
  18.    
  19.     local function start_cooldown(name, texture, start_time, duration, is_spell)
  20.         for _, ignored_name in cooline_ignore_list do
  21.             if strupper(name) == strupper(ignored_name) then
  22.                 return
  23.             end
  24.         end
  25.        
  26.         local end_time = start_time + duration
  27.            
  28.         for _, cooldown in pairs(cooldowns) do
  29.             if cooldown.end_time == end_time then
  30.                 return
  31.             end
  32.         end
  33.  
  34.         cooldowns[name] = cooldowns[name] or tremove(frame_pool) or cooline.cooldown_frame()
  35.         local frame = cooldowns[name]
  36.         frame:SetWidth(cooline.icon_size)
  37.         frame:SetHeight(cooline.icon_size)
  38.         frame.icon:SetTexture(texture)
  39.         if is_spell then
  40.             frame:SetBackdropColor(unpack(cooline_theme.spellcolor))
  41.         else
  42.             frame:SetBackdropColor(unpack(cooline_theme.nospellcolor))
  43.         end
  44.         frame:SetAlpha((end_time - GetTime() > 360) and 0.6 or 1)
  45.         frame.end_time = end_time
  46.         frame:Show()
  47.     end
  48.    
  49.     for bag = 0,4 do
  50.         if GetBagName(bag) then
  51.             for slot = 1, GetContainerNumSlots(bag) do
  52.                 local start_time, duration, enabled = GetContainerItemCooldown(bag, slot)
  53.                 if enabled == 1 then
  54.                     local name = cooline.hyperlink_name(GetContainerItemLink(bag, slot))
  55.                     if duration > 3 and duration < 3601 then
  56.                         start_cooldown(
  57.                             name,
  58.                             GetContainerItemInfo(bag, slot),
  59.                             start_time,
  60.                             duration,
  61.                             false
  62.                         )
  63.                     elseif duration == 0 then
  64.                         cooline.clear_cooldown(name)
  65.                     end
  66.                 end
  67.             end
  68.         end
  69.     end
  70.    
  71.     for slot=0,19 do
  72.         local start_time, duration, enabled = GetInventoryItemCooldown('player', slot)
  73.         if enabled == 1 then
  74.             local name = cooline.hyperlink_name(GetInventoryItemLink('player', slot))
  75.             if duration > 3 and duration < 3601 then
  76.                 start_cooldown(
  77.                     name,
  78.                     GetInventoryItemTexture('player', slot),
  79.                     start_time,
  80.                     duration,
  81.                     false
  82.                 )
  83.             elseif duration == 0 then
  84.                 cooline.clear_cooldown(name)
  85.             end
  86.         end
  87.     end
  88.    
  89.     local _, _, offset, spell_count = GetSpellTabInfo(GetNumSpellTabs())
  90.     local total_spells = offset + spell_count
  91.     for id=1,total_spells do
  92.         local start_time, duration, enabled = GetSpellCooldown(id, BOOKTYPE_SPELL)
  93.         local name = GetSpellName(id, BOOKTYPE_SPELL)
  94.         if enabled == 1 and duration > 2.5 then
  95.             start_cooldown(
  96.                 name,
  97.                 GetSpellTexture(id, BOOKTYPE_SPELL),
  98.                 start_time,
  99.                 duration,
  100.                 true
  101.             )
  102.         elseif duration == 0 then
  103.             cooline.clear_cooldown(name)
  104.         end
  105.     end
  106.    
  107.     cooline.on_update(true)
  108. end
  109.  
  110. function cooline.cooldown_frame()
  111.     local frame = CreateFrame('Frame', nil, cooline.border)
  112.     frame:SetBackdrop({ bgFile=[[Interface\AddOns\cooline\backdrop.tga]] })
  113.     frame.icon = frame:CreateTexture(nil, 'ARTWORK')
  114.     frame.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
  115.     frame.icon:SetPoint('TOPLEFT', 1, -1)
  116.     frame.icon:SetPoint('BOTTOMRIGHT', -1, 1)
  117.     return frame
  118. end
  119.  
  120. local function place_H(this, offset, just)
  121.     this:SetPoint(just or 'CENTER', cooline, 'LEFT', offset, 0)
  122. end
  123. local function place_HR(this, offset, just)
  124.     this:SetPoint(just or 'CENTER', cooline, 'LEFT', cooline_theme.width - offset, 0)
  125. end
  126. local function place_V(this, offset, just)
  127.     this:SetPoint(just or 'CENTER', cooline, 'BOTTOM', 0, offset)
  128. end
  129. local function place_VR(this, offset, just)
  130.     this:SetPoint(just or 'CENTER', cooline, 'BOTTOM', 0, cooline_theme.height - offset)
  131. end
  132.  
  133. function cooline.clear_cooldown(name)
  134.     if cooldowns[name] then
  135.         cooldowns[name]:Hide()
  136.         tinsert(frame_pool, cooldowns[name])
  137.         cooldowns[name] = nil
  138.     end
  139. end
  140.  
  141. local relevel, throt = false, 0
  142.  
  143. function getKeysSortedByValue(tbl, sortFunction)
  144.     local keys = {}
  145.     for key in pairs(tbl) do
  146.         table.insert(keys, key)
  147.     end
  148.  
  149.     table.sort(keys, function(a, b)
  150.         return sortFunction(tbl[a], tbl[b])
  151.     end)
  152.  
  153.     return keys
  154. end
  155.  
  156. function cooline.update_cooldown(name, frame, position, tthrot, relevel)
  157.     throt = min(throt, tthrot)
  158.    
  159.     if frame.end_time - GetTime() < cooline_theme.treshold then
  160.         local sorted = getKeysSortedByValue(cooldowns, function(a, b) return a.end_time > b.end_time end)
  161.         for i, k in ipairs(sorted) do
  162.             if name == k then
  163.                 frame:SetFrameLevel(i+2)
  164.             end
  165.         end
  166.     else
  167.         if relevel then
  168.             frame:SetFrameLevel(random(1,5) + 2)
  169.         end
  170.     end
  171.    
  172.     cooline.place(frame, position)
  173. end
  174.  
  175. do
  176.     local last_update, last_relevel = GetTime(), GetTime()
  177.    
  178.     function cooline.on_update(force)
  179.         if GetTime() - last_update < throt and not force then return end
  180.         last_update = GetTime()
  181.        
  182.         relevel = false
  183.         if GetTime() - last_relevel > 0.4 then
  184.             relevel, last_relevel = true, GetTime()
  185.         end
  186.        
  187.         isactive, throt = false, 1.5
  188.         for name, frame in pairs(cooldowns) do
  189.             local time_left = frame.end_time - GetTime()
  190.             isactive = isactive or time_left < 360
  191.            
  192.             if time_left < -1 then
  193.                 throt = min(throt, 0.2)
  194.                 isactive = true
  195.                 cooline.clear_cooldown(name)
  196.             elseif time_left < 0 then
  197.                 cooline.update_cooldown(name, frame, 0, 0, relevel)
  198.                 frame:SetAlpha(1 + time_left)  -- fades
  199.             elseif time_left < 0.3 then
  200.                 local size = cooline.icon_size * (0.5 - time_left) * 5  -- icon_size + icon_size * (0.3 - time_left) / 0.2
  201.                 frame:SetWidth(size)
  202.                 frame:SetHeight(size)
  203.                 cooline.update_cooldown(name, frame, cooline.section * time_left, 0, relevel)
  204.             elseif time_left < 1 then
  205.                 cooline.update_cooldown(name, frame, cooline.section * time_left, 0, relevel)
  206.             elseif time_left < 3 then
  207.                 cooline.update_cooldown(name, frame, cooline.section * (time_left + 1) * 0.5, 0.02, relevel)  -- 1 + (time_left - 1) / 2
  208.             elseif time_left < 10 then
  209.                 cooline.update_cooldown(name, frame, cooline.section * (time_left + 11) * 0.14286, time_left > 4 and 0.05 or 0.02, relevel)  -- 2 + (time_left - 3) / 7
  210.             elseif time_left < 30 then
  211.                 cooline.update_cooldown(name, frame, cooline.section * (time_left + 50) * 0.05, 0.06, relevel)  -- 3 + (time_left - 10) / 20
  212.             elseif time_left < 120 then
  213.                 cooline.update_cooldown(name, frame, cooline.section * (time_left + 330) * 0.011111, 0.18, relevel)  -- 4 + (time_left - 30) / 90
  214.             elseif time_left < 360 then
  215.                 cooline.update_cooldown(name, frame, cooline.section * (time_left + 1080) * 0.0041667, 1.2, relevel)  -- 5 + (time_left - 120) / 240
  216.                 frame:SetAlpha(cooline_theme.activealpha)
  217.             else
  218.                 cooline.update_cooldown(name, frame, 6 * cooline.section, 2, relevel)
  219.             end
  220.         end
  221.         cooline:SetAlpha(isactive and cooline_theme.activealpha or cooline_theme.inactivealpha)
  222.     end
  223. end
  224.  
  225. function cooline.label(text, offset, just)
  226.     local fs = cooline.overlay:CreateFontString(nil, 'OVERLAY')
  227.     fs:SetFont(cooline_theme.font, cooline_theme.fontsize)
  228.     fs:SetTextColor(unpack(cooline_theme.fontcolor))
  229.     fs:SetText(text)
  230.     fs:SetWidth(cooline_theme.fontsize * 3)
  231.     fs:SetHeight(cooline_theme.fontsize + 2)
  232.     fs:SetShadowColor(unpack(cooline_theme.bgcolor))
  233.     fs:SetShadowOffset(1, -1)
  234.     if just then
  235.         fs:ClearAllPoints()
  236.         if cooline_theme.vertical then
  237.             fs:SetJustifyH('CENTER')
  238.             just = cooline_theme.reverse and ((just == 'LEFT' and 'TOP') or 'BOTTOM') or ((just == 'LEFT' and 'BOTTOM') or 'TOP')
  239.         elseif cooline_theme.reverse then
  240.             just = (just == 'LEFT' and 'RIGHT') or 'LEFT'
  241.             offset = offset + ((just == 'LEFT' and 1) or -1)
  242.             fs:SetJustifyH(just)
  243.         else
  244.             offset = offset + ((just == 'LEFT' and 1) or -1)
  245.             fs:SetJustifyH(just)
  246.         end
  247.     else
  248.         fs:SetJustifyH('CENTER')
  249.     end
  250.     cooline.place(fs, offset, just)
  251.     return fs
  252. end
  253.  
  254. function cooline.VARIABLES_LOADED()
  255.  
  256.     cooline:SetClampedToScreen(true)
  257.     cooline:SetMovable(true)
  258.     cooline:RegisterForDrag('LeftButton')
  259.    
  260.     function cooline:on_drag_stop()
  261.         this:StopMovingOrSizing()
  262.         local x, y = this:GetCenter()
  263.         local ux, uy = UIParent:GetCenter()
  264.         cooline_settings.x, cooline_settings.y = floor(x - ux + 0.5), floor(y - uy + 0.5)
  265.         this.dragging = false
  266.     end
  267.     cooline:SetScript('OnDragStart', function()
  268.         this.dragging = true
  269.         this:StartMoving()
  270.     end)
  271.     cooline:SetScript('OnDragStop', function()
  272.         this:on_drag_stop()
  273.     end)
  274.     cooline:SetScript('OnUpdate', function()
  275.         this:EnableMouse(IsAltKeyDown())
  276.         if not IsAltKeyDown() and this.dragging then
  277.             this:on_drag_stop()
  278.         end
  279.         cooline.on_update()
  280.     end)
  281.  
  282.     cooline:SetWidth(cooline_theme.width)
  283.     cooline:SetHeight(cooline_theme.height)
  284.     cooline:SetPoint('CENTER', cooline_settings.x, cooline_settings.y)
  285.    
  286.     cooline.bg = cooline:CreateTexture(nil, 'ARTWORK')
  287.     cooline.bg:SetTexture(cooline_theme.statusbar)
  288.     cooline.bg:SetVertexColor(unpack(cooline_theme.bgcolor))
  289.     cooline.bg:SetAllPoints(cooline)
  290.     if cooline_theme.vertical then
  291.         cooline.bg:SetTexCoord(1, 0, 0, 0, 1, 1, 0, 1)
  292.     else
  293.         cooline.bg:SetTexCoord(0, 1, 0, 1)
  294.     end
  295.  
  296.     cooline.border = CreateFrame('Frame', nil, cooline)
  297.     cooline.border:SetPoint('TOPLEFT', -cooline_theme.borderinset, cooline_theme.borderinset)
  298.     cooline.border:SetPoint('BOTTOMRIGHT', cooline_theme.borderinset, -cooline_theme.borderinset)
  299.     cooline.border:SetBackdrop({
  300.         edgeFile = cooline_theme.border,
  301.         edgeSize = cooline_theme.bordersize,
  302.     })
  303.     cooline.border:SetBackdropBorderColor(unpack(cooline_theme.bordercolor))
  304.  
  305.     cooline.overlay = CreateFrame('Frame', nil, cooline.border)
  306.     cooline.overlay:SetFrameLevel(24) -- TODO this gets changed automatically later, to 9, find out why
  307.  
  308.     cooline.section = (cooline_theme.vertical and cooline_theme.height or cooline_theme.width) / 6
  309.     cooline.icon_size = (cooline_theme.vertical and cooline_theme.width or cooline_theme.height) + cooline_theme.iconoutset * 2
  310.     cooline.place = cooline_theme.vertical and (cooline_theme.reverse and place_VR or place_V) or (cooline_theme.reverse and place_HR or place_H)
  311.  
  312.     cooline.tick0 = cooline.label('0', 0, 'LEFT')
  313.     cooline.tick1 = cooline.label('1', cooline.section)
  314.     cooline.tick3 = cooline.label('3', cooline.section * 2)
  315.     cooline.tick10 = cooline.label('10', cooline.section * 3)
  316.     cooline.tick30 = cooline.label('30', cooline.section * 4)
  317.     cooline.tick120 = cooline.label('2m', cooline.section * 5)
  318.     cooline.tick300 = cooline.label('6m', cooline.section * 6, 'RIGHT')
  319.    
  320.     cooline:RegisterEvent('SPELL_UPDATE_COOLDOWN')
  321.     cooline:RegisterEvent('BAG_UPDATE_COOLDOWN')
  322.    
  323.     cooline.detect_cooldowns()
  324. end
  325.  
  326. function cooline.BAG_UPDATE_COOLDOWN()
  327.     cooline.detect_cooldowns()
  328. end
  329.  
  330. function cooline.SPELL_UPDATE_COOLDOWN()
  331.     cooline.detect_cooldowns()
  332. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement