MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ wire_canvas_drag()

MAYAFLUX_API void MayaFlux::Portal::Forma::Geometry::wire_canvas_drag ( Context ctx,
uint32_t  id,
std::shared_ptr< MappedState< std::vector< float > > >  state,
Kinesis::AABB2D  bounds 
)

Wire drag interaction for a drawable canvas element.

Registers a left-button drag callback on ctx that maps NDC cursor position to a sample index and amplitude, writes into state, and increments the version. Linear interpolation fills the range between the previously touched index and the current one, preventing sparse samples under fast drag.

Parameters
ctxContext owning the element.
idElement id from the Mapped.
stateMappedState<vector<float>> to write into.
boundsCanvas NDC bounds — must match those passed to drawable_canvas().

Definition at line 334 of file Geometry.cpp.

339{
340 struct DragState {
341 std::optional<size_t> prev_index;
342 };
343 auto ds = std::make_shared<DragState>();
344
345 ctx.on_drag(id, IO::MouseButtons::Left,
346 [state, bounds, ds](uint32_t, glm::vec2 ndc) {
347 auto& v = state->value;
348 if (v.empty())
349 return;
350
351 const float t = (ndc.x - bounds.min.x) / bounds.width();
352 const float a = (ndc.y - bounds.min.y) / bounds.height();
353 const size_t n = v.size();
354 const size_t idx = static_cast<size_t>(
355 std::clamp(t, 0.F, 1.F) * static_cast<float>(n - 1));
356 const float amp = std::clamp(a, 0.F, 1.F);
357
358 if (ds->prev_index && *ds->prev_index != idx) {
359 const size_t lo = std::min(*ds->prev_index, idx);
360 const size_t hi = std::max(*ds->prev_index, idx);
361 const float v0 = v[*ds->prev_index];
362 const auto span = static_cast<float>(hi - lo);
363 for (size_t i = lo; i <= hi; ++i) {
364 const float f = span > 0.F
365 ? static_cast<float>(i - lo) / span
366 : 1.F;
367 v[i] = glm::mix(v0, amp, f);
368 }
369 } else {
370 v[idx] = amp;
371 }
372
373 ds->prev_index = idx;
374 ++state->version;
375 });
376
377 ctx.on_release(id, IO::MouseButtons::Left, [ds](uint32_t, glm::vec2) {
378 ds->prev_index = std::nullopt;
379 });
380}
size_t a
void on_release(uint32_t id, IO::MouseButtons btn, PressFn fn)
Called when a mouse button is released over an element.
Definition Context.cpp:38
void on_drag(uint32_t id, IO::MouseButtons btn, MoveFn fn)
Called on each mouse-move event while btn is held, tracking the element where the drag began even whe...
Definition Context.cpp:48
float height() const noexcept
Definition Bounds.hpp:38
float width() const noexcept
Definition Bounds.hpp:37

References a, MayaFlux::Kinesis::AABB2D::height(), MayaFlux::IO::Left, MayaFlux::Kinesis::AABB2D::min, MayaFlux::Portal::Forma::Context::on_drag(), MayaFlux::Portal::Forma::Context::on_release(), and MayaFlux::Kinesis::AABB2D::width().

+ Here is the call graph for this function: