<commit_before>#include "simplerendering.hpp"
#include <glutils/baseshader.hpp>
#include <glutils/uniform.hpp>
#include <opengl.hpp>
#include <iostream>
#include <glutils/projection.hpp>
#include <SDL2/SDL.h>
#include <window.hpp>
#include "events.hpp"
#include <glutils/textureloader.hpp>
#include <gimgui/logic/boundarypropagator.hpp>
#include <gimgui/util/getorfallback.hpp>

Callback<Vec2, Vec2> moveOrResize = [] (gim::Element& self, const Vec2& position, const Vec2& delta)
{
    if(!getOrFallback(self, "resize", false))
    {
        Vec2 relativePosition = self.getAttribute<Vec2>("position");
        Vec2 newPosition;
        newPosition.x = relativePosition.x + delta.x;
        newPosition.y = relativePosition.y + delta.y;

        self.setAttribute("position", newPosition);
    }
    else
    {
        Vec2 size = self.getAttribute<Vec2>("size");
        Vec2 newSize;
        newSize.x = size.x + delta.x;
        newSize.y = size.y + delta.y;

        self.setAttribute("size", newSize);
    }
};

Callback<Vec2> setClickColor = [] (gim::Element& self, const Vec2& clickPos) {self.setAttribute("color", self.getAttribute<Color>("click_color"));};
Callback<> setOriginalColor = [] (gim::Element& self)
{
    self.setAttribute("color", self.getAttribute<Color>("original_color"));
    self.setAttribute("dragged", false);
    self.setAttribute("resize", false);
};
Callback<Vec2> printClicked = [] (gim::Element& self, const Vec2& clickPos)
{
    std::cout << self.getAttribute<std::string>("name") << " clicked\n";
};
Callback<Vec2> toggleStretchMode = [] (gim::Element& self, const Vec2& clickPos)
{
    gim::StretchMode currentStretchMode = self.getAttribute<gim::StretchMode>("stretch_mode");
    gim::StretchMode newStretchMode;

    gim::AbsoluteMap<Vec2> absoluteMap("position");

    const Vec2 position = absoluteMap.getAbsoluteOf(self);
    Vec2 relativePos;
    relativePos.x = clickPos.x - position.x;
    relativePos.y = clickPos.y - position.y;
    const Vec2 size = self.getAttribute<Vec2>("size");

    if(relativePos.x > size.x - 20 && relativePos.y > size.y - 20)
    {
        self.setAttribute("resize", true);
    }
    else
    {
        if(currentStretchMode == gim::StretchMode::STRETCHED)
            newStretchMode = gim::StretchMode::TILED;
        else if(currentStretchMode == gim::StretchMode::TILED)
            newStretchMode = gim::StretchMode::V_TILED;
        else if(currentStretchMode == gim::StretchMode::V_TILED)
            newStretchMode = gim::StretchMode::H_TILED;
        else if(currentStretchMode == gim::StretchMode::H_TILED)
            newStretchMode = gim::StretchMode::STRETCHED;

        self.setAttribute("stretch_mode", newStretchMode);
    }

    self.setAttribute("dragged", true);
};

