mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 05:51:51 +00:00
Merge pull request #976 from MihailRis/screenshot-function
add gui.screenshot function
This commit is contained in:
commit
507f32f1e5
9 changed files with 62 additions and 0 deletions
|
|
@ -61,6 +61,12 @@ gui.set_active_frame(
|
||||||
-- Used for custom projection (e.g., in 3D)
|
-- Used for custom projection (e.g., in 3D)
|
||||||
[optional] cursorLocator: function() -> number, number
|
[optional] cursorLocator: function() -> number, number
|
||||||
)
|
)
|
||||||
|
|
||||||
|
-- Creates a screenshot of a frame as a Canvas object if the frame ID is specified, or the entire window if nil.
|
||||||
|
gui.screenshot(
|
||||||
|
-- ID of the frame created via gui.create_frame
|
||||||
|
[optional] frameId: str
|
||||||
|
) -> Canvas | nil
|
||||||
```
|
```
|
||||||
|
|
||||||
## Markup
|
## Markup
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,12 @@ gui.set_active_frame(
|
||||||
-- Используется для пользовательской проекции (например в 3D)
|
-- Используется для пользовательской проекции (например в 3D)
|
||||||
[опционально] cursorLocator: function() -> number, number
|
[опционально] cursorLocator: function() -> number, number
|
||||||
)
|
)
|
||||||
|
|
||||||
|
-- Создаёт снимок фрейма в виде объекта Canvas если указан id фрейма, или всего окна, в случае nil.
|
||||||
|
gui.screenshot(
|
||||||
|
-- id фрейма, созданного через gui.create_frame
|
||||||
|
[опционально] frameId: str
|
||||||
|
) -> Canvas | nil
|
||||||
```
|
```
|
||||||
|
|
||||||
## Разметка
|
## Разметка
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,13 @@ std::shared_ptr<Texture> Framebuffer::getSharedTexture() const {
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<ImageData> Framebuffer::readData() const {
|
||||||
|
if (!texture) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return texture->readData();
|
||||||
|
}
|
||||||
|
|
||||||
uint Framebuffer::getWidth() const {
|
uint Framebuffer::getWidth() const {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
class Texture;
|
class Texture;
|
||||||
|
class ImageData;
|
||||||
|
|
||||||
class Framebuffer : public Bindable {
|
class Framebuffer : public Bindable {
|
||||||
uint fbo;
|
uint fbo;
|
||||||
|
|
@ -36,6 +37,8 @@ public:
|
||||||
|
|
||||||
std::shared_ptr<Texture> getSharedTexture() const;
|
std::shared_ptr<Texture> getSharedTexture() const;
|
||||||
|
|
||||||
|
std::unique_ptr<ImageData> readData() const;
|
||||||
|
|
||||||
/// @brief Get framebuffer width
|
/// @brief Get framebuffer width
|
||||||
uint getWidth() const;
|
uint getWidth() const;
|
||||||
/// @brief Get framebuffer height
|
/// @brief Get framebuffer height
|
||||||
|
|
|
||||||
|
|
@ -378,6 +378,14 @@ std::shared_ptr<gui::Frame> GUI::getActiveFrame() const {
|
||||||
return activeFrame;
|
return activeFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<gui::Frame> GUI::getFrame(const std::string& id) {
|
||||||
|
const auto& found = frames.find(id);
|
||||||
|
if (found == frames.end()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return found->second;
|
||||||
|
}
|
||||||
|
|
||||||
void GUI::remove(UINode* node) noexcept {
|
void GUI::remove(UINode* node) noexcept {
|
||||||
container->remove(node);
|
container->remove(node);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,8 @@ namespace gui {
|
||||||
|
|
||||||
std::shared_ptr<Frame> getActiveFrame() const;
|
std::shared_ptr<Frame> getActiveFrame() const;
|
||||||
|
|
||||||
|
std::shared_ptr<Frame> getFrame(const std::string& id);
|
||||||
|
|
||||||
/// @brief Remove node from the main container
|
/// @brief Remove node from the main container
|
||||||
void remove(UINode* node) noexcept;
|
void remove(UINode* node) noexcept;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,3 +66,10 @@ const std::string& gui::Frame::getOutputTexture() const {
|
||||||
const std::string& gui::Frame::getFrameId() const {
|
const std::string& gui::Frame::getFrameId() const {
|
||||||
return frameId;
|
return frameId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<ImageData> gui::Frame::takeScreenshot() const {
|
||||||
|
if (fbo == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return fbo->readData();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
class Framebuffer;
|
class Framebuffer;
|
||||||
class UiDocument;
|
class UiDocument;
|
||||||
|
class ImageData;
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class Frame final : public Container {
|
class Frame final : public Container {
|
||||||
|
|
@ -18,6 +19,8 @@ namespace gui {
|
||||||
const std::string& getOutputTexture() const;
|
const std::string& getOutputTexture() const;
|
||||||
|
|
||||||
const std::string& getFrameId() const;
|
const std::string& getFrameId() const;
|
||||||
|
|
||||||
|
std::unique_ptr<ImageData> takeScreenshot() const;
|
||||||
private:
|
private:
|
||||||
std::string frameId;
|
std::string frameId;
|
||||||
std::unique_ptr<Framebuffer> fbo;
|
std::unique_ptr<Framebuffer> fbo;
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
||||||
#include "items/Inventories.hpp"
|
#include "items/Inventories.hpp"
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
|
#include "window/Window.hpp"
|
||||||
#include "../usertypes/lua_type_canvas.hpp"
|
#include "../usertypes/lua_type_canvas.hpp"
|
||||||
|
|
||||||
using namespace gui;
|
using namespace gui;
|
||||||
|
|
@ -1218,7 +1219,26 @@ static int l_get_active_frame(lua::State* L) {
|
||||||
return lua::pushstring(L, frame->getId());
|
return lua::pushstring(L, frame->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_screenshot(lua::State* L) {
|
||||||
|
if (engine->isHeadless()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::unique_ptr<ImageData> image;
|
||||||
|
if (lua::isstring(L, 1)) {
|
||||||
|
auto& gui = engine->getGUI();
|
||||||
|
auto frame = gui.getFrame(lua::require_string(L, 1));
|
||||||
|
if (frame == nullptr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
image = frame->takeScreenshot();
|
||||||
|
} else {
|
||||||
|
image = engine->getWindow().takeScreenshot();
|
||||||
|
}
|
||||||
|
return lua::newuserdata<lua::LuaCanvas>(L, nullptr, std::move(image));
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg guilib[] = {
|
const luaL_Reg guilib[] = {
|
||||||
|
{"screenshot", lua::wrap<l_screenshot>},
|
||||||
{"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>},
|
||||||
{"setattr", lua::wrap<l_gui_setattr>},
|
{"setattr", lua::wrap<l_gui_setattr>},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue