Refactor and fixes
This commit is contained in:
@@ -20,7 +20,15 @@ GUI::~GUI() {
|
||||
delete container;
|
||||
}
|
||||
|
||||
void GUI::act() {
|
||||
void GUI::act(float delta) {
|
||||
for (IntervalEvent& event : intervalEvents) {
|
||||
event.timer += delta;
|
||||
if (event.timer > event.interval) {
|
||||
event.callback();
|
||||
event.timer = fmod(event.timer, event.interval);
|
||||
}
|
||||
}
|
||||
|
||||
container->size(vec2(Window::width, Window::height));
|
||||
int mx = Events::x;
|
||||
int my = Events::y;
|
||||
@@ -83,3 +91,7 @@ bool GUI::isFocusCaught() const {
|
||||
void GUI::add(shared_ptr<UINode> panel) {
|
||||
container->add(panel);
|
||||
}
|
||||
|
||||
void GUI::interval(float interval, ontimeout callback) {
|
||||
intervalEvents.push_back({callback, interval, 0.0f});
|
||||
}
|
||||
+12
-1
@@ -4,6 +4,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <functional>
|
||||
|
||||
class Batch2D;
|
||||
class Assets;
|
||||
@@ -43,11 +44,19 @@ namespace gui {
|
||||
class UINode;
|
||||
class Container;
|
||||
|
||||
typedef std::function<void()> ontimeout;
|
||||
struct IntervalEvent {
|
||||
ontimeout callback;
|
||||
float interval;
|
||||
float timer;
|
||||
};
|
||||
|
||||
class GUI {
|
||||
Container* container;
|
||||
std::shared_ptr<UINode> hover = nullptr;
|
||||
std::shared_ptr<UINode> pressed = nullptr;
|
||||
std::shared_ptr<UINode> focus = nullptr;
|
||||
std::vector<IntervalEvent> intervalEvents;
|
||||
public:
|
||||
GUI();
|
||||
~GUI();
|
||||
@@ -55,9 +64,11 @@ namespace gui {
|
||||
std::shared_ptr<UINode> getFocused() const;
|
||||
bool isFocusCaught() const;
|
||||
|
||||
void act();
|
||||
void act(float delta);
|
||||
void draw(Batch2D* batch, Assets* assets);
|
||||
void add(std::shared_ptr<UINode> panel);
|
||||
|
||||
void interval(float interval, ontimeout callback);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "Panel.h"
|
||||
|
||||
#include "../../graphics/Batch2D.h"
|
||||
|
||||
using gui::Panel;
|
||||
|
||||
Panel::Panel(glm::vec2 coord, glm::vec2 size, glm::vec4 color)
|
||||
: coord(coord), size(size), color(color) {
|
||||
|
||||
}
|
||||
|
||||
void Panel::draw(Batch2D* batch) {
|
||||
batch->texture(nullptr);
|
||||
batch->color = color;
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
}
|
||||
|
||||
bool Panel::isVisible() const {
|
||||
return visible;
|
||||
}
|
||||
|
||||
void Panel::setVisible(bool flag) {
|
||||
visible = flag;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef FRONTEND_GUI_PANEL_H_
|
||||
#define FRONTEND_GUI_PANEL_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Batch2D;
|
||||
|
||||
namespace gui {
|
||||
class Panel {
|
||||
glm::vec2 coord;
|
||||
glm::vec2 size;
|
||||
glm::vec4 color;
|
||||
bool visible = true;
|
||||
public:
|
||||
Panel(glm::vec2 coord, glm::vec2 size, glm::vec4 color);
|
||||
|
||||
void draw(Batch2D* batch);
|
||||
|
||||
void setVisible(bool flag);
|
||||
bool isVisible() const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // FRONTEND_GUI_PANEL_H_
|
||||
Reference in New Issue
Block a user