// Tools_GUIMenu_Widget_Base.cpp
//
// Created by Richard Rice - 30/11/2006
// 
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "GUIMenu/Tools_GUIMenu_Widget_Base.h"

#ifdef ASURA_USE_GUI_MENU_SYSTEM

#include "GUIMenu/Asura_GUIMenu_Widget_Base.h"
#include "Interface/Asura_Dialog.h"
#include "Interface/Asura_Overlay.h"
#include "Rendering/Asura_Render.h"
#include "Rendering/Asura_Text_Output.h"

#include "GUIMenu/Tools_Mode_GUIMenu.h"
#include "Misc/Tools_Modal_Overlay.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*-----------------------------**
** Tools_GUIMenu_PickItem_Link **
**-----------------------------*/

class Tools_GUIMenu_PickItem_Link : public Tools_GUIMenu_PickItem
{
public :
	Tools_GUIMenu_PickItem_Link(Tools_GUIMenu_Widget_Base* pxWidget,int iLink)
	{
		m_pxWidget = pxWidget;
		m_iLink = iLink;
	}

	virtual const char* GetPrompt(){return "Select linked widget";}

	virtual bool IsValidSelection(Tools_GUIMenu_Root* pxItem)
	{
		if(pxItem)
		{
			if(pxItem == m_pxWidget) return false;
			if ( pxItem->IsWidget() )
			{
				return Tools_GUIMenu_Widget_Base::GetCanBeSelectable(pxItem);
			}
			return false;
		}
		return true;
	}

	virtual void OnFinish(Tools_GUIMenu_Root* pxItem)
	{
		Asura_Assert(pxItem==0 || pxItem->IsWidget(),"");
		m_pxWidget->SetLink(m_iLink,(Tools_GUIMenu_Widget_Base*)pxItem);
	}

private :
	Tools_GUIMenu_Widget_Base* m_pxWidget;
	int m_iLink;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*---------------------------**
** Tools_GUIMenu_Widget_Base **
**---------------------------*/


bool Tools_GUIMenu_Widget_Base::s_bShowLinks = true;
bool Tools_GUIMenu_Widget_Base::s_bTwoWayLinks = true;

Tools_GUIMenu_Widget_Base::Tools_GUIMenu_Widget_Base(Asura_GUIMenu_Widget_Base* pxAsuraWidget,Tools_GUIMenu_Root* pxToolsParent)
:Tools_GUIMenu_Root(pxAsuraWidget,pxToolsParent)
{
	m_pxAsuraWidget = pxAsuraWidget;
}

void Tools_GUIMenu_Widget_Base::Update()
{
	PARENT::Update();
}

void Tools_GUIMenu_Widget_Base::InitialiseNewItem()
{
	PARENT::InitialiseNewItem();
}

void Tools_GUIMenu_Widget_Base::AddBasicOverlayControls(Asura_Overlay* pxOverlay,Asura_Vector_2& xOffset)
{
	PARENT::AddBasicOverlayControls(pxOverlay,xOffset);
}

void Tools_GUIMenu_Widget_Base::AddOverlayControls(Asura_Overlay* pxOverlay,Asura_Vector_2& xOffset)
{
	PARENT::AddOverlayControls(pxOverlay,xOffset);

	Asura_Dialog* pxDialog;

	pxDialog = new Asura_FunctionTickbox_Dialog("Selectable",xOffset,this,GetCanBeSelectable,SetCanBeSelectable);
	pxDialog->SetIsEnabledFunction(CanEditSelectable,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_FunctionTickbox_Dialog("Always Selectable",xOffset,this,GetIsAlwaysSelectable,SetIsAlwaysSelectable);
	pxDialog->SetIsEnabledFunction(CanEditIsAlwaysSelectable,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::LargeSpacing();

	pxDialog = new Asura_StringDisplay_Dialog("Add...",xOffset);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	float fX = xOffset.x;

	pxDialog = new Asura_Function_Dialog("Text",xOffset,AddElement_Text,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.x = pxDialog->GetMaxCoords().x + 1.0f;

	pxDialog = new Asura_Function_Dialog("Image",xOffset,AddElement_Image,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.x = pxDialog->GetMaxCoords().x + 1.0f;

	pxDialog = new Asura_Function_Dialog("Path",xOffset,AddElement_Path,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.x = pxDialog->GetMaxCoords().x + 1.0f;

	pxDialog = new Asura_Function_Dialog("Status Text",xOffset,AddElement_StatusText,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.x = fX;
	xOffset.y += Asura_Dialog::LargeSpacing();

	pxDialog = new Asura_Function_Dialog("Reset Nav Links",xOffset, ResetNavLinks, (int) this);
	pxDialog->SetHelpText("Resets any Nav Link that is editable.");
	pxDialog->m_iControlID = Asura_GetHashID("ResetLinks");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	//navigation options
	pxDialog = new Asura_Tickbox_Dialog("Show Links",xOffset,&s_bShowLinks);
	pxDialog->SetHelpText("Shows the navigation links to connected widgets");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Tickbox_Dialog("Add Reverse Links",xOffset,&s_bTwoWayLinks);
	pxDialog->SetHelpText("If ticked then adding a link to another widget will also cause the reverse link to be created.");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Function_Dialog("Up : ",xOffset, EditUpLink, (int) this);
	pxDialog->SetIsEnabledFunction(CanEditNavUp,(int)this);
	pxDialog->SetHelpText("Edit this navigation link.");
	pxDialog->m_iControlID = Asura_GetHashID("Up");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Function_Dialog("Down : ",xOffset, EditDownLink, (int) this);
	pxDialog->SetIsEnabledFunction(CanEditNavDown,(int)this);
	pxDialog->SetHelpText("Edit this navigation link.");
	pxDialog->m_iControlID = Asura_GetHashID("Down");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Function_Dialog("Left : ",xOffset, EditLeftLink, (int) this);
	pxDialog->SetIsEnabledFunction(CanEditNavLeft,(int)this);
	pxDialog->SetHelpText("Edit this navigation link.");
	pxDialog->m_iControlID = Asura_GetHashID("Left");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Function_Dialog("Right : ",xOffset, EditRightLink, (int) this);
	pxDialog->SetIsEnabledFunction(CanEditNavRight,(int)this);
	pxDialog->SetHelpText("Edit this navigation link.");
	pxDialog->m_iControlID = Asura_GetHashID("Right");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();
}

void Tools_GUIMenu_Widget_Base::AddExtraOverlayControls(Asura_Overlay* pxOverlay, Asura_Tabs_Dialog* pxTabs, Asura_Vector_2& xOffset)
{
	//Create a tab
	pxTabs->AddTab("Events");
	
	// On event commands...

	// roll on command
	bool bEnableRollInCommand = CanEditOnRollInCommand();
	m_pxAsuraWidget->m_xOnRollInCommand.AddOverlayControls( pxOverlay, xOffset, "On Roll-In Command", "Calls this command when the widget is given focus", true, &m_bChanged, bEnableRollInCommand );

	// roll off command
	bool bEnableRollOutCommand = CanEditOnRollOutCommand();
	m_pxAsuraWidget->m_xOnRollOutCommand.AddOverlayControls( pxOverlay, xOffset, "On Roll-Out Command", "Calls this command when the widget loses focus", true, &m_bChanged, bEnableRollOutCommand );

}

void Tools_GUIMenu_Widget_Base::UpdateOverlayControls(Asura_Overlay* pxOverlay)
{
	PARENT::UpdateOverlayControls(pxOverlay);

	UpdateLinkControl(pxOverlay,"Up : ",Asura_GetHashID("Up"), ASURA_GUIMENU_UI_NAV_EXIT_UP);
	UpdateLinkControl(pxOverlay,"Down : ",Asura_GetHashID("Down"), ASURA_GUIMENU_UI_NAV_EXIT_DOWN);
	UpdateLinkControl(pxOverlay,"Left : ",Asura_GetHashID("Left"), ASURA_GUIMENU_UI_NAV_EXIT_LEFT);
	UpdateLinkControl(pxOverlay,"Right : ",Asura_GetHashID("Right"), ASURA_GUIMENU_UI_NAV_EXIT_RIGHT);
}

void Tools_GUIMenu_Widget_Base::UpdateLinkControl(Asura_Overlay* pxOverlay,const char* szBaseText,Asura_Hash_ID uControlID,int iLink)
{
	//find the linked control
	Asura_Hash_ID uID = m_pxAsuraWidget->GetNavExitID_Raw((Asura_GUIMenu_Navigation_Exit)iLink);

	const char* szName;
	if(uID)
	{
		Tools_GUIMenu_Widget_Base* pxWidget = GetMenuPage()->FindWidget(uID);

		szName = pxWidget ? pxWidget->GetName() : "*Unknown*";
	}
	else
	{
		szName = "*None*";
	}

	char szText[100];
	Asura_PrintfToBfr(szText,"%s%s",sizeof(szText),true,szBaseText,szName);
	
	pxOverlay->UpdateDialogLabel(uControlID,szText);
}

bool Tools_GUIMenu_Widget_Base::CheckLinksCanBeEdited()
{
	if (!m_pxAsuraWidget->m_bCanBeSelectable)
	{
		if (!CanEditSelectable())
		{
			ShowMessageOverlay("This widget is not flagged as selectable, and the style prevents it from being made selectable, so you can't edit links");
			return false;
		}
		if(ShowYesNoOverlay("This widget is not flagged as selectable; adding links will make it selectable.\nGo ahead and flag this as being selectable?"))
		{
			m_pxAsuraWidget->m_bCanBeSelectable = true;
			m_bChanged = true;
			return true;
		}
		else
		{
			return false;
		}
	}
	return true;
}

void Tools_GUIMenu_Widget_Base::AddElement_Text()
{
	Tools_Mode_GUIMenu::GetInstance()->StartMenuItemPlacement(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_TEXT);
}

void Tools_GUIMenu_Widget_Base::AddElement_Image()
{
	Tools_Mode_GUIMenu::GetInstance()->StartMenuItemPlacement(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_IMAGE);
}

void Tools_GUIMenu_Widget_Base::AddElement_Path()
{
	Tools_Mode_GUIMenu::GetInstance()->StartMenuItemPlacement(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_PATH);
}

void Tools_GUIMenu_Widget_Base::AddElement_Anim()
{
	Tools_Mode_GUIMenu::GetInstance()->StartMenuItemPlacement(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_ANIMATION);
}

void Tools_GUIMenu_Widget_Base::AddElement_StatusText()
{
	Tools_Mode_GUIMenu::GetInstance()->StartMenuItemPlacement(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_STATUSTEXT);
}

void Tools_GUIMenu_Widget_Base::AddElement_FMV()
{
	Tools_Mode_GUIMenu::GetInstance()->StartMenuItemPlacement(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_FMV);
}

void Tools_GUIMenu_Widget_Base::EditUpLink()
{
	if (CheckLinksCanBeEdited())
	{
		Tools_GUIMenu_PickItem_Link* pxPickItem = new Tools_GUIMenu_PickItem_Link(this, ASURA_GUIMENU_UI_NAV_EXIT_UP);
		Tools_Mode_GUIMenu::GetInstance()->StartPromptedSelection(pxPickItem);
	}
}

void Tools_GUIMenu_Widget_Base::EditDownLink()
{
	if (CheckLinksCanBeEdited())
	{
		Tools_GUIMenu_PickItem_Link* pxPickItem = new Tools_GUIMenu_PickItem_Link(this, ASURA_GUIMENU_UI_NAV_EXIT_DOWN);
		Tools_Mode_GUIMenu::GetInstance()->StartPromptedSelection(pxPickItem);
	}
}

void Tools_GUIMenu_Widget_Base::EditLeftLink()
{
	if (CheckLinksCanBeEdited())
	{
		Tools_GUIMenu_PickItem_Link* pxPickItem = new Tools_GUIMenu_PickItem_Link(this, ASURA_GUIMENU_UI_NAV_EXIT_LEFT);
		Tools_Mode_GUIMenu::GetInstance()->StartPromptedSelection(pxPickItem);
	}
}

void Tools_GUIMenu_Widget_Base::EditRightLink()
{
	if (CheckLinksCanBeEdited())
	{
		Tools_GUIMenu_PickItem_Link* pxPickItem = new Tools_GUIMenu_PickItem_Link(this, ASURA_GUIMENU_UI_NAV_EXIT_RIGHT);
		Tools_Mode_GUIMenu::GetInstance()->StartPromptedSelection(pxPickItem);
	}
}

void Tools_GUIMenu_Widget_Base::ResetNavLinks( void)
{
	if( CanEditNavLeft())
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_LEFT, ASURA_HASH_ID_UNSET);
	}

	if( CanEditNavRight())
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_RIGHT, ASURA_HASH_ID_UNSET);
	}

	if( CanEditNavUp())
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_UP, ASURA_HASH_ID_UNSET);
	}

	if( CanEditNavDown())
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_DOWN, ASURA_HASH_ID_UNSET);
	}

	SetModified();
	UpdateOverlay();
}

