|
|
@ -27,6 +27,8 @@ |
|
|
|
#include "game/controls.hpp"
|
|
|
|
#include "game/strings.hpp"
|
|
|
|
#include "utility/hash/fnv1a.hpp"
|
|
|
|
#include <format>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
using namespace hash::literals; |
|
|
|
|
|
|
@ -38,17 +40,14 @@ keyboard_config_menu::keyboard_config_menu(game::context& ctx): |
|
|
|
{ |
|
|
|
debug::log::trace("Entering keyboard config menu state..."); |
|
|
|
|
|
|
|
// Add camera control menu items
|
|
|
|
add_control_item("move_forward"); |
|
|
|
add_control_item("move_back"); |
|
|
|
add_control_item("move_left"); |
|
|
|
add_control_item("move_right"); |
|
|
|
add_control_item("move_up"); |
|
|
|
add_control_item("move_down"); |
|
|
|
|
|
|
|
// Add application control menu items
|
|
|
|
add_control_item("toggle_fullscreen"); |
|
|
|
add_control_item("screenshot"); |
|
|
|
// Add control menu items
|
|
|
|
add_control_item(ctx.movement_controls, ctx.move_forward_control, "control_move_forward"_fnv1a32); |
|
|
|
add_control_item(ctx.movement_controls, ctx.move_back_control, "control_move_back"_fnv1a32); |
|
|
|
add_control_item(ctx.movement_controls, ctx.move_left_control, "control_move_left"_fnv1a32); |
|
|
|
add_control_item(ctx.movement_controls, ctx.move_right_control, "control_move_right"_fnv1a32); |
|
|
|
add_control_item(ctx.movement_controls, ctx.move_up_control, "control_move_up"_fnv1a32); |
|
|
|
add_control_item(ctx.movement_controls, ctx.move_down_control, "control_move_down"_fnv1a32); |
|
|
|
add_control_item(ctx.movement_controls, ctx.pause_control, "control_pause"_fnv1a32); |
|
|
|
|
|
|
|
// Construct menu item texts
|
|
|
|
scene::text* back_text = new scene::text(); |
|
|
@ -131,105 +130,82 @@ keyboard_config_menu::~keyboard_config_menu() |
|
|
|
debug::log::trace("Exited keyboard config menu state..."); |
|
|
|
} |
|
|
|
|
|
|
|
std::string keyboard_config_menu::get_binding_string(input::control* control) |
|
|
|
std::string keyboard_config_menu::get_mapping_string(const input::control_map& control_map, const input::control& control) |
|
|
|
{ |
|
|
|
std::string binding_string; |
|
|
|
/*
|
|
|
|
auto mappings = ctx.input_event_router->get_mappings(control); |
|
|
|
for (input::mapping* mapping: *mappings) |
|
|
|
std::string mapping_string; |
|
|
|
|
|
|
|
if (auto key_mappings = control_map.get_key_mappings(control); !key_mappings.empty()) |
|
|
|
{ |
|
|
|
std::string mapping_string; |
|
|
|
const auto& key_mapping = key_mappings.front(); |
|
|
|
|
|
|
|
switch (mapping->get_type()) |
|
|
|
// Get name of scancode string from scancode
|
|
|
|
std::string scancode_string_name = std::format("scancode_{:02x}", std::to_underlying(key_mapping.scancode)); |
|
|
|
|
|
|
|
// Set mapping string to scancode string
|
|
|
|
mapping_string = get_string(ctx, hash::fnv1a32(scancode_string_name.data(), scancode_string_name.length())); |
|
|
|
} |
|
|
|
else if (auto mouse_button_mappings = control_map.get_mouse_button_mappings(control); !mouse_button_mappings.empty()) |
|
|
|
{ |
|
|
|
const auto& mouse_button_mapping = mouse_button_mappings.front(); |
|
|
|
switch (mouse_button_mapping.button) |
|
|
|
{ |
|
|
|
|
|
|
|
case input::mapping_type::key: |
|
|
|
{ |
|
|
|
const input::key_mapping* key_mapping = static_cast<const input::key_mapping*>(mapping); |
|
|
|
const char* scancode_name = input::keyboard::get_scancode_name(key_mapping->scancode); |
|
|
|
mapping_string = scancode_name; |
|
|
|
case input::mouse_button::left: |
|
|
|
mapping_string = get_string(ctx, "mouse_button_left"_fnv1a32); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
case input::mapping_type::mouse_wheel: |
|
|
|
{ |
|
|
|
const input::mouse_wheel_mapping* wheel_mapping = static_cast<const input::mouse_wheel_mapping*>(mapping); |
|
|
|
|
|
|
|
switch (wheel_mapping->axis) |
|
|
|
{ |
|
|
|
case input::mouse_wheel_axis::negative_x: |
|
|
|
mapping_string = (*ctx.strings)["mouse_wheel_left"]; |
|
|
|
break; |
|
|
|
|
|
|
|
case input::mouse_wheel_axis::positive_x: |
|
|
|
mapping_string = (*ctx.strings)["mouse_wheel_right"]; |
|
|
|
break; |
|
|
|
|
|
|
|
case input::mouse_wheel_axis::negative_y: |
|
|
|
mapping_string = (*ctx.strings)["mouse_wheel_down"]; |
|
|
|
break; |
|
|
|
|
|
|
|
case input::mouse_wheel_axis::positive_y: |
|
|
|
mapping_string = (*ctx.strings)["mouse_wheel_up"]; |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
case input::mouse_button::middle: |
|
|
|
mapping_string = get_string(ctx, "mouse_button_middle"_fnv1a32); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
case input::mapping_type::mouse_button: |
|
|
|
{ |
|
|
|
const input::mouse_button_mapping* button_mapping = static_cast<const input::mouse_button_mapping*>(mapping); |
|
|
|
|
|
|
|
if (button_mapping->button == 1) |
|
|
|
{ |
|
|
|
mapping_string = (*ctx.strings)["mouse_button_left"]; |
|
|
|
} |
|
|
|
else if (button_mapping->button == 2) |
|
|
|
{ |
|
|
|
mapping_string = (*ctx.strings)["mouse_button_middle"]; |
|
|
|
} |
|
|
|
else if (button_mapping->button == 3) |
|
|
|
{ |
|
|
|
mapping_string = (*ctx.strings)["mouse_button_right"]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
const std::string& format = (*ctx.strings)["mouse_button_n"]; |
|
|
|
char buffer[64]; |
|
|
|
std::snprintf(buffer, 64, format.c_str(), button_mapping->button); |
|
|
|
mapping_string = buffer; |
|
|
|
} |
|
|
|
case input::mouse_button::right: |
|
|
|
mapping_string = get_string(ctx, "mouse_button_right"_fnv1a32); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
{ |
|
|
|
std::string format_string = get_string(ctx, "mouse_button_n_format"_fnv1a32); |
|
|
|
mapping_string = std::vformat(format_string, std::make_format_args(std::to_underlying(mouse_button_mapping.button))); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (auto mouse_scroll_mappings = control_map.get_mouse_scroll_mappings(control); !mouse_scroll_mappings.empty()) |
|
|
|
{ |
|
|
|
const auto& mouse_scroll_mapping = mouse_scroll_mappings.front(); |
|
|
|
|
|
|
|
if (!mapping_string.empty()) |
|
|
|
if (mouse_scroll_mapping.axis == input::mouse_scroll_axis::x) |
|
|
|
{ |
|
|
|
if (!mouse_scroll_mapping.direction) |
|
|
|
{ |
|
|
|
mapping_string = get_string(ctx, "mouse_scroll_left"_fnv1a32); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
mapping_string = get_string(ctx, "mouse_scroll_right"_fnv1a32); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if (binding_string.empty()) |
|
|
|
if (!mouse_scroll_mapping.direction) |
|
|
|
{ |
|
|
|
binding_string = mapping_string; |
|
|
|
mapping_string = get_string(ctx, "mouse_scroll_up"_fnv1a32); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
binding_string += " " + mapping_string; |
|
|
|
mapping_string = get_string(ctx, "mouse_scroll_down"_fnv1a32); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
return binding_string; |
|
|
|
else |
|
|
|
{ |
|
|
|
mapping_string = get_string(ctx, "control_unmapped"_fnv1a32); |
|
|
|
} |
|
|
|
|
|
|
|
return mapping_string; |
|
|
|
} |
|
|
|
|
|
|
|
void keyboard_config_menu::add_control_item(const std::string& control_name) |
|
|
|
void keyboard_config_menu::add_control_item(input::control_map& control_map, input::control& control, std::uint32_t control_name_hash) |
|
|
|
{ |
|
|
|
// Get pointer to control
|
|
|
|
//input::control* control = ctx.controls[control_name];
|
|
|
|
|
|
|
|
// Construct texts
|
|
|
|
scene::text* name_text = new scene::text(); |
|
|
|
scene::text* value_text = new scene::text(); |
|
|
@ -237,88 +213,71 @@ void keyboard_config_menu::add_control_item(const std::string& control_name) |
|
|
|
// Add texts to list of menu item texts
|
|
|
|
ctx.menu_item_texts.push_back({name_text, value_text}); |
|
|
|
|
|
|
|
// Set content of name text
|
|
|
|
name_text->set_content(control_name); |
|
|
|
// Set control name and mapping texts
|
|
|
|
name_text->set_content(get_string(ctx, control_name_hash)); |
|
|
|
value_text->set_content(get_mapping_string(control_map, control)); |
|
|
|
|
|
|
|
// Set content of value text
|
|
|
|
//value_text->set_content(get_binding_string( control));
|
|
|
|
|
|
|
|
auto select_callback = [this, &ctx = this->ctx, value_text]() |
|
|
|
// Callback invoked when an input has been mapped to the control
|
|
|
|
auto input_mapped_callback = [this, &ctx = this->ctx, control_map = &control_map, control = &control, value_text](const auto& event) |
|
|
|
{ |
|
|
|
// Disable menu controls
|
|
|
|
ctx.function_queue.push(std::bind(game::disable_menu_controls, std::ref(ctx))); |
|
|
|
// Remove key mappings, mouse button mappings, and mouse scroll mappings mapped to the control
|
|
|
|
control_map->remove_mappings(*control, input::mapping_type::key); |
|
|
|
control_map->remove_mappings(*control, input::mapping_type::mouse_button); |
|
|
|
control_map->remove_mappings(*control, input::mapping_type::mouse_scroll); |
|
|
|
|
|
|
|
// Clear binding string from value text
|
|
|
|
value_text->set_content(get_string(ctx, "ellipsis"_fnv1a32)); |
|
|
|
//if (event.mapping.scancode != input::scancode::escape && event.mapping.scancode != input::scancode::backspace)
|
|
|
|
{ |
|
|
|
// Map generated input mapping to the control
|
|
|
|
control_map->add_mapping(*control, event.mapping); |
|
|
|
} |
|
|
|
|
|
|
|
// Update control mapping text
|
|
|
|
value_text->set_content(this->get_mapping_string(*control_map, *control)); |
|
|
|
game::menu::align_text(ctx); |
|
|
|
game::menu::update_text_tweens(ctx); |
|
|
|
|
|
|
|
/*
|
|
|
|
// Remove keyboard and mouse event mappings from control
|
|
|
|
ctx.input_event_router->remove_mappings(control, input::mapping_type::key); |
|
|
|
ctx.input_event_router->remove_mappings(control, input::mapping_type::mouse_motion); |
|
|
|
ctx.input_event_router->remove_mappings(control, input::mapping_type::mouse_wheel); |
|
|
|
ctx.input_event_router->remove_mappings(control, input::mapping_type::mouse_button); |
|
|
|
// Queue disabling of input mapper re-enabling of menu controls
|
|
|
|
ctx.function_queue.push |
|
|
|
( |
|
|
|
[&ctx]() |
|
|
|
{ |
|
|
|
ctx.input_mapper.disconnect(); |
|
|
|
game::enable_menu_controls(ctx); |
|
|
|
} |
|
|
|
); |
|
|
|
}; |
|
|
|
|
|
|
|
// Callback invoked when the control menu item has been selected
|
|
|
|
auto select_callback = [this, &ctx = this->ctx, control_map = &control_map, control = &control, value_text, input_mapped_callback]() |
|
|
|
{ |
|
|
|
// Set control mapping text to "..."
|
|
|
|
value_text->set_content(get_string(ctx, "control_mapping"_fnv1a32)); |
|
|
|
game::menu::align_text(ctx); |
|
|
|
game::menu::update_text_tweens(ctx); |
|
|
|
|
|
|
|
// Setup input mapped callbacks
|
|
|
|
key_mapped_subscription = ctx.input_mapper.get_key_mapped_channel().subscribe |
|
|
|
( |
|
|
|
input_mapped_callback |
|
|
|
); |
|
|
|
mouse_button_mapped_subscription = ctx.input_mapper.get_mouse_button_mapped_channel().subscribe |
|
|
|
( |
|
|
|
input_mapped_callback |
|
|
|
); |
|
|
|
mouse_scroll_mapped_subscription = ctx.input_mapper.get_mouse_scroll_mapped_channel().subscribe |
|
|
|
( |
|
|
|
input_mapped_callback |
|
|
|
); |
|
|
|
|
|
|
|
// Setup input binding listener
|
|
|
|
ctx.input_listener->set_callback |
|
|
|
// Queue disabling of menu controls and enabling of input mapper
|
|
|
|
ctx.function_queue.push |
|
|
|
( |
|
|
|
[this, &ctx, control, value_text](const event_base& event) |
|
|
|
[&]() |
|
|
|
{ |
|
|
|
auto id = event.get_event_type_id(); |
|
|
|
if (id == key_pressed_event::event_type_id) |
|
|
|
{ |
|
|
|
// Map key pressed event to control
|
|
|
|
const key_pressed_event& key_event = static_cast<const key_pressed_event&>(event); |
|
|
|
|
|
|
|
if (key_event.scancode != input::scancode::escape && key_event.scancode != input::scancode::backspace) |
|
|
|
ctx.input_event_router->add_mapping(input::key_mapping(control, key_event.keyboard, key_event.scancode)); |
|
|
|
} |
|
|
|
else if (id == mouse_wheel_scrolled_event::event_type_id) |
|
|
|
{ |
|
|
|
// Map mouse wheel scrolled event to control
|
|
|
|
const mouse_wheel_scrolled_event& wheel_event = static_cast<const mouse_wheel_scrolled_event&>(event); |
|
|
|
input::mouse_wheel_axis axis; |
|
|
|
|
|
|
|
if (wheel_event.x < 0) |
|
|
|
axis = input::mouse_wheel_axis::negative_x; |
|
|
|
else if (wheel_event.x > 0) |
|
|
|
axis = input::mouse_wheel_axis::positive_x; |
|
|
|
else if (wheel_event.y < 0) |
|
|
|
axis = input::mouse_wheel_axis::negative_y; |
|
|
|
else if (wheel_event.y > 0) |
|
|
|
axis = input::mouse_wheel_axis::positive_y; |
|
|
|
else |
|
|
|
return; |
|
|
|
|
|
|
|
ctx.input_event_router->add_mapping(input::mouse_wheel_mapping(control, wheel_event.mouse, axis)); |
|
|
|
} |
|
|
|
else if (id == mouse_button_pressed_event::event_type_id) |
|
|
|
{ |
|
|
|
// Map mouse button pressed event to control
|
|
|
|
const mouse_button_pressed_event& button_event = static_cast<const mouse_button_pressed_event&>(event); |
|
|
|
ctx.input_event_router->add_mapping(input::mouse_button_mapping(control, button_event.mouse, button_event.button)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// Update menu text
|
|
|
|
value_text->set_content(this->get_binding_string(control)); |
|
|
|
game::menu::align_text(ctx); |
|
|
|
game::menu::update_text_tweens(ctx); |
|
|
|
|
|
|
|
// Disable input listener
|
|
|
|
ctx.input_listener->set_enabled(false); |
|
|
|
ctx.input_listener->set_callback(nullptr); |
|
|
|
|
|
|
|
// Queue menu control setup
|
|
|
|
ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx))); |
|
|
|
game::disable_menu_controls(ctx); |
|
|
|
ctx.input_mapper.connect(ctx.input_manager->get_event_queue()); |
|
|
|
} |
|
|
|
); |
|
|
|
ctx.input_listener->set_enabled(true); |
|
|
|
*/ |
|
|
|
}; |
|
|
|
|
|
|
|
// Register menu item callbacks
|
|
|
|