add 'frame' argument to gui.screenshot

This commit is contained in:
MihailRis 2026-09-17 22:07:18 +03:00
parent 5ae4c1b27b
commit 69edfc8c8a
7 changed files with 41 additions and 1 deletions

View file

@ -117,6 +117,13 @@ std::shared_ptr<Texture> Framebuffer::getSharedTexture() const {
return texture;
}
std::unique_ptr<ImageData> Framebuffer::readData() const {
if (!texture) {
return nullptr;
}
return texture->readData();
}
uint Framebuffer::getWidth() const {
return width;
}

View file

@ -6,6 +6,7 @@
#include <memory>
class Texture;
class ImageData;
class Framebuffer : public Bindable {
uint fbo;
@ -36,6 +37,8 @@ public:
std::shared_ptr<Texture> getSharedTexture() const;
std::unique_ptr<ImageData> readData() const;
/// @brief Get framebuffer width
uint getWidth() const;
/// @brief Get framebuffer height

View file

@ -378,6 +378,14 @@ std::shared_ptr<gui::Frame> GUI::getActiveFrame() const {
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 {
container->remove(node);
}

View file

@ -137,6 +137,8 @@ namespace gui {
std::shared_ptr<Frame> getActiveFrame() const;
std::shared_ptr<Frame> getFrame(const std::string& id);
/// @brief Remove node from the main container
void remove(UINode* node) noexcept;

View file

@ -66,3 +66,10 @@ const std::string& gui::Frame::getOutputTexture() const {
const std::string& gui::Frame::getFrameId() const {
return frameId;
}
std::unique_ptr<ImageData> gui::Frame::takeScreenshot() const {
if (fbo == nullptr) {
return nullptr;
}
return fbo->readData();
}

View file

@ -4,6 +4,7 @@
class Framebuffer;
class UiDocument;
class ImageData;
namespace gui {
class Frame final : public Container {
@ -18,6 +19,8 @@ namespace gui {
const std::string& getOutputTexture() const;
const std::string& getFrameId() const;
std::unique_ptr<ImageData> takeScreenshot() const;
private:
std::string frameId;
std::unique_ptr<Framebuffer> fbo;

View file

@ -1223,7 +1223,17 @@ static int l_screenshot(lua::State* L) {
if (engine->isHeadless()) {
return 0;
}
auto image = engine->getWindow().takeScreenshot();
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));
}