PagesControl, refactor

This commit is contained in:
MihailRis
2023-11-20 03:31:18 +03:00
parent 41f9c51a27
commit 4ea7bda249
15 changed files with 339 additions and 117 deletions
+15 -4
View File
@@ -45,6 +45,7 @@ void GUI::act(float delta) {
}
this->hover = hover;
auto prevfocus = focus;
if (Events::jclicked(0)) {
if (pressed == nullptr && this->hover) {
pressed = hover;
@@ -52,7 +53,10 @@ void GUI::act(float delta) {
if (focus && focus != pressed) {
focus->defocus();
}
focus = pressed;
if (focus != pressed) {
focus = pressed;
focus->focus(this);
}
}
if (this->hover == nullptr && focus) {
focus->defocus();
@@ -63,9 +67,7 @@ void GUI::act(float delta) {
pressed = nullptr;
}
if (focus) {
if (!focus->isfocused()){
focus = nullptr;
} else if (Events::jpressed(keycode::ESCAPE)) {
if (Events::jpressed(keycode::ESCAPE)) {
focus->defocus();
focus = nullptr;
} else {
@@ -78,8 +80,17 @@ void GUI::act(float delta) {
if (Events::clicked(mousecode::BUTTON_1)) {
focus->mouseMove(this, mx, my);
}
if (prevfocus == focus)
for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) {
if (Events::jclicked(i)) {
focus->clicked(this, i);
}
}
}
}
if (focus && !focus->isfocused()) {
focus = nullptr;
}
}
void GUI::draw(Batch2D* batch, Assets* assets) {
+1 -2
View File
@@ -52,7 +52,6 @@ UINode* UINode::getParent() const {
void UINode::click(GUI*, int x, int y) {
pressed_ = true;
focused_ = true;
}
void UINode::mouseRelease(GUI*, int x, int y) {
@@ -103,7 +102,7 @@ void UINode::size(vec2 size) {
this->size_ = size;
if (parent) {
sizelock = true;
parent->refresh();
//parent->refresh();
sizelock = false;
}
}
+2
View File
@@ -56,7 +56,9 @@ namespace gui {
virtual void margin(glm::vec4 margin);
glm::vec4 margin() const;
virtual void focus(GUI*) {focused_ = true;}
virtual void click(GUI*, int x, int y);
virtual void clicked(GUI*, int button) {}
virtual void mouseMove(GUI*, int x, int y) {};
virtual void mouseRelease(GUI*, int x, int y);
+45 -2
View File
@@ -5,6 +5,7 @@
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
#include "../../graphics/Font.h"
#include "../../util/stringutil.h"
using std::string;
using std::wstring;
@@ -36,7 +37,7 @@ void Label::draw(Batch2D* batch, Assets* assets) {
}
batch->color = color_;
Font* font = assets->getFont(fontName_);
vec2 size = this->size();
vec2 size = UINode::size();
vec2 newsize = vec2(font->calcWidth(text_), font->lineHeight());
if (newsize.x > size.x) {
this->size(newsize);
@@ -51,6 +52,11 @@ Label* Label::textSupplier(wstringsupplier supplier) {
return this;
}
void Label::size(vec2 sizenew) {
UINode::size(vec2(UINode::size().x, sizenew.y));
}
// ================================= Button ===================================
Button::Button(shared_ptr<UINode> content, glm::vec4 padding) : Panel(vec2(32,32), padding, 0) {
add(content);
}
@@ -86,12 +92,12 @@ Button* Button::listenAction(onaction action) {
return this;
}
// ================================ TextBox ===================================
TextBox::TextBox(wstring placeholder, vec4 padding)
: Panel(vec2(200,32), padding, 0, false),
input(L""),
placeholder(placeholder) {
label = new Label(L"");
label->align(Align::center);
add(shared_ptr<UINode>(label));
}
@@ -151,6 +157,42 @@ wstring TextBox::text() const {
return input;
}
// ============================== InputBindBox ================================
InputBindBox::InputBindBox(Binding& binding, vec4 padding)
: Panel(vec2(100,32), padding, 0, false),
binding(binding) {
label = new Label(L"");
//label->align(Align::center);
add(shared_ptr<UINode>(label));
}
shared_ptr<UINode> InputBindBox::getAt(vec2 pos, shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
void InputBindBox::drawBackground(Batch2D* batch, Assets* assets) {
vec2 coord = calcCoord();
batch->texture(nullptr);
batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_));
batch->rect(coord.x, coord.y, size_.x, size_.y);
label->text(util::str2wstr_utf8(binding.text()));
}
void InputBindBox::clicked(GUI*, int button) {
binding.type = inputtype::button;
binding.code = button;
defocus();
}
void InputBindBox::keyPressed(int key) {
if (key != keycode::ESCAPE) {
binding.type = inputtype::keyboard;
binding.code = key;
}
defocus();
}
// ================================ TrackBar ==================================
TrackBar::TrackBar(double min, double max, double value, double step, int trackWidth)
: UINode(vec2(), vec2(32)), min(min), max(max), value(value), step(step), trackWidth(trackWidth) {
color(vec4(0.f, 0.f, 0.f, 0.4f));
@@ -194,6 +236,7 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
}
}
// ================================ CheckBox ==================================
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) {
color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
}
+18
View File
@@ -8,6 +8,7 @@
#include <glm/glm.hpp>
#include "UINode.h"
#include "panels.h"
#include "../../window/input.h"
class Batch2D;
class Assets;
@@ -36,6 +37,7 @@ namespace gui {
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual Label* textSupplier(wstringsupplier supplier);
virtual void size(glm::vec2 size) override;
};
class Button : public Panel {
@@ -79,6 +81,22 @@ namespace gui {
virtual std::wstring text() const;
};
class InputBindBox : public Panel {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
Label* label;
Binding& binding;
public:
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f));
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void clicked(GUI*, int button) override;
virtual void keyPressed(int key) override;
virtual bool isfocuskeeper() const override {return true;}
};
class TrackBar : public UINode {
protected:
glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f};
+57 -5
View File
@@ -1,15 +1,14 @@
#include "panels.h"
#include <stdexcept>
#include "../../window/Window.h"
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
using std::shared_ptr;
using gui::UINode;
using gui::Container;
using gui::Panel;
using gui::Orientation;
using namespace gui;
using glm::vec2;
using glm::vec4;
@@ -138,7 +137,8 @@ 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->size(vec2(width, nodesize.y));;
node->refresh();
maxw = fmax(maxw, ex+node->size().x+margin.z+padding.z);
}
if (resizing_)
@@ -154,6 +154,7 @@ void Panel::refresh() {
float height = size.y - padding.y - padding.w - margin.y - margin.w;
node->size(vec2(nodesize.x, height));
node->refresh();
maxh = fmax(maxh, y+margin.y+node->size().y+margin.w+padding.w);
}
bool increased = maxh > size.y;
@@ -177,4 +178,55 @@ void Panel::lock(){
node->lock();
}
resizing_ = false;
}
PagesControl::PagesControl() : Container(vec2(), vec2(1)){
}
void PagesControl::add(std::string name, std::shared_ptr<UINode> panel) {
panel->visible(false);
pages[name] = Page{panel};
Container::add(panel);
}
void PagesControl::set(std::string name, bool history) {
auto found = pages.find(name);
if (found == pages.end()) {
throw std::runtime_error("no page found");
}
if (current_.panel) {
current_.panel->visible(false);
}
if (history) {
pageStack.push(curname_);
}
curname_ = name;
current_ = found->second;
current_.panel->visible(true);
size(current_.panel->size());
}
void PagesControl::back() {
if (pageStack.empty())
return;
std::string name = pageStack.top();
pageStack.pop();
set(name, false);
}
Page PagesControl::current() {
return current_;
}
void PagesControl::clearHistory() {
pageStack = std::stack<std::string>();
}
void PagesControl::reset() {
clearHistory();
if (current_.panel) {
curname_ = "";
current_.panel->visible(false);
current_ = Page{nullptr};
}
}
+24
View File
@@ -3,6 +3,8 @@
#include <glm/glm.hpp>
#include <vector>
#include <stack>
#include <string>
#include <memory>
#include "UINode.h"
@@ -56,5 +58,27 @@ namespace gui {
virtual void refresh() override;
virtual void lock() override;
};
struct Page {
std::shared_ptr<UINode> panel = nullptr;
};
class PagesControl : public Container {
protected:
std::unordered_map<std::string, Page> pages;
std::stack<std::string> pageStack;
Page current_;
std::string curname_ = "";
public:
PagesControl();
void set(std::string name, bool history=true);
void add(std::string name, std::shared_ptr<UINode> panel);
void back();
void clearHistory();
void reset();
Page current();
};
}
#endif // FRONTEND_GUI_PANELS_H_