view RatPageModel.lua @ 5:8428fa7cf0e4

Updated the profile to use macros and rename all the variables and functions with the prefix CyborgMMO. Added a tooltip to inform the user to assign the profile.
author madcatzinc@35b17cf1-18cd-47ff-9ca3-31d6b526ef09
date Tue, 29 May 2012 10:26:40 +0000
parents d186f8cd5000
children 6cb9a2936580
line wrap: on
line source
--~ Warcraft Plugin for Cyborg MMO7 
--~ Filename: RatPageModel.lua
--~ Description: Code model of the MMO7 mouse
--~ 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.
-- Constants --

local RAT7 = { BUTTONS = 13, MODES = 3, SHIFT = 0}

local MIDDLEMOUSE = 1;

CyborgMMO_RatPageModel = {
	new = function()
	local self = {}
		self.m_Mode = 1;
		self.ObserverCount = 0;
		self.ObserverList = {}
		self.Data = {}

		for i = 1,RAT7.MODES do
			self.Data[i] = {}
			for j = 1, RAT7.BUTTONS do
				self.Data[i][j] = {};
			end
		end

		self.InitSaveData = function(data)
			for i = 1,RAT7.MODES do
				if (nil == data["Rat"][i]) then
					data["Rat"][i] = {}
				end
				for j = 1, RAT7.BUTTONS do
					if (nil == data["Rat"][i][j]) then
						data["Rat"][i][j] = {}
					end
				end
			end
		end


		self.LoadData = function()
			msg("Loading...")
			local data = CyborgMMO_GetSaveData();


			if (nil == data["Rat"]) then
				data["Rat"] = {}
				self.InitSaveData(data);
			end

			self.Data = data["Rat"]
			if(data ~= nil) then
				for mode = 1,RAT7.MODES do
					for button = 1, RAT7.BUTTONS do
						if(self.Data[mode][button] ~= nil) then
							object = CyborgMMO_WowObject.Create(self.Data[mode][button].Type, self.Data[mode][button].Detail, self.Data[mode][button].Subdetail);
							self.SetObjectOnButtonNoUpdate(button, mode, object);
						else
							object = CyborgMMO_WowObject.Create("", "", "");
							self.SetObjectOnButtonNoUpdate(button, mode, object);
							self.Data[mode][button] = object;
						end
					end
				end
				self.UpdateObservers();
			end
		end

		self.SaveData = function()
			msg("Saving...")
			CyborgMMO_SetSaveData(self.Data, "Rat");
		end

		self.SetMode = function(mode)
			self.m_Mode = mode;
			self.UpdateObservers();
		end

		self.GetMode = function()
			return self.m_Mode;
		end

		self.GetData = function()
			return self.Data, self.m_Mode;
		end

		self.GetObjectOnButton = function(button)
			if(nil == self.Data[self.m_Mode][button]) then
				return nil;
			else
				return self.Data[self.m_Mode][button]
			end
		end

		self.SetObjectOnButtonNoUpdate = function(button, mode, object)
			--msg("button = "..tostring(button).." mode = "..tostring(mode))
			self.Data[mode][button] = object;

			if(nil ~= object) then
				object.SetBinding(CyborgMMO_WowCommands[((mode-1)*RAT7.BUTTONS)+button]);
				if("callback" == object.Type) then
					msg("trying to set texture")
					local slot = getglobal("CyborgMMO_MainPageSlotListSlot"..button);
					slot:SetNormalTexture(object.Texture)
				end
			else
				msg("clearing "..button)
				CyborgMMO_WowObject.ClearBinding(CyborgMMO_WowCommands[((mode-1)*RAT7.BUTTONS)+button])
			end
		end

		self.SetObjectOnButton = function(button, mode, object)
			self.SetObjectOnButtonNoUpdate(button, mode, object);
			self.UpdateObservers()
		end

		self.AddObserver = function(view)
			table.insert(self.ObserverList,view)
			self.observerCount = # self.ObserverList
		end
		
		self.GetAllObservers = function()
			return self.ObserverList;
		end

		self.UpdateObservers = function()
			for i = 1, (# self.ObserverList) do
				self.ObserverList[i].Update(self.Data, self.m_Mode)
			end
			self.SaveData()
		end
	return self;
	end,

	m_Instance = nil,

	Instance = function()
		if(nil == CyborgMMO_RatPageModel.m_Instance) then
			CyborgMMO_RatPageModel.m_Instance = CyborgMMO_RatPageModel.new();
		end
		return CyborgMMO_RatPageModel.m_Instance;
	end
}