summaryrefslogtreecommitdiffstats
path: root/src/Widget.cpp
blob: 1a522ca3a37775a67c8223a2613eb8cc2d564649 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "Widget.hpp"

void RootWidget::AttachWidget(std::unique_ptr<Widget> widget, Widget * parent)
{
    parent->childs.push_back(widget.get());
    this->allWidgets.push_back(std::move(widget));
}

void RootWidget::AttachWidget(std::unique_ptr<Widget> widget) {
    widget->parent = nullptr;
    this->childs.push_back(widget.get());
    this->allWidgets.push_back(std::move(widget));
}

std::vector<Widget*> RootWidget::GetRenderList()
{
    std::vector<Widget*> renderList;    

    std::function<void(Widget*)> treeWalker = [&](Widget* node) {
        for (auto it : node->childs)
            treeWalker(it);
        renderList.push_back(node);
    };

    for (auto& it : this->childs)
        treeWalker(it);

    return renderList;
}

void RootWidget::UpdateEvents(double mouseX, double mouseY, bool mouseButton) {

    LOG(INFO) << mouseX << "x" << mouseY;

    auto testIsHover = [&](double x, double y, Widget* widget) {
        bool isOnX = widget->x > x && widget->x + widget->w < x;
        bool isOnY = widget->y > y && widget->y + widget->h < y;
        if (mouseButton)
            LOG(INFO) << "X: " << isOnX << " Y: " << isOnY;
        return isOnX && isOnY;
    };

    std::function<void(Widget*)> treeWalker = [&](Widget* node) {
        for (auto it : node->childs)
            treeWalker(it);
        
        if (testIsHover(mouseX,mouseY,node)) {
            if (node->onHover)
                node->onHover(node);
            if (mouseButton && !prevBut)
                if (node->onPress)
                    node->onPress(node);
            else if (!mouseButton && prevBut)
                if (node->onRelease)
                    node->onRelease(node);
        }
        else {
            if (testIsHover(prevX, prevY, node))
                if (node->onUnhover)
                    node->onUnhover(node);
        }
        
        if (node->onUpdate)
            node->onUpdate(node);
    };

    for (auto it : childs)
        treeWalker(it);

    prevX = mouseX;
    prevY = mouseY;
    prevBut = mouseButton;
}

WidgetButton::WidgetButton()
{
    this->state = WidgetState::Idle;

    onHover = [](Widget* widget) {
        WidgetButton* w = dynamic_cast<WidgetButton*>(widget);
        if (w->state != WidgetState::Pressed)
            w->state = WidgetState::Hovering;
        LOG(INFO) << "Hover";
    };

    onPress = [](Widget* widget) {
        WidgetButton* w = dynamic_cast<WidgetButton*>(widget);
        w->state = WidgetState::Pressed;
        LOG(INFO) << "Press";
    };

    onRelease = [](Widget* widget) {
        WidgetButton* w = dynamic_cast<WidgetButton*>(widget);
        w->state = WidgetState::Idle;
        w->onClick(w);
        LOG(INFO) << "Release";
    };

    onUnhover = [](Widget *widget) {
        WidgetButton* w = dynamic_cast<WidgetButton*>(widget);
        if (w->state!=WidgetState::Pressed)
            w->state = WidgetState::Idle;
        LOG(INFO) << "Unhover";
    };

}

std::tuple<double, double, double, double> WidgetButton::GetTexture()
{
    double yOffset;
    switch (this->state) {
    case WidgetState::Idle:
        yOffset = 0.2578;
        break;
    case WidgetState::Hovering:
        yOffset = 0.3359;
        break;
    case WidgetState::Pressed:
        yOffset = 0.1796;
    }

    TextureCoordinates texture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/gui/widgets");
    return { texture.x,texture.y + texture.h * yOffset,texture.w * 0.7812,texture.h * 0.07812 };
}