//
// Asura_Graph_Dialog.cpp
//
// Created by KOR - 04/08/2004
//
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

#ifdef ASURA_TOOLS

// This file's header.
#include "Interface/Asura_Graph_Dialog.h"

// Asura includes
#include "Rendering/Asura_Colour.h"

// Tools include
#include "Interface/Tools_ColourPicker_Internal.h"



/// Template_Constructor //////////////////////////////////////////////////////////////////////////

void Asura_Graph_Dialog<float>::Template_Constructor() {}
void Asura_Graph_Dialog<Asura_Vector_2>::Template_Constructor() {}
void Asura_Graph_Dialog<Asura_Vector_3>::Template_Constructor() {}

void Asura_Graph_Dialog<Asura_RGBA>::Template_Constructor()
{
	Asura_Vector_2 xSize(2.f, 1.f);
	Asura_Vector_2 xOffset = m_pxCurrentTimeLabel->m_xOffset;
	xOffset.x -= xSize.x + 0.5f;

	m_pxColourPickerButton = new Tools_ColourPicker_Colour_Gadget(xOffset, xSize, &m_fR, &m_fG, &m_fB, 0, 1.f);
	AddGadget(m_pxColourPickerButton);

	m_pxColourPicker = new Tools_ColourPicker("Choose colour", &m_fR, &m_fG, &m_fB, 1.f, &m_bColourChanged);
	m_pxColourPicker->EnableHDR(true);
	m_pxColourPickerButton->SetHidden(true);
}

/// Template_Destructor ///////////////////////////////////////////////////////////////////////////

void Asura_Graph_Dialog<float>::Template_Destructor() {}
void Asura_Graph_Dialog<Asura_Vector_2>::Template_Destructor() {}
void Asura_Graph_Dialog<Asura_Vector_3>::Template_Destructor() {}

void Asura_Graph_Dialog<Asura_RGBA>::Template_Destructor()
{
	delete m_pxColourPicker;
}

/// Template_OnValueClick /////////////////////////////////////////////////////////////////////////

void Asura_Graph_Dialog<float>::Template_OnValueClick() {}
void Asura_Graph_Dialog<Asura_Vector_2>::Template_OnValueClick() {}
void Asura_Graph_Dialog<Asura_Vector_3>::Template_OnValueClick() {}

void Asura_Graph_Dialog<Asura_RGBA>::Template_OnValueClick()
{
	Asura_Assert(m_pxGraphGadget->m_pxCurrentPoint, "Shouldn't be able to try to open colour picker with no node selected");
	if(!m_pxGraphGadget->m_pxCurrentPoint) return;
	m_fR = m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fR;
	m_fG = m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fG;
	m_fB = m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fB;
	if(!m_pxColourPicker->IsOverlayOpen())
	{
		m_pxColourPicker->OpenOverlay();

		//add extra control for applyging the changes to all nodes
		m_bApplyColourChangeToAllNodes = false;

		Asura_Vector_2 xOffset(0.0f,1.0f);
		Asura_Dialog* pxDialog = new Asura_Tickbox_Dialog("Affect All Nodes",xOffset,&m_bApplyColourChangeToAllNodes);
		pxDialog->SetHelpText("Cause any colour changes to affect all nodes in the graph.\nNote that if this is used then cancelling won't work correctly.");
		m_pxColourPicker->AddDialog(pxDialog);


	}
}

/// Template_PerFrameUpdate ///////////////////////////////////////////////////////////////////////

void Asura_Graph_Dialog<float>::Template_PerFrameUpdate() {}
void Asura_Graph_Dialog<Asura_Vector_2>::Template_PerFrameUpdate() {}
void Asura_Graph_Dialog<Asura_Vector_3>::Template_PerFrameUpdate() {}

void Asura_Graph_Dialog<Asura_RGBA>::Template_PerFrameUpdate()
{
	if(m_bColourChanged)
	{
		if(m_bApplyColourChangeToAllNodes)
		{
			// Apply changes to all points
			// Skip first
			Asura_Graph_Point<T>* pxPoint = m_pxGraphGadget->m_pxGraph->GetControlPoints()->m_pxNext;
			// Skip last
			while(pxPoint->m_pxNext)
			{
				pxPoint->m_xValue.m_fR = m_fR;
				pxPoint->m_xValue.m_fG = m_fG;
				pxPoint->m_xValue.m_fB = m_fB;

				pxPoint = pxPoint->m_pxNext;
			};
		}
		else
		{
			m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fR = m_fR;
			m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fG = m_fG;
			m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fB = m_fB;
		}
		m_pxGraphGadget->m_pxGraph->Update();
		m_bColourChanged = false;
		if(m_pbChangedFlag) *m_pbChangedFlag = true;
	}
	else
	{
		if(m_pxGraphGadget->m_pxCurrentPoint && (m_pxGraphGadget->m_bCurrentPointChanged || m_pxGraphGadget->m_bDragging) && m_pxColourPicker)
		{
			const Asura_RGBA& xColour = m_pxGraphGadget->m_pxCurrentPoint->m_xValue;
			m_pxColourPicker->SetColour(xColour.m_fR, xColour.m_fG, xColour.m_fB);
			m_fR = m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fR;
			m_fG = m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fG;
			m_fB = m_pxGraphGadget->m_pxCurrentPoint->m_xValue.m_fB;
			m_pxGraphGadget->m_bCurrentPointChanged = false;
		}
	}
}

#endif // #ifdef ASURA_TOOLS
