MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Dropdown.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "LayoutCursor.hpp"
4
6
7class Surface;
8
9/**
10 * @struct Dropdown
11 * @brief A header showing the current choice that opens into a list of options.
12 *
13 * Configure with the setters, then place() once. After place(), selected,
14 * open, header_id, header_bounds and cursor_out are populated. The list
15 * overlays whatever lies below the header and does not advance the cursor.
16 * The header reads "v" before the current choice while closed and "^" while
17 * open. Choosing a row writes its index to selected, updates the header and
18 * closes the list. Lists longer than max_visible() scroll with the mouse wheel.
19 *
20 * selected is registered with the Bridge, so Forma::bridge().at(selected)
21 * reads the index like any other state. A Bridge inbound write changes the
22 * index but not the header caption.
23 *
24 * @code
25 * auto wave = Dropdown {}
26 * .options({ "sine", "saw", "square" })
27 * .on_select([](size_t index) { })
28 * .place(surface, cursor, 0.08F);
29 * @endcode
30 */
31struct Dropdown {
32 /// @brief Element id of the header strip. Valid after place().
33 uint32_t header_id {};
34
35 /// @brief Index of the chosen option. Valid after place().
36 std::shared_ptr<MappedState<size_t>> selected;
37
38 /// @brief Whether the list is showing. Valid after place().
39 std::shared_ptr<MappedState<bool>> open;
40
41 /// @brief Cursor positioned below the header. Valid after place().
43
44 /// @brief NDC region occupied by the header strip. Valid after place().
46
47 /// @brief Header strip region, satisfying Kinesis::HasBounds.
48 [[nodiscard]] Kinesis::AABB2D bounds() const noexcept { return header_bounds; }
49
50 /// @brief Option captions, in display order.
51 std::vector<std::string> m_labels;
52
53 /// @brief Index chosen before any interaction. Default: 0.
54 size_t m_initial {};
55
56 /// @brief Rows shown at once before the list scrolls. Default: 8.
57 uint32_t m_max_visible { 8 };
58
59 /// @brief Called with the index each time a row is chosen.
60 std::function<void(size_t)> m_on_select;
61
62 Dropdown& options(std::vector<std::string> labels)
63 {
64 m_labels = std::move(labels);
65 return *this;
66 }
67
69 {
71 return *this;
72 }
73
75 {
76 m_max_visible = std::max<uint32_t>(rows, 1);
77 return *this;
78 }
79
80 Dropdown& on_select(std::function<void(size_t)> fn)
81 {
82 m_on_select = std::move(fn);
83 return *this;
84 }
85
86 /**
87 * @brief Register the header and its option rows and wire their input.
88 *
89 * Advances @p cursor by @p row_h. With no options, logs a warning and
90 * leaves the struct unplaced.
91 *
92 * @param surface Surface to register on.
93 * @param cursor Layout cursor. Advanced by row_h on return.
94 * @param x_min Left edge in NDC.
95 * @param x_max Right edge in NDC.
96 * @param row_h Height of the header and of each option row, in NDC units.
97 * @return *this, with results populated.
98 */
99 MAYAFLUX_API Dropdown& place(
100 Surface& surface,
101 LayoutCursor& cursor,
102 float x_min, float x_max, float row_h);
103
104 /**
105 * @brief Register using the cursor's column extents.
106 */
107 MAYAFLUX_API Dropdown& place(
108 Surface& surface,
109 LayoutCursor& cursor,
110 float row_h);
111};
112
113} // namespace MayaFlux::Portal::Forma
uint32_t rows
uint32_t index
Definition VKDevice.cpp:122
Reactive Y-position accumulator for vertical primitive stacking.
Named owner of a (Window, Layer, Context) triple: the Forma canvas.
Definition Surface.hpp:121
Axis-aligned bounding rectangle in a 2D coordinate space.
Definition Bounds.hpp:63
MAYAFLUX_API Dropdown & place(Surface &surface, LayoutCursor &cursor, float x_min, float x_max, float row_h)
Register the header and its option rows and wire their input.
Definition Dropdown.cpp:22
std::shared_ptr< MappedState< size_t > > selected
Index of the chosen option. Valid after place().
Definition Dropdown.hpp:36
std::function< void(size_t)> m_on_select
Called with the index each time a row is chosen.
Definition Dropdown.hpp:60
Dropdown & options(std::vector< std::string > labels)
Definition Dropdown.hpp:62
Dropdown & max_visible(uint32_t rows)
Definition Dropdown.hpp:74
std::vector< std::string > m_labels
Option captions, in display order.
Definition Dropdown.hpp:51
uint32_t m_max_visible
Rows shown at once before the list scrolls. Default: 8.
Definition Dropdown.hpp:57
Dropdown & initial(size_t index)
Definition Dropdown.hpp:68
std::shared_ptr< MappedState< bool > > open
Whether the list is showing. Valid after place().
Definition Dropdown.hpp:39
uint32_t header_id
Element id of the header strip. Valid after place().
Definition Dropdown.hpp:33
Kinesis::AABB2D bounds() const noexcept
Header strip region, satisfying Kinesis::HasBounds.
Definition Dropdown.hpp:48
Kinesis::AABB2D header_bounds
NDC region occupied by the header strip. Valid after place().
Definition Dropdown.hpp:45
size_t m_initial
Index chosen before any interaction. Default: 0.
Definition Dropdown.hpp:54
Dropdown & on_select(std::function< void(size_t)> fn)
Definition Dropdown.hpp:80
LayoutCursor cursor_out
Cursor positioned below the header. Valid after place().
Definition Dropdown.hpp:42
A header showing the current choice that opens into a list of options.
Definition Dropdown.hpp:31