#include "Actor.h"
#include "EngineFrame/Scene/Scene.h"
#include "EngineCore/Game.h"
#include "EngineFrame/Component/RenderBase.h"
#include "EngineFrame/Render/RenderManager.h"
#include <algorithm>

Actor::Actor()
{
    Init();
}

void Actor::Init()
{
    addTag(Tag::ACTOR);
    addTag(Tag::RENDER);
    addTag(Tag::TRANSFORM);
    addTag(Tag::UPDATE);
}

void Actor::Render()
{
    // 如果有裁切视口
    if (this->_CropViewportFlag)
    {
        RenderManager *renderer = Game::GetInstance().GetRenderer();
        renderer->SetClipRect(&this->_CropViewport);
        BaseNode::Render();
        renderer->CloseClipRect();
    }
    else
    {
        BaseNode::Render();
    }
}

void Actor::AddComponent(RefPtr<Component> Component)
{
    BaseNode::AddChild(Component);
}

void Actor::RemoveComponent(RefPtr<Component> Component)
{
    BaseNode::RemoveChild(Component);
}

void Actor::SetCropViewport(SDL_Rect CropViewport)
{
    if (CropViewport.x == 0 && CropViewport.y == 0 && CropViewport.w == 0 && CropViewport.h == 0)
    {
        this->_CropViewportFlag = false;
    }
    else
    {
        this->_CropViewportFlag = true;
        this->_CropViewport = CropViewport;
    }
}

SDL_Rect Actor::GetCropViewport()
{
    return this->_CropViewport;
}
