refactor: add GUI instance reference to UI nodes
This commit is contained in:
+61
-43
@@ -1,13 +1,15 @@
|
||||
#include "GUI.hpp"
|
||||
|
||||
#include "gui_util.hpp"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#include "elements/UINode.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
#include "elements/Label.hpp"
|
||||
#include "elements/Menu.hpp"
|
||||
#include "elements/Panel.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "elements/UINode.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "frontend/UiDocument.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
@@ -15,31 +17,36 @@
|
||||
#include "graphics/core/Shader.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "graphics/core/Shader.hpp"
|
||||
#include "gui_util.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
#include "window/Events.hpp"
|
||||
#include "window/Window.hpp"
|
||||
#include "window/input.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
using namespace gui;
|
||||
|
||||
GUI::GUI()
|
||||
: batch2D(std::make_unique<Batch2D>(1024)),
|
||||
container(std::make_shared<Container>(glm::vec2(1000))) {
|
||||
GUI::GUI(Engine& engine)
|
||||
: engine(engine),
|
||||
input(engine.getInput()),
|
||||
batch2D(std::make_unique<Batch2D>(1024)),
|
||||
container(std::make_shared<Container>(*this, glm::vec2(1000))) {
|
||||
container->setId("root");
|
||||
uicamera = std::make_unique<Camera>(glm::vec3(), Window::height);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
|
||||
menu = std::make_shared<Menu>();
|
||||
menu = std::make_shared<Menu>(*this);
|
||||
menu->setId("menu");
|
||||
menu->setZIndex(10);
|
||||
container->add(menu);
|
||||
container->setScrollable(false);
|
||||
|
||||
tooltip = guiutil::create(
|
||||
*this,
|
||||
"<container color='#000000A0' interactive='false' z-index='999'>"
|
||||
"<label id='tooltip.label' pos='2' autoresize='true' multiline='true' text-wrap='false'></label>"
|
||||
"</container>"
|
||||
@@ -65,12 +72,15 @@ std::shared_ptr<Menu> GUI::getMenu() {
|
||||
}
|
||||
|
||||
void GUI::onAssetsLoad(Assets* assets) {
|
||||
assets->store(std::make_unique<UiDocument>(
|
||||
"core:root",
|
||||
uidocscript {},
|
||||
std::dynamic_pointer_cast<gui::UINode>(container),
|
||||
nullptr
|
||||
), "core:root");
|
||||
assets->store(
|
||||
std::make_unique<UiDocument>(
|
||||
"core:root",
|
||||
uidocscript {},
|
||||
std::dynamic_pointer_cast<gui::UINode>(container),
|
||||
nullptr
|
||||
),
|
||||
"core:root"
|
||||
);
|
||||
}
|
||||
|
||||
void GUI::resetTooltip() {
|
||||
@@ -79,11 +89,13 @@ void GUI::resetTooltip() {
|
||||
}
|
||||
|
||||
void GUI::updateTooltip(float delta) {
|
||||
if (hover == nullptr || !hover->isInside(Events::cursor)) {
|
||||
const auto& cursor = input.getCursor();
|
||||
if (hover == nullptr || !hover->isInside(cursor.pos)) {
|
||||
return resetTooltip();
|
||||
}
|
||||
if (tooltipTimer + delta >= hover->getTooltipDelay()) {
|
||||
auto label = std::dynamic_pointer_cast<gui::Label>(get("tooltip.label"));
|
||||
auto label =
|
||||
std::dynamic_pointer_cast<gui::Label>(get("tooltip.label"));
|
||||
const auto& text = hover->getTooltip();
|
||||
if (text.empty() && tooltip->isVisible()) {
|
||||
return resetTooltip();
|
||||
@@ -91,11 +103,11 @@ void GUI::updateTooltip(float delta) {
|
||||
if (label && !text.empty()) {
|
||||
tooltip->setVisible(true);
|
||||
label->setText(langs::get(text));
|
||||
auto size = label->getSize()+glm::vec2(4.0f);
|
||||
auto pos = Events::cursor+glm::vec2(10.0f);
|
||||
auto size = label->getSize() + glm::vec2(4.0f);
|
||||
auto pos = cursor.pos + glm::vec2(10.0f);
|
||||
auto rootSize = container->getSize();
|
||||
pos.x = glm::min(pos.x, rootSize.x-size.x);
|
||||
pos.y = glm::min(pos.y, rootSize.y-size.y);
|
||||
pos.x = glm::min(pos.x, rootSize.x - size.x);
|
||||
pos.y = glm::min(pos.y, rootSize.y - size.y);
|
||||
tooltip->setSize(size);
|
||||
tooltip->setPos(pos);
|
||||
}
|
||||
@@ -103,9 +115,9 @@ void GUI::updateTooltip(float delta) {
|
||||
tooltipTimer += delta;
|
||||
}
|
||||
|
||||
/// @brief Mouse related input and logic handling
|
||||
void GUI::actMouse(float delta) {
|
||||
float mouseDelta = glm::length(Events::delta);
|
||||
/// @brief Mouse related input and logic handling
|
||||
void GUI::actMouse(float delta, const CursorState& cursor) {
|
||||
float mouseDelta = glm::length(cursor.delta);
|
||||
doubleClicked = false;
|
||||
doubleClickTimer += delta + mouseDelta * 0.1f;
|
||||
|
||||
@@ -116,21 +128,21 @@ void GUI::actMouse(float delta) {
|
||||
if (hover) {
|
||||
hover->setHover(true);
|
||||
|
||||
int scroll = Events::getScroll();
|
||||
int scroll = input.getScroll();
|
||||
if (scroll) {
|
||||
hover->scrolled(scroll);
|
||||
}
|
||||
}
|
||||
this->hover = hover;
|
||||
|
||||
if (Events::jclicked(mousecode::BUTTON_1)) {
|
||||
if (input.jclicked(mousecode::BUTTON_1)) {
|
||||
if (pressed == nullptr && this->hover) {
|
||||
pressed = hover;
|
||||
if (doubleClickTimer < doubleClickDelay) {
|
||||
pressed->doubleClick(this, Events::cursor.x, Events::cursor.y);
|
||||
pressed->doubleClick(cursor.pos.x, cursor.pos.y);
|
||||
doubleClicked = true;
|
||||
} else {
|
||||
pressed->click(this, Events::cursor.x, Events::cursor.y);
|
||||
pressed->click(cursor.pos.x, cursor.pos.y);
|
||||
}
|
||||
doubleClickTimer = 0.0f;
|
||||
if (focus && focus != pressed) {
|
||||
@@ -138,7 +150,7 @@ void GUI::actMouse(float delta) {
|
||||
}
|
||||
if (focus != pressed) {
|
||||
focus = pressed;
|
||||
focus->onFocus(this);
|
||||
focus->onFocus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -146,22 +158,22 @@ void GUI::actMouse(float delta) {
|
||||
focus->defocus();
|
||||
focus = nullptr;
|
||||
}
|
||||
} else if (!Events::clicked(mousecode::BUTTON_1) && pressed) {
|
||||
pressed->mouseRelease(this, Events::cursor.x, Events::cursor.y);
|
||||
} else if (!input.clicked(mousecode::BUTTON_1) && pressed) {
|
||||
pressed->mouseRelease(cursor.pos.x, cursor.pos.y);
|
||||
pressed = nullptr;
|
||||
}
|
||||
|
||||
if (hover) {
|
||||
for (mousecode code : MOUSECODES_ALL) {
|
||||
if (Events::jclicked(code)) {
|
||||
hover->clicked(this, code);
|
||||
if (input.jclicked(code)) {
|
||||
hover->clicked(code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GUI::actFocused() {
|
||||
if (Events::jpressed(keycode::ESCAPE)) {
|
||||
if (input.jpressed(keycode::ESCAPE)) {
|
||||
focus->defocus();
|
||||
focus = nullptr;
|
||||
return;
|
||||
@@ -173,12 +185,13 @@ void GUI::actFocused() {
|
||||
focus->keyPressed(key);
|
||||
}
|
||||
|
||||
if (!Events::isCursorLocked()) {
|
||||
if (Events::clicked(mousecode::BUTTON_1) &&
|
||||
(Events::jclicked(mousecode::BUTTON_1) || Events::delta.x || Events::delta.y))
|
||||
{
|
||||
const auto& cursor = input.getCursor();
|
||||
if (!cursor.locked) {
|
||||
if (input.clicked(mousecode::BUTTON_1) &&
|
||||
(input.jclicked(mousecode::BUTTON_1) || cursor.delta.x ||
|
||||
cursor.delta.y)) {
|
||||
if (!doubleClicked) {
|
||||
focus->mouseMove(this, Events::cursor.x, Events::cursor.y);
|
||||
focus->mouseMove(cursor.pos.x, cursor.pos.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,15 +203,16 @@ void GUI::act(float delta, const Viewport& vp) {
|
||||
auto prevfocus = focus;
|
||||
|
||||
updateTooltip(delta);
|
||||
|
||||
const auto& cursor = input.getCursor();
|
||||
if (!Events::isCursorLocked()) {
|
||||
actMouse(delta);
|
||||
actMouse(delta, cursor);
|
||||
} else {
|
||||
if (hover) {
|
||||
hover->setHover(false);
|
||||
hover = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (focus) {
|
||||
actFocused();
|
||||
}
|
||||
@@ -316,7 +330,7 @@ void GUI::setFocus(std::shared_ptr<UINode> node) {
|
||||
}
|
||||
focus = std::move(node);
|
||||
if (focus) {
|
||||
focus->onFocus(this);
|
||||
focus->onFocus();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,3 +353,7 @@ float GUI::getDoubleClickDelay() const {
|
||||
void GUI::toggleDebug() {
|
||||
debug = !debug;
|
||||
}
|
||||
|
||||
const Input& GUI::getInput() const {
|
||||
return engine.getInput();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user