buttons-related semantics update + refactor

This commit is contained in:
MihailRis 2024-02-03 04:48:05 +03:00
parent 537a0ca358
commit 6cdb45485b
12 changed files with 130 additions and 110 deletions

View File

@ -9,7 +9,7 @@
#include <set>
#include "../typedefs.h"
typedef std::set<unsigned char> DrawGroups;
using DrawGroups = std::set<unsigned char>;
class Block;
class ItemDef;

View File

@ -13,16 +13,11 @@
#include "../../window/input.h"
#include "../../window/Camera.h"
using glm::vec2;
using glm::vec3;
using std::string;
using std::shared_ptr;
using namespace gui;
GUI::GUI() {
container = new Container(vec2(0, 0), vec2(1000));
uicamera = new Camera(vec3(), Window::height);
container = std::make_shared<Container>(glm::vec2(0, 0), glm::vec2(1000));
uicamera = std::make_unique<Camera>(glm::vec3(), Window::height);
uicamera->perspective = false;
uicamera->flipped = true;
@ -32,8 +27,6 @@ GUI::GUI() {
}
GUI::~GUI() {
delete uicamera;
delete container;
}
std::shared_ptr<PagesControl> GUI::getMenu() {
@ -85,7 +78,7 @@ void GUI::actMouse(float delta) {
}
void GUI::act(float delta) {
container->setSize(vec2(Window::width, Window::height));
container->setSize(glm::vec2(Window::width, Window::height));
container->act(delta);
auto prevfocus = focus;
@ -132,7 +125,7 @@ void GUI::draw(const GfxContext* pctx, Assets* assets) {
container->draw(pctx, assets);
}
shared_ptr<UINode> GUI::getFocused() const {
std::shared_ptr<UINode> GUI::getFocused() const {
return focus;
}
@ -144,19 +137,19 @@ void GUI::addBack(std::shared_ptr<UINode> panel) {
container->addBack(panel);
}
void GUI::add(shared_ptr<UINode> panel) {
void GUI::add(std::shared_ptr<UINode> panel) {
container->add(panel);
}
void GUI::remove(shared_ptr<UINode> panel) {
void GUI::remove(std::shared_ptr<UINode> panel) {
container->remove(panel);
}
void GUI::store(string name, shared_ptr<UINode> node) {
void GUI::store(std::string name, std::shared_ptr<UINode> node) {
storage[name] = node;
}
shared_ptr<UINode> GUI::get(string name) {
std::shared_ptr<UINode> GUI::get(std::string name) {
auto found = storage.find(name);
if (found == storage.end()) {
return nullptr;
@ -164,11 +157,11 @@ shared_ptr<UINode> GUI::get(string name) {
return found->second;
}
void GUI::remove(string name) {
void GUI::remove(std::string name) {
storage.erase(name);
}
void GUI::setFocus(shared_ptr<UINode> node) {
void GUI::setFocus(std::shared_ptr<UINode> node) {
if (focus) {
focus->defocus();
}

View File

@ -49,13 +49,13 @@ namespace gui {
class PagesControl;
class GUI {
Container* container;
std::shared_ptr<Container> container;
std::shared_ptr<UINode> hover = nullptr;
std::shared_ptr<UINode> pressed = nullptr;
std::shared_ptr<UINode> focus = nullptr;
std::unordered_map<std::string, std::shared_ptr<UINode>> storage;
Camera* uicamera;
std::unique_ptr<Camera> uicamera;
std::shared_ptr<PagesControl> menu;
void actMouse(float delta);
public:

View File

@ -88,6 +88,14 @@ void UINode::setInteractive(bool flag) {
interactive = flag;
}
void UINode::setResizing(bool flag) {
resizing = flag;
}
bool UINode::isResizing() const {
return resizing;
}
vec2 UINode::calcCoord() const {
if (parent) {
return coord + parent->calcCoord() + parent->contentOffset();

View File

@ -13,12 +13,13 @@ namespace gui {
class UINode;
class GUI;
typedef std::function<void(GUI*)> onaction;
typedef std::function<void(GUI*, double)> onnumberchange;
using onaction = std::function<void(GUI*)>;
using onnumberchange = std::function<void(GUI*, double)>;
enum class Align {
left, center, right
};
class UINode {
protected:
glm::vec2 coord;
@ -31,6 +32,7 @@ namespace gui {
bool pressed = false;
bool focused = false;
bool interactive = true;
bool resizing = true;
Align align = Align::left;
UINode* parent = nullptr;
UINode(glm::vec2 coord, glm::vec2 size);
@ -96,6 +98,9 @@ namespace gui {
/* Make the element opaque (true) or transparent (false) for cursor */
virtual void setInteractive(bool flag);
virtual void setResizing(bool flag);
virtual bool isResizing() const;
/* Get inner content offset. Used for scroll */
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);};
/* Calculate screen position of the element */

View File

@ -49,11 +49,19 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
Font* font = assets->getFont(fontName_);
vec2 size = getSize();
vec2 newsize = vec2(font->calcWidth(text), font->lineHeight());
if (newsize.x > size.x) {
setSize(newsize);
size = newsize;
}
vec2 coord = calcCoord();
switch (align) {
case Align::left:
break;
case Align::center:
coord.x += (size.x-newsize.x)*0.5f;
break;
case Align::right:
coord.x += size.x-newsize.x;
break;
}
font->draw(batch, text, coord.x, coord.y);
}
@ -62,10 +70,6 @@ Label* Label::textSupplier(wstringsupplier supplier) {
return this;
}
void Label::setSize(vec2 sizenew) {
UINode::setSize(vec2(UINode::getSize().x, sizenew.y));
}
// ================================= Image ====================================
Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
setInteractive(false);
@ -92,9 +96,19 @@ Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
}
Button::Button(std::wstring text, glm::vec4 padding, onaction action)
: Panel(vec2(32,32), padding, 0)
Button::Button(
std::wstring text,
vec4 padding,
onaction action,
vec2 size
) : Panel(size, padding, 0)
{
size = vec2(
glm::max(padding.x + padding.z + text.length()*8, size.x),
glm::max(padding.y + padding.w + 16, size.y)
);
setSize(size);
if (action) {
listenAction(action);
}
@ -102,6 +116,7 @@ Button::Button(std::wstring text, glm::vec4 padding, onaction action)
label = std::make_shared<Label>(text);
label->setAlign(Align::center);
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y));
add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
}
@ -188,7 +203,7 @@ void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
// ================================ TextBox ===================================
TextBox::TextBox(std::wstring placeholder, vec4 padding)
: Panel(vec2(200,32), padding, 0, false),
: Panel(vec2(200,32), padding, 0),
input(L""),
placeholder(placeholder) {
label = std::make_shared<Label>(L"");
@ -312,7 +327,7 @@ void TextBox::text(std::wstring value) {
// ============================== InputBindBox ================================
InputBindBox::InputBindBox(Binding& binding, vec4 padding)
: Panel(vec2(100,32), padding, 0, false),
: Panel(vec2(100,32), padding, 0),
binding(binding) {
label = std::make_shared<Label>(L"");
add(label);
@ -437,7 +452,7 @@ FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
: Panel(size),
checkbox(std::make_shared<CheckBox>(checked)){
setColor(vec4(0.0f));
orientation(Orientation::horizontal);
setOrientation(Orientation::horizontal);
add(checkbox);

View File

@ -17,16 +17,16 @@ class Batch2D;
class Assets;
namespace gui {
typedef std::function<std::wstring()> wstringsupplier;
typedef std::function<void(std::wstring)> wstringconsumer;
using wstringsupplier = std::function<std::wstring()>;
using wstringconsumer = std::function<void(std::wstring)>;
typedef std::function<double()> doublesupplier;
typedef std::function<void(double)> doubleconsumer;
using doublesupplier = std::function<double()>;
using doubleconsumer = std::function<void(double)>;
typedef std::function<bool()> boolsupplier;
typedef std::function<void(bool)> boolconsumer;
using boolsupplier = std::function<bool()>;
using boolconsumer = std::function<void(bool)>;
typedef std::function<bool(const std::wstring&)> wstringchecker;
using wstringchecker = std::function<bool(const std::wstring&)>;
class Label : public UINode {
protected:
@ -43,7 +43,6 @@ namespace gui {
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual Label* textSupplier(wstringsupplier supplier);
virtual void setSize(glm::vec2 size) override;
};
class Image : public UINode {
@ -66,7 +65,8 @@ namespace gui {
Button(std::wstring text,
glm::vec4 padding,
onaction action);
onaction action,
glm::vec2 size=glm::vec2(-1));
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
@ -85,7 +85,6 @@ namespace gui {
class RichButton : public Container {
protected:
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
std::vector<onaction> actions;
public:

View File

@ -139,23 +139,27 @@ void Container::listenInterval(float interval, ontimeout callback, int repeat) {
intervalEvents.push_back({callback, interval, 0.0f, repeat});
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
void Container::setSize(glm::vec2 size) {
UINode::setSize(size);
refresh();
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval)
: Container(vec2(), size),
padding(padding),
interval(interval),
resizing_(resizing) {
interval(interval) {
setColor(vec4(0.0f, 0.0f, 0.0f, 0.75f));
}
Panel::~Panel() {
}
void Panel::maxLength(int value) {
maxLength_ = value;
void Panel::setMaxLength(int value) {
maxLength = value;
}
int Panel::maxLength() const {
return maxLength_;
int Panel::getMaxLength() const {
return maxLength;
}
void Panel::setPadding(glm::vec4 padding) {
@ -166,43 +170,42 @@ glm::vec4 Panel::getPadding() const {
return padding;
}
void Panel::cropToContent() {
if (maxLength > 0.0f) {
setSize(vec2(getSize().x, glm::min(maxLength, actualLength)));
} else {
setSize(vec2(getSize().x, actualLength));
}
}
void Panel::add(std::shared_ptr<UINode> node) {
Container::add(node);
refresh();
cropToContent();
}
void Panel::refresh() {
float x = padding.x;
float y = padding.y;
vec2 size = getSize();
if (orientation_ == Orientation::vertical) {
if (orientation == Orientation::vertical) {
float maxw = size.x;
for (auto& node : nodes) {
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
y += margin.y;
float ex;
float spacex = size.x - margin.z - padding.z;
switch (node->getAlign()) {
case Align::center:
ex = x + fmax(0.0f, spacex - nodesize.x) / 2.0f;
break;
case Align::right:
ex = x + spacex - nodesize.x;
break;
default:
ex = x + margin.x;
}
float ex = x + margin.x;
node->setCoord(vec2(ex, y));
y += nodesize.y + margin.w + interval;
float width = size.x - padding.x - padding.z - margin.x - margin.z;
node->setSize(vec2(width, nodesize.y));;
if (node->isResizing()) {
node->setSize(vec2(width, nodesize.y));
}
node->refresh();
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
}
if (resizing_) {
if (maxLength_)
setSize(vec2(size.x, glm::min(maxLength_, (int)(y+padding.w))));
else
setSize(vec2(size.x, y+padding.w));
}
actualLength = y + padding.w;
} else {
float maxh = size.y;
@ -216,22 +219,16 @@ void Panel::refresh() {
node->refresh();
maxh = fmax(maxh, y+margin.y+node->getSize().y+margin.w+padding.w);
}
if (resizing_) {
if (maxLength_)
setSize(vec2(glm::min(maxLength_, (int)(x+padding.z)), size.y));
else
setSize(vec2(x+padding.z, size.y));
}
actualLength = size.y;
}
}
void Panel::orientation(Orientation orientation) {
this->orientation_ = orientation;
void Panel::setOrientation(Orientation orientation) {
this->orientation = orientation;
}
Orientation Panel::orientation() const {
return orientation_;
Orientation Panel::getOrientation() const {
return orientation;
}
PagesControl::PagesControl() : Container(vec2(), vec2(1)){

View File

@ -12,7 +12,8 @@ class Batch2D;
class Assets;
namespace gui {
typedef std::function<void()> ontimeout;
using ontimeout = std::function<void()>;
struct IntervalEvent {
ontimeout callback;
float interval;
@ -45,31 +46,34 @@ namespace gui {
virtual void scrollable(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);
};
class Panel : public Container {
protected:
Orientation orientation_ = Orientation::vertical;
Orientation orientation = Orientation::vertical;
glm::vec4 padding {2.0f};
float interval = 2.0f;
bool resizing_;
int maxLength_ = 0;
int maxLength = 0;
public:
Panel(
glm::vec2 size,
glm::vec4 padding=glm::vec4(2.0f),
float interval=2.0f,
bool resizing=true
float interval=2.0f
);
virtual ~Panel();
virtual void orientation(Orientation orientation);
Orientation orientation() const;
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 maxLength(int value);
int maxLength() const;
virtual void setMaxLength(int value);
int getMaxLength() const;
virtual void setPadding(glm::vec4 padding);
glm::vec4 getPadding() const;

View File

@ -98,7 +98,7 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
for (int ax = 0; ax < 3; ax++){
auto sub = std::make_shared<Panel>(vec2(10, 27), vec4(0.0f));
sub->orientation(Orientation::horizontal);
sub->setOrientation(Orientation::horizontal);
std::wstring str = L"x: ";
str[0] += ax;

View File

@ -72,8 +72,8 @@ static std::shared_ptr<Panel> create_page(
static std::shared_ptr<Button> create_button(
std::wstring text,
glm::vec4 padding,
glm::vec4 margin,
vec4 padding,
vec4 margin,
gui::onaction action
) {
auto btn = std::make_shared<Button>(
@ -100,7 +100,7 @@ static void show_content_missing(
for (auto& entry : lut->getMissingContent()) {
auto hpanel = std::make_shared<Panel>(vec2(500, 30));
hpanel->setColor(vec4(0.0f));
hpanel->orientation(Orientation::horizontal);
hpanel->setOrientation(Orientation::horizontal);
auto namelabel = std::make_shared<Label>(util::str2wstr_utf8(entry.name));
namelabel->setColor(vec4(1.0f, 0.2f, 0.2f, 0.5f));
@ -112,7 +112,7 @@ static void show_content_missing(
hpanel->add(namelabel);
subpanel->add(hpanel);
}
subpanel->maxLength(400);
subpanel->setMaxLength(400);
panel->add(subpanel);
panel->add(std::make_shared<Button>(
@ -213,7 +213,7 @@ void open_world(std::string name, Engine* engine) {
std::shared_ptr<Panel> create_worlds_panel(Engine* engine) {
auto panel = std::make_shared<Panel>(vec2(390, 200), vec4(5.0f));
panel->setColor(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->maxLength(400);
panel->setMaxLength(400);
auto paths = engine->getPaths();
@ -277,7 +277,7 @@ std::shared_ptr<Panel> create_packs_panel(
auto assets = engine->getAssets();
auto panel = std::make_shared<Panel>(vec2(PACKS_PANEL_WIDTH, 200), vec4(5.0f));
panel->setColor(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->maxLength(400);
panel->setMaxLength(400);
panel->scrollable(true);
for (auto& pack : packs) {
@ -314,7 +314,7 @@ std::shared_ptr<Panel> create_packs_panel(
descriptionlabel->setColor(vec4(1, 1, 1, 0.7f));
packpanel->add(descriptionlabel, vec2(80, 28));
packpanel->add(std::make_shared<Image>(icon, glm::vec2(64)), vec2(8));
packpanel->add(std::make_shared<Image>(icon, vec2(64)), vec2(8));
packpanel->setColor(vec4(0.06f, 0.12f, 0.18f, 0.7f));
panel->add(packpanel);
@ -479,13 +479,13 @@ void create_controls_panel(Engine* engine) {
auto scrollPanel = std::make_shared<Panel>(vec2(400, 200), vec4(2.0f), 1.0f);
scrollPanel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.3f));
scrollPanel->maxLength(400);
scrollPanel->setMaxLength(400);
for (auto& entry : Events::bindings){
std::string bindname = entry.first;
auto subpanel = std::make_shared<Panel>(vec2(400, 40), vec4(5.0f), 1.0f);
subpanel->setColor(vec4(0.0f));
subpanel->orientation(Orientation::horizontal);
subpanel->setOrientation(Orientation::horizontal);
subpanel->add(std::make_shared<InputBindBox>(entry.second));
auto label = std::make_shared<Label>(langs::get(util::str2wstr_utf8(bindname)));

View File

@ -4,18 +4,17 @@
#include <stdlib.h>
#include <stdint.h>
typedef unsigned int uint;
using uint = unsigned int;
// use for bytes arrays
typedef uint8_t ubyte;
using ubyte = uint8_t;
// content indices
typedef uint32_t itemid_t;
typedef uint16_t blockid_t;
using itemid_t = uint32_t;
using blockid_t = uint16_t;
typedef uint32_t itemcount_t;
typedef uint16_t blockstate_t;
typedef uint16_t light_t;
using itemcount_t = uint32_t;
using blockstate_t = uint16_t;
using light_t = uint16_t;
#endif