MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Prompt.cpp
Go to the documentation of this file.
1#include "Prompt.hpp"
2
3#include "TextField.hpp"
4
7
10
12
13namespace {
14
15 constexpr uint32_t k_width_px = 520;
16 constexpr uint32_t k_min_row_px = 48;
17 constexpr uint32_t k_row_padding_px = 16;
18
19 constexpr glm::vec4 k_accepted_color { 1.F, 1.F, 1.F, 1.F };
20 constexpr glm::vec4 k_refused_color { 1.F, 0.45F, 0.4F, 1.F };
21
22 struct PromptState {
23 AskCallback callback;
24 AskValidator validator;
25 std::weak_ptr<Core::Window> window;
26 bool done {};
27
28 [[nodiscard]] bool accepts(std::string_view text) const
29 {
30 return !validator || validator(text);
31 }
32
33 void finish(std::optional<std::string> result, bool close_window)
34 {
35 if (done) {
36 return;
37 }
38 done = true;
39
40 if (close_window) {
41 if (auto w = window.lock()) {
42 w->request_close();
43 }
44 }
45 if (callback) {
46 callback(std::move(result));
47 }
48 }
49 };
50
51} // namespace
52
53namespace internal {
54
55 std::function<void()> ask_in(
56 Surface& surface,
57 FormaTarget target,
58 uint32_t row_px,
59 std::string initial,
60 AskValidator validator,
61 AskCallback callback)
62 {
63 auto state = std::make_shared<PromptState>();
64 state->callback = std::move(callback);
65 state->validator = std::move(validator);
66 state->window = surface.window();
67
68 const glm::uvec2 dims { surface.window()->get_create_info().width, row_px };
69 auto params = std::make_shared<Text::PressParams>(row_press_params(dims, row_idle_color()));
70
71 const auto tint = [params, state](std::string_view text) {
72 params->color = state->accepts(text) ? k_accepted_color : k_refused_color;
73 };
74 tint(initial);
75
76 TextField field;
77 field.single_line()
78 .on_change([tint](const std::string& text) { tint(text); })
79 .on_commit([state](const std::string& text) {
80 if (state->accepts(text)) {
81 state->finish(text, true);
82 }
83 })
84 .on_cancel([state] { state->finish(std::nullopt, true); });
85
86 field.place(std::move(target), surface, surface.full(), params, std::move(initial));
87 surface.ctx().set_focus(field.element_id);
88
89 return [state] { state->finish(std::nullopt, false); };
90 }
91
92} // namespace internal
93
94void ask(
95 std::string title,
96 std::string initial,
97 AskValidator validator,
98 AskCallback callback)
99{
100 uint32_t row_px = k_min_row_px;
101 if (auto atlas = Text::TypeFaceFoundry::instance().get_default_glyph_atlas()) {
102 row_px = std::max(row_px, static_cast<uint32_t>(atlas->pixel_size()) + k_row_padding_px);
103 }
104
105 auto& atelier = internal::atelier();
106
107 auto window = atelier.create_window(Core::WindowCreateInfo {
108 .title = std::move(title) + " (Enter accepts, Escape cancels)",
109 .width = k_width_px,
110 .height = row_px,
111 .resizable = false,
112 });
113
114 auto cancel = std::make_shared<std::function<void()>>();
115
116 Surface surface = atelier.create_surface(SurfaceConfig {
117 .window = window,
118 .name = window->get_create_info().title,
119 .on_close = [cancel] {
120 if (*cancel) {
121 (*cancel)();
122 }
123 },
124 });
125
126 *cancel = internal::ask_in(
127 surface,
129 row_px, std::move(initial), std::move(validator), std::move(callback));
130}
131
132void ask(
133 std::string title,
134 std::string initial,
135 AskCallback callback)
136{
137 ask(std::move(title), std::move(initial), AskValidator {}, std::move(callback));
138}
139
140} // namespace MayaFlux::Portal::Forma
bool set_focus(uint32_t id)
Give keyboard focus to an element from code.
Definition Context.cpp:277
Where an element's geometry and texture are written.
Definition Target.hpp:22
Kinesis::AABB2D full() const noexcept
The entire NDC surface.
Definition Surface.hpp:314
const std::shared_ptr< Core::Window > & window() const noexcept
Access the rendering target window.
Definition Surface.hpp:178
Context & ctx() noexcept
Access the event router.
Definition Surface.hpp:170
Named owner of a (Window, Layer, Context) triple: the Forma canvas.
Definition Surface.hpp:121
Surface create_surface(SurfaceConfig config)
Build a Surface from a SurfaceConfig, creating Layer and Context when config.layer/config....
Definition Atelier.cpp:210
FormaTarget batch_slice(Surface &surface, Graphics::PrimitiveTopology topology)
A new slice of surface_batch(), of topology.
Definition Atelier.cpp:189
std::shared_ptr< Core::Window > create_window(const Core::WindowCreateInfo &info)
Create a window through the stored WindowManager and show it.
Definition Atelier.cpp:108
Text::PressParams row_press_params(glm::uvec2 dims, glm::vec4 background)
White text over background, laid out at dims pixels.
Definition TextRow.cpp:18
Atelier & atelier()
Accessor for the process-wide Atelier.
Definition Atelier.hpp:352
std::function< void()> ask_in(Surface &surface, FormaTarget target, uint32_t row_px, std::string initial, AskValidator validator, AskCallback callback)
The prompt itself, placed on a surface that already exists.
Definition Prompt.cpp:55
std::function< void(std::optional< std::string >)> AskCallback
Receives the accepted text, or nullopt when the prompt was cancelled or closed.
void ask(std::string title, std::string initial, AskValidator validator, AskCallback callback)
Open a one line text prompt and report what the user typed.
Definition Prompt.cpp:94
std::function< bool(std::string_view)> AskValidator
Decides whether a text may be accepted.
Configuration for creating a single window instance.
The single argument every Surface-creating function in this module takes, at every level of abstracti...
Definition Surface.hpp:26
uint32_t element_id
Element id of the text field. Valid after place().
TextField & single_line(bool enabled=true)
Keep the text on one line, with Enter committing it.
MAYAFLUX_API TextField & place(FormaTarget in_buf, Surface &surface, Kinesis::AABB2D bounds, std::shared_ptr< Portal::Text::PressParams > in_params, std::string initial_text={})
Register the text field element and wire text editing onto it.
TextField & on_change(std::function< void(const std::string &)> fn)
Call fn with the new text after every edit, before it is drawn.
An editable text region: keyboard/text-input wiring over an Element the caller already has a buffer f...