view CyborgMMO7.lua @ 34:6ce173840e68

Reworked the whole "wow object" system: - Only save what is strictly necessary. - Save appropriate persistent information for all objects (like spellIDs instead of spellBook+spellIndex). - Fixed Battle Pets objects (non-combat pets in pre-MoP). - Fixed item objects. - Cleaned and simplified most objects implementation. - Moved the settings and button profile to the root of the saved data, rather than in a per-character sub-table (that data is already tagged as saved per character). This should fix most issues with objects changing without user interaction on diverse occasions. Old profiles are not converted to the new system. This will come soon. Some issues persist due to the asynchronous loading of some informations: - Pet icons are never properly loaded from saved data. - Items are not properly loaded the first time the UI is started (a "/reload ui" or disconnect/connect cycle fixes this problem).
author madcatzinc@35b17cf1-18cd-47ff-9ca3-31d6b526ef09
date Thu, 25 Apr 2013 01:31:31 +0000
parents fc5f52d60d16
children 16b2ff47b6db
line wrap: on
line source
--~ Warcraft Plugin for Cyborg MMO7
--~ Filename: CyborgMMO7.lua
--~ Description: Plugin entry point, String tables and other generic crap that I could not think to put anywhere else.
--~ Copyright (C) 2012 Mad Catz Inc.
--~ Author: Christopher Hooks

--~ This program is free software; you can redistribute it and/or
--~ modify it under the terms of the GNU General Public License
--~ as published by the Free Software Foundation; either version 2
--~ of the License, or (at your option) any later version.

--~ This program is distributed in the hope that it will be useful,
--~ but WITHOUT ANY WARRANTY; without even the implied warranty of
--~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--~ GNU General Public License for more details.

--~ You should have received a copy of the GNU General Public License
--~ along with this program; if not, write to the Free Software
--~ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

function CyborgMMO_LoadStrings(self)
--	CyborgMMO_DPrint("LoadStrings("..self:GetName()..") = "..CyborgMMO_StringTable[self:GetName()])
	self:SetText(CyborgMMO_StringTable[self:GetName()])
end

local VarsLoaded = false
local EnteredWorld = false
local BindingsLoaded = false
local SettingsLoaded = false
local SaveName = GetRealmName().."_"..UnitName("player")
local Settings = nil


function CyborgMMO_MiniMapButtonReposition(pos)
	CyborgMMO_MiniMapFrame:SetPoint("TOPLEFT",
		"Minimap",
		"TOPLEFT",
		52 - (80 * cos(pos)),
		(80 * sin(pos)) - 52)
end


function CyborgMMO_MiniMapButtonOnUpdate()

	local xpos,ypos = GetCursorPosition()
	local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()

	xpos = (xmin)-(xpos / UIParent:GetScale())
	ypos = (ypos/ UIParent:GetScale())-(ymin)

	local degrees = math.deg(math.atan2(ypos, xpos))

	while degrees < 0 do
		degrees = degrees + 360
	end

	CyborgMMO_MiniMapButtonReposition(degrees)
end

function CyborgMMO_MouseModeChange(mode)
	local MiniMapTexture = getglobal("CyborgMMO_MiniMapFrameButton_Icon")
	local MiniMapGlowTexture = getglobal("CyborgMMO_MiniMapFrameButton_IconGlow")
	local OpenButtonTexture = getglobal("CyborgMMO_OpenButtonPage".."OpenMainForm"):GetNormalTexture()
	local OpenButtonGlowTexture = getglobal("CyborgMMO_OpenButtonPage".."OpenMainForm"):GetHighlightTexture()
	if mode == 1 then
		MiniMapTexture:SetVertexColor(1,0,0,1)
		MiniMapGlowTexture:SetVertexColor(1,0.26,0.26,.75)
		OpenButtonTexture:SetVertexColor(1,0,0,0.75)
		OpenButtonGlowTexture:SetVertexColor(1,0.26,0.26,0.50)
	elseif mode == 2 then
		MiniMapTexture:SetVertexColor(0.07,0.22,1,1)
		MiniMapGlowTexture:SetVertexColor(0.13,0.56,1,.75)
		OpenButtonTexture:SetVertexColor(0.07,0.22,1,0.75)
		OpenButtonGlowTexture:SetVertexColor(0.13,0.56,1,0.5)
	elseif mode == 3 then
		MiniMapTexture:SetVertexColor(0.52,0.08,0.89,1)
		MiniMapGlowTexture:SetVertexColor(0.67,0.31,0.85,.75)
		OpenButtonTexture:SetVertexColor(0.52,0.08,0.89,0.75)
		OpenButtonGlowTexture:SetVertexColor(0.67,0.31,0.85,0.5)
	end
end

function CyborgMMO_GetSaveData()
	assert(VarsLoaded)
	if not CyborgMMO7SaveData then
		CyborgMMO7SaveData = {}
	end
	return CyborgMMO7SaveData
end

function CyborgMMO_SetRatSaveData(data)
	assert(VarsLoaded)
	local saveData = {}
	for mode=1,#data do
		saveData[mode] = {}
		for button=1,#data[mode] do
			if data[mode][button] then
				saveData[mode][button] = data[mode][button]:SaveData()
			end
		end
	end
	CyborgMMO_GetSaveData().Rat = saveData
end

