feat(plot): set_annotation(), so a readout annotation can be moved
A host whose annotation is a readout -- the point where a cursor crosses a trace -- has to move it every frame, and `add_annotation()` only appends: the id would change every frame and the annotation count grow without bound. The setter keeps the id, ignores a stale one exactly as the cursor setters do, and pairs with parking (`visible = false`) since there is no `remove_annotation`. Tested by `an_annotation_can_be_moved_by_id` in tests/test_plot.cpp.
This commit is contained in:
1 parent
e337c6a60c
commit
133cd680ce
3 files changed
+46
No files matched your search
@@ -2584,3 +2584,37 @@ PLOT_TEST(xy_series_measurement_uses_the_true_nearest_samples) {
|
||||
PLOT_CHECK_NEAR(stats.min, 10.0, 1e-9);
|
||||
PLOT_CHECK_NEAR(stats.max, 10.0, 1e-9);
|
||||
}
|
||||
|
||||
// A host whose annotation is a readout -- the point where a cursor crosses a trace --
|
||||
// moves it every frame. The id has to survive that, and a stale id must be ignored
|
||||
// rather than read or written out of bounds, exactly as the cursors' setters behave.
|
||||
PLOT_TEST(an_annotation_can_be_moved_by_id) {
|
||||
plot::Plot p;
|
||||
const plot::Annotation pin{1.0, 2.0, "first", plot::Color::hex(0xE8E8E8), 0u, 0u, true};
|
||||
const u32 id = p.add_annotation(pin);
|
||||
PLOT_CHECK(p.annotation(id).x == 1.0);
|
||||
PLOT_CHECK(p.annotation(id).text == std::string("first"));
|
||||
|
||||
plot::Annotation moved = pin;
|
||||
moved.x = 3.5;
|
||||
moved.y = -1.25;
|
||||
moved.text = "moved";
|
||||
p.set_annotation(id, moved);
|
||||
PLOT_CHECK_NEAR(p.annotation(id).x, 3.5, 1e-12);
|
||||
PLOT_CHECK_NEAR(p.annotation(id).y, -1.25, 1e-12);
|
||||
PLOT_CHECK(p.annotation(id).text == std::string("moved"));
|
||||
PLOT_CHECK(p.annotation_count() == 1u); // moved, not added
|
||||
|
||||
// Parked rather than removed, like a cursor: the id stays valid and reads back hidden.
|
||||
plot::Annotation parked = moved;
|
||||
parked.visible = false;
|
||||
p.set_annotation(id, parked);
|
||||
PLOT_CHECK(!p.annotation(id).visible);
|
||||
PLOT_CHECK(p.annotation_count() == 1u);
|
||||
|
||||
// A stale id changes nothing (and reads a default, as the accessors promise).
|
||||
plot::Annotation other = pin;
|
||||
other.x = 99.0;
|
||||
p.set_annotation(42u, other);
|
||||
PLOT_CHECK_NEAR(p.annotation(id).x, 3.5, 1e-12);
|
||||
}
|
||||
Reference in new issue
Block a user