feat(examples): a three-row synced Figure demo, on a GLFW host that draws one

The multi-plot example the phase exists for: three plots stacked in one
Figure, sharing one x link with following armed, each row carrying its own
named signals and its own legend, driven by six StreamSeries at 500 kS/s.
Two cursors and a region with an exact statistics readout reach the direct
manipulation of spec §5 in the same frame, and PLOT_DEMO_SYNC=1 is a
self-checking mode that exits non-zero unless all three rows hold one range
and the shared right edge keeps tracking the newest sample through a zoom, a
pan on another row and more arriving data.

The GLFW host gains the figure mode the demo needs: set_figure() makes the
run loop draw the Figure instead of its own plot and route each pointer,
wheel and key event to the row it lands in, through one Controller per row
(row_controller()). All of it is opt-in; the single-plot path is untouched.
This commit is contained in:
Martino Ferrari committed 2026-09-28 15:24:22 +02:00
1 parent 770da4c2b2
commit 942f88c568
9 files changed
+917 -39

No files matched your search

+45
View File
@@ -1,12 +1,15 @@
#pragma once
#include <functional>
#include <memory>
#include <optional>
#include <vector>
#include <plot/core/rect.hpp>
#include <plot/core/status.hpp>
#include <plot/core/types.hpp>
#include <plot/input/controller.hpp>
#include <plot/plot/figure.hpp>
#include <plot/plot/plot.hpp>
#include <plot/render/renderer.hpp>
@@ -96,6 +99,25 @@ class Host {
/// The plot the frame callback populates. Valid for the host's whole life.
Plot& plot() noexcept { return plot_; }
/// Draw a whole `Figure` instead of the host's own plot: the multi-plot shape,
/// several rows sharing one x axis, which one `Plot` cannot express. While a
/// figure is set the run loop lays it out over the framebuffer, routes each
/// pointer, wheel and key event to the row the event lands in (one `Controller`
/// per row, created here, each bound to that row's own `Plot`), and draws it with
/// `Renderer::draw(Figure&)`. The frame callback is handed the figure's **row 0**
/// as its `Plot&`, so `plot()` and the frame argument agree.
///
/// The figure is borrowed and must outlive the run. Passing null restores the
/// single-plot loop, which is what every other caller uses; the host's own
/// `plot_` is then untouched by any of this.
void set_figure(Figure* figure) noexcept;
Figure* figure() noexcept { return figure_; }
/// One figure row's controller -- the one a gesture on that row reaches -- or
/// null while no figure is set or the row does not exist. A host reads a drag
/// mode or a hover out of it (`controller()` is the single-plot loop's, and is
/// not the one figure gestures go to).
input::Controller* row_controller(u32 row) noexcept;
/// The gesture controller the host feeds. Valid for the host's whole life.
input::Controller& controller() noexcept { return controller_; }
@@ -133,10 +155,33 @@ class Host {
void on_scroll(f64 dx, f64 dy) noexcept;
void on_key(int key, int action, int mods) noexcept;
/// The controller an event at framebuffer pixel (x, y) belongs to: `controller_`
/// -- the single-plot loop -- while no figure is set, and the controller of the
/// figure row that pixel lands in otherwise. The row rects come from
/// `Figure::layout()`, the same ones the renderer draws, so a gesture lands on the
/// row it was aimed at; a pixel outside every row (the row gap, or a chrome strip
/// below the last row) is clamped to the nearest row rather than dropped, so a
/// drag that leaves the data area still ends where it should.
///
/// It also hands that row its own viewport, which is what keeps the controller's
/// hit-test geometry in step with the layout between two frames.
input::Controller& controller_at(f32 x, f32 y) noexcept;
Plot plot_;
input::Controller controller_; ///< forwards its viewport to `plot_`
std::optional<Renderer> renderer_;
GLFWwindow* window_ = nullptr;
/// The figure the host draws instead of `plot_`, and one controller per row for
/// it -- one per `Plot`, because a `Controller` is bound to a plot at construction
/// and this is what makes a gesture on any row reach that row. Both are empty
/// until `set_figure()` is called, and the run loop's single-plot path reads
/// neither.
Figure* figure_ = nullptr;
std::vector<std::unique_ptr<input::Controller>> row_controllers_;
/// The row the last event landed in, which is where a key event goes: a key has
/// no position of its own, and the row the pointer is over is the one the user is
/// looking at.
u32 last_row_ = 0u;
u32 fb_width_ = 0; ///< framebuffer size of the last refresh_sizes()
u32 fb_height_ = 0;