-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompatible2D.h
101 lines (81 loc) · 2.48 KB
/
Compatible2D.h
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
#pragma once
#include <irrlicht.h>
#include <sol/sol.hpp>
#include "LuaLime.h"
#include "IrrManagers.h"
class Compatible2D {
public:
virtual ~Compatible2D() = default;
irr::gui::IGUIButton* button = nullptr;
bool clickable = false;
sol::function onClick;
sol::function onHover;
virtual irr::gui::IGUIElement* getNode() const = 0;
void setParent(sol::optional<Compatible2D*> parent) {
irr::gui::IGUIElement* node = getNode();
if (!node || !(*parent)) return;
// add child etc.
(*parent)->getNode()->addChild(node);
}
bool getClickable() {
return getNode() ? button != nullptr : false;
}
bool getHovered() {
if (!getNode())
return false;
irr::core::position2di mousePos = device->getCursorControl()->getPosition();
irr::core::recti rect(
getNode()->getAbsoluteClippingRect().UpperLeftCorner,
getNode()->getAbsoluteClippingRect().LowerRightCorner
);
return rect.isPointInside(mousePos);
}
void setHovered() {
}
void setClickable(sol::function f) {
if (!getNode())
return;
if (f && !button) {
clickable = true;
updateButton();
receiver->buttonCallbackClick.push_back(ButtonCallbackPairClick(button, f));
}
else if (!f && button) {
receiver->removeImg(button);
button->remove();
clickable = false;
}
}
void setHover(sol::function hov) {
if (!getNode()) return;
if (!clickable) {
bool cl = clickable;
clickable = true;
updateButton();
clickable = cl;
}
receiver->buttonCallbackHover.push_back(ButtonCallbackPairHover(button, hov));
}
bool getPressed() {
if (!getNode()) return false;
return button ? button->isPressed() : false;
}
void updateButton() {
if (!clickable)
return;
irr::core::recti r = getNode()->getRelativePosition();
if (button)
receiver->removeImg(button);
button = guienv->addButton(irr::core::recti(0, 0, r.LowerRightCorner.X - r.UpperLeftCorner.X, r.LowerRightCorner.Y - r.UpperLeftCorner.Y), getNode());
button->setDrawBorder(false);
button->setUseAlphaChannel(true);
}
};
inline void bindCompatible2D() {
sol::usertype<Compatible2D> bind_type = lua->new_usertype<Compatible2D>("Compatible2D",
"hovered", sol::property(&Compatible2D::getHovered, &Compatible2D::setHovered),
"pressed", sol::property(&Compatible2D::getPressed, &Compatible2D::setHovered));
bind_type["setParent"] = &Compatible2D::setParent;
bind_type["fireOnClick"] = &Compatible2D::setClickable;
bind_type["fireOnHover"] = &Compatible2D::setHover;
}