View Single Post
02-25-20, 07:34 PM   #1
falsifyed
A Kobold Labourer
Join Date: Feb 2020
Posts: 1
[Wow Classic] I need help getting my addon to work

I can't figure out how to get my text, title and button to display on my content1 tab, I followed a tutorial on youtube to add tabs to a frame. I'm using a ScrollFrame because that's what the tutorial used. I also tried using a normal Frame instead, but the Tab_OnClick(self) function wasn't working when i used that. I don't want a scrollbar.

PS: this is my first addon so please don't point out my sloppy coding, I'm still learning, thank you

Code:
isFirstLoad = true
posX, posY = 0
aPoint = "TOP"
aRelativePoint = "TOP"
local ADDON_NAME = "XP Tracker"
local DEBUG_MODE = true
local WINDOW_TITLE = ADDON_NAME
local WINDOW_HEIGHT = 200
local WINDOW_WIDTH = 150
local FONT_SIZE = 12
local FONT_NAME = "Fonts\\FRIZQT__.TTF"
local totalXp = 0
local killXP = 0
local questXP = 0
local xpPerHour = 0
local xpTimer = 0
local xpTimerStart = 0
local isXpTimerStarted = false -- IMPORTANT: Must be set to false!
local lastTime = 0;
local totalKills = 0
local timeElapsed = 0;
local lastXpDrop = 0;
local UIFrame = CreateFrame("Frame", "Main_Frame", UIParent, "UIPanelDialogTemplate");

local function Tab_OnClick(self)
	printdb("Clicked Tab")
	PanelTemplates_SetTab(self:GetParent(), self:GetID())	
	local child = UIFrame.scrollFrame:GetScrollChild()

	if(child) then
		child.Hide()
	end
	UIFrame.scrollFrame:SetScrollChild(self.content)
	self.content:Show()
end

local function setTabs(frame, numTabs, ...)
	frame.numTabs = numTabs
	local contents = {}
	local frameName = frame:GetName()
	
	for i=1,numTabs do
		local tab = CreateFrame("Button", frameName .. "Tab" .. i, frame, "CharacterFrameTabButtonTemplate")
		tab:SetID(i)
		tab:SetText(select(i, ...))
		tab:SetScript("OnClick", Tab_OnClick)
		
		tab.content = CreateFrame("Frame", nil, UIFrame.scrollFrame)
		tab.content:SetSize(WINDOW_HEIGHT, WINDOW_WIDTH)
		tab.content:Hide()
		
		table.insert(contents, tab.content)
		
		if (i == 1) then
			tab:SetPoint("TOPLEFT", UIFrame, "BOTTOMLEFT", 5, 7)
		else
			tab:SetPoint("TOPLEFT", _G[frameName .. "Tab" .. (i - 1)], "TOPRIGHT", -14, 0)
		end
		
	end
	
	Tab_OnClick(_G[frameName .. "Tab1"])
	
	return unpack(contents)
end


function OnUpdate()
	local now = time()
	
	if((now - lastTime) < 1) then
		 do return end
	end
	
	lastTime = now
	if (isXpTimerStarted == true) then
		timeElapsed = now - xpTimerStart
		UIFrame.content1.timeElapsedText:SetText(secondsToClock(timeElapsed))
		UIFrame.content1.xpPerHourText:SetFormattedText("XP per Hour: %d", getXpPerHour())
	end
end

function secondsToClock(seconds)
  local seconds = tonumber(seconds)

  if seconds <= 0 then
    return "00:00:00";
  else
    hours = string.format("%02.f", math.floor(seconds/3600));
    mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
    secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
    return hours..":"..mins..":"..secs
  end
end

function getXpPerHour()
	local now = time()
	
	if (timeElapsed <= 0) then
		return 0
	end
	
	return (1 / (timeElapsed / 3600) * totalXp)	
	--return totalXp / (newTime / 3600)
end

local update = CreateFrame("Frame")
update:SetScript("OnUpdate", OnUpdate)

local chatMsgCombatXpEvent = CreateFrame("Frame")
chatMsgCombatXpEvent:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN")
chatMsgCombatXpEvent:SetScript("OnEvent", function(self, event, ...)
printdb("CHAT_MSG_COMBAT_XP_GAIN event triggered")
	if (isXpTimerStarted == false) then
		isXpTimerStarted = true
		xpTimerStart = time()
	end
	
	local xpDrop = parseString(...)
	
	printdb("XP Gained: " .. xpDrop)	
	totalKills = totalKills + 1
	lastXpDrop = xpDrop
	totalXp = totalXp + xpDrop
	UIFrame.content1.totalKillsText:SetFormattedText("Total Kills: %d", totalKills)
	UIFrame.content1.totalXpText:SetFormattedText("Total XP: %d", totalXp)
	UIFrame.content1.killsRemText:SetFormattedText("Kills Left: %d", getKillsRemaining(lastXpDrop))
end)

