mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 06:51:51 +00:00
add gui.create_frame
This commit is contained in:
parent
3014329280
commit
463098aa1e
8 changed files with 84 additions and 24 deletions
|
|
@ -127,7 +127,12 @@ function on_hud_open()
|
||||||
|
|
||||||
hud.default_hand_controller = update_hand
|
hud.default_hand_controller = update_hand
|
||||||
|
|
||||||
gui.root.root:add("<frame size='16' color='#FF0000'><label>Hello, World</label></frame>")
|
local name = "gui/no_icon"
|
||||||
|
gui.create_frame(name, {16, 16})
|
||||||
|
|
||||||
|
local document = Document.new(name)
|
||||||
|
document.root:add("<label>Hello</label>")
|
||||||
|
document.root.color = {255, 0, 0, 255}
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_hud_render()
|
function on_hud_render()
|
||||||
|
|
|
||||||
|
|
@ -305,6 +305,7 @@ void Engine::renderFrame() {
|
||||||
screen->draw(time.getDelta());
|
screen->draw(time.getDelta());
|
||||||
|
|
||||||
DrawContext ctx(nullptr, *window, nullptr);
|
DrawContext ctx(nullptr, *window, nullptr);
|
||||||
|
gui->renderFrames(ctx, *assets->getStorage());
|
||||||
gui->draw(ctx, *assets->getStorage());
|
gui->draw(ctx, *assets->getStorage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include "elements/Label.hpp"
|
#include "elements/Label.hpp"
|
||||||
#include "elements/Menu.hpp"
|
#include "elements/Menu.hpp"
|
||||||
#include "elements/Panel.hpp"
|
#include "elements/Panel.hpp"
|
||||||
|
#include "elements/Frame.hpp"
|
||||||
#include "elements/UINode.hpp"
|
#include "elements/UINode.hpp"
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "frontend/UiDocument.hpp"
|
#include "frontend/UiDocument.hpp"
|
||||||
|
|
@ -262,6 +263,21 @@ void GUI::postAct() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GUI::renderFrames(const DrawContext& pctx, Assets& assets) {
|
||||||
|
auto ctx = pctx.sub(batch2D.get());
|
||||||
|
|
||||||
|
auto uishader = assets.get<Shader>("ui");
|
||||||
|
uishader->use();
|
||||||
|
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||||
|
|
||||||
|
batch2D->begin();
|
||||||
|
for (auto& [outputTexture, frame] : frames) {
|
||||||
|
frame->updateOutput(assets);
|
||||||
|
frame->draw(ctx, assets);
|
||||||
|
}
|
||||||
|
batch2D->flush();
|
||||||
|
}
|
||||||
|
|
||||||
void GUI::draw(const DrawContext& pctx, const Assets& assets) {
|
void GUI::draw(const DrawContext& pctx, const Assets& assets) {
|
||||||
auto ctx = pctx.sub(batch2D.get());
|
auto ctx = pctx.sub(batch2D.get());
|
||||||
|
|
||||||
|
|
@ -337,6 +353,10 @@ void GUI::add(std::shared_ptr<UINode> node) {
|
||||||
container->add(std::move(node));
|
container->add(std::move(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GUI::addFrame(std::shared_ptr<Frame> frame) {
|
||||||
|
frames[frame->getOutputTexture()] = std::move(frame);
|
||||||
|
}
|
||||||
|
|
||||||
void GUI::remove(UINode* node) noexcept {
|
void GUI::remove(UINode* node) noexcept {
|
||||||
container->remove(node);
|
container->remove(node);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ namespace gui {
|
||||||
class UINode;
|
class UINode;
|
||||||
class Container;
|
class Container;
|
||||||
class Menu;
|
class Menu;
|
||||||
|
class Frame;
|
||||||
|
|
||||||
/// @brief The main UI controller
|
/// @brief The main UI controller
|
||||||
class GUI {
|
class GUI {
|
||||||
|
|
@ -80,6 +81,7 @@ namespace gui {
|
||||||
std::shared_ptr<Menu> menu;
|
std::shared_ptr<Menu> menu;
|
||||||
std::queue<runnable> postRunnables;
|
std::queue<runnable> postRunnables;
|
||||||
std::vector<std::weak_ptr<UINode>> mouseOver;
|
std::vector<std::weak_ptr<UINode>> mouseOver;
|
||||||
|
std::unordered_map<std::string, std::shared_ptr<Frame>> frames;
|
||||||
|
|
||||||
float tooltipTimer = 0.0f;
|
float tooltipTimer = 0.0f;
|
||||||
float doubleClickTimer = 0.0f;
|
float doubleClickTimer = 0.0f;
|
||||||
|
|
@ -112,6 +114,8 @@ namespace gui {
|
||||||
/// @param viewport window size
|
/// @param viewport window size
|
||||||
void act(float delta, const glm::uvec2& viewport);
|
void act(float delta, const glm::uvec2& viewport);
|
||||||
|
|
||||||
|
void renderFrames(const DrawContext& pctx, Assets& assets);
|
||||||
|
|
||||||
/// @brief Draw all visible elements on main container
|
/// @brief Draw all visible elements on main container
|
||||||
/// @param pctx parent graphics context
|
/// @param pctx parent graphics context
|
||||||
/// @param assets active assets storage
|
/// @param assets active assets storage
|
||||||
|
|
@ -123,6 +127,8 @@ namespace gui {
|
||||||
/// @param node UI element
|
/// @param node UI element
|
||||||
void add(std::shared_ptr<UINode> node);
|
void add(std::shared_ptr<UINode> node);
|
||||||
|
|
||||||
|
void addFrame(std::shared_ptr<Frame> frame);
|
||||||
|
|
||||||
/// @brief Remove node from the main container
|
/// @brief Remove node from the main container
|
||||||
void remove(UINode* node) noexcept;
|
void remove(UINode* node) noexcept;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include "graphics/core/Framebuffer.hpp"
|
#include "graphics/core/Framebuffer.hpp"
|
||||||
#include "graphics/core/DrawContext.hpp"
|
#include "graphics/core/DrawContext.hpp"
|
||||||
#include "graphics/core/Texture.hpp"
|
#include "graphics/core/Texture.hpp"
|
||||||
|
#include "frontend/UiDocument.hpp"
|
||||||
#include "window/Window.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "../GUI.hpp"
|
#include "../GUI.hpp"
|
||||||
|
|
||||||
|
|
@ -12,25 +13,23 @@ using namespace gui;
|
||||||
|
|
||||||
static inline constexpr int MAX_TEXTURE_SIZE = 2048;
|
static inline constexpr int MAX_TEXTURE_SIZE = 2048;
|
||||||
|
|
||||||
gui::Frame::Frame(GUI& gui) : Container(gui, {}), fbo(nullptr) {}
|
gui::Frame::Frame(GUI& gui, std::string outputTexture)
|
||||||
|
: Container(gui, {}),
|
||||||
|
fbo(nullptr),
|
||||||
|
outputTexture(std::move(outputTexture)) {
|
||||||
|
}
|
||||||
|
|
||||||
gui::Frame::~Frame() = default;
|
gui::Frame::~Frame() = default;
|
||||||
|
|
||||||
void gui::Frame::draw(const DrawContext& pctx, const Assets& assets) {
|
void gui::Frame::draw(const DrawContext& pctx, const Assets& assets) {
|
||||||
|
if (fbo == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
glm::ivec2 size = getSize();
|
glm::ivec2 size = getSize();
|
||||||
if (size.x <= 0 || size.y <= 0 ||
|
if (size.x <= 0 || size.y <= 0 ||
|
||||||
size.x > MAX_TEXTURE_SIZE || size.y > MAX_TEXTURE_SIZE) {
|
size.x > MAX_TEXTURE_SIZE || size.y > MAX_TEXTURE_SIZE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto& nonConstASsets = const_cast<Assets&>(assets);
|
|
||||||
if (fbo && (fbo->getWidth() != size.x || fbo->getHeight() != size.y)) {
|
|
||||||
fbo->resize(size.x, size.y);
|
|
||||||
nonConstASsets.store(fbo->getSharedTexture(), texture);
|
|
||||||
} else if (fbo == nullptr) {
|
|
||||||
fbo = std::make_unique<Framebuffer>(size.x, size.y, true);
|
|
||||||
nonConstASsets.store(fbo->getSharedTexture(), texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ui uses flipped camera with matrix based on main viewport
|
// ui uses flipped camera with matrix based on main viewport
|
||||||
setPos({0, pctx.getViewport().y - size.y});
|
setPos({0, pctx.getViewport().y - size.y});
|
||||||
|
|
||||||
|
|
@ -40,3 +39,17 @@ void gui::Frame::draw(const DrawContext& pctx, const Assets& assets) {
|
||||||
Container::draw(ctx, assets);
|
Container::draw(ctx, assets);
|
||||||
ctx.getBatch2D()->flush();
|
ctx.getBatch2D()->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void gui::Frame::updateOutput(Assets& assets) {
|
||||||
|
if (fbo && (fbo->getWidth() != size.x || fbo->getHeight() != size.y)) {
|
||||||
|
fbo->resize(size.x, size.y);
|
||||||
|
assets.store(fbo->getSharedTexture(), outputTexture);
|
||||||
|
} else if (fbo == nullptr) {
|
||||||
|
fbo = std::make_unique<Framebuffer>(size.x, size.y, true);
|
||||||
|
assets.store(fbo->getSharedTexture(), outputTexture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& gui::Frame::getOutputTexture() const {
|
||||||
|
return outputTexture;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,21 @@
|
||||||
#include "Container.hpp"
|
#include "Container.hpp"
|
||||||
|
|
||||||
class Framebuffer;
|
class Framebuffer;
|
||||||
|
class UiDocument;
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class Frame final : public Container {
|
class Frame final : public Container {
|
||||||
public:
|
public:
|
||||||
explicit Frame(GUI& gui);
|
Frame(GUI& gui, std::string outputTexture);
|
||||||
virtual ~Frame();
|
virtual ~Frame();
|
||||||
|
|
||||||
void draw(const DrawContext& pctx, const Assets& assets) override;
|
void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||||
|
|
||||||
|
void updateOutput(Assets& assets);
|
||||||
|
|
||||||
|
const std::string& getOutputTexture() const;
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Framebuffer> fbo;
|
std::unique_ptr<Framebuffer> fbo;
|
||||||
std::string texture = "gui/no_icon";
|
std::string outputTexture;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@
|
||||||
#include "elements/Button.hpp"
|
#include "elements/Button.hpp"
|
||||||
#include "elements/Canvas.hpp"
|
#include "elements/Canvas.hpp"
|
||||||
#include "elements/CheckBox.hpp"
|
#include "elements/CheckBox.hpp"
|
||||||
#include "elements/Frame.hpp"
|
|
||||||
#include "elements/Image.hpp"
|
#include "elements/Image.hpp"
|
||||||
#include "elements/InlineFrame.hpp"
|
#include "elements/InlineFrame.hpp"
|
||||||
#include "elements/InputBindBox.hpp"
|
#include "elements/InputBindBox.hpp"
|
||||||
|
|
@ -851,15 +850,6 @@ static std::shared_ptr<UINode> read_page_box(
|
||||||
return menu;
|
return menu;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::shared_ptr<UINode> read_frame(
|
|
||||||
UiXmlReader& reader, const xml::xmlelement& element
|
|
||||||
) {
|
|
||||||
auto& gui = reader.getGUI();
|
|
||||||
auto frame = std::make_shared<Frame>(gui);
|
|
||||||
read_container_impl(reader, element, *frame, true);
|
|
||||||
return frame;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::shared_ptr<UINode> read_iframe(
|
static std::shared_ptr<UINode> read_iframe(
|
||||||
UiXmlReader& reader, const xml::xmlelement& element
|
UiXmlReader& reader, const xml::xmlelement& element
|
||||||
) {
|
) {
|
||||||
|
|
@ -877,7 +867,6 @@ UiXmlReader::UiXmlReader(gui::GUI& gui, scriptenv&& env)
|
||||||
contextStack.emplace("");
|
contextStack.emplace("");
|
||||||
add("image", read_image);
|
add("image", read_image);
|
||||||
add("canvas", read_canvas);
|
add("canvas", read_canvas);
|
||||||
add("frame", read_frame);
|
|
||||||
add("iframe", read_iframe);
|
add("iframe", read_iframe);
|
||||||
add("label", read_label);
|
add("label", read_label);
|
||||||
add("panel", read_panel);
|
add("panel", read_panel);
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
#include "graphics/ui/elements/TrackBar.hpp"
|
#include "graphics/ui/elements/TrackBar.hpp"
|
||||||
#include "graphics/ui/elements/InlineFrame.hpp"
|
#include "graphics/ui/elements/InlineFrame.hpp"
|
||||||
#include "graphics/ui/elements/ModelViewer.hpp"
|
#include "graphics/ui/elements/ModelViewer.hpp"
|
||||||
|
#include "graphics/ui/elements/Frame.hpp"
|
||||||
#include "graphics/ui/gui_util.hpp"
|
#include "graphics/ui/gui_util.hpp"
|
||||||
#include "graphics/ui/markdown.hpp"
|
#include "graphics/ui/markdown.hpp"
|
||||||
#include "graphics/core/Font.hpp"
|
#include "graphics/core/Font.hpp"
|
||||||
|
|
@ -1149,6 +1150,25 @@ static int l_set_syntax_styles(lua::State* L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_create_frame(lua::State* L) {
|
||||||
|
if (engine->isHeadless()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::string outputTexture = lua::require_string(L, 1);
|
||||||
|
auto size = lua::tovec2(L, 2);
|
||||||
|
|
||||||
|
auto& gui = engine->getGUI();
|
||||||
|
auto frame = std::make_shared<gui::Frame>(gui, outputTexture);
|
||||||
|
frame->setSize(std::move(size));
|
||||||
|
auto& assets = *engine->getAssets();
|
||||||
|
auto document = std::make_shared<UiDocument>(
|
||||||
|
outputTexture, UiDocScript {}, frame, nullptr
|
||||||
|
);
|
||||||
|
assets.store(document, document->getId());
|
||||||
|
gui.addFrame(std::move(frame));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg guilib[] = {
|
const luaL_Reg guilib[] = {
|
||||||
{"get_viewport", lua::wrap<l_gui_getviewport>},
|
{"get_viewport", lua::wrap<l_gui_getviewport>},
|
||||||
{"getattr", lua::wrap<l_gui_getattr>},
|
{"getattr", lua::wrap<l_gui_getattr>},
|
||||||
|
|
@ -1162,6 +1182,7 @@ const luaL_Reg guilib[] = {
|
||||||
{"alert", lua::wrap<l_gui_alert>},
|
{"alert", lua::wrap<l_gui_alert>},
|
||||||
{"load_document", lua::wrap<l_gui_load_document>},
|
{"load_document", lua::wrap<l_gui_load_document>},
|
||||||
{"set_syntax_styles", lua::wrap<l_set_syntax_styles>},
|
{"set_syntax_styles", lua::wrap<l_set_syntax_styles>},
|
||||||
|
{"create_frame", lua::wrap<l_create_frame>},
|
||||||
{"__reindex", lua::wrap<l_gui_reindex>},
|
{"__reindex", lua::wrap<l_gui_reindex>},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue