// Tools_GUIMenu_Widget_Slider.cpp
//
// Created by Richard Rice - 05/12/2006
// 
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

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

#ifdef ASURA_USE_GUI_MENU_SYSTEM
#include "Interface/Asura_CommandConsole.h"
#include "Interface/Asura_Dialog.h"
#include "Interface/Asura_Overlay.h"
#include "Interface/Asura_Overlay_System.h"
#include "GUIMenu/Asura_GUIMenu_Widget_Slider.h"
#include "Sound/Asura_Sound_Event.h"
#include "Sound/Asura_Sound_Event_Storage.h"

#include "GUIMenu/Tools_GUIMenu_CommandConsole.h"
#include "GUIMenu/Tools_GUIMenu_PreviewOverlay.h"
#include "GUIMenu/Tools_Mode_GUIMenu.h"

#include "Misc/Tools_Modal_Overlay.h"
#include "Sound/Tools_PickSoundEvent_Overlay.h"

#include "Tools_Overlays.h"

Tools_GUIMenu_Widget_Slider::Tools_GUIMenu_Widget_Slider(Asura_GUIMenu_Widget_Slider* pxAsuraSlider,Tools_GUIMenu_Root* pxToolsParent)
: Tools_GUIMenu_Widget_Base( pxAsuraSlider,pxToolsParent)
, m_pxAsuraSlider( pxAsuraSlider )
, m_bDataChanged( false )
, m_bNumStepsChanged( false )
, m_uNumSteps ( 0 )
{

}


void Tools_GUIMenu_Widget_Slider::InitialiseNewItem()
{
	PARENT::InitialiseNewItem();

	//The slider needs a path
	Asura_Vector_2 xPos = m_pxAsuraSlider->GetScaledWorldPos();
	Tools_GUIMenu_Root* pxPath = Tools_Mode_GUIMenu::GetInstance()->CreateItem(this,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_PATH,xPos);
	if(pxPath)
	{
		m_pxAsuraSlider->SetActivePathFromHash(pxPath->GetAsuraData()->GetID());

		//and the path needs an image (I think)
		Tools_Mode_GUIMenu::GetInstance()->CreateItem(pxPath,Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_IMAGE,xPos);
	}
}


void Tools_GUIMenu_Widget_Slider::CheckRequiredReferences()
{
	PARENT::CheckRequiredReferences();

	//make sure the slider knows about a child path
	if(!m_pxAsuraSlider->GetActivePathHash())
	{
		//search for a child text element
		for(u_int i=0;i<m_pxAsuraSlider->GetContainerCount();i++)		
		{
			Asura_GUIMenu_Root* pxChild = m_pxAsuraSlider->GetContainerItem(i);
			if(pxChild->GetType() == Asura_GUIMenu_Root::ASURA_GUIMENU_TYPEID_ELEMENT_PATH)
			{
				m_pxAsuraSlider->SetActivePathFromHash(pxChild->GetID());
				SetModified();
				break;
			}
		}
	}
}

// ---------------------------------------------------------------------------------------------------

u_int Tools_GUIMenu_Widget_Slider::GetFlagsToEdit() const
{
	return PARENT::GetFlagsToEdit() | ASURA_GUIMENU_ACTION_FLAG_NOTIFY_DATA_CHANGE_IMMEDIATE | ASURA_GUIMENU_ACTION_FLAG_NOTIFY_DATA_CHANGE_ON_EXIT;
}

const char* Tools_GUIMenu_Widget_Slider::GetLocalDataFlagName( u_int uFlagValue ) const
{
	switch(uFlagValue)
	{
		case Asura_GUIMenu_Widget_Slider::ASURA_GUIMENU_WIDGET_SLIDER_NUM_STEPS :
			return "Num Steps";

		case Asura_GUIMenu_Widget_Slider::ASURA_GUIMENU_WIDGET_SLIDER_FLAGS :
			return "Slider Flags";

		default:
			return PARENT::GetLocalDataFlagName( uFlagValue );
	}
}

// ---------------------------------------------------------------------------------------------------

void Tools_GUIMenu_Widget_Slider::Update( void)
{
	PARENT::Update();

	if( m_bDataChanged)
	{
		Asura_Sound_Event* pxEvent = NULL;
		Asura_Char acText[256];

		Asura_PrintfToBfr( &acText[0], "*None*", 256, true);

		pxEvent = Asura_Sound_Event_Storage::GetEventByHash( m_pxAsuraSlider->GetEditingAudioHash());
		if( pxEvent != NULL)
		{
			Asura_PrintfToBfr( &acText[0], "%s", 256, true, pxEvent->GetName());
		}

		Asura_Overlay* pxOverlay = Asura_Overlay_System::GetOverlay(TOOLS_OVERLAY_GUIMENU_SELECTED);
		if(pxOverlay)
		{
			pxOverlay->UpdateDialogLabel( Asura_GetHashID( "Volume"), acText);
		}

		m_bDataChanged = false;
		m_bChanged = true;
		SetModified();

	}

	if( m_bNumStepsChanged)
	{
		m_pxAsuraSlider->SetNumSteps( m_uNumSteps );
		m_bNumStepsChanged = false;
		SetModified();
		m_bChanged = true;
	}
}

// ---------------------------------------------------------------------------------------------------

void Tools_GUIMenu_Widget_Slider::AddOverlayControls(Asura_Overlay* pxOverlay,Asura_Vector_2& xOffset)
{
	Asura_Char acText[256];
	Asura_Dialog* pxDialog;
	Asura_Sound_Event* pxEvent = NULL;
	Asura_Vector_2 xRightOffset;

	PARENT::AddOverlayControls(pxOverlay,xOffset);

	xOffset.y += Asura_Dialog::DefaultSpacing();

	m_uNumSteps = m_pxAsuraSlider->GetNumSteps();

	pxDialog = new Asura_Numerical_Dialog("Number of Steps", xOffset, &m_uNumSteps, 0, 0xFFFFFFFF, 1, "%d", &m_bNumStepsChanged);
	pxDialog->SetIsEnabledFunction(CanEditNumSteps,(int)this);
	pxDialog->SetHelpText("Sets the number of steps the slider has. If this value is zero or one, the slider will move it's position smoothly rather than in steps.");
	pxOverlay->AddDialog( pxDialog );
	xOffset.y += Asura_Dialog::DefaultSpacing();

	Asura_PrintfToBfr( &acText[0], "Set Data Change Audio : ", 256, true);
	pxDialog = new Asura_Function_Dialog( acText, xOffset, SelectAudioEvent,(int)this);
	pxDialog->SetHelpText("Set a slider data change sound event");
	pxOverlay->AddDialog(pxDialog);
	
	xRightOffset.x = pxDialog->m_xMaxCoord.x + 1.0f;
	xRightOffset.y = xOffset.y;


	Asura_PrintfToBfr( &acText[0], ": *None*", 256, true);

	pxEvent = Asura_Sound_Event_Storage::GetEventByHash( m_pxAsuraSlider->GetEditingAudioHash());
	if( pxEvent != NULL)
	{
		Asura_PrintfToBfr( &acText[0], " : %s", 256, true, pxEvent->GetName());
	}

	pxDialog = new Asura_StringDisplay_Dialog( acText, xRightOffset);
	pxDialog->m_iControlID = Asura_GetHashID( "Volume");
	pxOverlay->AddDialog(pxDialog);

	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_FlagTickbox_Dialog( "Affect Volume On Change", xOffset, m_pxAsuraSlider->GetSliderFlagsPtr(), ASURA_GUIMENU_WIDGET_SLIDER_FLAGS_CHANGE_VOLUME_ON_LOCAL_AUDIO_EVENT);
	pxDialog->SetIsEnabledFunction(CanEditButtonFlags,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_FlagTickbox_Dialog( "Loop Audio On Edit", xOffset, m_pxAsuraSlider->GetSliderFlagsPtr(), ASURA_GUIMENU_WIDGET_SLIDER_FLAGS_LOOP_LOCAL_AUDIO_EVENT);
	pxDialog->SetIsEnabledFunction(CanEditButtonFlags,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_FlagTickbox_Dialog( "Always in Edit Mode When Highlighted", xOffset, m_pxAsuraSlider->GetSliderFlagsPtr(), ASURA_GUIMENU_WIDGET_SLIDER_FLAGS_ALWAYS_IN_EDIT_MODE_WHEN_HIGHLIGHTED);
	pxDialog->SetHelpText( "When this slider is selected it will go directly to edit mode without requiring a button press to activate it" );
	pxDialog->SetIsEnabledFunction(CanEditButtonFlags,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	m_pxAsuraSlider->m_xConsoleVar.AddOverlayControls( pxOverlay,  
													   xOffset,
													   "Console Var",
													   "The console variable that will be altered by this Slider",
													   Asura_CommandConsole_VarRepository::Var::TYPE_FLOAT,
													   &m_bChanged);
}

// -----------------------------------------------------------------------------------

void Tools_GUIMenu_Widget_Slider::SelectAudioEvent( void)
{
	Asura_Hash_ID uSoundEvent = ASURA_HASH_ID_UNSET;

	//For sound events this gets used when creating the phonon
	Tools_PickSoundEvent_Overlay xOverlay("Pick event for new phonon");
	uSoundEvent = xOverlay.DoModal(0);
	
	m_pxAsuraSlider->SetEditingAudioHash( uSoundEvent);

	m_bDataChanged = true;
}

// -----------------------------------------------------------------------------------

bool Tools_GUIMenu_Widget_Slider::CanEditNumSteps()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Slider::ASURA_GUIMENU_WIDGET_SLIDER_NUM_STEPS);
}

bool Tools_GUIMenu_Widget_Slider::CanEditButtonFlags()
{
	return ShouldStyleUseLocalDataFor(Asura_GUIMenu_Widget_Slider::ASURA_GUIMENU_WIDGET_SLIDER_FLAGS);
}

void Tools_GUIMenu_Widget_Slider::AddToPreviewOverlay() const
{
	PARENT::AddToPreviewOverlay();

	if(!m_pxAsuraSlider->m_xConsoleVar.IsBlank())
	{
		Tools_GUIMenu_PreviewOverlay::RegisterConsoleVariable( m_pxAsuraSlider->m_xConsoleVar.GetName().GetString(),Asura_CommandConsole_VarRepository::Var::TYPE_FLOAT);
	}
}

#endif //ASURA_USE_GUI_MENU_SYSTEM
