class DProps : LuaWidget;

class DPropsShared
{
	def mWidgetTypes = {};
	def mWidgetTypeOrder = {};
	def mObjects = {};
	def mWidgetToLuaWidget = {};
	def mCurAddWidgetType;
	def mLastAddWidgetX;
	def mLastAddWidgetY;
	def mGridOn;
	def mGridSizeX;
	def mGridSizeY;
	def mGridOfsX;
	def mGridOfsY;
}

function DPropsShared:Load()
{
	mLastAddWidgetX = gCApp:RegistryRead("LastAddWidgetX");
	mLastAddWidgetY = gCApp:RegistryRead("LastAddWidgetY");
	mGridOn = gCApp:RegistryRead("GridOn", false);
	mGridSizeX = gCApp:RegistryRead("GridSizeX", 8);
	mGridSizeY = gCApp:RegistryRead("GridSizeY", 8);
	mGridOfsX = gCApp:RegistryRead("GridOfsX", 0);
	mGridOfsY = gCApp:RegistryRead("GridOfsY", 0);
}

function DPropsShared:Save()
{
	gCApp:RegistryWrite("LastAddWidgetX", mLastAddWidgetX);
	gCApp:RegistryWrite("LastAddWidgetY", mLastAddWidgetY);
	gCApp:RegistryWrite("GridOn", mGridOn);
	gCApp:RegistryWrite("GridSizeX", mGridSizeX);
	gCApp:RegistryWrite("GridSizeY", mGridSizeY);
	gCApp:RegistryWrite("GridOfsX", mGridOfsX);
	gCApp:RegistryWrite("GridOfsY", mGridOfsY);
}

DProps.mShared = DPropsShared:new();
DProps.mEditing = false;

function dptrack(thePos, theTrackInfo)
{
	local anObj = theTrackInfo[0];
	
	// Get object tab
	local anObjTab = DProps.mShared.mObjects[anObj];
	
	// Get file table
	local aFileTab = anObjTab.mFiles[thePos[0]];
	if (aFileTab == nil)
	{
		aFileTab = {};
		anObjTab.mFiles[thePos[0]] = aFileTab;
	}
	
	// Store setting information
	aFileTab[thePos[1]] = {theTrackInfo[1], theTrackInfo[2], theTrackInfo[3], theTrackInfo[4]};
	
	// Get props table
	for (aPropNum < *theTrackInfo[3])
	{
		anObjTab.mProps[theTrackInfo[3][aPropNum]] = theTrackInfo[4][aPropNum];
		local aPropsPos = anObjTab.mPropsPos[theTrackInfo[3][aPropNum]];
		if (aPropsPos != nil)
			anObjTab.mFiles[aPropsPos[0]][aPropsPos[1]] = nil; // Remove the old entry
		anObjTab.mPropsPos[theTrackInfo[3][aPropNum]] = thePos;
	}
}

function DProps.AddWidgetType(theName, theClass, theImage, theImageNum)
{
	if (*DProps.mShared.mWidgetTypeOrder == 0)
		DProps.mShared.mCurAddWidgetType = theName;
	
	if (DProps.mShared.mWidgetTypes[theName] == nil)
		DProps.mShared.mWidgetTypeOrder[*DProps.mShared.mWidgetTypeOrder] = theName;
	
	DProps.mShared.mWidgetTypes[theName] = 
		{mName=theName, mClass=theClass, mImage=theImage, mImageNum=theImageNum};
}

function DProps.WidgetCreated(theWidget)
{
	local aCWidget = theWidget.mCObj:GetWidget();
	DProps.mShared.mWidgetToLuaWidget[aCWidget] = theWidget;
	
	local anObjTab = {mProps={}, mPropsPos={}, mFiles={}, mDrawOrder=0};
	DProps.mShared.mObjects[theWidget] = anObjTab;
}

function DProps.WidgetDeleted(theWidget)
{
	DProps.mShared.mObjects[theWidget] = nil;
	local aCWidget = theWidget.mCObj:GetWidget();
	DProps.mShared.mWidgetToLuaWidget[aCWidget] = nil;
}

//////////////////////////////////////////////////////////////////////////

function DProps.DProps_Loader(theCObj)
{
	DProps.mShared:Load();
	require("DProps_Editor.lua");
	return DProps:new(theCObj);
}

RegisterLinkedConstructor("DPropsWidget", DProps.DProps_Loader);
