/******************************************************************************
*	File		-	orthographic_camera.cpp
*	Author		-	Joey Pollack
*	Date		-	2022/01/25 (y/m/d)
*	Mod Date	-	2022/01/25 (y/m/d)
*	Description	-	A 2D camera to be used with the World
******************************************************************************/

#include "orthographic_camera.h"
#include <glm/gtc/matrix_transform.hpp>

namespace lunarium
{
    OrthographicCamera::OrthographicCamera(Vec2f position, Sizef viewport_size)
        : mPosition(position), mRotation(0.0f), mViewportSize(viewport_size), mView(1.0f)
    {
        RecalculateView();
    }

    void OrthographicCamera::MoveLeft(float amt)
    {
        mPosition.X += amt;
        RecalculateView();
    }

    void OrthographicCamera::MoveUp(float amt)
    {
        mPosition.Y += amt;
        RecalculateView();
    }

    void OrthographicCamera::Rotate(float amt)
    {
        mRotation += amt;
        RecalculateView();
    }

    void OrthographicCamera::SetPosition(Vec2f position)
    {
        mPosition = position;
        RecalculateView();
    }

    void OrthographicCamera::SetRotation(float rot)
    {
        mRotation = rot;
        RecalculateView();
    }
    
    glm::mat4 OrthographicCamera::GetViewProjection()
    {
        return mViewProj;
    }

    Sizef OrthographicCamera::GetViewport() const
    {
        return mViewportSize;
    }

    void OrthographicCamera::SetViewportSize(Sizef size)
    {
        mViewportSize = size;
        RecalculateView();
    }

    void OrthographicCamera::RecalculateView()
    {
        mProjection = glm::ortho(0.0f, mViewportSize.Width, mViewportSize.Height, 0.0f, -1.0f, 1.0f);

        glm::mat4 transform = glm::translate(glm::mat4(1.0f), (glm::vec3)mPosition);    
		transform *= glm::rotate(glm::mat4(1.0f), glm::radians(mRotation), glm::vec3(0, 0, 1));

		mView = glm::inverse(transform);
		mViewProj = mProjection * mView;
    }

}