#include "pch.h"
#include "legacy_canvas_draw_merge_services.h"
#include "log.h"
#include "legacy_ui_gl_dispatch.h"
#include "node_image_texture.h"
#include "shader.h"
#include "node_image.h"

Node* NodeImageTexture::clone_instantiate() const
{
    return new NodeImageTexture();
}

void NodeImageTexture::clone_copy(Node* dest) const
{
    Node::clone_copy(dest);
    NodeImageTexture* n = static_cast<NodeImageTexture*>(dest);
    n->tex = tex;
}

void NodeImageTexture::draw()
{
    tex ? tex->bind() : pp::legacy::ui_gl::unbind_texture_2d("NodeImageTexture");
    auto& sampler = tex && tex->has_mips ? NodeImage::m_sampler_mips : NodeImage::m_sampler;
    sampler.bind(0);
    pp::legacy::ui_gl::set_blend_enabled(true, "NodeImageTexture");
    pp::panopainter::setup_legacy_canvas_draw_merge_texture_shader({.mvp = m_mvp, .texture_slot = 0});
    NodeImage::m_plane.draw_fill();
    sampler.unbind();
    pp::legacy::ui_gl::set_blend_enabled(false, "NodeImageTexture");
}
