// This file is part of ThriceBall.
//
// Author: Kai Hillenbrand (AnTeevY)
// © Copyright, 2009 by Kai Hillenbrand, http://www.anteevy.de

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

#include "interface.h"

CInterface::CInterface(CRenderer *renderer) : m_pRenderer(renderer), m_pSceneMgr(renderer->getSceneManager()),
m_fEnergy(0.f), m_fPossibleEnergy(0.f)
{
	m_pOCRenderer = new CEGUI::OgreCEGUIRenderer(m_pRenderer->getWindow(), 
		Ogre::RENDER_QUEUE_OVERLAY, false, 3000, m_pRenderer->getSceneManager());
	m_pSystem = new CEGUI::System(m_pOCRenderer);

	setupHUD();		// setup interface (boxes)
	createFont();	// create the font resource
	createInfo();	// create the info text

	std::cout << "ThriceBall Log: Created Interface" << std::endl;	// for the log
}

void CInterface::setupHUD()
{
	std::cout << "ThriceBall Log: Setup HUD" << std::endl;	// for the log

	// create the materials for the box and bars
	MaterialPtr energyBarMat = MaterialManager::getSingleton().create("EnergyBar", "General");
	energyBarMat->setAmbient(1,0,0);
	energyBarMat->setDiffuse(1,0,0,1);

	MaterialPtr possibleEnergyBarMat = MaterialManager::getSingleton().create("PossibleEnergyBar", "General");
	possibleEnergyBarMat->setAmbient(0,1,0);
	possibleEnergyBarMat->setDiffuse(0,1,0,1);

	MaterialPtr backBoxMat = MaterialManager::getSingleton().create("BackBox", "General");
	backBoxMat->setAmbient(0.9,0.9,0.9);
	backBoxMat->setDiffuse(0.9,0.9,0.9,1);

	// create the actual box and bars
	m_pEnergyBar = new Rectangle2D(false);
	m_pEnergyBar->setMaterial("EnergyBar");

	m_pPossibleEnergyBar = new Rectangle2D(false);
	m_pPossibleEnergyBar->setMaterial("PossibleEnergyBar");

	m_pBackBox = new Rectangle2D(false);
	m_pBackBox->setCorners(-0.95, 0.95, 0.95, 0.9);
	m_pBackBox->setMaterial("BackBox");

	// create the bounding box
	AxisAlignedBox aabInf;
	aabInf.setInfinite();
	m_pEnergyBar->setBoundingBox(aabInf);
	m_pPossibleEnergyBar->setBoundingBox(aabInf);
	m_pBackBox->setBoundingBox(aabInf);

	// create the scene nodes and attack the box and bars to it
	SceneNode* node = m_pSceneMgr->getRootSceneNode()->createChildSceneNode("EnergyBarNode");
	node->attachObject(m_pEnergyBar);

	node = m_pSceneMgr->getRootSceneNode()->createChildSceneNode("PossibleEnergyBarNode");
	node->attachObject(m_pPossibleEnergyBar);

	node = m_pSceneMgr->getRootSceneNode()->createChildSceneNode("BackBoxNode");
	node->attachObject(m_pBackBox);
	node->setVisible(false);
}

void CInterface::updateHUD()
{
	// from possible energy to current energy (red)
	m_pEnergyBar->setCorners(-0.95 + 1.9 * m_fPossibleEnergy / 100.f, -0.9, -0.95 + 1.9 * m_fEnergy / 100.f, -0.95);
	// from zero to possible energy (green)
	m_pPossibleEnergyBar->setCorners(-0.95, -0.9, -0.95 + 1.9 * m_fPossibleEnergy / 100.f, -0.95);

	// update the bounding box
	AxisAlignedBox aabInf;
	aabInf.setInfinite();
	m_pEnergyBar->setBoundingBox(aabInf);
	m_pPossibleEnergyBar->setBoundingBox(aabInf);
	m_pBackBox->setBoundingBox(aabInf);
}

