frontend/gui refactor

This commit is contained in:
MihailRis
2024-01-31 04:26:35 +03:00
parent 49fdcdfb27
commit 8c722b146e
17 changed files with 331 additions and 332 deletions
+48 -50
View File
@@ -5,8 +5,7 @@
#include "../../window/Window.h"
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
using std::shared_ptr;
#include "../../graphics/GfxContext.h"
using namespace gui;
@@ -17,7 +16,7 @@ Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
actualLength = size.y;
}
shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
std::shared_ptr<UINode> Container::getAt(vec2 pos, std::shared_ptr<UINode> self) {
if (!interactive) {
return nullptr;
}
@@ -25,7 +24,7 @@ shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
for (int i = nodes.size()-1; i >= 0; i--) {
auto& node = nodes[i];
if (!node->visible())
if (!node->isVisible())
continue;
auto hover = node->getAt(pos, node);
if (hover != nullptr) {
@@ -54,14 +53,14 @@ void Container::act(float delta) {
), intervalEvents.end());
for (auto node : nodes) {
if (node->visible()) {
if (node->isVisible()) {
node->act(delta);
}
}
}
void Container::scrolled(int value) {
int diff = (actualLength-size().y);
int diff = (actualLength-getSize().y);
if (diff > 0 && scrollable_) {
scroll += value * 40;
if (scroll > 0)
@@ -78,46 +77,50 @@ void Container::scrollable(bool flag) {
scrollable_ = flag;
}
void Container::draw(Batch2D* batch, Assets* assets) {
void Container::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
vec2 size = this->size();
drawBackground(batch, assets);
vec2 size = getSize();
drawBackground(pctx, assets);
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->render();
Window::pushScissor(vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) {
if (node->visible())
node->draw(batch, assets);
{
GfxContext ctx = pctx->sub();
ctx.scissors(vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) {
if (node->isVisible())
node->draw(pctx, assets);
}
batch->render();
}
batch->render();
Window::popScissor();
}
void Container::addBack(shared_ptr<UINode> node) {
void Container::addBack(std::shared_ptr<UINode> node) {
nodes.insert(nodes.begin(), node);
node->setParent(this);
refresh();
}
void Container::add(shared_ptr<UINode> node) {
void Container::add(std::shared_ptr<UINode> node) {
nodes.push_back(node);
node->setParent(this);
refresh();
}
void Container::add(UINode* node) {
add(shared_ptr<UINode>(node));
add(std::shared_ptr<UINode>(node));
}
void Container::add(shared_ptr<UINode> node, glm::vec2 coord) {
void Container::add(std::shared_ptr<UINode> node, glm::vec2 coord) {
node->setCoord(coord);
add(node);
}
void Container::remove(shared_ptr<UINode> selected) {
void Container::remove(std::shared_ptr<UINode> selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
[selected](const shared_ptr<UINode> node) {
[selected](const std::shared_ptr<UINode> node) {
return node == selected;
}
), nodes.end());
@@ -133,17 +136,19 @@ Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
padding(padding),
interval(interval),
resizing_(resizing) {
color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f);
setColor(vec4(0.0f, 0.0f, 0.0f, 0.75f));
}
Panel::~Panel() {
}
void Panel::drawBackground(Batch2D* batch, Assets* assets) {
void Panel::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = color_;
batch->rect(coord.x, coord.y, size_.x, size_.y);
batch->color = color;
batch->rect(coord.x, coord.y, size.x, size.y);
}
void Panel::maxLength(int value) {
@@ -157,22 +162,22 @@ int Panel::maxLength() const {
void Panel::refresh() {
float x = padding.x;
float y = padding.y;
vec2 size = this->size();
vec2 size = getSize();
if (orientation_ == Orientation::vertical) {
float maxw = size.x;
for (auto& node : nodes) {
vec2 nodesize = node->size();
const vec4 margin = node->margin();
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
y += margin.y;
float ex;
float spacex = size.x - margin.z - padding.z;
switch (node->align()) {
switch (node->getAlign()) {
case Align::center:
ex = x + fmax(0.0f, spacex - node->size().x) / 2.0f;
ex = x + fmax(0.0f, spacex - nodesize.x) / 2.0f;
break;
case Align::right:
ex = x + spacex - node->size().x;
ex = x + spacex - nodesize.x;
break;
default:
ex = x + margin.x;
@@ -181,36 +186,36 @@ void Panel::refresh() {
y += nodesize.y + margin.w + interval;
float width = size.x - padding.x - padding.z - margin.x - margin.z;
node->size(vec2(width, nodesize.y));;
node->setSize(vec2(width, nodesize.y));;
node->refresh();
maxw = fmax(maxw, ex+node->size().x+margin.z+padding.z);
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
}
if (resizing_) {
if (maxLength_)
this->size(vec2(size.x, glm::min(maxLength_, (int)(y+padding.w))));
setSize(vec2(size.x, glm::min(maxLength_, (int)(y+padding.w))));
else
this->size(vec2(size.x, y+padding.w));
setSize(vec2(size.x, y+padding.w));
}
actualLength = y + padding.w;
} else {
float maxh = size.y;
for (auto& node : nodes) {
vec2 nodesize = node->size();
const vec4 margin = node->margin();
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
x += margin.x;
node->setCoord(vec2(x, y+margin.y));
x += nodesize.x + margin.z + interval;
float height = size.y - padding.y - padding.w - margin.y - margin.w;
node->size(vec2(nodesize.x, height));
node->setSize(vec2(nodesize.x, height));
node->refresh();
maxh = fmax(maxh, y+margin.y+node->size().y+margin.w+padding.w);
maxh = fmax(maxh, y+margin.y+node->getSize().y+margin.w+padding.w);
}
if (resizing_) {
if (maxLength_)
this->size(vec2(glm::min(maxLength_, (int)(x+padding.z)), size.y));
setSize(vec2(glm::min(maxLength_, (int)(x+padding.z)), size.y));
else
this->size(vec2(x+padding.z, size.y));
setSize(vec2(x+padding.z, size.y));
}
actualLength = size.y;
}
@@ -224,13 +229,6 @@ Orientation Panel::orientation() const {
return orientation_;
}
void Panel::lock(){
for (auto node : nodes) {
node->lock();
}
resizing_ = false;
}
PagesControl::PagesControl() : Container(vec2(), vec2(1)){
}
@@ -243,7 +241,7 @@ void PagesControl::add(std::string name, std::shared_ptr<UINode> panel) {
}
void PagesControl::add(std::string name, UINode* panel) {
add(name, shared_ptr<UINode>(panel));
add(name, std::shared_ptr<UINode>(panel));
}
void PagesControl::set(std::string name, bool history) {
@@ -260,7 +258,7 @@ void PagesControl::set(std::string name, bool history) {
curname_ = name;
current_ = found->second;
Container::add(current_.panel);
size(current_.panel->size());
setSize(current_.panel->getSize());
}
void PagesControl::back() {