Menu screen prototype, refactor

This commit is contained in:
MihailRis
2023-11-16 03:00:05 +03:00
parent 5af04e20d1
commit b03bcb5191
14 changed files with 267 additions and 52 deletions
+19 -11
View File
@@ -3,33 +3,35 @@
#include "panels.h"
#include <iostream>
#include <algorithm>
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
#include "../../graphics/Shader.h"
#include "../../window/Events.h"
#include "../../window/input.h"
#include "../../window/Camera.h"
using std::shared_ptr;
using namespace gui;
GUI::GUI() {
container = new Container(vec2(0, 0), vec2(Window::width, Window::height));
uicamera = new Camera(vec3(), Window::height);
uicamera->perspective = false;
uicamera->flipped = true;
}
GUI::~GUI() {
delete uicamera;
delete container;
}
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));
container->act(delta);
int mx = Events::x;
int my = Events::y;
@@ -77,6 +79,12 @@ void GUI::act(float delta) {
}
void GUI::draw(Batch2D* batch, Assets* assets) {
uicamera->fov = Window::height;
Shader* uishader = assets->getShader("ui");
uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView());
batch->begin();
container->draw(batch, assets);
}
@@ -93,6 +101,6 @@ void GUI::add(shared_ptr<UINode> panel) {
container->add(panel);
}
void GUI::interval(float interval, ontimeout callback) {
intervalEvents.push_back({callback, interval, 0.0f});
}
void GUI::remove(std::shared_ptr<UINode> panel) {
container->remove(panel);
}
+3 -10
View File
@@ -8,6 +8,7 @@
class Batch2D;
class Assets;
class Camera;
/*
Some info about padding and margin.
@@ -44,19 +45,12 @@ 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;
Camera* uicamera;
public:
GUI();
~GUI();
@@ -67,8 +61,7 @@ namespace gui {
void act(float delta);
void draw(Batch2D* batch, Assets* assets);
void add(std::shared_ptr<UINode> panel);
void interval(float interval, ontimeout callback);
void remove(std::shared_ptr<UINode> panel);
};
}
+1
View File
@@ -35,6 +35,7 @@ namespace gui {
UINode(glm::vec2 coord, glm::vec2 size);
public:
virtual ~UINode();
virtual void act(float delta) {};
virtual void draw(Batch2D* batch, Assets* assets) = 0;
virtual void visible(bool flag);
+26
View File
@@ -29,6 +29,20 @@ shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
void Container::act(float delta) {
for (IntervalEvent& event : intervalEvents) {
event.timer += delta;
if (event.timer > event.interval) {
event.callback();
event.timer = fmod(event.timer, event.interval);
}
}
for (auto node : nodes) {
if (node->visible())
node->act(delta);
}
}
void Container::draw(Batch2D* batch, Assets* assets) {
vec2 coord = calcCoord();
vec2 size = this->size();
@@ -50,6 +64,18 @@ void Container::add(shared_ptr<UINode> node) {
refresh();
}
void Container::remove(shared_ptr<UINode> selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(), [selected](const shared_ptr<UINode> node) {
return node == selected;
}), nodes.end());
refresh();
}
void Container::listenInterval(float interval, ontimeout callback) {
intervalEvents.push_back({callback, interval, 0.0f});
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
: Container(vec2(), size), padding(padding), interval(interval), resizing_(resizing) {
color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f);
+12 -1
View File
@@ -10,18 +10,29 @@ class Batch2D;
class Assets;
namespace gui {
typedef std::function<void()> ontimeout;
struct IntervalEvent {
ontimeout callback;
float interval;
float timer;
};
enum class Orientation { vertical, horizontal };
class Container : public UINode {
protected:
std::vector<std::shared_ptr<UINode>> nodes;
std::vector<IntervalEvent> intervalEvents;
public:
Container(glm::vec2 coord, glm::vec2 size);
virtual void act(float delta) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) {};
virtual void draw(Batch2D* batch, Assets* assets);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void add(std::shared_ptr<UINode> node);
virtual void remove(std::shared_ptr<UINode> node);
void listenInterval(float interval, ontimeout callback);
};
class Panel : public Container {
+17 -11
View File
@@ -28,6 +28,8 @@
#include "gui/panels.h"
#include "gui/UINode.h"
#include "gui/GUI.h"
#include "screens.h"
#include "../engine.h"
using std::wstring;
using std::shared_ptr;
@@ -42,18 +44,18 @@ inline Label* create_label(gui::wstringsupplier supplier) {
return label;
}
HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level), assets(assets), guiController(gui) {
HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(engine->getAssets()), guiController(engine->getGUI()) {
batch = new Batch2D(1024);
uicamera = new Camera(vec3(), Window::height);
uicamera->perspective = false;
uicamera->flipped = true;
gui->interval(1.0f, [this]() {
Panel* panel = new Panel(vec2(200, 200), vec4(5.0f), 1.0f);
panel->listenInterval(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](){
return L"chunks: "+std::to_wstring(this->level->chunks->chunksCount);
@@ -102,9 +104,9 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
panel->add(shared_ptr<UINode>(sub));
}
panel->refresh();
debugPanel = panel;
debugPanel = shared_ptr<UINode>(panel);
pauseMenu = new Panel(vec2(350, 200));
Panel* pauseMenu = new Panel(vec2(350, 200));
pauseMenu->color(vec4(0.0f));
{
Button* button = new Button(L"Continue", vec4(12.0f, 10.0f, 12.0f, 12.0f));
@@ -114,17 +116,21 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
pauseMenu->add(shared_ptr<UINode>(button));
}
{
Button* button = new Button(L"Save and Quit", vec4(12.0f, 10.0f, 12.0f, 12.0f));
button->listenAction([this](GUI*){
Window::setShouldClose(true);
Button* button = new Button(L"Save and Quit to Menu", vec4(12.0f, 10.0f, 12.0f, 12.0f));
button->listenAction([this, engine](GUI*){
this->pauseMenu->visible(false);
engine->setScreen(new MenuScreen(engine));
});
pauseMenu->add(shared_ptr<UINode>(button));
}
guiController->add(shared_ptr<UINode>(debugPanel));
guiController->add(shared_ptr<UINode>(pauseMenu));
this->pauseMenu = std::shared_ptr<gui::UINode>(pauseMenu);
guiController->add(this->debugPanel);
guiController->add(this->pauseMenu);
}
HudRenderer::~HudRenderer() {
guiController->remove(debugPanel);
guiController->remove(pauseMenu);
delete batch;
delete uicamera;
}
@@ -216,7 +222,7 @@ void HudRenderer::drawInventory(Player* player) {
void HudRenderer::draw(){
debugPanel->visible(level->player->debug);
pauseMenu->visible(pause);
pauseMenu->setCoord(vec2(Window::width/2.0f, Window::height/2.0f) - pauseMenu->size() / 2.0f);
pauseMenu->setCoord((Window::size() - pauseMenu->size()) / 2.0f);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
+7 -5
View File
@@ -2,6 +2,7 @@
#define SRC_HUD_H_
#include <string>
#include <memory>
class Batch2D;
class Camera;
@@ -9,10 +10,11 @@ class Level;
class Assets;
class Player;
class Level;
class Engine;
namespace gui {
class GUI;
class Panel;
class UINode;
}
class HudRenderer {
@@ -27,13 +29,13 @@ class HudRenderer {
std::wstring fpsString;
bool occlusion;
bool inventoryOpen = false;
bool pause = true;
bool pause = false;
gui::Panel* debugPanel;
gui::Panel* pauseMenu;
std::shared_ptr<gui::UINode> debugPanel;
std::shared_ptr<gui::UINode> pauseMenu;
gui::GUI* guiController;
public:
HudRenderer(gui::GUI* gui, Level* level, Assets* assets);
HudRenderer(Engine* engine, Level* level);
~HudRenderer();
void drawInventory(Player* player);
void draw();
+75 -1
View File
@@ -2,10 +2,13 @@
#include <iostream>
#include <memory>
#include <glm/glm.hpp>
#include <filesystem>
#include "../window/Camera.h"
#include "../window/Events.h"
#include "../window/input.h"
#include "../graphics/Shader.h"
#include "../assets/Assets.h"
#include "../world/Level.h"
#include "../world/World.h"
@@ -16,13 +19,84 @@
#include "world_render.h"
#include "hud.h"
#include "gui/GUI.h"
#include "gui/panels.h"
#include "gui/controls.h"
#include "../engine.h"
#include "../files/engine_files.h"
#include "../util/stringutil.h"
using std::wstring;
using glm::vec3;
using glm::vec4;
using std::shared_ptr;
using std::filesystem::directory_iterator;
using namespace gui;
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->color(vec4(0.0f));
panel->setCoord(vec2(10, 10));
{
Button* button = new Button(L"Continue", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([this, panel](GUI*) {
EngineSettings& settings = engine->getSettings();
World* world = new World("world", enginefs::get_worlds_folder()/"world", 42, settings);
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
Player* player = new Player(playerPosition, 4.0f, camera);
engine->setScreen(new LevelScreen(engine, world->loadLevel(player, settings)));
});
panel->add(shared_ptr<UINode>(button));
}
// ATTENTION: FUNCTIONALITY INCOMPLETE ZONE
/*Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f));
worldsPanel->color(vec4(0.1f));
for (auto const& entry : directory_iterator(enginefs::get_worlds_folder())) {
std::string name = entry.path().filename();
Button* button = new Button(util::str2wstr_utf8(name), vec4(10.0f, 8.0f, 10.0f, 8.0f));
button->color(vec4(0.5f));
button->listenAction([this, panel, name](GUI*) {
EngineSettings& settings = engine->getSettings();
World* world = new World(name, enginefs::get_worlds_folder()/name, 42, settings);
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
Player* player = new Player(playerPosition, 4.0f, camera);
engine->setScreen(new LevelScreen(engine, world->loadLevel(player, settings)));
});
worldsPanel->add(shared_ptr<UINode>(button));
}
panel->add(shared_ptr<UINode>(worldsPanel));*/
{
Button* button = new Button(L"Quit", vec4(12.0f, 10.0f, 12.0f, 10.0f));
button->listenAction([this](GUI*) {
Window::setShouldClose(true);
});
panel->add(shared_ptr<UINode>(button));
}
this->panel = shared_ptr<UINode>(panel);
engine->getGUI()->add(this->panel);
}
MenuScreen::~MenuScreen() {
engine->getGUI()->remove(panel);
}
void MenuScreen::update(float delta) {
}
void MenuScreen::draw(float delta) {
panel->setCoord((Window::size() - panel->size()) / 2.0f);
Window::clear();
Window::setBgColor(vec3(0.2f, 0.2f, 0.2f));
}
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine), level(level) {
worldRenderer = new WorldRenderer(level, engine->getAssets());
hud = new HudRenderer(engine->getGUI(), level, engine->getAssets());
hud = new HudRenderer(engine, level);
}
LevelScreen::~LevelScreen() {
+16
View File
@@ -1,6 +1,7 @@
#ifndef FRONTEND_SCREENS_H_
#define FRONTEND_SCREENS_H_
#include <memory>
#include "../settings.h"
class Assets;
@@ -8,6 +9,11 @@ class Level;
class WorldRenderer;
class HudRenderer;
class Engine;
class Camera;
namespace gui {
class UINode;
}
/* Screen is a mainloop state */
@@ -21,6 +27,16 @@ public:
virtual void draw(float delta) = 0;
};
class MenuScreen : public Screen {
std::shared_ptr<gui::UINode> panel;
public:
MenuScreen(Engine* engine);
~MenuScreen();
void update(float delta) override;
void draw(float delta) override;
};
class LevelScreen : public Screen {
Level* level;
WorldRenderer* worldRenderer;