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

◆ handle_path()

MAYAFLUX_API Form< std::vector< glm::vec2 > > MayaFlux::Portal::Forma::Geometry::handle_path ( Kinesis::AABB2D bounds,
size_t handles,
glm::vec3 path_color = glm::vec3(0.3F, 0.7F, 1.F),
glm::vec3 handle_color = glm::vec3(0.95F),
float thickness = 1.5F,
float handle_size = 0.03F,
float grab_radius = 0.1F,
const std::function< glm::vec2()> & live_offset = {} )
nodiscard

Geometry function for a path of draggable points in NDC space.

The state holds the points in unit space, so any writer sees positions independent of where the area sits on the surface.

Parameters
boundsNDC region the unit square is mapped onto.
handlesPoint count the buffer is sized for.
path_colorStroke color.
handle_colorColor of the box drawn at each point.
thicknessLineVertex thickness of the stroke and the boxes.
handle_sizeSide of each box in NDC units.
grab_radiusLargest NDC distance from a point at which a press grabs it.
live_offsetOptional. Current translation of the drawn area.

Definition at line 445 of file FormFactory.cpp.

454{
455 using Points = std::vector<glm::vec2>;
456
457 GeometryFn<Points> fn = [bounds, path_color, handle_color, thickness, handle_size, live_offset](
458 const Points& v, std::vector<uint8_t>& out, Element& el) {
459 const glm::vec2 shift = live_offset ? live_offset() : glm::vec2(0.F);
460 const Kinesis::AABB2D live = bounds.translated(shift);
461
462 std::vector<glm::vec2> ndc;
463 ndc.reserve(v.size());
464 for (const glm::vec2 p : v)
465 ndc.push_back(live.point_at(p));
466
467 auto verts = Kinesis::polyline(ndc, glm::vec4(path_color, 1.F), thickness);
468 for (const glm::vec2 c : ndc) {
469 const auto box = Kinesis::rect_outline(
470 Kinesis::AABB2D::from_ndc(c, glm::vec2(handle_size * 0.5F)),
471 glm::vec4(handle_color, 1.F), thickness);
472 verts.insert(verts.end(), box.begin(), box.end());
473 }
474
475 write_verts(out, verts);
476
477 el.bounds_hint = live;
478 el.contains = {};
479 };
480
481 Form<Points>::Wire wire = [bounds, grab_radius, live_offset](
482 Context& ctx, uint32_t id, const std::shared_ptr<MappedState<Points>>& state) {
483 const auto held = std::make_shared<std::optional<size_t>>();
484 const auto project = Kinesis::unit_square(bounds, live_offset);
485
486 ctx.on_press(id, IO::MouseButtons::Left,
487 [state, held, bounds, grab_radius, live_offset](uint32_t, glm::vec2 ndc) {
488 const glm::vec2 shift = live_offset ? live_offset() : glm::vec2(0.F);
489 const Kinesis::AABB2D live = bounds.translated(shift);
490 float nearest = grab_radius * grab_radius;
491 held->reset();
492 for (size_t i = 0; i < state->value.size(); ++i) {
493 const glm::vec2 offset = live.point_at(state->value[i]) - ndc;
494 const float distance = glm::dot(offset, offset);
495 if (distance <= nearest) {
496 nearest = distance;
497 *held = i;
498 }
499 }
500 });
501
502 ctx.on_drag(id, IO::MouseButtons::Left,
503 [state, held, project](uint32_t, glm::vec2 ndc) {
504 if (!*held || **held >= state->value.size())
505 return;
506 Points moved = state->value;
507 moved[**held] = project(ndc);
508 state->write(std::move(moved));
509 });
510
511 ctx.on_release(id, IO::MouseButtons::Left, [held](uint32_t, glm::vec2) {
512 held->reset();
513 });
514 };
515
516 Form<Points> form { std::move(fn) };
518 form.capacity = (handles > 0 ? (handles - 1) * 2 + handles * 8 : 0) * sizeof(Kakshya::LineVertex);
519 form.wire = std::move(wire);
520 return form;
521}
std::array< Kakshya::LineVertex, 8 > rect_outline(AABB2D region, glm::vec4 color, float thickness)
Rectangle outline as a LINE_LIST (4 edges, 8 vertices).
std::function< glm::vec2(glm::vec2)> unit_square(AABB2D bounds, const std::function< glm::vec2()> &live_offset={}) noexcept
Unit-square coordinates of a point within bounds.
std::vector< Kakshya::LineVertex > polyline(std::span< const glm::vec2 > pts, glm::vec4 color, float thickness)
Polyline as a LINE_LIST (open path, 2 * (pts.size() - 1) vertices).
void write_verts(std::vector< uint8_t > &out, const std::vector< V > &verts)
Write a vertex array into a GeometryFn output buffer.
Definition Geometry.hpp:35
std::function< void(T value, std::vector< uint8_t > &out_bytes, Element &element)> GeometryFn
Geometry function signature.
Definition Mapped.hpp:71
static AABB2D from_ndc(glm::vec2 center, glm::vec2 half) noexcept
Definition Bounds.hpp:94
bool contains(glm::vec2 p) const noexcept
Definition Bounds.hpp:67
AABB2D translated(glm::vec2 offset) const noexcept
Definition Bounds.hpp:84
glm::vec2 point_at(glm::vec2 unit) const noexcept
The point at a unit-space position: (0, 0) is min and (1, 1) is max.
Definition Bounds.hpp:115
Axis-aligned bounding rectangle in a 2D coordinate space.
Definition Bounds.hpp:63
A bounded, renderable region on a window surface.
Definition Element.hpp:62
std::function< void(Context &, uint32_t, std::shared_ptr< MappedState< T > >)> Wire
Registers interaction against the realized element.
Definition Geometry.hpp:100
Graphics::PrimitiveTopology topology
Topology matching the vertex kind geometry writes.
Definition Geometry.hpp:88
A geometry function plus everything its realization requires.
Definition Geometry.hpp:83

References MayaFlux::Portal::Forma::Geometry::Form< T >::capacity, MayaFlux::Kinesis::AABB2D::contains(), MayaFlux::Kinesis::AABB2D::from_ndc(), MayaFlux::IO::Left, MayaFlux::Portal::Graphics::LINE_LIST, MayaFlux::Kinesis::AABB2D::point_at(), MayaFlux::Kinesis::polyline(), MayaFlux::Kinesis::rect_outline(), MayaFlux::Portal::Forma::Geometry::Form< T >::topology, MayaFlux::Kinesis::AABB2D::translated(), MayaFlux::Kinesis::unit_square(), MayaFlux::Portal::Forma::Geometry::Form< T >::wire, and write_verts().

Referenced by MayaFlux::Portal::Forma::handle_path_form().

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