#include "Web_Plot_Session.h" #include "Pixel_Frame.h" #include "render_2D/scene/Render_Scene_2D.h" #include #include namespace renderive::web { struct Web_Plot_Session::Impl { Size viewport{960, 600}; std::uint64_t sequence{}; std::unique_ptr scene; Impl() { rebuild(); } void rebuild() { Render_Scene_2D::State state; state.viewport = viewport; scene = Render_Scene_2D::Builder{state}.build( std::make_shared()); scene->activate_view(); } std::optional handle(const Web_Event& event) { return std::visit([this](const auto& value) -> std::optional { using T = std::decay_t; if constexpr (std::same_as) { const auto result = scene->render_frame(true); if (!result || scene->wait_for_render() != Scene_Render_Result::none) return std::nullopt; std::string pixels; scene->with_frame([&](Image_View image) { pixels = encode_pixel_frame(image, ++sequence); }); if (pixels.empty()) return std::nullopt; return Web_Response{Web_Response_Type::Pixels, std::move(pixels)}; } else if constexpr (std::same_as) { viewport = value.size; rebuild(); } else if constexpr (std::derived_from) { scene->dispatch_event(value); } return std::nullopt; }, event); } }; Web_Plot_Session::Web_Plot_Session() : impl_(std::make_unique()) {} Web_Plot_Session::~Web_Plot_Session() = default; std::optional Web_Plot_Session::handle(const Web_Event& event) { return impl_->handle(event); } } // namespace renderive::web