function CyborgMMO_Event(self, event, ...)
	if event == "VARIABLES_LOADED" then
		VarsLoaded = true
	elseif event == "PLAYER_ENTERING_WORLD" then
		EnteredWorld = true
	elseif event == "PLAYER_REGEN_DISABLED" then
		CyborgMMO_Close()
	else
		CyborgMMO_DPrint("Event is "..tostring(event))
	end

	-- Fire Loading if and only if the player is in the world and vars are loaded
	if not BindingsLoaded and VarsLoaded and EnteredWorld then
		local data = CyborgMMO_GetSaveData()

		CyborgMMO_RatPageModel:LoadData()
		BindingsLoaded = true

		CyborgMMO_SetupModeCallbacks(1)
		CyborgMMO_SetupModeCallbacks(2)
		CyborgMMO_SetupModeCallbacks(3)

		Settings = data.Settings
		if not Settings then
			Settings = {}
			Settings.Cyborg = 0.75
			Settings.Plugin = 0.75
			Settings.MiniMapButton = true
			Settings.CyborgButton = true
			data.Settings = Settings
		end
		SettingsLoaded = true

		-- Reload Slider values:
		CyborgMMO_SetOpenButtonSize(Settings.Cyborg)
		CyborgMMO_SetMainPageSize(Settings.Plugin)

		CyborgMMO_SetMiniMapButton(Settings.MiniMapButton)
		CyborgMMO_SetCyborgHeadButton(Settings.CyborgButton)

		local xmin,ymin = Minimap:GetLeft(),Minimap:GetBottom()
		CyborgMMO_MiniMapButtonReposition(math.deg(math.atan2(ymin, xmin)))
		-- Close the main window for now
		CyborgMMO_Close()
	end
end

function CyborgMMO_SetDefaultSettings()
	CyborgMMO_OpenButtonPageOpenMainForm:ClearAllPoints()
	CyborgMMO_MainPage:ClearAllPoints()
	CyborgMMO_OpenButtonPageOpenMainForm:SetPoint("LEFT", UIParent, "LEFT", 0, 0)
	CyborgMMO_MainPage:SetPoint("LEFT", UIParent, "LEFT", 0, 0)

	CyborgMMO_SetOpenButtonSize(0.75)
	CyborgMMO_SetMainPageSize(0.75)
	CyborgMMO_SetMiniMapButton(true)
	CyborgMMO_SetCyborgHeadButton(true)
end


function CyborgMMO_SetupModeCallbacks(modeNum)
	local fn = function()
		CyborgMMO_MouseModeChange(modeNum)
		CyborgMMO_RatPageModel:SetMode(modeNum)
	end

	local buttonFrame,parentFrame,name = CyborgMMO_CallbackFactory:AddCallback(fn)
	SetOverrideBindingClick(parentFrame, true, CyborgMMO_Mode[modeNum], name, "LeftButton")
end

function CyborgMMO_Loaded()
	CyborgMMO_MainPage:RegisterEvent("VARIABLES_LOADED")
	CyborgMMO_MainPage:RegisterEvent("PLAYER_ENTERING_WORLD")
	CyborgMMO_MainPage:RegisterEvent("PLAYER_REGEN_DISABLED")
end

function CyborgMMO_Close()
	CyborgMMO_MainPage:Hide()
end

function CyborgMMO_Open()
	CyborgMMO_MainPage:Show()
	CyborgMMO_RatQuickPage:Hide()
end

function CyborgMMO_IsOpen()
	if CyborgMMO_MainPage:IsVisible() == 1 then
		return true
	else
		return false
	end
end

function CyborgMMO_Toggle()
	if CyborgMMO_IsOpen() then
		CyborgMMO_Close()
	else
		CyborgMMO_Open()
	end
end

function CyborgMMO_GetDebugFrame()
	for i=1,NUM_CHAT_WINDOWS do
		local windowName = GetChatWindowInfo(i);
		if windowName == "Debug" then
			return getglobal("ChatFrame" .. i)
		end
	end
end

local log_prefix = "|cffff6666".."CyborgMMO".."|r:"

function CyborgMMO_DPrint(...)
	local debugframe = CyborgMMO_GetDebugFrame()
	if debugframe then
		local t = {log_prefix, ...}
		for i=1,select('#', ...)+1 do
			t[i] = tostring(t[i])
		end
		debugframe:AddMessage(table.concat(t, ' '))
	end
end

function CyborgMMO_SetMainPageSize(percent)
	CyborgMMO_MainPage:SetScale(percent)
	CyborgMMO_OptionPagePluginSizeSlider:SetValue(percent)
	if BindingsLoaded then
		Settings.Plugin = percent
	end
end

function CyborgMMO_SetOpenButtonSize(percent)
	CyborgMMO_OpenButtonPage:SetScale(percent)
	CyborgMMO_OptionPageCyborgSizeSlider:SetValue(percent)
	if SettingsLoaded then
		Settings.Cyborg = percent
	end
end

function CyborgMMO_SetCyborgHeadButton(visible)
	if visible then
		CyborgMMO_OpenButtonPage:Show()
	else
		CyborgMMO_OpenButtonPage:Hide()
	end
	CyborgMMO_OptionPageCyborgButton:SetChecked(visible)
	if SettingsLoaded then
		Settings.CyborgButton = visible
	end
end

function CyborgMMO_SetMiniMapButton(visible)
	if visible then
		CyborgMMO_MiniMapFrameButton:Show()
	else
		CyborgMMO_MiniMapFrameButton:Hide()
	end
	CyborgMMO_OptionPageMiniMapButton:SetChecked(visible)
	if SettingsLoaded then
		Settings.MiniMapButton = visible
	end
end