MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Interaction.hpp
Go to the documentation of this file.
1#pragma once
2
7
9
10/**
11 * @struct InteractionSource
12 * @brief What an interaction provider may use: the placement bounds, when there are any.
13 */
15 std::optional<Kinesis::AABB2D> placement;
16};
17
18/** @brief The interaction registration of a Form, run once after its element exists. */
19template <typename T>
21
22/** @brief Produces the interaction of a Form of state type T, or nullopt with an error. */
23template <typename T>
24using InteractionProvider = std::function<std::optional<InteractionFn<T>>(const InteractionSource&, std::string&)>;
25
26/**
27 * @class InteractionTable
28 * @brief Interaction providers for one state type, by name.
29 *
30 * A plain value, with nothing registered until its builder adds it. An
31 * factory that lets a document choose its interaction declares a name among
32 * its described inputs and resolves it here, so interaction is selected by the
33 * document and implemented in C++.
34 *
35 * @tparam T State type the handlers write.
36 */
37template <typename T>
39public:
40 /** @brief Register a provider, replacing any of the same name. */
41 void expose(std::string name, InteractionProvider<T> provider)
42 {
43 m_providers.insert_or_assign(name, std::move(provider));
44 }
45
46 /** @brief Add another table under a prefix, as FlatMap::merge_prefixed. */
47 bool merge(const InteractionTable& other, std::string_view prefix,
48 std::string& error, bool replace = false)
49 {
50 return m_providers.merge_prefixed(other.m_providers, prefix, error, replace);
51 }
52
53 [[nodiscard]] bool contains(std::string_view name) const
54 {
55 return m_providers.find(name) != m_providers.end();
56 }
57
58 /** @brief Registered names in sorted order. */
59 [[nodiscard]] std::vector<std::string> names() const
60 {
61 std::vector<std::string> result;
62 for (const auto& entry : m_providers)
63 result.push_back(entry.first);
64 return result;
65 }
66
67 /** @brief Run the provider of this name on a source. */
68 [[nodiscard]] std::optional<InteractionFn<T>> resolve(
69 std::string_view name, const InteractionSource& source, std::string& error) const
70 {
71 const auto provider = m_providers.find(name);
72 if (provider == m_providers.end()) {
73 error = "no registered interaction " + std::string(name);
74 return std::nullopt;
75 }
76 return std::invoke(provider->second, source, error);
77 }
78
79 /**
80 * @brief Resolve a name onto a Form's interaction.
81 *
82 * An empty name leaves the Form as it is and succeeds, so an input that
83 * does not name an interaction needs no check by the caller.
84 *
85 * @param placement Bounds the provider may use, when the placement has any.
86 * @param error Receives the reason when false is returned.
87 */
88 bool attach(Geometry::Form<T>& form, std::string_view name,
89 std::optional<Kinesis::AABB2D> placement, std::string& error) const
90 {
91 if (name.empty())
92 return true;
93 const auto interaction = resolve(name, InteractionSource { .placement = placement }, error);
94 if (!interaction)
95 return false;
96 form.wire = *interaction;
97 return true;
98 }
99
100private:
102};
103
104/** @brief Provider for an interaction that needs nothing from the placement. */
105template <typename T>
107{
108 return [interaction = std::move(interaction)](const InteractionSource&, std::string&)
109 -> std::optional<InteractionFn<T>> { return interaction; };
110}
111
112/**
113 * @brief Provider for an interaction made from the placement bounds.
114 *
115 * The provider reports an error when the placement has no bounds, so the
116 * callable never has to check.
117 *
118 * @param make Callable taking the bounds and returning the interaction.
119 */
120template <typename T>
122 std::function<InteractionFn<T>(const Kinesis::AABB2D&)> make)
123{
124 return [make = std::move(make)](const InteractionSource& source, std::string& error)
125 -> std::optional<InteractionFn<T>> {
126 if (!source.placement) {
127 error = "interaction needs placement bounds";
128 return std::nullopt;
129 }
130 return make(*source.placement);
131 };
132}
133
134/** @brief Provider whose handler flips a bool on a left press. Register it under any name. */
136{
137 return [](const InteractionSource&, std::string&) -> std::optional<InteractionFn<bool>> {
139 };
140}
141
142}
Illustrative geometry functions for common Mapped use cases.
Map kept as one sorted vector, cheap to copy.
Definition FlatMap.hpp:21
std::vector< std::string > names() const
Registered names in sorted order.
bool attach(Geometry::Form< T > &form, std::string_view name, std::optional< Kinesis::AABB2D > placement, std::string &error) const
Resolve a name onto a Form's interaction.
Memory::FlatMap< std::string, InteractionProvider< T >, std::less<> > m_providers
std::optional< InteractionFn< T > > resolve(std::string_view name, const InteractionSource &source, std::string &error) const
Run the provider of this name on a source.
bool merge(const InteractionTable &other, std::string_view prefix, std::string &error, bool replace=false)
Add another table under a prefix, as FlatMap::merge_prefixed.
void expose(std::string name, InteractionProvider< T > provider)
Register a provider, replacing any of the same name.
bool contains(std::string_view name) const
Interaction providers for one state type, by name.
auto press_flip(IO::MouseButtons btn=IO::MouseButtons::Left)
Produce a Form::wire that registers a press flipping a bool state.
InteractionProvider< T > fixed_interaction(InteractionFn< T > interaction)
Provider for an interaction that needs nothing from the placement.
InteractionProvider< T > bounded_interaction(std::function< InteractionFn< T >(const Kinesis::AABB2D &)> make)
Provider for an interaction made from the placement bounds.
InteractionProvider< bool > press_flip_interaction()
Provider whose handler flips a bool on a left press.
std::function< std::optional< InteractionFn< T > >(const InteractionSource &, std::string &)> InteractionProvider
Produces the interaction of a Form of state type T, or nullopt with an error.
typename Geometry::Form< T >::Wire InteractionFn
The interaction registration of a Form, run once after its element exists.
Axis-aligned bounding rectangle in a 2D coordinate space.
Definition Bounds.hpp:63
std::function< void(Context &, uint32_t, std::shared_ptr< MappedState< T > >)> Wire
Registers interaction against the realized element.
Definition Geometry.hpp:100
A geometry function plus everything its realization requires.
Definition Geometry.hpp:83
std::optional< Kinesis::AABB2D > placement
What an interaction provider may use: the placement bounds, when there are any.