void Tools_GUIMenu_Widget_Base::SetLink(int iLink,Tools_GUIMenu_Widget_Base* pxItem)
{
	Asura_Assert(m_pxAsuraWidget->m_bCanBeSelectable, "shouldn't set link on non-selectable item");

	Asura_Hash_ID uLinkID = pxItem ? pxItem->m_pxAsuraWidget->GetID() : 0;
	m_pxAsuraWidget->SetNavExitID( (Asura_GUIMenu_Navigation_Exit) iLink,uLinkID);

	if(pxItem && s_bTwoWayLinks)
	{
		//add reverse link as well
		int iReverse;
		switch(iLink)
		{
			case ASURA_GUIMENU_UI_NAV_EXIT_LEFT :
				iReverse = ASURA_GUIMENU_UI_NAV_EXIT_RIGHT;
				break;
			case ASURA_GUIMENU_UI_NAV_EXIT_RIGHT :
				iReverse = ASURA_GUIMENU_UI_NAV_EXIT_LEFT;
				break;
			case ASURA_GUIMENU_UI_NAV_EXIT_UP :
				iReverse = ASURA_GUIMENU_UI_NAV_EXIT_DOWN;
				break;
			case ASURA_GUIMENU_UI_NAV_EXIT_DOWN :
				iReverse = ASURA_GUIMENU_UI_NAV_EXIT_UP;
				break;
			default :
				Asura_Assert(0,"Unexpected nav exit");
				break;
		}
		if(pxItem->IsNavExitSupported(iReverse))
		{
			pxItem->m_pxAsuraWidget->SetNavExitID( (Asura_GUIMenu_Navigation_Exit) iReverse, m_pxAsuraWidget->GetID());
		}
	}

	SetModified();

	UpdateOverlay();
}

