/// Copyright (c) 2009 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// @file LetterCDTTestRunner.cpp
/// This source file is part of LetterCDT test.
/// The file holds the LetterCDTTestRunner class implementation.
/// @author Ruben Penalva (ruben.penalva@rpenalva.com)
#include "LetterCDTTestRunner.h"

using namespace LetterCDT;

LetterCDTTestRunner::LetterCDTTestRunner(void)
{
	m_entries.push_back(GlutWrapper::MenuEntry("ShowPoints", ShowPoints));
	m_entries.push_back(GlutWrapper::MenuEntry("ShowOutlines", ShowOutlines));
	m_entries.push_back(GlutWrapper::MenuEntry("ShowTriangles", ShowTriangles));
	m_entries.push_back(GlutWrapper::MenuEntry("Reset", Reset));
}

LetterCDTTestRunner::~LetterCDTTestRunner(void)
{
}
	
void LetterCDTTestRunner::InitializeOpengl(int width, int height)
{
	m_render.LoadPresetConfiguration(width, height);
}

void LetterCDTTestRunner::Run(void)
{
	Render();
}

void LetterCDTTestRunner::Reshape(int width, int height)
{
	m_render.LoadPresetConfiguration(width, height);
}

void LetterCDTTestRunner::ProcessMenuEvents(int option)
{
	switch (option)
	{
		case ShowPoints :
			m_logic.EnablePoints();
			break;

		case ShowOutlines : 
			m_logic.EnableSegments();
			break;

		case ShowTriangles : 
			m_logic.EnableTriangles();
			break;

		case Reset :
			m_logic.Reset();
			break;
	}
}

const std::vector<GlutWrapper::MenuEntry>& LetterCDTTestRunner::GetMenuEntries(void) const
{
	return m_entries;
}

void LetterCDTTestRunner::ProcessNormalKeys(unsigned char key)
{
	switch(key)
	{
		case GlutWrapper::BackSpace :
		case GlutWrapper::Enter :
		case GlutWrapper::Delete :
		case GlutWrapper::Escape :
			break;
		
		default :
			m_logic.PushLetter(key);
			break;
	}
}

void LetterCDTTestRunner::Render(void)
{
	m_render.BeginFrame();
	
	if (m_logic.ArePointsEnabled())
	{
		m_render.SetMaterial(1.0f, 0.0f, 0.0f);
		m_render.DrawPoints(m_logic.GetPoints());
	}
	
	if (m_logic.AreSegmentsEnabled())
	{
		m_render.SetMaterial(0.0f, 1.0f, 0.0f);
		m_render.DrawLines(m_logic.GetLines());
	}

	if (m_logic.AreTrianglesEnabled())
	{
		m_render.SetMaterial(0.0f, 0.0f, 1.0f);
		m_render.DrawTriangles(m_logic.GetPoints(),
							   m_logic.GetTriangles());
	}

	m_render.EndFrame();
}