MayaFlux 0.5.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 387 of file FormFactory.cpp.

392{
393 struct DragState {
394 std::optional<size_t> prev_index;
395 };
396 auto ds = std::make_shared<DragState>();
397
398 ctx.on_drag(id, IO::MouseButtons::Left,
399 [state, bounds, ds](uint32_t, glm::vec2 ndc) {
400 auto& v = state->value;
401 if (v.empty())
402 return;
403
404 const float t = (ndc.x - bounds.min.x) / bounds.width();
405 const float a = (ndc.y - bounds.min.y) / bounds.height();
406 const size_t n = v.size();
407 const size_t idx = static_cast<size_t>(
408 std::clamp(t, 0.F, 1.F) * static_cast<float>(n - 1));
409 const float amp = std::clamp(a, 0.F, 1.F);
410
411 if (ds->prev_index && *ds->prev_index != idx) {
412 const size_t lo = std::min(*ds->prev_index, idx);
413 const size_t hi = std::max(*ds->prev_index, idx);
414 const float v0 = v[*ds->prev_index];
415 const auto span = static_cast<float>(hi - lo);
416 for (size_t i = lo; i <= hi; ++i) {
417 const float f = span > 0.F
418 ? static_cast<float>(i - lo) / span
419 : 1.F;
420 v[i] = glm::mix(v0, amp, f);
421 }
422 } else {
423 v[idx] = amp;
424 }
425
426 ds->prev_index = idx;
427 ++state->version;
428 });
429
430 ctx.on_release(id, IO::MouseButtons::Left, [ds](uint32_t, glm::vec2) {
431 ds->prev_index = std::nullopt;
432 });
433}
size_t a
float lo
float hi
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(), hi, MayaFlux::IO::Left, lo, MayaFlux::Kinesis::AABB2D::min, MayaFlux::Portal::Forma::Context::on_drag(), MayaFlux::Portal::Forma::Context::on_release(), and MayaFlux::Kinesis::AABB2D::width().

Referenced by paint_over().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: