view ProspectMe_Config.lua @ 17:4f5357900609

Fixed a bug in the database reset function
author Geoff Brock <mischivin@gmail.com>
date Sun, 04 Sep 2016 16:50:14 -0400
parents dabd5c6540ca
children 78a07841e004
line wrap: on
line source
if not PM_Config then
	PM_Config = {}
end

local ProspectMeFrame = CreateFrame( "Frame", "PM_ConfigFrame", InterfaceOptionsFramePanelContainer)
local Title = PM_ConfigFrame:CreateFontString( "ProspectMeTitle", nil, "GameFontNormalLarge")
local ResultsToggle = CreateFrame( "CheckButton", "ProspectMeResultsToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local EpicToggle = CreateFrame( "CheckButton", "ProspectMeEpicToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local RareToggle = CreateFrame( "CheckButton", "ProspectMeRareToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local UncommonToggle = CreateFrame( "CheckButton", "ProspectMeUncommonToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local CommonToggle = CreateFrame( "CheckButton", "ProspectMeCommonToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local PoorToggle = CreateFrame( "CheckButton", "ProspectMePoorToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local SessionToggle = CreateFrame( "CheckButton", "ProspectMeSessionToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local PercentToggle = CreateFrame( "CheckButton", "ProspectMePercentToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local NumberToggle = CreateFrame( "CheckButton", "ProspectMeNumberToggle", ProspectMeFrame, "InterfaceOptionsCheckButtonTemplate" )
local ResetResults = CreateFrame( "Button", "ProspectMeResetButton", ProspectMeFrame, "UIPanelButtonTemplate" )
local ResetText = PM_ConfigFrame:CreateFontString( "ProspectMeResetText", nil, "GameFontNormal" )

local function SetDefaults()
	PM_Config.results = true
	PM_Config.qualities ={[0] = false,[1] = true, [2] = true, [3] = true, [4] = true}
	PM_Config.session = false
	PM_Config.percent = true
	PM_Config.number = true
	PM_Config.savedVariables = true
end

local function SpewMessage(handle, msg)
	if handle == "loaded" then
		print(PM_LOADMSG)
	elseif handle == "help" then
		print(PM_HELPMSG)
		SpewMessage(PM_CONFIG, PM_CONFIGTOOLTIP)
		SpewMessage(PM_RESULTS, PM_RESULTSTOOLTIP)
		SpewMessage(PM_EPIC, PM_EPICTOOLTIP)
		SpewMessage(PM_RARE, PM_RARETOOLTIP)
		SpewMessage(PM_UNCOMMON, PM_UNCOMMONTOOLTIP)
		SpewMessage(PM_COMMON, PM_COMMONTOOLTIP)
		SpewMessage(PM_POOR, PM_POORTOOLTIP)
		SpewMessage(PM_SESSION, PM_SESSIONTOOLTIP)
		SpewMessage(PM_PERCENT, PM_PERCENTTOOLTIP)
		SpewMessage(PM_NUMBER, PM_NUMBERTOOLTIP)
		SpewMessage(PM_RESET, PM_RESETTOOLTIP)
	else
		print("|cFFFFCC00" .. handle .. "|r - " .. msg)
	end
end

local function ClearEntry(id)
	if PM_ResultsTable[id] then
		wipe(PM_ResultsTable[id])
	end
	if PM_SessionTable[id] then
		wipe(PM_SessionTable[id])
	end
end

local function ResetDatabase()
	if PM_ResultsTable then
		for i, v in pairs(PM_ResultsTable) do
			ClearEntry(i)
		end
		wipe(PM_ResultsTable)
	end
	if PM_SessionTable then
		wipe(PM_SessionTable)
	end
end

local function UpdateConfig()
	
	PM_Config.results = ProspectMeResultsToggle:GetChecked()
	PM_Config.qualities[0] = ProspectMePoorToggle:GetChecked()
	PM_Config.qualities[1] = ProspectMeCommonToggle:GetChecked()
	PM_Config.qualities[2] = ProspectMeUncommonToggle:GetChecked()
	PM_Config.qualities[3] = ProspectMeRareToggle:GetChecked()
	PM_Config.qualities[4] = ProspectMeEpicToggle:GetChecked()
	PM_Config.session = ProspectMeSessionToggle:GetChecked()
	PM_Config.percent = ProspectMePercentToggle:GetChecked()
	PM_Config.number = ProspectMeNumberToggle:GetChecked()
		
	if (PM_Config.results) then
		
		ProspectMePoorToggle:Enable()
		ProspectMeCommonToggle:Enable()
		ProspectMeUncommonToggle:Enable()
		ProspectMeRareToggle:Enable()
		ProspectMeEpicToggle:Enable()

		PM_Config.qualities[0] = ProspectMePoorToggle:GetChecked()
		PM_Config.qualities[1] = ProspectMeCommonToggle:GetChecked()
		PM_Config.qualities[2] = ProspectMeUncommonToggle:GetChecked()
		PM_Config.qualities[3] = ProspectMeRareToggle:GetChecked()
		PM_Config.qualities[4] = ProspectMeEpicToggle:GetChecked()
	
	else
	
		ProspectMePoorToggle:Disable()
		ProspectMeCommonToggle:Disable()
		ProspectMeUncommonToggle:Disable()
		ProspectMeRareToggle:Disable()
		ProspectMeEpicToggle:Disable()
		
	end
end

local function PM_ConfigInit()
	StaticPopupDialogs["PM_ConfirmReset"] = {
  		text = PM_RESETWARNING,
  		button1 = YES,
  		button2 = NO,
  		OnAccept = function()
      		ResetDatabase()
  		end,
  		timeout = 30,
  		whileDead = true,
  		hideOnEscape = true,
  		preferredIndex = 3,
		}

	ProspectMeFrame.name = "Prospect Me"
 
	ProspectMeTitle:SetPoint("TOP", 0, -16)
	ProspectMeTitle:SetText(GetAddOnMetadata("ProspectMe", "Title") .. " v" .. GetAddOnMetadata("ProspectMe", "Version"))
   	
	ProspectMeSessionToggle:SetPoint( "TOPLEFT", 32, -46 )
	ProspectMeSessionToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeSessionToggle:SetChecked(PM_Config.session)
	ProspectMeSessionToggleText:SetText(PM_SESSIONTOGGLE)
	ProspectMeSessionToggle.tooltipText = PM_SESSIONTOOLTIP
	
	ProspectMePercentToggle:SetPoint( "TOPLEFT", 32, -70 )
	ProspectMePercentToggle:SetScript("OnClick", UpdateConfig)
	ProspectMePercentToggle:SetChecked(PM_Config.percent)
	ProspectMePercentToggleText:SetText(PM_PERCENTTOGGLE)
	ProspectMePercentToggle.tooltipText = PM_PERCENTTOOLTIP
  
	ProspectMeNumberToggle:SetPoint( "TOPLEFT", 32, -94 )
	ProspectMeNumberToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeNumberToggle:SetChecked(PM_Config.number)
	ProspectMeNumberToggleText:SetText(PM_NUMBERTOGGLE)
	ProspectMeNumberToggle.tooltipText = PM_NUMBERTOOLTIP
	
	ProspectMeResultsToggle:SetPoint( "TOPLEFT", 32, -118 )
	ProspectMeResultsToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeResultsToggle:SetChecked(PM_Config.results)
	ProspectMeResultsToggleText:SetText(PM_RESULTSTOGGLE)
	ProspectMeResultsToggle.tooltipText = PM_RESULTSTOOLTIP

	ProspectMeEpicToggle:SetPoint( "TOPLEFT", 60, -142 )
	ProspectMeEpicToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeEpicToggle:SetChecked(PM_Config.qualities[4])
	ProspectMeEpicToggleText:SetText(PM_EPICTOGGLE)
	ProspectMeEpicToggle.tooltipText = PM_EPICTOOLTIP
	
	ProspectMeRareToggle:SetPoint( "TOPLEFT", 60, -166 )
	ProspectMeRareToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeRareToggle:SetChecked(PM_Config.qualities[3])
	ProspectMeRareToggleText:SetText(PM_RARETOGGLE)
	ProspectMeRareToggle.tooltipText = PM_RARETOOLTIP
		
	ProspectMeUncommonToggle:SetPoint( "TOPLEFT", 60, -190 )
	ProspectMeUncommonToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeUncommonToggle:SetChecked(PM_Config.qualities[2])
	ProspectMeUncommonToggleText:SetText(PM_UNCOMMONTOGGLE)
	ProspectMeUncommonToggle.tooltipText = PM_UNCOMMONTOOLTIP
	
	ProspectMeCommonToggle:SetPoint( "TOPLEFT", 60, -214 )
	ProspectMeCommonToggle:SetScript("OnClick", UpdateConfig)
	ProspectMeCommonToggle:SetChecked(PM_Config.qualities[1])
	ProspectMeCommonToggleText:SetText(PM_COMMONTOGGLE)
	ProspectMeCommonToggle.tooltipText = PM_COMMONTOOLTIP
	
	ProspectMePoorToggle:SetPoint( "TOPLEFT", 60, -238 )
	ProspectMePoorToggle:SetScript("OnClick", UpdateConfig)
	ProspectMePoorToggle:SetChecked(PM_Config.qualities[0])
	ProspectMePoorToggleText:SetText(PM_POORTOGGLE)
	ProspectMePoorToggle.tooltipText = PM_POORTOOLTIP
	
	if (PM_Config.results == false) then
		ProspectMePoorToggle:Disable()
		ProspectMeCommonToggle:Disable()
		ProspectMeUncommonToggle:Disable()
		ProspectMeRareToggle:Disable()
		ProspectMeEpicToggle:Disable()
	end

	ProspectMeResetButton:SetPoint("TOP", 0, -310 )
	ProspectMeResetButton:SetScript("OnClick", 	function(self) StaticPopup_Show("PM_ConfirmReset") end)
	ProspectMeResetButton:SetSize(250, 25)
	ProspectMeResetButton:SetText(PM_RESETBUTTON)

	ProspectMeResetText:SetPoint("TOP", 0, -286)
	ProspectMeResetText:SetText(PM_RESETTOOLTIP)

	InterfaceOptions_AddCategory(ProspectMeFrame)

end

function ProspectMeEventHandler(event, arg1)
	if (PM_Config.savedVariables) then
		PM_ConfigInit()
	else
		SetDefaults()
		PM_ConfigInit()
	end
	SpewMessage("loaded")
end

function SlashCmdList.PROSPECTME(msg, editbox)
	if msg == PM_CONFIG then
		InterfaceOptionsFrame_OpenToCategory(ProspectMeFrame)
	elseif msg == PM_RESULTS then
		ProspectMeResultsToggle:SetChecked(not ProspectMeResultsToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_EPIC then
		ProspectMeEpicToggle:SetChecked(not ProspectMeEpicToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_RARE then
		ProspectMeRareToggle:SetChecked(not ProspectMeRareToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_UNCOMMON then
		ProspectMeUncommonToggle:SetChecked(not ProspectMeUncommonToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_COMMON then
		ProspectMeCommonToggle:SetChecked(not ProspectMeCommonToggle:GetChecked())
		UpdateConfig()		
	elseif msg == PM_POOR then
		ProspectMePoorToggle:SetChecked(not ProspectMePoorToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_SESSION then
		ProspectMeSessionToggle:SetChecked(not ProspectMeSessionToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_PERCENT then
		ProspectMePercentToggle:SetChecked(not ProspectMePercentToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_NUMBER then
		ProspectMeNumberToggle:SetChecked(not ProspectMeNumberToggle:GetChecked())
		UpdateConfig()
	elseif msg == PM_RESET then
		StaticPopup_Show("PM_ConfirmReset")
	else
		SpewMessage("help")
	end
end

ProspectMeFrame:RegisterEvent("VARIABLES_LOADED")
ProspectMeFrame:SetScript("OnEvent", ProspectMeEventHandler)