void CInterface::drawInfo(String text, bool forceText)
{
	// forceText is normally false, but if its set true, then the text will be shown
	// even if the game's over
	if ((m_pRenderer->getPlayer()->isStillPlaying()) || (forceText))
	{
		// update the text and show the info and back box
		OverlayManager::getSingleton().getOverlayElement("InfoTextArea")->setCaption(text);
		OverlayManager::getSingleton().getByName("InfoOverlay")->show();
		m_pSceneMgr->getSceneNode("BackBoxNode")->setVisible(true);
	}
}

void CInterface::hideInfo()
{
	// only hide if still playing -> should not hide the game over message
	if (m_pRenderer->getPlayer()->isStillPlaying())
	{
		// hide the info and back box
		OverlayManager::getSingleton().getByName("InfoOverlay")->hide();
		m_pSceneMgr->getSceneNode("BackBoxNode")->setVisible(false);
	}
}

void CInterface::createFont()
{
	std::cout << "ThriceBall Log: Create Font" << std::endl;	// for the log

	// create the font resource
	ResourceGroupManager::getSingleton().addResourceLocation("media", "FileSystem");
	ResourcePtr font = FontManager::getSingleton().create("MyFont","General");
	font->setParameter("type","truetype");
	font->setParameter("source","Candara.ttf");
	font->setParameter("size","26");
	font->setParameter("resolution","96");
	font->load();
}

void CInterface::createInfo()
{
	std::cout << "ThriceBall Log: Create Info" << std::endl;	// for the log

	// create the panel
	Ogre::OverlayContainer* panel = static_cast<OverlayContainer*>(
	   OverlayManager::getSingleton().createOverlayElement("Panel", "InfoPanel"));
	panel->setMetricsMode(Ogre::GMM_PIXELS);
	panel->setDimensions(300, 120);

	switch (m_pRenderer->getResolution())
	{
	case 0: // 1024x768
		panel->setPosition(37, 22);
		break;
	case 1: // 1280x800
		panel->setPosition(37, 22);
		break;
	case 2: // 1280x1024
		panel->setPosition(37, 29);
		break;
	case 3: // 1440 x 900
		panel->setPosition(37, 25);
		break;
	case 4: // 1680 x 1050
		panel->setPosition(37, 28);
		break;
	case 5: // 1920 x 1080
		panel->setPosition(37, 28);
		break;
	default:
		panel->setPosition(37, 29);
		break;
	}	

	// create a text area
	TextAreaOverlayElement* textArea = static_cast<TextAreaOverlayElement*>(
	   OverlayManager::getSingleton().createOverlayElement("TextArea", "InfoTextArea"));
	textArea->setMetricsMode(Ogre::GMM_PIXELS);
	textArea->setPosition(0, 0);
	textArea->setDimensions(300, 120);
	
	switch (m_pRenderer->getResolution())
	{
	case 0: // 1024x768
		textArea->setCharHeight(19);
		break;
	case 1: // 1280x800
		textArea->setCharHeight(25);
		break;
	case 2: // 1280x1024
		textArea->setCharHeight(25);
		break;
	case 3: // 1440 x 900
		textArea->setCharHeight(25);
		break;
	case 4: // 1680 x 1050
		textArea->setCharHeight(25);
		break;
	case 5: // 1920 x 1080
		textArea->setCharHeight(25);
		break;
	default:
		textArea->setCharHeight(25);
		break;
	}

	textArea->setColourTop(ColourValue(0.8,0.8,0.8,1.0));
	textArea->setColourBottom(ColourValue(1.0,0.8,0,1.0));
	textArea->setFontName("MyFont");
	textArea->setCaption("REPLACE_ME"); 

	// create overlay and add panel
	Ogre::Overlay* overlay = OverlayManager::getSingleton().create("InfoOverlay");
	overlay->add2D(panel);
	panel->addChild(textArea);
}