SimpleRendering::SimpleRendering(const Vec2& viewSize):
    mQuit(false),
    mTriangles(Buffer::ARRAY_BUFFER, Buffer::DYNAMIC),
    mColors(Buffer::ARRAY_BUFFER, Buffer::DYNAMIC),
    mTexCoords(Buffer::ARRAY_BUFFER, Buffer::DYNAMIC),
    mLastPosition({-1000, -1000}),
    mRoot({"container"},
        {
            {"name" , std::string("red")},
            {"color",    Color(140, 35, 24)},
            {"original_color",    Color(140, 35, 24)},
            {"click_color",    Color(174, 70, 56)},
            {"position", Vec2({200, 150})},
            {"size",     Vec2({256, 256})},
            {"stretch_mode", gim::StretchMode::STRETCHED},
            {"image_id", 0},
            {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({32, 32}))},
            {"dragged", false},
            {"resize", false},
            {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode})},
            {"on_mouse_release", setOriginalColor},
            {"on_drag", moveOrResize}
        },
        {
            gim::Element({"child"},
            {
                {"name" , std::string("blue")},
                {"color",    Color(94, 140, 106)},
                {"original_color",    Color(94, 140, 106)},
                {"click_color",    Color(124, 170, 136)},
                {"position", Vec2({20, 20})},
                {"size",     Vec2({64, 64})},
                {"stretch_mode", gim::StretchMode::STRETCHED},
                {"image_id", 0},
                {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({32, 32}))},
                {"block_event", true},
                {"dragged", false},
                {"resize", false},
                {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode})},
                {"on_mouse_release", setOriginalColor},
                {"on_drag", moveOrResize}
            }),
            gim::Element({"child"},
            {
                {"name" , std::string("green")},
                {"color",    Color(136, 166, 94)},
                {"original_color",    Color(136, 166, 94)},
                {"click_color",    Color(166, 196, 124)},
                {"position", Vec2({90, 20})},
                {"size",     Vec2({64, 64})},
                {"stretch_mode", gim::StretchMode::STRETCHED},
                {"image_id", 0},
                {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({4, 4}))},
                {"dragged", false},
                {"resize", false},
                {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode})},
                {"on_mouse_release", setOriginalColor},
                {"block_event", true},
                {"on_drag", moveOrResize}
            }),
            gim::Element({"child"},
            {
                {"name" , std::string("yellow")},
                {"color",    Color(191, 179, 90)},
                {"original_color",    Color(191, 179, 90)},
                {"click_color",    Color(221, 209, 124)},
                {"position", Vec2({20, 90})},
                {"size",     Vec2({64, 64})},
                {"stretch_mode", gim::StretchMode::STRETCHED},
                {"image_id", 0},
                {"dragged", false},
                {"resize", false},
                {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({32, 32}))},
                {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode})},
                {"on_mouse_release", setOriginalColor},
                {"block_event", true},
                {"on_drag", moveOrResize}
            })
        })
{
    //load textures

    mTextures.emplace(0, loadTexture("resources/xpattern.png"));

    mRenderDataGenerator.registerImageInfo(0, {32, 32});
    //rendering<commit_msg>Refactored example code<commit_after>#include "simplerendering.hpp"
#include <glutils/baseshader.hpp>
#include <glutils/uniform.hpp>
#include <opengl.hpp>
#include <iostream>
#include <glutils/projection.hpp>
#include <SDL2/SDL.h>
#include <window.hpp>
#include "events.hpp"
#include <glutils/textureloader.hpp>
#include <gimgui/logic/boundarypropagator.hpp>
#include <gimgui/util/getorfallback.hpp>

Callback<Vec2, Vec2> moveOrResize = [] (gim::Element& self, const Vec2& position, const Vec2& delta)
{
    if(!getOrFallback(self, "resize", false))
    {
        Vec2 relativePosition = self.getAttribute<Vec2>("position");
        Vec2 newPosition;
        newPosition.x = relativePosition.x + delta.x;
        newPosition.y = relativePosition.y + delta.y;

        self.setAttribute("position", newPosition);
    }
    else
    {
        Vec2 size = self.getAttribute<Vec2>("size");
        Vec2 newSize;
        newSize.x = size.x + delta.x;
        newSize.y = size.y + delta.y;

        self.setAttribute("size", newSize);
    }
};

Callback<Vec2> setResize = [] (gim::Element& self, const Vec2& clickPos)
{
    gim::AbsoluteMap<Vec2> absoluteMap("position");
    const Vec2 position = absoluteMap.getAbsoluteOf(self);

    Vec2 relativeClickPos;
    relativeClickPos.x = clickPos.x - position.x;
    relativeClickPos.y = clickPos.y - position.y;
    const Vec2 size = self.getAttribute<Vec2>("size");

    if(relativeClickPos.x > size.x - 20 && relativeClickPos.y > size.y - 20)
    {
        self.setAttribute("resize", true);
    }
};

Callback<> unsetResize = [] (gim::Element& self)
{
    self.setAttribute("resize", false);
};

Callback<Vec2> setClickColor = [] (gim::Element& self, const Vec2& clickPos) {self.setAttribute("color", self.getAttribute<Color>("click_color"));};
Callback<> setOriginalColor = [] (gim::Element& self)
{
    self.setAttribute("color", self.getAttribute<Color>("original_color"));
};
Callback<Vec2> printClicked = [] (gim::Element& self, const Vec2& clickPos)
{
    std::cout << self.getAttribute<std::string>("name") << " clicked\n";
};
Callback<Vec2> toggleStretchMode = [] (gim::Element& self, const Vec2& clickPos)
{
    gim::StretchMode currentStretchMode = self.getAttribute<gim::StretchMode>("stretch_mode");
    gim::StretchMode newStretchMode;

    if(currentStretchMode == gim::StretchMode::STRETCHED)
        newStretchMode = gim::StretchMode::TILED;
    else if(currentStretchMode == gim::StretchMode::TILED)
        newStretchMode = gim::StretchMode::V_TILED;
    else if(currentStretchMode == gim::StretchMode::V_TILED)
        newStretchMode = gim::StretchMode::H_TILED;
    else if(currentStretchMode == gim::StretchMode::H_TILED)
        newStretchMode = gim::StretchMode::STRETCHED;

    self.setAttribute("stretch_mode", newStretchMode);
};

SimpleRendering::SimpleRendering(const Vec2& viewSize):
    mQuit(false),
    mTriangles(Buffer::ARRAY_BUFFER, Buffer::DYNAMIC),
    mColors(Buffer::ARRAY_BUFFER, Buffer::DYNAMIC),
    mTexCoords(Buffer::ARRAY_BUFFER, Buffer::DYNAMIC),
    mLastPosition({-1000, -1000}),
    mRoot({"container"},
        {
            {"name" , std::string("red")},
            {"color",    Color(140, 35, 24)},
            {"original_color",    Color(140, 35, 24)},
            {"click_color",    Color(174, 70, 56)},
            {"position", Vec2({200, 150})},
            {"size",     Vec2({256, 256})},
            {"stretch_mode", gim::StretchMode::STRETCHED},
            {"image_id", 0},
            {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({32, 32}))},
            {"dragged", false},
            {"resize", false},
            {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode, setResize})},
            {"on_mouse_release", CallbackList<>({setOriginalColor, unsetResize})},
            {"on_drag", moveOrResize}
        },
        {
            gim::Element({"child"},
            {
                {"name" , std::string("blue")},
                {"color",    Color(94, 140, 106)},
                {"original_color",    Color(94, 140, 106)},
                {"click_color",    Color(124, 170, 136)},
                {"position", Vec2({20, 20})},
                {"size",     Vec2({64, 64})},
                {"stretch_mode", gim::StretchMode::STRETCHED},
                {"image_id", 0},
                {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({32, 32}))},
                {"block_event", true},
                {"dragged", false},
                {"resize", false},
                {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode, setResize})},
                {"on_mouse_release", CallbackList<>({setOriginalColor, unsetResize})},
                {"on_drag", moveOrResize}
            }),
            gim::Element({"child"},
            {
                {"name" , std::string("green")},
                {"color",    Color(136, 166, 94)},
                {"original_color",    Color(136, 166, 94)},
                {"click_color",    Color(166, 196, 124)},
                {"position", Vec2({90, 20})},
                {"size",     Vec2({64, 64})},
                {"stretch_mode", gim::StretchMode::STRETCHED},
                {"image_id", 0},
                {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({4, 4}))},
                {"dragged", false},
                {"resize", false},
                {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode, setResize})},
                {"on_mouse_release", CallbackList<>({setOriginalColor, unsetResize})},
                {"block_event", true},
                {"on_drag", moveOrResize}
            }),
            gim::Element({"child"},
            {
                {"name" , std::string("yellow")},
                {"color",    Color(191, 179, 90)},
                {"original_color",    Color(191, 179, 90)},
                {"click_color",    Color(221, 209, 124)},
                {"position", Vec2({20, 90})},
                {"size",     Vec2({64, 64})},
                {"stretch_mode", gim::StretchMode::STRETCHED},
                {"image_id", 0},
                {"dragged", false},
                {"resize", false},
                {"image_coords", gim::Rectangle<Vec2>(Vec2({0, 0}), Vec2({32, 32}))},
                {"on_click", CallbackList<Vec2>({setClickColor, printClicked, toggleStretchMode, setResize})},
                {"on_mouse_release", CallbackList<>({setOriginalColor, unsetResize})},
                {"block_event", true},
                {"on_drag", moveOrResize}
            })
        })
{
    //load textures

    mTextures.emplace(0, loadTexture("resources/xpattern.png"));

    mRenderDataGenerator.registerImageInfo(0, {32, 32});
    //rendering
{'fraction_non_alphanumeric': 0.16859157042147893, 'fraction_numerical': 0.0335983200839958, 'mean_word_length': 2.5315148305084745, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2973504', 'n_tokens_mistral': 4534, 'n_tokens_neox': 4143, 'n_words': 715}