graphics/ui/elements/containers deleted
This commit is contained in:
parent
c82d95b97d
commit
85f7febd30
@ -11,7 +11,6 @@
|
||||
#include "../graphics/core/GfxContext.h"
|
||||
#include "../graphics/core/Shader.h"
|
||||
#include "../graphics/render/BlocksPreview.h"
|
||||
#include "../graphics/ui/elements/containers.h"
|
||||
#include "../graphics/ui/elements/controls.h"
|
||||
#include "../items/Inventories.h"
|
||||
#include "../items/Inventory.h"
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../graphics/ui/elements/UINode.h"
|
||||
#include "../graphics/ui/elements/containers.h"
|
||||
#include "../graphics/ui/elements/Container.hpp"
|
||||
#include "../graphics/ui/elements/controls.h"
|
||||
#include "../items/ItemStack.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#include "UiDocument.h"
|
||||
|
||||
#include "../files/files.h"
|
||||
#include "../graphics/ui/elements/containers.h"
|
||||
#include "../graphics/ui/elements/UINode.h"
|
||||
#include "../graphics/ui/gui_xml.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
|
||||
@ -15,9 +15,9 @@
|
||||
#include "../graphics/core/Texture.h"
|
||||
#include "../graphics/render/BlocksPreview.h"
|
||||
#include "../graphics/render/WorldRenderer.h"
|
||||
#include "../graphics/ui/elements/containers.h"
|
||||
#include "../graphics/ui/elements/controls.h"
|
||||
#include "../graphics/ui/elements/UINode.h"
|
||||
#include "../graphics/ui/elements/Menu.hpp"
|
||||
#include "../graphics/ui/elements/Plotter.hpp"
|
||||
#include "../graphics/ui/GUI.h"
|
||||
#include "../items/Inventories.h"
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
#include "../../interfaces/Task.h"
|
||||
#include "../../graphics/ui/GUI.h"
|
||||
#include "../../graphics/ui/gui_util.h"
|
||||
#include "../../graphics/ui/elements/containers.h"
|
||||
#include "../../graphics/ui/elements/Menu.hpp"
|
||||
#include "../../graphics/ui/elements/controls.h"
|
||||
#include "../screens.h"
|
||||
#include "../UiDocument.h"
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
#include "../graphics/core/Shader.h"
|
||||
#include "../graphics/core/TextureAnimation.h"
|
||||
#include "../graphics/render/WorldRenderer.h"
|
||||
#include "../graphics/ui/elements/containers.h"
|
||||
#include "../graphics/ui/elements/Menu.hpp"
|
||||
#include "../graphics/ui/GUI.h"
|
||||
#include "../logic/ChunksController.h"
|
||||
#include "../logic/LevelController.h"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "GUI.h"
|
||||
#include "elements/UINode.h"
|
||||
#include "elements/containers.h"
|
||||
#include "elements/Menu.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
152
src/graphics/ui/elements/Container.cpp
Normal file
152
src/graphics/ui/elements/Container.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
#include "Container.hpp"
|
||||
|
||||
#include "../../core/GfxContext.h"
|
||||
#include "../../core/Batch2D.h"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
Container::Container(glm::vec2 size) : UINode(size) {
|
||||
actualLength = size.y;
|
||||
setColor(glm::vec4());
|
||||
}
|
||||
|
||||
std::shared_ptr<UINode> Container::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
|
||||
if (!interactive) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!isInside(pos)) return nullptr;
|
||||
|
||||
for (int i = nodes.size()-1; i >= 0; i--) {
|
||||
auto& node = nodes[i];
|
||||
if (!node->isVisible())
|
||||
continue;
|
||||
auto hover = node->getAt(pos, node);
|
||||
if (hover != nullptr) {
|
||||
return hover;
|
||||
}
|
||||
}
|
||||
return UINode::getAt(pos, self);
|
||||
}
|
||||
|
||||
void Container::act(float delta) {
|
||||
for (auto node : nodes) {
|
||||
if (node->isVisible()) {
|
||||
node->act(delta);
|
||||
}
|
||||
}
|
||||
for (IntervalEvent& event : intervalEvents) {
|
||||
event.timer += delta;
|
||||
if (event.timer > event.interval) {
|
||||
event.callback();
|
||||
event.timer = fmod(event.timer, event.interval);
|
||||
if (event.repeat > 0) {
|
||||
event.repeat--;
|
||||
}
|
||||
}
|
||||
}
|
||||
intervalEvents.erase(std::remove_if(
|
||||
intervalEvents.begin(), intervalEvents.end(),
|
||||
[](const IntervalEvent& event) {
|
||||
return event.repeat == 0;
|
||||
}
|
||||
), intervalEvents.end());
|
||||
}
|
||||
|
||||
void Container::scrolled(int value) {
|
||||
int diff = (actualLength-getSize().y);
|
||||
if (scroll < 0 && diff <= 0) {
|
||||
scroll = 0;
|
||||
}
|
||||
if (diff > 0 && scrollable) {
|
||||
scroll += value * scrollStep;
|
||||
if (scroll > 0)
|
||||
scroll = 0;
|
||||
if (-scroll > diff) {
|
||||
scroll = -diff;
|
||||
}
|
||||
} else if (parent) {
|
||||
parent->scrolled(value);
|
||||
}
|
||||
}
|
||||
|
||||
void Container::setScrollable(bool flag) {
|
||||
scrollable = flag;
|
||||
}
|
||||
|
||||
void Container::draw(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
glm::vec2 size = getSize();
|
||||
drawBackground(pctx, assets);
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->flush();
|
||||
{
|
||||
GfxContext ctx = pctx->sub();
|
||||
ctx.setScissors(glm::vec4(pos.x, pos.y, size.x, size.y));
|
||||
for (auto node : nodes) {
|
||||
if (node->isVisible())
|
||||
node->draw(pctx, assets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec4 color = isPressed() ? pressedColor : (hover ? hoverColor : this->color);
|
||||
if (color.a <= 0.001f)
|
||||
return;
|
||||
glm::vec2 pos = calcPos();
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->setColor(color);
|
||||
batch->rect(pos.x, pos.y, size.x, size.y);
|
||||
}
|
||||
|
||||
void Container::add(std::shared_ptr<UINode> node) {
|
||||
nodes.push_back(node);
|
||||
node->setParent(this);
|
||||
node->reposition();
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Container::add(std::shared_ptr<UINode> node, glm::vec2 pos) {
|
||||
node->setPos(pos);
|
||||
add(node);
|
||||
}
|
||||
|
||||
void Container::remove(std::shared_ptr<UINode> selected) {
|
||||
selected->setParent(nullptr);
|
||||
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
|
||||
[selected](const std::shared_ptr<UINode> node) {
|
||||
return node == selected;
|
||||
}
|
||||
), nodes.end());
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Container::listenInterval(float interval, ontimeout callback, int repeat) {
|
||||
intervalEvents.push_back({callback, interval, 0.0f, repeat});
|
||||
}
|
||||
|
||||
void Container::setSize(glm::vec2 size) {
|
||||
if (size == getSize()) {
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
UINode::setSize(size);
|
||||
refresh();
|
||||
for (auto& node : nodes) {
|
||||
node->reposition();
|
||||
}
|
||||
}
|
||||
|
||||
void Container::refresh() {
|
||||
std::stable_sort(nodes.begin(), nodes.end(), [](const auto& a, const auto& b) {
|
||||
return a->getZIndex() < b->getZIndex();
|
||||
});
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<UINode>>& Container::getNodes() const {
|
||||
return nodes;
|
||||
}
|
||||
39
src/graphics/ui/elements/Container.hpp
Normal file
39
src/graphics/ui/elements/Container.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_CONTAINER_HPP_
|
||||
#define GRAPHICS_UI_ELEMENTS_CONTAINER_HPP_
|
||||
|
||||
#include "UINode.h"
|
||||
#include "commons.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace gui {
|
||||
class Container : public UINode {
|
||||
protected:
|
||||
std::vector<std::shared_ptr<UINode>> nodes;
|
||||
std::vector<IntervalEvent> intervalEvents;
|
||||
int scroll = 0;
|
||||
int scrollStep = 40;
|
||||
int actualLength = 0;
|
||||
bool scrollable = true;
|
||||
public:
|
||||
Container(glm::vec2 size);
|
||||
|
||||
virtual void act(float delta) override;
|
||||
virtual void drawBackground(const GfxContext* pctx, Assets* assets);
|
||||
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
||||
virtual void add(std::shared_ptr<UINode> node);
|
||||
virtual void add(std::shared_ptr<UINode> node, glm::vec2 pos);
|
||||
virtual void remove(std::shared_ptr<UINode> node);
|
||||
virtual void scrolled(int value) override;
|
||||
virtual void setScrollable(bool flag);
|
||||
void listenInterval(float interval, ontimeout callback, int repeat=-1);
|
||||
virtual glm::vec2 contentOffset() override {return glm::vec2(0.0f, scroll);};
|
||||
virtual void setSize(glm::vec2 size) override;
|
||||
virtual void refresh() override;
|
||||
|
||||
const std::vector<std::shared_ptr<UINode>>& getNodes() const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICS_UI_ELEMENTS_CONTAINER_HPP_
|
||||
93
src/graphics/ui/elements/Menu.cpp
Normal file
93
src/graphics/ui/elements/Menu.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#include "Menu.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace gui;
|
||||
|
||||
Menu::Menu() : Container(glm::vec2(1)){
|
||||
}
|
||||
|
||||
bool Menu::has(const std::string& name) {
|
||||
return pages.find(name) != pages.end() ||
|
||||
pageSuppliers.find(name) != pageSuppliers.end();
|
||||
}
|
||||
|
||||
void Menu::addPage(std::string name, std::shared_ptr<UINode> panel) {
|
||||
pages[name] = Page{name, panel};
|
||||
}
|
||||
|
||||
void Menu::addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier) {
|
||||
pageSuppliers[name] = pageSupplier;
|
||||
}
|
||||
|
||||
std::shared_ptr<UINode> Menu::fetchPage(const std::string& name) {
|
||||
auto found = pages.find(name);
|
||||
if (found == pages.end()) {
|
||||
auto supplier = pageSuppliers.find(name);
|
||||
if (supplier == pageSuppliers.end()) {
|
||||
if (pagesLoader) {
|
||||
return pagesLoader(name);
|
||||
}
|
||||
return nullptr;
|
||||
} else {
|
||||
return supplier->second();
|
||||
// supplied pages caching is not implemented
|
||||
}
|
||||
} else {
|
||||
return found->second.panel;
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::setPage(std::string name, bool history) {
|
||||
Page page {name, fetchPage(name)};
|
||||
if (page.panel == nullptr) {
|
||||
throw std::runtime_error("no page found");
|
||||
}
|
||||
setPage(page, history);
|
||||
}
|
||||
|
||||
void Menu::setPage(Page page, bool history) {
|
||||
if (current.panel) {
|
||||
Container::remove(current.panel);
|
||||
}
|
||||
if (history) {
|
||||
pageStack.push(current);
|
||||
}
|
||||
current = page;
|
||||
Container::add(current.panel);
|
||||
setSize(current.panel->getSize());
|
||||
}
|
||||
|
||||
void Menu::back() {
|
||||
if (pageStack.empty())
|
||||
return;
|
||||
Page page = pageStack.top();
|
||||
pageStack.pop();
|
||||
|
||||
auto updated = fetchPage(page.name);
|
||||
if (updated) {
|
||||
page.panel = updated;
|
||||
}
|
||||
|
||||
setPage(page, false);
|
||||
}
|
||||
|
||||
void Menu::setPageLoader(page_loader_func loader) {
|
||||
pagesLoader = loader;
|
||||
}
|
||||
|
||||
Page& Menu::getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
void Menu::clearHistory() {
|
||||
pageStack = std::stack<Page>();
|
||||
}
|
||||
|
||||
void Menu::reset() {
|
||||
clearHistory();
|
||||
if (current.panel) {
|
||||
Container::remove(current.panel);
|
||||
current = Page{"", nullptr};
|
||||
}
|
||||
}
|
||||
60
src/graphics/ui/elements/Menu.hpp
Normal file
60
src/graphics/ui/elements/Menu.hpp
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_MENU_HPP_
|
||||
#define GRAPHICS_UI_ELEMENTS_MENU_HPP_
|
||||
|
||||
#include "Container.hpp"
|
||||
|
||||
#include <stack>
|
||||
|
||||
namespace gui {
|
||||
struct Page {
|
||||
std::string name;
|
||||
std::shared_ptr<UINode> panel = nullptr;
|
||||
};
|
||||
|
||||
using page_loader_func = std::function<std::shared_ptr<UINode>(const std::string& name)>;
|
||||
|
||||
class Menu : public Container {
|
||||
protected:
|
||||
std::unordered_map<std::string, Page> pages;
|
||||
std::stack<Page> pageStack;
|
||||
Page current;
|
||||
std::unordered_map<std::string, supplier<std::shared_ptr<UINode>>> pageSuppliers;
|
||||
page_loader_func pagesLoader = nullptr;
|
||||
public:
|
||||
Menu();
|
||||
|
||||
/// @brief Check menu have page or page supplier
|
||||
/// @param name page name
|
||||
bool has(const std::string& name);
|
||||
|
||||
/// @brief Set current page to specified one.
|
||||
/// @param name page or page supplier name
|
||||
/// @param history previous page will not be saved in history if false
|
||||
void setPage(std::string name, bool history=true);
|
||||
void setPage(Page page, bool history=true);
|
||||
void addPage(std::string name, std::shared_ptr<UINode> panel);
|
||||
std::shared_ptr<UINode> fetchPage(const std::string& name);
|
||||
|
||||
/// @brief Add page supplier used if page is not found
|
||||
/// @param name page name
|
||||
/// @param pageSupplier page supplier function
|
||||
void addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier);
|
||||
|
||||
/// @brief Page loader is called if accessed page is not found
|
||||
void setPageLoader(page_loader_func loader);
|
||||
|
||||
/// @brief Set page to previous saved in history
|
||||
void back();
|
||||
|
||||
/// @brief Clear pages history
|
||||
void clearHistory();
|
||||
|
||||
/// @brief Clear history and remove and set page to null
|
||||
void reset();
|
||||
|
||||
/// @brief Get current page
|
||||
Page& getCurrent();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICS_UI_ELEMENTS_MENU_HPP_
|
||||
99
src/graphics/ui/elements/Panel.cpp
Normal file
99
src/graphics/ui/elements/Panel.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
#include "Panel.hpp"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
Panel::Panel(glm::vec2 size, glm::vec4 padding, float interval)
|
||||
: Container(size),
|
||||
padding(padding),
|
||||
interval(interval)
|
||||
{
|
||||
setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.75f));
|
||||
}
|
||||
|
||||
Panel::~Panel() {
|
||||
}
|
||||
|
||||
void Panel::setMaxLength(int value) {
|
||||
maxLength = value;
|
||||
}
|
||||
|
||||
int Panel::getMaxLength() const {
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
void Panel::setPadding(glm::vec4 padding) {
|
||||
this->padding = padding;
|
||||
refresh();
|
||||
}
|
||||
|
||||
glm::vec4 Panel::getPadding() const {
|
||||
return padding;
|
||||
}
|
||||
|
||||
void Panel::cropToContent() {
|
||||
if (maxLength > 0.0f) {
|
||||
setSize(glm::vec2(getSize().x, glm::min(maxLength, actualLength)));
|
||||
} else {
|
||||
setSize(glm::vec2(getSize().x, actualLength));
|
||||
}
|
||||
}
|
||||
|
||||
void Panel::fullRefresh() {
|
||||
refresh();
|
||||
cropToContent();
|
||||
Container::fullRefresh();
|
||||
}
|
||||
|
||||
void Panel::add(std::shared_ptr<UINode> node) {
|
||||
node->setResizing(true);
|
||||
Container::add(node);
|
||||
fullRefresh();
|
||||
}
|
||||
|
||||
void Panel::refresh() {
|
||||
UINode::refresh();
|
||||
float x = padding.x;
|
||||
float y = padding.y;
|
||||
glm::vec2 size = getSize();
|
||||
if (orientation == Orientation::vertical) {
|
||||
float maxw = size.x;
|
||||
for (auto& node : nodes) {
|
||||
glm::vec2 nodesize = node->getSize();
|
||||
const glm::vec4 margin = node->getMargin();
|
||||
y += margin.y;
|
||||
|
||||
float ex = x + margin.x;
|
||||
node->setPos(glm::vec2(ex, y));
|
||||
y += nodesize.y + margin.w + interval;
|
||||
|
||||
float width = size.x - padding.x - padding.z - margin.x - margin.z;
|
||||
if (node->isResizing()) {
|
||||
node->setSize(glm::vec2(width, nodesize.y));
|
||||
}
|
||||
node->refresh();
|
||||
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
|
||||
}
|
||||
actualLength = y + padding.w;
|
||||
} else {
|
||||
float maxh = size.y;
|
||||
for (auto& node : nodes) {
|
||||
glm::vec2 nodesize = node->getSize();
|
||||
const glm::vec4 margin = node->getMargin();
|
||||
x += margin.x;
|
||||
node->setPos(glm::vec2(x, y+margin.y));
|
||||
x += nodesize.x + margin.z + interval;
|
||||
|
||||
node->refresh();
|
||||
maxh = fmax(maxh, y+margin.y+node->getSize().y+margin.w+padding.w);
|
||||
}
|
||||
actualLength = size.y;
|
||||
}
|
||||
}
|
||||
|
||||
void Panel::setOrientation(Orientation orientation) {
|
||||
this->orientation = orientation;
|
||||
}
|
||||
|
||||
Orientation Panel::getOrientation() const {
|
||||
return orientation;
|
||||
}
|
||||
40
src/graphics/ui/elements/Panel.hpp
Normal file
40
src/graphics/ui/elements/Panel.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_PANEL_HPP_
|
||||
#define GRAPHICS_UI_ELEMENTS_PANEL_HPP_
|
||||
|
||||
#include "commons.hpp"
|
||||
#include "Container.hpp"
|
||||
|
||||
namespace gui {
|
||||
class Panel : public Container {
|
||||
protected:
|
||||
Orientation orientation = Orientation::vertical;
|
||||
glm::vec4 padding {2.0f};
|
||||
float interval = 2.0f;
|
||||
int maxLength = 0;
|
||||
public:
|
||||
Panel(
|
||||
glm::vec2 size,
|
||||
glm::vec4 padding=glm::vec4(2.0f),
|
||||
float interval=2.0f
|
||||
);
|
||||
virtual ~Panel();
|
||||
|
||||
virtual void cropToContent();
|
||||
|
||||
virtual void setOrientation(Orientation orientation);
|
||||
Orientation getOrientation() const;
|
||||
|
||||
virtual void add(std::shared_ptr<UINode> node) override;
|
||||
|
||||
virtual void refresh() override;
|
||||
virtual void fullRefresh() override;
|
||||
|
||||
virtual void setMaxLength(int value);
|
||||
int getMaxLength() const;
|
||||
|
||||
virtual void setPadding(glm::vec4 padding);
|
||||
glm::vec4 getPadding() const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICS_UI_ELEMENTS_PANEL_HPP_
|
||||
@ -1,6 +1,6 @@
|
||||
#include "UINode.h"
|
||||
|
||||
#include "containers.h"
|
||||
#include "Container.hpp"
|
||||
#include "../../core/Batch2D.h"
|
||||
|
||||
using gui::UINode;
|
||||
|
||||
20
src/graphics/ui/elements/commons.hpp
Normal file
20
src/graphics/ui/elements/commons.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_COMMONS_HPP_
|
||||
#define GRAPHICS_UI_ELEMENTS_COMMONS_HPP_
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace gui {
|
||||
enum class Orientation { vertical, horizontal };
|
||||
|
||||
using ontimeout = std::function<void()>;
|
||||
|
||||
struct IntervalEvent {
|
||||
ontimeout callback;
|
||||
float interval;
|
||||
float timer;
|
||||
// -1 - infinity, 1 - one time event
|
||||
int repeat;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICS_UI_ELEMENTS_COMMONS_HPP_
|
||||
@ -1,342 +0,0 @@
|
||||
#include "containers.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
|
||||
#include "../../../window/Window.h"
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../core/Batch2D.h"
|
||||
#include "../../core/GfxContext.h"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
Container::Container(glm::vec2 size) : UINode(size) {
|
||||
actualLength = size.y;
|
||||
setColor(glm::vec4());
|
||||
}
|
||||
|
||||
std::shared_ptr<UINode> Container::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
|
||||
if (!interactive) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!isInside(pos)) return nullptr;
|
||||
|
||||
for (int i = nodes.size()-1; i >= 0; i--) {
|
||||
auto& node = nodes[i];
|
||||
if (!node->isVisible())
|
||||
continue;
|
||||
auto hover = node->getAt(pos, node);
|
||||
if (hover != nullptr) {
|
||||
return hover;
|
||||
}
|
||||
}
|
||||
return UINode::getAt(pos, self);
|
||||
}
|
||||
|
||||
void Container::act(float delta) {
|
||||
for (auto node : nodes) {
|
||||
if (node->isVisible()) {
|
||||
node->act(delta);
|
||||
}
|
||||
}
|
||||
for (IntervalEvent& event : intervalEvents) {
|
||||
event.timer += delta;
|
||||
if (event.timer > event.interval) {
|
||||
event.callback();
|
||||
event.timer = fmod(event.timer, event.interval);
|
||||
if (event.repeat > 0) {
|
||||
event.repeat--;
|
||||
}
|
||||
}
|
||||
}
|
||||
intervalEvents.erase(std::remove_if(
|
||||
intervalEvents.begin(), intervalEvents.end(),
|
||||
[](const IntervalEvent& event) {
|
||||
return event.repeat == 0;
|
||||
}
|
||||
), intervalEvents.end());
|
||||
}
|
||||
|
||||
void Container::scrolled(int value) {
|
||||
int diff = (actualLength-getSize().y);
|
||||
if (scroll < 0 && diff <= 0) {
|
||||
scroll = 0;
|
||||
}
|
||||
if (diff > 0 && scrollable) {
|
||||
scroll += value * scrollStep;
|
||||
if (scroll > 0)
|
||||
scroll = 0;
|
||||
if (-scroll > diff) {
|
||||
scroll = -diff;
|
||||
}
|
||||
} else if (parent) {
|
||||
parent->scrolled(value);
|
||||
}
|
||||
}
|
||||
|
||||
void Container::setScrollable(bool flag) {
|
||||
scrollable = flag;
|
||||
}
|
||||
|
||||
void Container::draw(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
glm::vec2 size = getSize();
|
||||
drawBackground(pctx, assets);
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->flush();
|
||||
{
|
||||
GfxContext ctx = pctx->sub();
|
||||
ctx.setScissors(glm::vec4(pos.x, pos.y, size.x, size.y));
|
||||
for (auto node : nodes) {
|
||||
if (node->isVisible())
|
||||
node->draw(pctx, assets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec4 color = isPressed() ? pressedColor : (hover ? hoverColor : this->color);
|
||||
if (color.a <= 0.001f)
|
||||
return;
|
||||
glm::vec2 pos = calcPos();
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->setColor(color);
|
||||
batch->rect(pos.x, pos.y, size.x, size.y);
|
||||
}
|
||||
|
||||
void Container::add(std::shared_ptr<UINode> node) {
|
||||
nodes.push_back(node);
|
||||
node->setParent(this);
|
||||
node->reposition();
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Container::add(std::shared_ptr<UINode> node, glm::vec2 pos) {
|
||||
node->setPos(pos);
|
||||
add(node);
|
||||
}
|
||||
|
||||
void Container::remove(std::shared_ptr<UINode> selected) {
|
||||
selected->setParent(nullptr);
|
||||
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
|
||||
[selected](const std::shared_ptr<UINode> node) {
|
||||
return node == selected;
|
||||
}
|
||||
), nodes.end());
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Container::listenInterval(float interval, ontimeout callback, int repeat) {
|
||||
intervalEvents.push_back({callback, interval, 0.0f, repeat});
|
||||
}
|
||||
|
||||
void Container::setSize(glm::vec2 size) {
|
||||
if (size == getSize()) {
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
UINode::setSize(size);
|
||||
refresh();
|
||||
for (auto& node : nodes) {
|
||||
node->reposition();
|
||||
}
|
||||
}
|
||||
|
||||
void Container::refresh() {
|
||||
std::stable_sort(nodes.begin(), nodes.end(), [](const auto& a, const auto& b) {
|
||||
return a->getZIndex() < b->getZIndex();
|
||||
});
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<UINode>>& Container::getNodes() const {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
Panel::Panel(glm::vec2 size, glm::vec4 padding, float interval)
|
||||
: Container(size),
|
||||
padding(padding),
|
||||
interval(interval)
|
||||
{
|
||||
setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.75f));
|
||||
}
|
||||
|
||||
Panel::~Panel() {
|
||||
}
|
||||
|
||||
void Panel::setMaxLength(int value) {
|
||||
maxLength = value;
|
||||
}
|
||||
|
||||
int Panel::getMaxLength() const {
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
void Panel::setPadding(glm::vec4 padding) {
|
||||
this->padding = padding;
|
||||
refresh();
|
||||
}
|
||||
|
||||
glm::vec4 Panel::getPadding() const {
|
||||
return padding;
|
||||
}
|
||||
|
||||
void Panel::cropToContent() {
|
||||
if (maxLength > 0.0f) {
|
||||
setSize(glm::vec2(getSize().x, glm::min(maxLength, actualLength)));
|
||||
} else {
|
||||
setSize(glm::vec2(getSize().x, actualLength));
|
||||
}
|
||||
}
|
||||
|
||||
void Panel::fullRefresh() {
|
||||
refresh();
|
||||
cropToContent();
|
||||
Container::fullRefresh();
|
||||
}
|
||||
|
||||
void Panel::add(std::shared_ptr<UINode> node) {
|
||||
node->setResizing(true);
|
||||
Container::add(node);
|
||||
fullRefresh();
|
||||
}
|
||||
|
||||
void Panel::refresh() {
|
||||
UINode::refresh();
|
||||
float x = padding.x;
|
||||
float y = padding.y;
|
||||
glm::vec2 size = getSize();
|
||||
if (orientation == Orientation::vertical) {
|
||||
float maxw = size.x;
|
||||
for (auto& node : nodes) {
|
||||
glm::vec2 nodesize = node->getSize();
|
||||
const glm::vec4 margin = node->getMargin();
|
||||
y += margin.y;
|
||||
|
||||
float ex = x + margin.x;
|
||||
node->setPos(glm::vec2(ex, y));
|
||||
y += nodesize.y + margin.w + interval;
|
||||
|
||||
float width = size.x - padding.x - padding.z - margin.x - margin.z;
|
||||
if (node->isResizing()) {
|
||||
node->setSize(glm::vec2(width, nodesize.y));
|
||||
}
|
||||
node->refresh();
|
||||
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
|
||||
}
|
||||
actualLength = y + padding.w;
|
||||
} else {
|
||||
float maxh = size.y;
|
||||
for (auto& node : nodes) {
|
||||
glm::vec2 nodesize = node->getSize();
|
||||
const glm::vec4 margin = node->getMargin();
|
||||
x += margin.x;
|
||||
node->setPos(glm::vec2(x, y+margin.y));
|
||||
x += nodesize.x + margin.z + interval;
|
||||
|
||||
node->refresh();
|
||||
maxh = fmax(maxh, y+margin.y+node->getSize().y+margin.w+padding.w);
|
||||
}
|
||||
actualLength = size.y;
|
||||
}
|
||||
}
|
||||
|
||||
void Panel::setOrientation(Orientation orientation) {
|
||||
this->orientation = orientation;
|
||||
}
|
||||
|
||||
Orientation Panel::getOrientation() const {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
Menu::Menu() : Container(glm::vec2(1)){
|
||||
}
|
||||
|
||||
bool Menu::has(const std::string& name) {
|
||||
return pages.find(name) != pages.end() ||
|
||||
pageSuppliers.find(name) != pageSuppliers.end();
|
||||
}
|
||||
|
||||
void Menu::addPage(std::string name, std::shared_ptr<UINode> panel) {
|
||||
pages[name] = Page{name, panel};
|
||||
}
|
||||
|
||||
void Menu::addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier) {
|
||||
pageSuppliers[name] = pageSupplier;
|
||||
}
|
||||
|
||||
std::shared_ptr<UINode> Menu::fetchPage(const std::string& name) {
|
||||
auto found = pages.find(name);
|
||||
if (found == pages.end()) {
|
||||
auto supplier = pageSuppliers.find(name);
|
||||
if (supplier == pageSuppliers.end()) {
|
||||
if (pagesLoader) {
|
||||
return pagesLoader(name);
|
||||
}
|
||||
return nullptr;
|
||||
} else {
|
||||
return supplier->second();
|
||||
// supplied pages caching is not implemented
|
||||
}
|
||||
} else {
|
||||
return found->second.panel;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Menu::setPage(std::string name, bool history) {
|
||||
Page page {name, fetchPage(name)};
|
||||
if (page.panel == nullptr) {
|
||||
throw std::runtime_error("no page found");
|
||||
}
|
||||
setPage(page, history);
|
||||
}
|
||||
|
||||
void Menu::setPage(Page page, bool history) {
|
||||
if (current.panel) {
|
||||
Container::remove(current.panel);
|
||||
}
|
||||
if (history) {
|
||||
pageStack.push(current);
|
||||
}
|
||||
current = page;
|
||||
Container::add(current.panel);
|
||||
setSize(current.panel->getSize());
|
||||
}
|
||||
|
||||
void Menu::back() {
|
||||
if (pageStack.empty())
|
||||
return;
|
||||
Page page = pageStack.top();
|
||||
pageStack.pop();
|
||||
|
||||
auto updated = fetchPage(page.name);
|
||||
if (updated) {
|
||||
page.panel = updated;
|
||||
}
|
||||
|
||||
setPage(page, false);
|
||||
}
|
||||
|
||||
void Menu::setPageLoader(page_loader_func loader) {
|
||||
pagesLoader = loader;
|
||||
}
|
||||
|
||||
Page& Menu::getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
void Menu::clearHistory() {
|
||||
pageStack = std::stack<Page>();
|
||||
}
|
||||
|
||||
void Menu::reset() {
|
||||
clearHistory();
|
||||
if (current.panel) {
|
||||
Container::remove(current.panel);
|
||||
current = Page{"", nullptr};
|
||||
}
|
||||
}
|
||||
@ -1,142 +0,0 @@
|
||||
#ifndef GRAPHICS_UI_ELEMENTS_CONTAINERS_H_
|
||||
#define GRAPHICS_UI_ELEMENTS_CONTAINERS_H_
|
||||
|
||||
#include "UINode.h"
|
||||
#include "../../../delegates.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class Batch2D;
|
||||
class Assets;
|
||||
|
||||
namespace gui {
|
||||
using ontimeout = std::function<void()>;
|
||||
|
||||
struct IntervalEvent {
|
||||
ontimeout callback;
|
||||
float interval;
|
||||
float timer;
|
||||
// -1 - infinity, 1 - one time event
|
||||
int repeat;
|
||||
};
|
||||
|
||||
enum class Orientation { vertical, horizontal };
|
||||
|
||||
class Container : public UINode {
|
||||
protected:
|
||||
std::vector<std::shared_ptr<UINode>> nodes;
|
||||
std::vector<IntervalEvent> intervalEvents;
|
||||
int scroll = 0;
|
||||
int scrollStep = 40;
|
||||
int actualLength = 0;
|
||||
bool scrollable = true;
|
||||
public:
|
||||
Container(glm::vec2 size);
|
||||
|
||||
virtual void act(float delta) override;
|
||||
virtual void drawBackground(const GfxContext* pctx, Assets* assets);
|
||||
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
||||
virtual void add(std::shared_ptr<UINode> node);
|
||||
virtual void add(std::shared_ptr<UINode> node, glm::vec2 pos);
|
||||
virtual void remove(std::shared_ptr<UINode> node);
|
||||
virtual void scrolled(int value) override;
|
||||
virtual void setScrollable(bool flag);
|
||||
void listenInterval(float interval, ontimeout callback, int repeat=-1);
|
||||
virtual glm::vec2 contentOffset() override {return glm::vec2(0.0f, scroll);};
|
||||
virtual void setSize(glm::vec2 size) override;
|
||||
virtual void refresh() override;
|
||||
|
||||
const std::vector<std::shared_ptr<UINode>>& getNodes() const;
|
||||
};
|
||||
|
||||
class Panel : public Container {
|
||||
protected:
|
||||
Orientation orientation = Orientation::vertical;
|
||||
glm::vec4 padding {2.0f};
|
||||
float interval = 2.0f;
|
||||
int maxLength = 0;
|
||||
public:
|
||||
Panel(
|
||||
glm::vec2 size,
|
||||
glm::vec4 padding=glm::vec4(2.0f),
|
||||
float interval=2.0f
|
||||
);
|
||||
virtual ~Panel();
|
||||
|
||||
virtual void cropToContent();
|
||||
|
||||
virtual void setOrientation(Orientation orientation);
|
||||
Orientation getOrientation() const;
|
||||
|
||||
virtual void add(std::shared_ptr<UINode> node) override;
|
||||
|
||||
virtual void refresh() override;
|
||||
virtual void fullRefresh() override;
|
||||
|
||||
virtual void setMaxLength(int value);
|
||||
int getMaxLength() const;
|
||||
|
||||
virtual void setPadding(glm::vec4 padding);
|
||||
glm::vec4 getPadding() const;
|
||||
};
|
||||
|
||||
struct Page {
|
||||
std::string name;
|
||||
std::shared_ptr<UINode> panel = nullptr;
|
||||
|
||||
~Page() {
|
||||
panel = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
using page_loader_func = std::function<std::shared_ptr<UINode>(const std::string& name)>;
|
||||
|
||||
class Menu : public Container {
|
||||
protected:
|
||||
std::unordered_map<std::string, Page> pages;
|
||||
std::stack<Page> pageStack;
|
||||
Page current;
|
||||
std::unordered_map<std::string, supplier<std::shared_ptr<UINode>>> pageSuppliers;
|
||||
page_loader_func pagesLoader = nullptr;
|
||||
public:
|
||||
Menu();
|
||||
|
||||
/// @brief Check menu have page or page supplier
|
||||
/// @param name page name
|
||||
bool has(const std::string& name);
|
||||
|
||||
/// @brief Set current page to specified one.
|
||||
/// @param name page or page supplier name
|
||||
/// @param history previous page will not be saved in history if false
|
||||
void setPage(std::string name, bool history=true);
|
||||
void setPage(Page page, bool history=true);
|
||||
void addPage(std::string name, std::shared_ptr<UINode> panel);
|
||||
std::shared_ptr<UINode> fetchPage(const std::string& name);
|
||||
|
||||
/// @brief Add page supplier used if page is not found
|
||||
/// @param name page name
|
||||
/// @param pageSupplier page supplier function
|
||||
void addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier);
|
||||
|
||||
/// @brief Page loader is called if accessed page is not found
|
||||
void setPageLoader(page_loader_func loader);
|
||||
|
||||
/// @brief Set page to previous saved in history
|
||||
void back();
|
||||
|
||||
/// @brief Clear pages history
|
||||
void clearHistory();
|
||||
|
||||
/// @brief Clear history and remove and set page to null
|
||||
void reset();
|
||||
|
||||
/// @brief Get current page
|
||||
Page& getCurrent();
|
||||
};
|
||||
}
|
||||
#endif // GRAPHICS_UI_ELEMENTS_CONTAINERS_H_
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
#include "../GUI.h"
|
||||
#include "UINode.h"
|
||||
#include "containers.h"
|
||||
#include "Panel.hpp"
|
||||
#include "../../../window/input.h"
|
||||
#include "../../../delegates.h"
|
||||
#include "../../../typedefs.h"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "gui_util.h"
|
||||
#include "elements/controls.h"
|
||||
#include "elements/containers.h"
|
||||
#include "elements/Menu.hpp"
|
||||
#include "gui_xml.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <charconv>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "elements/containers.h"
|
||||
#include "elements/Panel.hpp"
|
||||
#include "elements/controls.h"
|
||||
|
||||
#include "../../frontend/locale/langs.h"
|
||||
|
||||
@ -10,6 +10,8 @@
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../../graphics/ui/gui_util.h"
|
||||
#include "../../../graphics/ui/elements/UINode.h"
|
||||
#include "../../../graphics/ui/elements/Panel.hpp"
|
||||
#include "../../../graphics/ui/elements/Menu.hpp"
|
||||
#include "../../../graphics/ui/elements/controls.h"
|
||||
#include "../../../frontend/UiDocument.h"
|
||||
#include "../../../frontend/locale/langs.h"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user