Quantcast
phUnitFrames = {

	init = function(self)

		SLASH_PHUNITFRAMES1 = '/showauraids';
		SlashCmdList["PHUNITFRAMES"] = function(msg,editbox)
			local unit = "player"
			for i=1,100 do
				local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit,i,"HELPFUL");
				if name == nil then
					break;
				end;
				print(name,spellId,icon);
			end;
			for i=1,100 do
				local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit,i,"HARMFUL");
				if name == nil then
					break;
				end;
				print(name,spellId,icon);
			end;
        end;

		phMenuFrame = CreateFrame("Frame", "phMenuFrame", UIParent, "UIDropDownMenuTemplate");

        phTooltip = CreateFrame("GameTooltip")
        phTooltip:SetOwner(UIParent, "ANCHOR_NONE")
        phTooltip.leftTexts = {};
        phTooltip.rightTexts = {};
        for i = 1, 5 do
            phTooltip.leftTexts[i] = phTooltip:CreateFontString()
            phTooltip.rightTexts[i] = phTooltip:CreateFontString()
            phTooltip:AddFontStrings(phTooltip.leftTexts[i],phTooltip.rightTexts[i])
        end

        local frames = phUnitFramesConfig.frames;
        for i,frame in pairs(frames) do
            if not frame.refreshRate then
                frame.refreshRate = 30;
            end;
            self:createGroupFrame(frame);
        end;

