summaryrefslogtreecommitdiffstats
path: root/src/Widget.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-08-27 17:24:28 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:28 +0100
commit04ab1a3420b46af046a898ee5510e0d9b25ed24c (patch)
tree4e9d300bb38d434305d00337535c7c4077bc57c4 /src/Widget.cpp
parent2017-08-23 (diff)
downloadAltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.tar
AltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.tar.gz
AltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.tar.bz2
AltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.tar.lz
AltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.tar.xz
AltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.tar.zst
AltCraft-04ab1a3420b46af046a898ee5510e0d9b25ed24c.zip
Diffstat (limited to '')
-rw-r--r--src/Widget.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/Widget.cpp b/src/Widget.cpp
new file mode 100644
index 0000000..1a522ca
--- /dev/null
+++ b/src/Widget.cpp
@@ -0,0 +1,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 };
+}