MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ViewportPreset.cpp
Go to the documentation of this file.
1#include "ViewportPreset.hpp"
2
3#include "Chronie.hpp"
4
9
11
12namespace MayaFlux {
13
14namespace {
15
16 struct PresetRecord {
17 Core::InputConfig saved_config;
18 std::vector<std::string> registered_events;
19 };
20
21 std::unordered_map<std::string, PresetRecord> s_registry;
22
23 std::string make_key(const std::shared_ptr<Core::Window>& window, const std::string& name)
24 {
25 return std::to_string(reinterpret_cast<uintptr_t>(window.get())) + "_" + name;
26 }
27
28 std::string event_name(const std::string& prefix, const char* suffix)
29 {
30 return "vp_" + prefix + "_" + suffix;
31 }
32
33} // namespace
34
36 const std::shared_ptr<Core::Window>& window,
37 const std::shared_ptr<Buffers::RenderProcessor>& processor,
39 const ViewportPresetConfig& config,
40 const std::string& name)
41{
42 if (mode != ViewportPresetMode::Fly) {
44 "ViewportPresetMode {} is not yet implemented",
45 static_cast<int>(mode));
46 return;
47 }
48
49 auto& record = s_registry[make_key(window, name)];
50 record.saved_config = window->get_input_config();
51 record.registered_events.clear();
52
53 auto st = std::make_shared<Kinesis::NavigationState>(
55
56 on_mouse_pressed(window, IO::MouseButtons::Right, [st](double /*x*/, double /*y*/) {
57 st->rmb_held = true;
58 st->first_mouse = true; }, event_name(name, "rmb_dn"));
59
60 on_mouse_released(window, IO::MouseButtons::Right, [st](double /*x*/, double /*y*/) { st->rmb_held = false; }, event_name(name, "rmb_up"));
61
62 on_key_pressed(window, IO::Keys::W, [st] { st->forward_held = true; }, event_name(name, "w_dn"));
63 on_key_released(window, IO::Keys::W, [st] { st->forward_held = false; }, event_name(name, "w_up"));
64 on_key_pressed(window, IO::Keys::S, [st] { st->back_held = true; }, event_name(name, "s_dn"));
65 on_key_released(window, IO::Keys::S, [st] { st->back_held = false; }, event_name(name, "s_up"));
66 on_key_pressed(window, IO::Keys::A, [st] { st->left_held = true; }, event_name(name, "a_dn"));
67 on_key_released(window, IO::Keys::A, [st] { st->left_held = false; }, event_name(name, "a_up"));
68 on_key_pressed(window, IO::Keys::D, [st] { st->right_held = true; }, event_name(name, "d_dn"));
69 on_key_released(window, IO::Keys::D, [st] { st->right_held = false; }, event_name(name, "d_up"));
70 on_key_pressed(window, IO::Keys::Q, [st] { st->down_held = true; }, event_name(name, "q_dn"));
71 on_key_released(window, IO::Keys::Q, [st] { st->down_held = false; }, event_name(name, "q_up"));
72 on_key_pressed(window, IO::Keys::E, [st] { st->up_held = true; }, event_name(name, "e_dn"));
73 on_key_released(window, IO::Keys::E, [st] { st->up_held = false; }, event_name(name, "e_up"));
74
75 on_mouse_move(window, [st](double x, double y) {
76 if (!st->rmb_held) {
77 st->first_mouse = true;
78 return;
79 }
80 if (st->first_mouse) {
81 st->last_x = x;
82 st->last_y = y;
83 st->first_mouse = false;
84 return;
85 }
87 static_cast<float>(x - st->last_x),
88 static_cast<float>(y - st->last_y));
89 st->last_x = x;
90 st->last_y = y; }, event_name(name, "mouse"));
91
92 on_scroll(window, [st](double /*dx*/, double dy) { Kinesis::apply_scroll(*st, static_cast<float>(dy)); }, event_name(name, "scroll"));
93
94 on_key_pressed(window, IO::Keys::KP1, [st] { Kinesis::snap_ortho(*st, 0); }, event_name(name, "kp1"));
95 on_key_pressed(window, IO::Keys::KP3, [st] { Kinesis::snap_ortho(*st, 1); }, event_name(name, "kp3"));
96 on_key_pressed(window, IO::Keys::KP7, [st] { Kinesis::snap_ortho(*st, 2); }, event_name(name, "kp7"));
97 on_key_pressed(window, IO::Keys::KP9, [st] { Kinesis::snap_ortho(*st, 3); }, event_name(name, "kp9"));
98
99 processor->set_view_transform_source(
100 [st, window_weak = std::weak_ptr<Core::Window>(window)]() -> Kinesis::ViewTransform {
101 auto win = window_weak.lock();
102 if (!win) {
103 return {};
104 }
105 const auto& ws = win->get_state();
106 const float aspect = (ws.current_height > 0)
107 ? static_cast<float>(ws.current_width) / static_cast<float>(ws.current_height)
108 : 1.0F;
109 return Kinesis::compute_view_transform(*st, aspect);
110 });
111}
112
114 const std::shared_ptr<Core::Window>& window,
116 const ViewportPresetConfig& config,
117 const std::string& name)
118{
119 for (const auto& buf : window->get_rendering_buffers()) {
120 auto rp = buf->get_render_processor();
121 if (!rp) {
122 continue;
123 }
124 bind_viewport_preset(window, rp, mode, config, name);
125 }
126}
127
129 const std::shared_ptr<Core::Window>& window,
130 const std::string& name)
131{
132 const std::string key = make_key(window, name);
133 auto it = s_registry.find(key);
134 if (it == s_registry.end()) {
135 return;
136 }
137 const Core::InputConfig saved = it->second.saved_config;
138 s_registry.erase(it);
139
140 window->set_input_config(saved);
141
142 static const char* const k_suffixes[] = {
143 "w_dn", "w_up", "s_dn", "s_up",
144 "a_dn", "a_up", "d_dn", "d_up",
145 "q_dn", "q_up", "e_dn", "e_up",
146 "rmb_dn", "rmb_up",
147 "mouse", "scroll",
148 "kp1", "kp3", "kp7", "kp9"
149 };
150
151 for (const auto& ev : it->second.registered_events) {
153 }
154}
155
156} // namespace MayaFlux
#define MF_RT_ERROR(comp, ctx,...)
std::vector< std::string > registered_events
Core::InputConfig saved_config
@ EventDispatch
Event dispatching and coordination.
@ API
MayaFlux/API Wrapper and convenience functions.
NavigationState make_navigation_state(const NavigationConfig &config)
Construct a NavigationState from a NavigationConfig.
void apply_mouse_delta(NavigationState &st, float dx, float dy)
Apply a mouse delta to yaw and pitch.
void apply_scroll(NavigationState &st, float ticks)
Dolly eye along the current forward vector.
ViewTransform compute_view_transform(NavigationState &st, float aspect)
Compute a ViewTransform from the current NavigationState.
void snap_ortho(NavigationState &st, int view)
Snap to a named ortho view.
bool cancel_event_handler(const std::string &name)
Cancel an event handler by name.
Definition Chronie.cpp:241
void on_mouse_move(const std::shared_ptr< Core::Window > &window, std::function< void(double, double)> callback, std::string name)
Schedule a mouse movement handler.
Definition Chronie.cpp:209
void on_scroll(const std::shared_ptr< Core::Window > &window, std::function< void(double, double)> callback, std::string name)
Schedule a mouse scroll handler.
Definition Chronie.cpp:225
ViewportPresetMode
Selects which navigation controller bind_viewport_preset installs.
@ Fly
First-person fly: WASD/QE translate, RMB drag yaw/pitch, scroll dolly, KP ortho snaps.
void on_mouse_pressed(const std::shared_ptr< Core::Window > &window, IO::MouseButtons button, std::function< void(double, double)> callback, std::string name)
Schedule a mouse button press handler.
Definition Chronie.cpp:175
void bind_viewport_preset(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< Buffers::RenderProcessor > &processor, ViewportPresetMode mode, const ViewportPresetConfig &config, const std::string &name)
Bind a navigation preset to a window and render processor.
void on_key_released(const std::shared_ptr< Core::Window > &window, IO::Keys key, std::function< void()> callback, std::string name)
Schedule a key release handler.
Definition Chronie.cpp:142
void on_mouse_released(const std::shared_ptr< Core::Window > &window, IO::MouseButtons button, std::function< void(double, double)> callback, std::string name)
Schedule a mouse button release handler.
Definition Chronie.cpp:192
void unbind_viewport_preset(const std::shared_ptr< Core::Window > &window, const std::string &name)
Cancel all event handlers registered by bind_viewport_preset() and restore the window input config to...
void on_key_pressed(const std::shared_ptr< Core::Window > &window, IO::Keys key, std::function< void()> callback, std::string name)
Schedule a key press handler.
Definition Chronie.cpp:125
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12
Input configuration for a window.
Tuning parameters for a first-person fly-navigation controller.
View and projection matrices as a named push constant slot.