--[[		phUnitFramesConfig.Focus = phLib.deepcopy(phUnitFramesDefaultConfig);
		phUnitFramesConfig.Focus.title = "Focus";
		phUnitFramesConfig.Focus.units = { "target","focus" };
		phUnitFramesConfig.Focus.point = "TOPRIGHT";
		phUnitFramesConfig.Focus.x = -80;
		phUnitFramesConfig.Focus.y = -500;
		self:createGroupFrame(phUnitFramesConfig.Focus);--]]

		print("phUnitFrames initialized");
	end,

	hideBlizzardFrame = function(self,frameName)
		local frame = _G[frameName];
		local func;
		func = frame:GetScript("OnEvent");
		if func then
			phBlizzardFrames[frameName.."OnEvent"] = func;
			frame:SetScript("OnEvent",nil);
		end;
		frame:Hide();
	end,

	showBlizzardFrame = function(self,frameName)
		local frame = _G[frameName];
		local func;
		func = phBlizzardFrames[frameName.."OnEvent"];
		if func then
			frame:SetScript("OnEvent",func);
		end;
		frame:Show();
	end,

	togglePartyFrame = function(self)
		if InCombatLockdown() then
			return;
		end;

		if phUnitFramesConfig.showBlizzardPartyFrame then
			phUnitFramesConfig.showBlizzardPartyFrame = false;
		else
			phUnitFramesConfig.showBlizzardPartyFrame = true;
		end;
		for i=1, MAX_PARTY_MEMBERS, 1 do
			if phUnitFramesConfig.showBlizzardPartyFrame then
				self:showBlizzardFrame("PartyMemberFrame"..i);
			else
				self:hideBlizzardFrame("PartyMemberFrame"..i);
			end;
		end
	end,

	createGroupFrame = function(self,config,frame)

		if not frame then
			frame = CreateFrame("Button",nil,UIParent);
			frame:SetPoint(config.point,config.x,config.y);
			frame.config = config;

			frame:SetMovable(true);
			frame:RegisterForDrag("LeftButton");
			frame:SetScript("OnDragStart",function(self) self:StartMoving(); end);
			frame:SetScript("OnDragStop",function(self) self:StopMovingOrSizing(); end);

			frame:RegisterForClicks("AnyUp");
			frame:SetScript("OnClick",function(self,button,down)
				if button == "RightButton" then
                    phUnitFrames.createGroupMenu(frame);
				end;
			end);

            frame.lastChildIdx = 0;
			frame.updateUnits = self.updateUnits;
			frame.updateBars = self.updateBars;
			frame.lastUpdateUnits = GetTime();
			frame.lastUpdateBars = GetTime();

			--[[frame:RegisterEvent("RAID_ROSTER_UPDATE");
			frame:RegisterEvent("PARTY_MEMBERS_CHANGED");
			frame:RegisterEvent("PLAYER_ENTERING_WORLD");
			frame:RegisterEvent("PLAYER_REGEN_DISABLED");
			frame:RegisterEvent("PLAYER_REGEN_ENABLED");
			frame:SetScript("OnEvent",function(frame,event,...)
				frame:update();
			end);--]]
			frame:SetScript("OnUpdate",function(frame,timediff)
				frame:updateUnits();
                frame:updateBars();
			end);

			frame.text = frame:CreateFontString(nil,"ARTWORK");
			frame.text:SetFontObject(GameFontHighlightSmall);
			frame.text:SetPoint("CENTER",0,0);

			frame.childs = {};
			if config.units == "raid" then
				for i=1,40 do
					frame.childs[i] = phUnitFrame.create(frame);
				end;
			else
				for i,unit in pairs(config.units) do
					frame.childs[i] = phUnitFrame.create(frame);
					frame.childs[i]:changeUnit(unit);
				end
			end;

		else
			frame.config = config;
			for i,child in ipairs(frame.childs) do
				phUnitFrame.create(frame,child);
			end;
		end;

		frame.text:SetText(config.title);
		frame:SetWidth(config.width);
		frame:SetHeight(config.height);
		frame.text:SetFont(STANDARD_TEXT_FONT,config.fontsize);

		frame:Show();

		return frame;
	end,

    createGroupMenu = function(frame)
        local config = frame.config;

        local configMenu = {};
        for idx,model in pairs(phUnitFramesModels) do
            table.insert(configMenu,{ text = model.name, checked = (config.model == idx), arg1 = frame, arg2 = idx, func = phUnitFrames.changeModel });
        end;

		local menu = {
			{ text = phLib.getLocale("Show Blizzard Party Frame"), checked = phUnitFramesConfig.showBlizzardPartyFrame,func = function() phUnitFrames:togglePartyFrame(); end },
			{ text = phLib.getLocale("Show heal over time values"), checked = phUnitFramesConfig.showHotValues,func = function() phUnitFramesConfig.showHotValues = not phUnitFramesConfig.showHotValues; end },
			{ text = phLib.getLocale("Show role"), checked = config.showrole, arg1 = frame, func = phUnitFrames.changeShowRole },
			{ text = phLib.getLocale("Width"), notCheckable = true, hasArrow = true, menuList = {
					{ text = 100, checked = (config.width==100), arg1 = frame, arg2 = 100, func = phUnitFrames.changeWidth },
					{ text = 125, checked = (config.width==125), arg1 = frame, arg2 = 125, func = phUnitFrames.changeWidth },
					{ text = 150, checked = (config.width==150), arg1 = frame, arg2 = 150, func = phUnitFrames.changeWidth },
					{ text = 175, checked = (config.width==175), arg1 = frame, arg2 = 175, func = phUnitFrames.changeWidth },
					{ text = 200, checked = (config.width==200), arg1 = frame, arg2 = 200, func = phUnitFrames.changeWidth },
					{ text = 225, checked = (config.width==225), arg1 = frame, arg2 = 225, func = phUnitFrames.changeWidth },
					{ text = 250, checked = (config.width==250), arg1 = frame, arg2 = 250, func = phUnitFrames.changeWidth },
				}
			},
			{ text = phLib.getLocale("Height"), notCheckable = true, hasArrow = true, menuList = {
					{ text = 12, checked = (config.height==12), arg1 = frame, arg2 = 12, func = phUnitFrames.changeHeight },
					{ text = 14, checked = (config.height==14), arg1 = frame, arg2 = 14, func = phUnitFrames.changeHeight },
					{ text = 16, checked = (config.height==16), arg1 = frame, arg2 = 16, func = phUnitFrames.changeHeight },
					{ text = 18, checked = (config.height==18), arg1 = frame, arg2 = 18, func = phUnitFrames.changeHeight },
					{ text = 20, checked = (config.height==20), arg1 = frame, arg2 = 20, func = phUnitFrames.changeHeight },
					{ text = 22, checked = (config.height==22), arg1 = frame, arg2 = 22, func = phUnitFrames.changeHeight },
					{ text = 24, checked = (config.height==24), arg1 = frame, arg2 = 24, func = phUnitFrames.changeHeight },
				}
			},
			{ text = phLib.getLocale("Refresh rate"), notCheckable = true, hasArrow = true, menuList = {
					{ text = "1 Hz", checked = (config.refreshRate==1), arg1 = frame, arg2 = 1, func = phUnitFrames.changeRefreshRate },
					{ text = "10 Hz", checked = (config.refreshRate==10), arg1 = frame, arg2 = 10, func = phUnitFrames.changeRefreshRate },
					{ text = "20 Hz", checked = (config.refreshRate==20), arg1 = frame, arg2 = 20, func = phUnitFrames.changeRefreshRate },
					{ text = "30 Hz", checked = (config.refreshRate==30), arg1 = frame, arg2 = 30, func = phUnitFrames.changeRefreshRate },
					{ text = "40 Hz", checked = (config.refreshRate==40), arg1 = frame, arg2 = 40, func = phUnitFrames.changeRefreshRate },
					{ text = "50 Hz", checked = (config.refreshRate==50), arg1 = frame, arg2 = 50, func = phUnitFrames.changeRefreshRate },
					{ text = "60 Hz", checked = (config.refreshRate==60), arg1 = frame, arg2 = 60, func = phUnitFrames.changeRefreshRate },
				}
			},
            { text = phLib.getLocale("Model"), notCheckable = true, hasArrow = true, menuList = configMenu },
		};
		EasyMenu(menu, phMenuFrame, "cursor", 0 , 0, "MENU", 5);
    end,

    changeModel = function(menu,frame,idx)
		CloseDropDownMenus();
        frame.config.model = idx;
        phUnitFrames:createGroupFrame(frame.config,frame);
    end,

	changeShowRole = function(menu,frame)
		CloseDropDownMenus();
		if frame.config.showrole then
			frame.config.showrole = false;
		else
			frame.config.showrole = true;
		end;
	end,

	changeWidth = function(menu,frame,size)
		CloseDropDownMenus();
		frame.config.width = size;
		phUnitFrames:createGroupFrame(frame.config,frame);
	end,

	changeHeight = function(menu,frame,size)
		CloseDropDownMenus();
		frame.config.height = size;
		frame.config.fontsize = size-4;
		phUnitFrames:createGroupFrame(frame.config,frame);
	end,

	changeRefreshRate = function(menu,frame,refreshRate)
		CloseDropDownMenus();
		frame.config.refreshRate = refreshRate;
	end,

	changeFontSize = function(menu,frame,size)
		CloseDropDownMenus();
		frame.config.fontsize = size;
		phUnitFrames:createGroupFrame(frame.config,frame);
	end,

    updateBars = function(frame)
        local refreshTime = 1.0 / frame.config.refreshRate;

		if (GetTime()-frame.lastUpdateBars) < refreshTime then
			return;
		end;
		frame.lastUpdateBars = GetTime();

        local count = 0;
        local childs = frame.childs;
        local childCount = table.getn(childs);
        for i=1,childCount do
            if not childs[frame.lastChildIdx] then
                frame.lastChildIdx = 1;
            end;
            --childs[frame.lastChildIdx]:updateUnit();
            childs[frame.lastChildIdx]:updateBars();
            frame.lastChildIdx = frame.lastChildIdx + 1;
            if (GetTime()-frame.lastUpdateBars) >= refreshTime/2 then
                break;
            end;
            count = count + 1;
        end;
        --print(count);
        --print(GetTime()-frame.lastUpdateBars);
    end,

	updateUnits = function(frame)


		if GetTime() - frame.lastUpdateUnits < 1.0 then
			return;
		end;
		frame.lastUpdateUnits = GetTime();
        frame.lastUpdateBars = GetTime();

        local childs = frame.childs;
		if not InCombatLockdown() then
            local config = frame.config;
            if config.units == "raid" then
                --[[for i,child in pairs(childs) do
                    child:changeUnit("player");
                end;--]]
                if IsInRaid() then
                    for i,child in pairs(childs) do
                        child:changeUnit("raid"..i,i);
                    end;
                else
                    childs[1]:changeUnit("player");
                    if GetNumGroupMembers() > 0 then
	                    for i=1,4 do
	                        childs[i+1]:changeUnit("party"..i);
	                    end;
	                    --childs[6]:changeUnit("playerpet");
	                    for i=6,table.getn(childs) do
	                        childs[i]:changeUnit(nil);
	                    end;
	                end;
                end;
            end;
        end;

        for i,child in pairs(childs) do
            child:updateUnit();
            child:updateBars();
        end;

		if not InCombatLockdown() then

            local order = {};
            for i,child in pairs(childs) do
                if child.unit and UnitExists(child.unit) then
                    table.insert(order,child);
                end;
            end

            table.sort(order,function(a,b)
                if a == nil or b == nil then
                    return true;
                end;
                if a.disp_role == b.disp_role then
                    if a.order == b.order then
                        return a.name < b.name;
                    end;
                    return a.order < b.order;
                end;
                return a.disp_role < b.disp_role
            end);

            local lastframe = frame;
            local lastrole;
            for i,child in ipairs(order) do
                child:ClearAllPoints();
                local y = 0;
                if not lastrole or lastrole ~= child.disp_role then
                    y = -4;
                end;
                child:SetPoint("TOP",lastframe,"BOTTOM",0,y);
                lastframe = child;
                lastrole = child.disp_role;
            end;
        end;

	end,

}

local frame = CreateFrame("FRAME","phMainFrame");
frame:RegisterEvent("VARIABLES_LOADED");
frame:SetScript("OnEvent",function(self,event,...)
	phUnitFrames:init();
end);

phBlizzardFrames = {};
phPlayerRoles = {};

phUnitFramesConfig = {
    showBlizzardPartyFrame = true;
    frames = {
        {
            title = "Raid",
            units = "raid",
            point = "TOPLEFT", x = 80, y = -80,
            width = 175,
            height = 18,
            fontsize = 14,
            model = 1,
        }
    },
};