void Tools_GUIMenu_Widget_Base::RenderUI()
{
	PARENT::RenderUI();

	if(s_bShowLinks)
	{
		RenderLink( ASURA_GUIMENU_UI_NAV_EXIT_LEFT,"L");
		RenderLink( ASURA_GUIMENU_UI_NAV_EXIT_RIGHT,"R");
		RenderLink( ASURA_GUIMENU_UI_NAV_EXIT_UP,"U");
		RenderLink( ASURA_GUIMENU_UI_NAV_EXIT_DOWN,"D");
	}
}

void Tools_GUIMenu_Widget_Base::RenderLink(int iLink,const char* szLabel)
{
	//is there a link?
	Asura_Hash_ID uLinkID = m_pxAsuraWidget->GetNavExitID_Raw( (Asura_GUIMenu_Navigation_Exit)iLink);
	if(!uLinkID) return;

	//find the linked widget
	Tools_GUIMenu_Widget_Base* pxWidget = GetMenuPage()->FindWidget(uLinkID);
	if(!pxWidget) return;

	//draw a line between them
	Asura_Vector_2 xStart = GetRectangle().CalculateCentre();
	Asura_Vector_2 xEnd = pxWidget->GetRectangle().CalculateCentre();

	xStart = Tools_Mode_GUIMenu::ConvertFromMenuCoords(xStart);
	xEnd = Tools_Mode_GUIMenu::ConvertFromMenuCoords(xEnd);

	Asura_Render::Render2DLine(xStart.x,xStart.y,xEnd.x,xEnd.y,0xffffffff);

	//write label at the midpoint
	Asura_Vector_2 xMid = (xStart + xEnd)*0.5f;
	Asura_Text_Output::ConvertPixelsToPrintCoords(xMid.x,xMid.y);
	Asura_Text_Output::Print(xMid.x,xMid.y-0.5f,0xffffff00,ASURA_TEXT_FORMAT_CENTREJUSTIFIED,"%s",szLabel);
	
	
}