local playerEnteringWorldFrame = CreateFrame("Frame")
playerEnteringWorldFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
playerEnteringWorldFrame:SetScript("OnEvent", function(self, event, ...)
	printdb("PLAYER_ENTERING_WORLD event triggered")
	printdb("isFirstLoad = " .. tostring(isFirstLoad))
	printdb("posX = " .. tostring(posX))
	printdb("posY = " .. tostring(posY))
	createInterface()
end)

function parseString(str)
	return string.match(str, "%d+")
end

function printdb(s)
	if(DEBUG_MODE == true) then
		print("DEBUG: " .. s)
	end
end

function getKillsRemaining(xp)
	if (lastXpDrop == 0) then
		return 0
	end
	
	local RemainingXP = UnitXPMax("player") - UnitXP("player")
	local result = RemainingXP / xp
	printdb("Kills Remaining: " .. tostring(result))
	return math.ceil(result)
end

function createInterface()
	
	-- Main Frame --
	
	UIFrame:EnableMouse(true);
	UIFrame:SetMovable(true);
	UIFrame:SetSize(WINDOW_HEIGHT, WINDOW_WIDTH);
	--if (isFirstLoad) then
		UIFrame:SetPoint("TOP");
	--else	
		--UIFrame:SetPoint(aPoint, nil, aRelativePoint, posX, posY);
	--end
	UIFrame:SetScript("OnMouseDown", function(self, button)
		if button == "LeftButton" and not self.isMoving then
			self:StartMoving();
			self.isMoving = true;
		end
	end)
	UIFrame:SetScript("OnMouseUp", function(self, button)
		if button == "LeftButton" and self.isMoving then
			self:StopMovingOrSizing();
			self.isMoving = false;
		end
	end)
	UIFrame:SetScript("OnHide", function(self)
		if ( self.isMoving ) then
			self:StopMovingOrSizing();
			self.isMoving = false;
		end
	end)
	
	UIFrame.scrollFrame = CreateFrame("ScrollFrame", nil, UIFrame, "UIPanelScrollFrameTemplate")
	UIFrame.scrollFrame:SetPoint("TOPLEFT", Main_FrameDialogBG, "TOPLEFT", 0, 0)
	UIFrame.scrollFrame:SetPoint("BOTTOMLEFT", Main_FrameDialogBG, "BOTTOMLEFT", 0, 0)
	UIFrame.scrollFrame:SetClipsChildren(true)
	
	local decrementAmount = 14
	local yOffset = 32
	local xOffset = 25	
	local content1, content2, content3 = setTabs(UIFrame, 3, "XP", "Loot", "Other")
	local totalXpText = content1:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge");
	local xpPerHourText = content1:CreateFontString(nil, "ARTWORK", "ChatFontNormal");
	local totalKillsText = content1:CreateFontString(nil, "ARTWORK", "ChatFontNormal");
	local timeElapsedText = content1:CreateFontString(nil, "ARTWORK", "ChatFontNormal");
	local killsRemText = content1:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge");
	-- Title --
	
	--content1.title = content1:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge");
	UIFrame.title:ClearAllPoints()
	UIFrame.title:SetFontObject("GameFontHighlight")
	UIFrame.title:SetPoint("CENTER", Main_FrameTitleBG, "CENTER", 0, 0);
	UIFrame.title:SetText(WINDOW_TITLE);
	--content1.title:Show();

	-- Text --
	
	timeElapsedText:SetTextColor(0.8, 0.8, 0.8, 1);
	timeElapsedText:SetPoint("CENTER", 0, yOffset);
	timeElapsedText:SetFont(FONT_NAME, FONT_SIZE);
	timeElapsedText:SetText("00:00:00")

	
	yOffset = yOffset - decrementAmount
	
	killsRemText:SetTextColor(0.8, 0.8, 0.8, 1);
	--killsRemText:SetPoint("LEFT", xOffset, yOffset);
	killsRemText:SetPoint("CENTER", 0, yOffset);
	killsRemText:SetFont(FONT_NAME, FONT_SIZE);
	killsRemText:SetFormattedText("Kills Left: %d", getKillsRemaining(lastXpDrop))

	
	yOffset = yOffset - decrementAmount
	
	totalKillsText:SetTextColor(0.8, 0.8, 0.8, 1);
	totalKillsText:SetPoint("CENTER", 0, yOffset);
	totalKillsText:SetFont(FONT_NAME, FONT_SIZE);
	totalKillsText:SetFormattedText("Total Kills: %d", totalKills)

	
	yOffset = yOffset - decrementAmount
	
	totalXpText:SetTextColor(0.8, 0.8, 0.8, 1);
	totalXpText:SetPoint("CENTER", 0, yOffset);
	totalXpText:SetFont(FONT_NAME, FONT_SIZE);
	totalXpText:SetFormattedText("Total XP: %d", totalXp)
	totalXpText:Show();

	yOffset = yOffset - decrementAmount

	xpPerHourText:SetTextColor(0.8, 0.8, 0.8, 1);
	xpPerHourText:SetPoint("CENTER", 0, yOffset);
	xpPerHourText:SetFont(FONT_NAME, FONT_SIZE);
	xpPerHourText:SetFormattedText("XP per Hour: %d", xpPerHour)
	
	-- Button --

	content1.resetButton = CreateFrame("Button", nil, content1, "GameMenuButtonTemplate");
	content1.resetButton:SetPoint("BOTTOM")
	content1.resetButton:SetSize(64, 26);
	content1.resetButton:SetText("Reset");
	content1.resetButton:SetNormalFontObject("GameFontNormalLarge");
	content1.resetButton:SetHighlightFontObject("GameFontHighlightLarge");
	content1.resetButton:SetScript(
		"OnClick",
		function()
			resetData()
		end
	)
	
