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_
|
||||
+25
-16
@@ -34,12 +34,7 @@ using std::shared_ptr;
|
||||
using glm::vec2;
|
||||
using glm::vec3;
|
||||
using glm::vec4;
|
||||
using gui::GUI;
|
||||
using gui::UINode;
|
||||
using gui::Panel;
|
||||
using gui::Label;
|
||||
using gui::Button;
|
||||
using gui::TextBox;
|
||||
using namespace gui;
|
||||
|
||||
inline Label* create_label(gui::wstringsupplier supplier) {
|
||||
Label* label = new Label(L"-");
|
||||
@@ -49,10 +44,15 @@ inline Label* create_label(gui::wstringsupplier supplier) {
|
||||
|
||||
HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level), assets(assets), guiController(gui) {
|
||||
batch = new Batch2D(1024);
|
||||
uicamera = new Camera(glm::vec3(), Window::height);
|
||||
uicamera = new Camera(vec3(), Window::height);
|
||||
uicamera->perspective = false;
|
||||
uicamera->flipped = true;
|
||||
|
||||
gui->interval(1.0f, [this]() {
|
||||
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
||||
fpsMin = fps;
|
||||
fpsMax = fps;
|
||||
});
|
||||
Panel* panel = new Panel(vec2(200, 200), vec4(5.0f), 1.0f);
|
||||
panel->setCoord(vec2(10, 10));
|
||||
panel->add(shared_ptr<Label>(create_label([this](){
|
||||
@@ -74,11 +74,14 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
|
||||
})));
|
||||
for (int ax = 0; ax < 3; ax++){
|
||||
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
|
||||
sub->orientation(gui::Orientation::horizontal);
|
||||
sub->orientation(Orientation::horizontal);
|
||||
|
||||
Label* label = new Label(wstring({L'x'+ax})+L": ");
|
||||
label->margin(vec4(2, 3, 2, 3));
|
||||
sub->add(shared_ptr<UINode>(label));
|
||||
sub->color(vec4(0.0f));
|
||||
|
||||
// Coordinate input
|
||||
TextBox* box = new TextBox(L"");
|
||||
box->textSupplier([this, ax]() {
|
||||
Hitbox* hitbox = this->level->player->hitbox;
|
||||
@@ -86,7 +89,9 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
|
||||
});
|
||||
box->textConsumer([this, ax](wstring text) {
|
||||
try {
|
||||
this->level->player->hitbox->position[ax] = std::stoi(text);
|
||||
vec3 position = this->level->player->hitbox->position;
|
||||
position[ax] = std::stoi(text);
|
||||
this->level->player->teleport(position);
|
||||
} catch (std::invalid_argument& _){
|
||||
}
|
||||
});
|
||||
@@ -125,12 +130,7 @@ HudRenderer::~HudRenderer() {
|
||||
|
||||
void HudRenderer::drawDebug(int fps, bool occlusion){
|
||||
this->occlusion = occlusion;
|
||||
if (fpsFrame % 60 == 0) {
|
||||
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
||||
fpsMin = fps;
|
||||
fpsMax = fps;
|
||||
}
|
||||
fpsFrame++;
|
||||
this->fps = fps;
|
||||
fpsMin = min(fps, fpsMin);
|
||||
fpsMax = max(fps, fpsMax);
|
||||
}
|
||||
@@ -240,6 +240,7 @@ void HudRenderer::draw(){
|
||||
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||
}
|
||||
Player* player = level->player;
|
||||
|
||||
batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
|
||||
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
|
||||
@@ -259,7 +260,7 @@ void HudRenderer::draw(){
|
||||
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 2);
|
||||
|
||||
batch->texture(blocks);
|
||||
Player* player = level->player;
|
||||
|
||||
{
|
||||
Block* cblock = Block::blocks[player->choosenBlock];
|
||||
if (cblock->model == BLOCK_MODEL_CUBE){
|
||||
@@ -296,3 +297,11 @@ void HudRenderer::draw(){
|
||||
}
|
||||
batch->render();
|
||||
}
|
||||
|
||||
bool HudRenderer::isInventoryOpen() const {
|
||||
return inventoryOpen;
|
||||
}
|
||||
|
||||
bool HudRenderer::isPause() const {
|
||||
return pause;
|
||||
}
|
||||
+4
-1
@@ -21,9 +21,9 @@ class HudRenderer {
|
||||
Batch2D* batch;
|
||||
Camera* uicamera;
|
||||
|
||||
int fps = 60;
|
||||
int fpsMin = 60;
|
||||
int fpsMax = 60;
|
||||
int fpsFrame = 0;
|
||||
std::wstring fpsString;
|
||||
bool occlusion;
|
||||
bool inventoryOpen = false;
|
||||
@@ -38,6 +38,9 @@ public:
|
||||
void drawInventory(Player* player);
|
||||
void draw();
|
||||
void drawDebug(int fps, bool occlusion);
|
||||
|
||||
bool isInventoryOpen() const;
|
||||
bool isPause() const;
|
||||
};
|
||||
|
||||
#endif /* SRC_HUD_H_ */
|
||||
|
||||
Reference in New Issue
Block a user