void Tools_GUIMenu_Widget_Base::UpdateReferences(Asura_Hash_ID uOldID,Asura_Hash_ID uNewID,bool bPostCopy)
{
	PARENT::UpdateReferences(uOldID, uNewID, bPostCopy);

	bool bAnyChange = false;
	
	if(m_pxAsuraWidget->GetNavExitID_Raw( ASURA_GUIMENU_UI_NAV_EXIT_LEFT) == uOldID)
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_LEFT, uNewID);
		bAnyChange = true;
	}
	if(m_pxAsuraWidget->GetNavExitID_Raw( ASURA_GUIMENU_UI_NAV_EXIT_RIGHT) == uOldID)
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_RIGHT, uNewID);
		bAnyChange = true;
	}
	if(m_pxAsuraWidget->GetNavExitID_Raw( ASURA_GUIMENU_UI_NAV_EXIT_UP) == uOldID)
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_UP, uNewID);
		bAnyChange = true;
	}
	if(m_pxAsuraWidget->GetNavExitID_Raw( ASURA_GUIMENU_UI_NAV_EXIT_DOWN) == uOldID)
	{
		m_pxAsuraWidget->SetNavExitID( ASURA_GUIMENU_UI_NAV_EXIT_DOWN, uNewID);
		bAnyChange = true;
	}

	if(bAnyChange) SetModified();
}

bool Tools_GUIMenu_Widget_Base::IsValidChildType(int iType) const
{
	//allow elements
	return iType >= Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_BEGIN && iType <=Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_END;
}

const char* Tools_GUIMenu_Widget_Base::GetLocalDataFlagName(u_int uFlagValue) const
{
	switch(uFlagValue)
	{
		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_UP :
			return "Nav Exit Up";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_DOWN :
			return "Nav Exit Down";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_LEFT :
			return "Nav Exit Left";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_RIGHT :
			return "Nav Exit Right";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_SELECTABLE:
			return "Selectable";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_ISALWAYSSELECTABLE:
			return "Is Always Selectable";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_ONROLLIN_COMMAND:
			return "On Roll-In Command";
			break;

		case Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_ONROLLOUT_COMMAND:
			return "On Roll-Out Command";
			break;

		default :
			return PARENT::GetLocalDataFlagName(uFlagValue);

	}
}

bool Tools_GUIMenu_Widget_Base::CanBeMoved() const
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Root::ASURA_GUIMENU_ROOT_LOCAL_FLAG_POSITION);
}

bool Tools_GUIMenu_Widget_Base::CanEditNavLeft()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_LEFT) && IsNavExitSupported(ASURA_GUIMENU_UI_NAV_EXIT_LEFT);
}

bool Tools_GUIMenu_Widget_Base::CanEditNavRight()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_RIGHT) && IsNavExitSupported(ASURA_GUIMENU_UI_NAV_EXIT_RIGHT);
}

bool Tools_GUIMenu_Widget_Base::CanEditNavUp()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_UP) && IsNavExitSupported(ASURA_GUIMENU_UI_NAV_EXIT_UP);
}

bool Tools_GUIMenu_Widget_Base::CanEditNavDown()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_NAVID_DOWN) && IsNavExitSupported(ASURA_GUIMENU_UI_NAV_EXIT_DOWN);
}

bool Tools_GUIMenu_Widget_Base::CanEditSelectable()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_SELECTABLE);
}

bool Tools_GUIMenu_Widget_Base::CanEditIsAlwaysSelectable()
{
	return (ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_ISALWAYSSELECTABLE) && GetCanBeSelectable());
}

bool Tools_GUIMenu_Widget_Base::CanEditOnRollInCommand()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_ONROLLIN_COMMAND);
}

bool Tools_GUIMenu_Widget_Base::CanEditOnRollOutCommand()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Base::ASURA_GUIMENU_WIDGET_BASE_LOCAL_FLAG_ONROLLOUT_COMMAND);
}