end

function resetData()
	printdb("Resetting Data")
	isXpTimerStarted = false
	xpTimerStart = 0
	totalXp = 0
	xpPerHour = 0
	xpTimer = 0
	totalKills = 0
	timeElapsed = 0
	lastXpDrop = 0
	UIFrame.content1.timeElapsedText:SetText(secondsToClock(0))
	UIFrame.content1.xpPerHourText:SetFormattedText("XP per Hour: %d", xpPerHour)
	UIFrame.content1.totalXpText:SetFormattedText("Total XP: %d", totalXp)
	UIFrame.content1.totalKillsText:SetFormattedText("Total Kills: %d", totalKills)
	UIFrame.content1.killsRemText:SetFormattedText("Kills Left: %d", getKillsRemaining(lastXpDrop))
	printdb("Successful!")
end

local playerLogoutFrame = CreateFrame("Frame")
playerLogoutFrame:RegisterEvent("PLAYER_LOGOUT")
playerLogoutFrame:SetScript("OnEvent", function(self, event, ...)
	printdb("PLAYER_LOGOUT event triggered")
	local point, relativeTo, relativePoint, xOfs, yOfs = UIFrame:GetPoint(n)
	isFirstLoad = false
	aPoint = point
	arelativePoint = relativePoint
	posX = xOfs
	posY = yOfs
end)

local addonLoadedFrame = CreateFrame("FRAME");
addonLoadedFrame:RegisterEvent("ADDON_LOADED");
addonLoadedFrame:SetScript("OnEvent", function(self, event, ...)
	printdb("ADDON_LOADED event triggered")
end)

local function MyAddonCommands(msg, editbox)
	if msg == 'show' then
		if(not UIFrame:IsShown()) then
			UIFrame:SetShown(true)
		end
	elseif msg == 'hide' then
		if(UIFrame:IsShown()) then
			UIFrame:SetShown(false)
		end
	elseif msg == 'toggle' then
		UIFrame:SetShown(not UIFrame:IsShown())	
	elseif msg == 'help' then
		print(" ")
		print("-- XP Tracker Commands --")
		print("/xpt show - shows the frame.")
		print("/xpt hide - hides the frame.")
		print("/xpt toggle - toggles the frame.")
		print("-------------------------")
		print(" ")
	else
		print(" ")
		print("-- XP Tracker Commands --")
		print("/xpt show - shows the frame.")
		print("/xpt hide - hides the frame.")
		print("/xpt toggle - toggles the frame.")
		print("-------------------------")
		print(" ")
	end
  
end

SLASH_CORE1 = '/xpt'

SlashCmdList["CORE"] = MyAddonCommands

Last edited by falsifyed : 02-25-20 at 08:57 PM.
  Reply With Quote