20 std::unordered_map<std::string, PresetRecord> s_registry;
22 std::string make_key(
const std::shared_ptr<Core::Window>& window,
const std::string& name)
24 return std::to_string(
reinterpret_cast<uintptr_t
>(window.get())) +
"_" + name;
27 std::string event_name(
const std::string& prefix,
const char* suffix)
29 return "vp_" + prefix +
"_" + suffix;
35 const std::shared_ptr<Core::Window>& window,
36 const std::shared_ptr<Buffers::RenderProcessor>& processor,
38 const std::string& name)
54 "ViewportPresetMode {} is not yet implemented",
55 static_cast<int>(mode));
60 const std::shared_ptr<Core::Window>& window,
62 const std::string& name)
64 for (
const auto& buf : window->get_rendering_buffers()) {
65 auto rp = buf->get_render_processor();
73 const std::shared_ptr<Core::Window>& window,
74 const std::shared_ptr<Buffers::RenderProcessor>& processor,
77 const std::string& name)
79 auto& record = s_registry[make_key(window, name)];
80 record.saved_config = window->get_input_config();
81 record.registered_events.clear();
83 auto st = std::make_shared<Kinesis::NavigationState>(
88 st->first_mouse =
true; }, event_name(name,
"rmb_dn"));
92 on_key_pressed(window, key_map.
forward, [st] { st->forward_held = true; }, event_name(name,
"fwd_dn"));
94 on_key_pressed(window, key_map.
back, [st] { st->back_held = true; }, event_name(name,
"bck_dn"));
95 on_key_released(window, key_map.
back, [st] { st->back_held = false; }, event_name(name,
"bck_up"));
96 on_key_pressed(window, key_map.
left, [st] { st->left_held = true; }, event_name(name,
"lft_dn"));
97 on_key_released(window, key_map.
left, [st] { st->left_held = false; }, event_name(name,
"lft_up"));
98 on_key_pressed(window, key_map.
right, [st] { st->right_held = true; }, event_name(name,
"rgt_dn"));
99 on_key_released(window, key_map.
right, [st] { st->right_held = false; }, event_name(name,
"rgt_up"));
100 on_key_pressed(window, key_map.
down, [st] { st->down_held = true; }, event_name(name,
"dwn_dn"));
101 on_key_released(window, key_map.
down, [st] { st->down_held = false; }, event_name(name,
"dwn_up"));
102 on_key_pressed(window, key_map.
up, [st] { st->up_held = true; }, event_name(name,
"up_dn"));
103 on_key_released(window, key_map.
up, [st] { st->up_held = false; }, event_name(name,
"up_up"));
116 st->first_mouse = true;
119 if (st->first_mouse) {
122 st->first_mouse = false;
126 static_cast<float>(x - st->last_x),
127 static_cast<float>(y - st->last_y));
129 st->last_y = y; }, event_name(name,
"mouse"));
133 processor->set_view_transform_source(
135 auto win = window_weak.lock();
139 const auto& ws = win->get_state();
140 const float aspect = (ws.current_height > 0)
141 ?
static_cast<float>(ws.current_width) /
static_cast<float>(ws.current_height)
148 const std::shared_ptr<Core::Window>& window,
151 const std::string& name)
153 for (
const auto& buf : window->get_rendering_buffers()) {
154 auto rp = buf->get_render_processor();
163 const std::shared_ptr<Core::Window>& window,
164 const std::shared_ptr<Buffers::RenderProcessor>& processor,
167 const std::string& name)
169 auto& record = s_registry[make_key(window, name)];
170 record.saved_config = window->get_input_config();
171 record.registered_events.clear();
177 st->first_mouse =
true; }, event_name(name,
"mmb_dn"));
186 st->first_mouse = true;
189 if (st->first_mouse) {
192 st->first_mouse = false;
195 const auto dx =
static_cast<float>(x - st->last_x);
196 const auto dy =
static_cast<float>(y - st->last_y);
204} }, event_name(name,
"mouse"));
213 on_key_pressed(window, *key_map.
ortho_top, [st] { Kinesis::snap_orbit_ortho(*st, 2); }, event_name(name,
"kp_top"));
215 on_key_pressed(window, *key_map.
ortho_flip, [st] { Kinesis::snap_orbit_ortho(*st, 3); }, event_name(name,
"kp_flip"));
217 processor->set_view_transform_source(
219 auto win = window_weak.lock();
222 const auto& ws = win->get_state();
223 const float aspect = (ws.current_height > 0)
224 ?
static_cast<float>(ws.current_width) /
static_cast<float>(ws.current_height)
231 const std::shared_ptr<Core::Window>& window,
234 const std::string& name)
236 for (
const auto& buf : window->get_rendering_buffers()) {
237 auto rp = buf->get_render_processor();
245 const std::shared_ptr<Core::Window>& window,
246 const std::shared_ptr<Buffers::RenderProcessor>& processor,
249 const std::string& name)
251 auto& record = s_registry[make_key(window, name)];
252 record.saved_config = window->get_input_config();
253 record.registered_events.clear();
258 st->drag_held = true;
259 st->first_mouse = true; }, event_name(name,
"drag_dn"));
263 on_mouse_move(window, [st, window_weak = std::weak_ptr<Core::Window>(window)](
double x,
double y) {
264 if (!st->drag_held) {
265 st->first_mouse = true;
268 if (st->first_mouse) {
271 st->first_mouse = false;
274 const auto dx =
static_cast<float>(x - st->last_x);
275 const auto dy =
static_cast<float>(y - st->last_y);
279 auto win = window_weak.lock();
282 const auto& ws = win->get_state();
283 if (ws.current_width > 0 && ws.current_height > 0) {
285 static_cast<float>(ws.current_width),
286 static_cast<float>(ws.current_height));
287 } }, event_name(name,
"mouse"));
291 processor->set_view_transform_source(
293 auto win = window_weak.lock();
296 const auto& ws = win->get_state();
297 const float aspect = (ws.current_height > 0)
298 ?
static_cast<float>(ws.current_width) /
static_cast<float>(ws.current_height)
305 const std::shared_ptr<Core::Window>& window,
308 const std::string& name)
310 for (
const auto& buf : window->get_rendering_buffers()) {
311 auto rp = buf->get_render_processor();
319 const std::shared_ptr<Core::Window>& window,
320 const std::shared_ptr<Buffers::RenderProcessor>& processor,
323 const std::string& name)
325 auto& record = s_registry[make_key(window, name)];
326 record.saved_config = window->get_input_config();
327 record.registered_events.clear();
329 auto st = std::make_shared<Kinesis::NavigationState>(
334 st->first_mouse = true; }, event_name(name,
"drag_dn"));
340 st->first_mouse = true;
343 if (st->first_mouse) {
346 st->first_mouse = false;
349 const auto dx =
static_cast<float>(x - st->last_x);
350 const auto dy =
static_cast<float>(y - st->last_y);
354 const glm::vec3 forward {
355 std::cos(st->pitch) * std::sin(st->yaw),
357 std::cos(st->pitch) * std::cos(st->yaw)
359 const glm::vec3 right = glm::normalize(glm::cross(forward, glm::vec3(0.0F, 1.0F, 0.0F)));
360 const glm::vec3 up = glm::normalize(glm::cross(right, forward));
362 st->eye -= right * (dx * st->mouse_sensitivity);
363 st->eye += up * (dy * st->mouse_sensitivity); }, event_name(name,
"mouse"));
367 processor->set_view_transform_source(
369 auto win = window_weak.lock();
372 const auto& ws = win->get_state();
373 const float aspect = (ws.current_height > 0)
374 ?
static_cast<float>(ws.current_width) /
static_cast<float>(ws.current_height)
381 const std::shared_ptr<Core::Window>& window,
384 const std::string& name)
386 for (
const auto& buf : window->get_rendering_buffers()) {
387 auto rp = buf->get_render_processor();
395 const std::shared_ptr<Core::Window>& window,
396 const std::string& name)
398 const std::string key = make_key(window, name);
399 auto it = s_registry.find(key);
400 if (it == s_registry.end())
403 window->set_input_config(it->second.saved_config);
405 for (
const auto& ev : it->second.registered_events)
408 s_registry.erase(it);
#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.
void apply_orbit_rotate(OrbitState &st, float dx, float dy)
Apply a mouse delta to azimuth and elevation.
ViewTransform compute_pan_zoom_view_transform(const PanZoom2DState &st, float aspect)
Build a ViewTransform from the current PanZoom2DState.
PanZoom2DState make_pan_zoom_state(const PanZoom2DConfig &config)
Construct a PanZoom2DState from a PanZoom2DConfig.
void apply_pan_zoom_scroll(PanZoom2DState &st, float ticks)
Zoom by multiplying the half-height, clamped to [min_zoom, max_zoom].
void apply_pan_zoom_pan(PanZoom2DState &st, float dx, float dy, float viewport_width, float viewport_height)
Pan by a pixel delta, scaled to world units by the current zoom level.
void apply_scroll(NavigationState &st, float ticks)
Dolly eye along the current forward vector.
void apply_orbit_scroll(OrbitState &st, float ticks)
Dolly the camera toward or away from the focal point.
ViewTransform compute_orbit_view_transform(const OrbitState &st, float aspect)
Build a ViewTransform from the current OrbitState.
NavigationState make_navigation_state(const NavigationConfig &config)
Construct a NavigationState from a NavigationConfig.
void apply_orbit_pan(OrbitState &st, float dx, float dy)
Pan the focal point in the camera's local right/up plane.
ViewTransform compute_view_transform(NavigationState &st, float aspect)
Compute a ViewTransform from the current NavigationState.
OrbitState make_orbit_state(const OrbitConfig &config)
Construct an OrbitState from an OrbitConfig.
void apply_mouse_delta(NavigationState &st, float dx, float dy)
Apply a mouse delta to yaw and pitch.
ViewTransform build_view_transform(const NavigationState &st, float aspect)
Build a ViewTransform from the current NavigationState without mutating it.
bool cancel_event_handler(const std::string &name)
Cancel an event handler by name.
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.
void on_scroll(const std::shared_ptr< Core::Window > &window, std::function< void(double, double)> callback, std::string name)
Schedule a mouse scroll handler.
void bind_screenspace_preset(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< Buffers::RenderProcessor > &processor, const Kinesis::NavigationConfig &config, const Kinesis::ScreenspaceKeyMap &key_map, const std::string &name)
Bind the screenspace navigation controller to a window and render processor.
ViewportPresetMode
Selects which navigation controller bind_viewport_preset installs.
@ PanZoom2D
Orthographic 2D pan and zoom (not yet implemented)
@ Orbit
Tumble around a focal point (not yet implemented)
@ Fly
First-person fly: WASD/QE translate, RMB drag yaw/pitch, scroll dolly, KP ortho snaps.
@ Screenspace
Perspective pan in camera's local right/up plane, scroll dolly, no rotation.
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.
void bind_viewport_preset(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< Buffers::RenderProcessor > &processor, ViewportPresetMode mode, const std::string &name)
Bind a navigation preset to a window and render processor using default settings.
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.
void bind_pan_zoom_preset(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< Buffers::RenderProcessor > &processor, const Kinesis::PanZoom2DConfig &config, const Kinesis::PanZoom2DKeyMap &key_map, const std::string &name)
Bind the 2D pan/zoom controller to a window and render processor.
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.
void bind_orbit_preset(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< Buffers::RenderProcessor > &processor, const Kinesis::OrbitConfig &config, const Kinesis::OrbitKeyMap &key_map, const std::string &name)
Bind the orbit navigation controller to a window and render processor.
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.
void bind_fly_preset(const std::shared_ptr< Core::Window > &window, const std::shared_ptr< Buffers::RenderProcessor > &processor, const ViewportPresetConfig &config, const Kinesis::FlyKeyMap &key_map, const std::string &name)
Bind the fly navigation controller to a window and render processor.
Main namespace for the Maya Flux audio engine.
std::optional< IO::Keys > ortho_top
Snap to top (+Y) view.
std::optional< IO::Keys > ortho_right
Snap to right (+X) view.
std::optional< IO::Keys > ortho_flip
Flip to opposite view.
std::optional< IO::Keys > ortho_front
Snap to front (+Z) view.
Key assignments for the Fly navigation preset.
Tuning parameters for a first-person fly-navigation controller.
Construction parameters for an orbit navigation controller.
std::optional< IO::Keys > ortho_right
IO::Keys pan_modifier
Held during MMB drag to pan instead of rotate.
std::optional< IO::Keys > ortho_flip
std::optional< IO::Keys > ortho_front
std::optional< IO::Keys > ortho_top
Key assignments for the Orbit navigation preset.
Construction parameters for a 2D pan/zoom orthographic controller.
IO::MouseButtons drag_button
Button held to pan.
Input assignments for the PanZoom2D navigation preset.
IO::MouseButtons drag_button
Button held to pan.
Input assignments for the Screenspace navigation preset.