bool Tools_GUIMenu_Widget_Base::GetCanBeSelectable()
{
	return m_pxAsuraWidget->m_bCanBeSelectable;
}

bool Tools_GUIMenu_Widget_Base::GetIsAlwaysSelectable()
{
	return m_pxAsuraWidget->m_bIsAlwaysSelectable;
}


void Tools_GUIMenu_Widget_Base::SetCanBeSelectable( bool bCanBeSelectable )
{
	// check if selectable flag should change
	if (bCanBeSelectable != m_pxAsuraWidget->m_bCanBeSelectable)
	{
		bool bOKtoChange = true;
		// see if we're removing the selectable flag from a widget with links (or from a master style).
		if (!bCanBeSelectable)
		{
			bool bHasLinks = false;
			for (u_int u = 0; u < ASURA_GUIMENU_UI_NAV_EXIT_MAX; ++u)
			{
				if (m_pxAsuraWidget->GetNavExitID_Raw( Asura_GUIMenu_Navigation_Exit(u) ) != ASURA_HASH_ID_UNSET)
				{
					bHasLinks = true;
					break;
				}
			}

			if (IsStyle())
			{
				if (!ShowYesNoOverlay("This will cause all existing navigation links for any widgets using this style to be cleared.\nGo ahead and unflag this as being selectable?"))
				{
					bOKtoChange = false;
				}
			}
			else if (bHasLinks)
			{
				// If the change is being made by the style don't bother prompting.
				if(!CanEditSelectable() || ShowYesNoOverlay("This will cause all existing navigation links for this widget to be cleared.\nGo ahead and unflag this as being selectable?"))
				{
					// remove all links from this widget
					for( u_int u = 0; u < ASURA_GUIMENU_UI_NAV_EXIT_MAX; ++u)
					{
						m_pxAsuraWidget->m_auHashNavID[u] = ASURA_HASH_ID_UNSET;
					}
					UpdateOverlay();
				}
				else
				{
					// restore to selectable.
					bOKtoChange = false;
				}
			}
		}

		if (bOKtoChange)
		{
			m_pxAsuraWidget->m_bCanBeSelectable = bCanBeSelectable;
			m_bChanged = true;
		}
	}
}

void Tools_GUIMenu_Widget_Base::SetIsAlwaysSelectable( bool bIsAlwaysSelectable )
{
	// check if selectable flag should change
	if (bIsAlwaysSelectable != m_pxAsuraWidget->m_bIsAlwaysSelectable)
	{
		m_pxAsuraWidget->m_bIsAlwaysSelectable = bIsAlwaysSelectable;
		m_bChanged = true;
	}
}


bool Tools_GUIMenu_Widget_Base::Verify()
{
	bool bRes;

	bRes = PARENT::Verify();

	//check for broken nav links

	for( u_int u = 0; u < ASURA_GUIMENU_UI_NAV_EXIT_MAX; ++u)
	{
		Asura_Hash_ID uLinkID = m_pxAsuraWidget->m_auHashNavID[u];
		if(uLinkID)
		{
			Tools_GUIMenu_Root* pxLink = Tools_Mode_GUIMenu::GetInstance()->FindItemByID(uLinkID);
			if(!pxLink)
			{
				const char* szDir = "";
				switch(u)
				{
					case ASURA_GUIMENU_UI_NAV_EXIT_LEFT : szDir = "Left"; break;
					case ASURA_GUIMENU_UI_NAV_EXIT_RIGHT : szDir = "Right"; break;
					case ASURA_GUIMENU_UI_NAV_EXIT_UP : szDir = "Up"; break;
					case ASURA_GUIMENU_UI_NAV_EXIT_DOWN : szDir = "Down"; break;
					default : Asura_Assert(0,""); break;
				}
				
				Tools_Mode_GUIMenu::AddVerifyMessage(this,"%s nav link for widget %s can't be found.",szDir,GetName());
				bRes = false; // Any error has occured
			}
		}
	}

	return bRes;
}

bool Tools_GUIMenu_Widget_Base::ConfirmDeletion()
{
	return ShowYesNoOverlay("Delete '%s'\n\nWould you like to delete it?",GetName());
}

#endif //ASURA_USE_GUI_MENU_SYSTEM
