/**
 * @file Gui.cpp
 * @author Petr Egert (egertpet@fel.cvut.cz)
 * @brief Contains Gui class implementation
 */

#include "Gui.h"

using namespace RD;

/**
 * @brief Performs class initialization
 * @return RES_OK on success
 */
Result Gui::Init()
{
    rootElement = new GuiElement();

    return RES_OK;
}

/**
 * @brief Performs class deinitialization
 * @return RES_OK on success
 */
Result Gui::Release()
{
    rootElement->Release();
    delete rootElement;

    return RES_OK;
}

/**
 * @brief Sets the gui position
 * @param pos Required position
 */
void Gui::SetPos(const vec2 &pos)
{
    this->pos = pos;
    UpdateMatrix();
}

/**
 * @brief Sets the gui size
 * @param size Required size
 */
void Gui::SetSize(const vec2 &size)
{
    this->size = size;
    UpdateMatrix();
}

/**
 * @brief Recursively updates all contained elements
 */
void Gui::Update()
{
    rootElement->Update();
}

/**
 * @brief Recursively renders all contained elements
 */
void Gui::Render()
{
    rootElement->Render();
}

/**
 * @brief Registers a new element as a child of the root element
 *
 * No check is performed whether the element is already registered
 * @param element Element to be registered
 */
void Gui::RegisterElement(GuiElement *element)
{
    if(element)
    {
        rootElement->AddChild(element);
        element->SetGui(this);
    }
}

/**
 * @brief Updates the tranformation matrix of the gui
 */
void Gui::UpdateMatrix()
{
    rootElement->SetTransform(mat44::Translation(-1.0 + pos.x, 1.0 - pos.y, 0.0) * mat44::Scaling(2.0 / size.x, -2.0 / size.y, 1.0));
    //rootElement->SetTransform(mat44::Scaling(1.0 / width, 1.0 / height, 1.0));
//    rootElement->SetTransform(mat44::Identity());
}
