Browse Source

Add support for loading mouse motion bindings

master
C. J. Howard 3 years ago
parent
commit
3c78e5a99b
6 changed files with 86 additions and 176 deletions
  1. +0
    -5
      src/game/bootloader.cpp
  2. +0
    -1
      src/game/context.hpp
  3. +4
    -4
      src/game/states/brood.cpp
  4. +26
    -24
      src/game/states/forage.cpp
  5. +55
    -141
      src/game/states/loading.cpp
  6. +1
    -1
      src/resources/json-loader.cpp

+ 0
- 5
src/game/bootloader.cpp View File

@ -171,7 +171,6 @@ void parse_options(game::context* ctx, int argc, char** argv)
{ {
cxxopts::Options options("Antkeeper", "Ant colony simulation game"); cxxopts::Options options("Antkeeper", "Ant colony simulation game");
options.add_options() options.add_options()
("b,biome", "Selects the biome to load", cxxopts::value<std::string>())
("c,continue", "Continues from the last save") ("c,continue", "Continues from the last save")
("d,data", "Sets the data package path", cxxopts::value<std::string>()) ("d,data", "Sets the data package path", cxxopts::value<std::string>())
("f,fullscreen", "Starts in fullscreen mode") ("f,fullscreen", "Starts in fullscreen mode")
@ -182,10 +181,6 @@ void parse_options(game::context* ctx, int argc, char** argv)
("w,windowed", "Starts in windowed mode"); ("w,windowed", "Starts in windowed mode");
auto result = options.parse(argc, argv); auto result = options.parse(argc, argv);
// --biome
if (result.count("biome"))
ctx->option_biome = result["biome"].as<std::string>();
// --continue // --continue
if (result.count("continue")) if (result.count("continue"))
ctx->option_continue = true; ctx->option_continue = true;

+ 0
- 1
src/game/context.hpp View File

@ -111,7 +111,6 @@ struct context
std::ofstream log_filestream; std::ofstream log_filestream;
// Command-line options // Command-line options
std::optional<std::string> option_biome;
std::optional<bool> option_continue; std::optional<bool> option_continue;
std::optional<std::string> option_data; std::optional<std::string> option_data;
std::optional<bool> option_fullscreen; std::optional<bool> option_fullscreen;

+ 4
- 4
src/game/states/brood.cpp View File

@ -340,7 +340,7 @@ void setup_controls(game::context* ctx)
); );
// Pan left // Pan left
ctx->controls["mouse_left"]->set_active_callback
ctx->controls["pan_left_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, pan_speed, mouse_rotate](float value) [ctx, three_dof_eid, pan_speed, mouse_rotate](float value)
{ {
@ -353,7 +353,7 @@ void setup_controls(game::context* ctx)
); );
// Pan right // Pan right
ctx->controls["mouse_right"]->set_active_callback
ctx->controls["pan_right_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, pan_speed, mouse_rotate](float value) [ctx, three_dof_eid, pan_speed, mouse_rotate](float value)
{ {
@ -366,7 +366,7 @@ void setup_controls(game::context* ctx)
); );
// Tilt up // Tilt up
ctx->controls["mouse_up"]->set_active_callback
ctx->controls["tilt_up_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, tilt_speed, mouse_rotate](float value) [ctx, three_dof_eid, tilt_speed, mouse_rotate](float value)
{ {
@ -380,7 +380,7 @@ void setup_controls(game::context* ctx)
); );
// Tilt down // Tilt down
ctx->controls["mouse_down"]->set_active_callback
ctx->controls["tilt_down_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, tilt_speed, mouse_rotate](float value) [ctx, three_dof_eid, tilt_speed, mouse_rotate](float value)
{ {

+ 26
- 24
src/game/states/forage.cpp View File

@ -271,11 +271,13 @@ void setup_controls(game::context* ctx)
const float dolly_speed = 20.0f; const float dolly_speed = 20.0f;
const float truck_speed = dolly_speed; const float truck_speed = dolly_speed;
const float pedestal_speed = 30.0f; const float pedestal_speed = 30.0f;
const float pan_speed_mouse = math::radians(8.0f);
const float tilt_speed_mouse = pan_speed_mouse;
float mouse_sensitivity = 1.0f;
float gamepad_sensitivity = 1.0f;
const float pan_speed = math::radians(110.0f);
const float tilt_speed = pan_speed;
if (ctx->config->contains("mouse_sensitivity"))
mouse_sensitivity = math::radians((*ctx->config)["mouse_sensitivity"].get<float>());
if (ctx->config->contains("gamepad_sensitivity"))
gamepad_sensitivity = math::radians((*ctx->config)["gamepad_sensitivity"].get<float>());
const input::control* move_slow = ctx->controls["move_slow"]; const input::control* move_slow = ctx->controls["move_slow"];
const input::control* move_fast = ctx->controls["move_fast"]; const input::control* move_fast = ctx->controls["move_fast"];
@ -399,89 +401,89 @@ void setup_controls(game::context* ctx)
); );
// Pan left // Pan left
ctx->controls["pan_left"]->set_active_callback
ctx->controls["pan_left_gamepad"]->set_active_callback
( (
[ctx, three_dof_eid, pan_speed](float value)
[ctx, three_dof_eid, gamepad_sensitivity](float value)
{ {
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.yaw += pan_speed * value * (1.0f / 60.0f);
three_dof.yaw += gamepad_sensitivity * value * (1.0f / 60.0f);
} }
); );
ctx->controls["pan_left_mouse"]->set_active_callback ctx->controls["pan_left_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, pan_speed_mouse, mouse_rotate](float value)
[ctx, three_dof_eid, mouse_sensitivity, mouse_rotate](float value)
{ {
if (!mouse_rotate->is_active()) if (!mouse_rotate->is_active())
return; return;
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.yaw += pan_speed_mouse * value * (1.0f / 60.0f);
three_dof.yaw += mouse_sensitivity * value * (1.0f / 60.0f);
} }
); );
// Pan right // Pan right
ctx->controls["pan_right"]->set_active_callback
ctx->controls["pan_right_gamepad"]->set_active_callback
( (
[ctx, three_dof_eid, pan_speed](float value)
[ctx, three_dof_eid, gamepad_sensitivity](float value)
{ {
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.yaw -= pan_speed * value * (1.0f / 60.0f);
three_dof.yaw -= gamepad_sensitivity * value * (1.0f / 60.0f);
} }
); );
ctx->controls["pan_right_mouse"]->set_active_callback ctx->controls["pan_right_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, pan_speed_mouse, mouse_rotate](float value)
[ctx, three_dof_eid, mouse_sensitivity, mouse_rotate](float value)
{ {
if (!mouse_rotate->is_active()) if (!mouse_rotate->is_active())
return; return;
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.yaw -= pan_speed_mouse * value * (1.0f / 60.0f);
three_dof.yaw -= mouse_sensitivity * value * (1.0f / 60.0f);
} }
); );
// Tilt up // Tilt up
ctx->controls["tilt_up"]->set_active_callback
ctx->controls["tilt_up_gamepad"]->set_active_callback
( (
[ctx, three_dof_eid, tilt_speed](float value)
[ctx, three_dof_eid, gamepad_sensitivity](float value)
{ {
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.pitch -= tilt_speed * value * (1.0f / 60.0f);
three_dof.pitch -= gamepad_sensitivity * value * (1.0f / 60.0f);
three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch); three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
} }
); );
ctx->controls["tilt_up_mouse"]->set_active_callback ctx->controls["tilt_up_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, tilt_speed_mouse, mouse_rotate](float value)
[ctx, three_dof_eid, mouse_sensitivity, mouse_rotate](float value)
{ {
if (!mouse_rotate->is_active()) if (!mouse_rotate->is_active())
return; return;
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.pitch -= tilt_speed_mouse * value * (1.0f / 60.0f);
three_dof.pitch -= mouse_sensitivity * value * (1.0f / 60.0f);
three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch); three_dof.pitch = std::max<float>(math::radians(-90.0f), three_dof.pitch);
} }
); );
// Tilt down // Tilt down
ctx->controls["tilt_down"]->set_active_callback
ctx->controls["tilt_down_gamepad"]->set_active_callback
( (
[ctx, three_dof_eid, tilt_speed](float value)
[ctx, three_dof_eid, gamepad_sensitivity](float value)
{ {
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.pitch += tilt_speed * value * (1.0f / 60.0f);
three_dof.pitch += gamepad_sensitivity * value * (1.0f / 60.0f);
three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch); three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
} }
); );
ctx->controls["tilt_down_mouse"]->set_active_callback ctx->controls["tilt_down_mouse"]->set_active_callback
( (
[ctx, three_dof_eid, tilt_speed_mouse, mouse_rotate](float value)
[ctx, three_dof_eid, mouse_sensitivity, mouse_rotate](float value)
{ {
if (!mouse_rotate->is_active()) if (!mouse_rotate->is_active())
return; return;
auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid); auto& three_dof = ctx->entity_registry->get<entity::component::constraint::three_dof>(three_dof_eid);
three_dof.pitch += tilt_speed_mouse * value * (1.0f / 60.0f);
three_dof.pitch += mouse_sensitivity * value * (1.0f / 60.0f);
three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch); three_dof.pitch = std::min<float>(math::radians(90.0f), three_dof.pitch);
} }
); );

+ 55
- 141
src/game/states/loading.cpp View File

@ -125,6 +125,29 @@ void exit(game::context* ctx)
void load_controls(game::context* ctx) void load_controls(game::context* ctx)
{ {
// Allocate known controls
ctx->controls["toggle_fullscreen"] = new input::control();
ctx->controls["screenshot"] = new input::control();
ctx->controls["menu_back"] = new input::control();
ctx->controls["dolly_forward"] = new input::control();
ctx->controls["dolly_backward"] = new input::control();
ctx->controls["truck_left"] = new input::control();
ctx->controls["truck_right"] = new input::control();
ctx->controls["pedestal_up"] = new input::control();
ctx->controls["pedestal_down"] = new input::control();
ctx->controls["move_slow"] = new input::control();
ctx->controls["move_fast"] = new input::control();
ctx->controls["mouse_rotate"] = new input::control();
ctx->controls["pan_left_gamepad"] = new input::control();
ctx->controls["pan_left_mouse"] = new input::control();
ctx->controls["pan_right_gamepad"] = new input::control();
ctx->controls["pan_right_mouse"] = new input::control();
ctx->controls["tilt_up_gamepad"] = new input::control();
ctx->controls["tilt_up_mouse"] = new input::control();
ctx->controls["tilt_down_gamepad"] = new input::control();
ctx->controls["tilt_down_mouse"] = new input::control();
ctx->controls["use_tool"] = new input::control();
// Get keyboard and mouse devices // Get keyboard and mouse devices
input::keyboard* keyboard = ctx->app->get_keyboard(); input::keyboard* keyboard = ctx->app->get_keyboard();
input::mouse* mouse = ctx->app->get_mouse(); input::mouse* mouse = ctx->app->get_mouse();
@ -184,7 +207,6 @@ void load_controls(game::context* ctx)
else else
{ {
control = new input::control; control = new input::control;
control->set_deadzone(0.15f);
ctx->controls[name] = control; ctx->controls[name] = control;
} }
@ -236,13 +258,13 @@ void load_controls(game::context* ctx)
// Parse mouse wheel axis // Parse mouse wheel axis
std::string wheel = (*it)["wheel"].get<std::string>(); std::string wheel = (*it)["wheel"].get<std::string>();
input::mouse_wheel_axis axis; input::mouse_wheel_axis axis;
if (wheel == "+x")
if (wheel == "x+")
axis = input::mouse_wheel_axis::positive_x; axis = input::mouse_wheel_axis::positive_x;
else if (wheel == "-x")
else if (wheel == "x-")
axis = input::mouse_wheel_axis::negative_x; axis = input::mouse_wheel_axis::negative_x;
else if (wheel == "+y")
else if (wheel == "y+")
axis = input::mouse_wheel_axis::positive_y; axis = input::mouse_wheel_axis::positive_y;
else if (wheel == "-y")
else if (wheel == "y-")
axis = input::mouse_wheel_axis::negative_y; axis = input::mouse_wheel_axis::negative_y;
else else
{ {
@ -255,6 +277,29 @@ void load_controls(game::context* ctx)
ctx->logger->log("Mapped control \"" + name + "\" to mouse wheel axis " + wheel); ctx->logger->log("Mapped control \"" + name + "\" to mouse wheel axis " + wheel);
} }
else if (it->contains("motion"))
{
std::string motion = (*it)["motion"].get<std::string>();
input::mouse_motion_axis axis;
if (motion == "x+")
axis = input::mouse_motion_axis::positive_x;
else if (motion == "x-")
axis = input::mouse_motion_axis::negative_x;
else if (motion == "y+")
axis = input::mouse_motion_axis::positive_y;
else if (motion == "y-")
axis = input::mouse_motion_axis::negative_y;
else
{
ctx->logger->warning("Control \"" + name + "\" is mapped to invalid mouse motion axis \"" + motion + "\"");
continue;
}
// Map control to mouse motion axis
ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, axis));
ctx->logger->log("Mapped control \"" + name + "\" to mouse motion axis " + motion);
}
else else
{ {
ctx->logger->warning("Control \"" + name + "\" has invalid mouse mapping"); ctx->logger->warning("Control \"" + name + "\" has invalid mouse mapping");
@ -320,12 +365,13 @@ void load_controls(game::context* ctx)
} }
} }
// Toggle fullscreen
if (!ctx->controls.count("toggle_fullscreen"))
// Set all control deadzones to 0.15
for (auto control: ctx->controls)
{ {
input::control* control = new input::control();
ctx->controls["toggle_fullscreen"] = control;
control.second->set_deadzone(0.15f);
} }
// Toggle fullscreen
ctx->controls["toggle_fullscreen"]->set_activated_callback ctx->controls["toggle_fullscreen"]->set_activated_callback
( (
[ctx]() [ctx]()
@ -348,11 +394,6 @@ void load_controls(game::context* ctx)
); );
// Screenshot // Screenshot
if (!ctx->controls.count("screenshot"))
{
input::control* control = new input::control();
ctx->controls["screenshot"] = control;
}
ctx->controls["screenshot"]->set_activated_callback ctx->controls["screenshot"]->set_activated_callback
( (
[ctx]() [ctx]()
@ -363,137 +404,10 @@ void load_controls(game::context* ctx)
); );
// Menu back // Menu back
if (!ctx->controls.count("menu_back"))
{
input::control* control = new input::control();
ctx->controls["menu_back"] = control;
}
ctx->controls["menu_back"]->set_activated_callback ctx->controls["menu_back"]->set_activated_callback
( (
std::bind(&application::close, ctx->app, 0) std::bind(&application::close, ctx->app, 0)
); );
// Dolly forward
if (!ctx->controls.count("dolly_forward"))
{
input::control* control = new input::control();
ctx->controls["dolly_forward"] = control;
}
// Dolly backward
if (!ctx->controls.count("dolly_backward"))
{
input::control* control = new input::control();
ctx->controls["dolly_backward"] = control;
}
// Truck left
if (!ctx->controls.count("truck_left"))
{
input::control* control = new input::control();
ctx->controls["truck_left"] = control;
}
// Truck right
if (!ctx->controls.count("truck_right"))
{
input::control* control = new input::control();
ctx->controls["truck_right"] = control;
}
// Pedestal up
if (!ctx->controls.count("pedestal_up"))
{
input::control* control = new input::control();
ctx->controls["pedestal_up"] = control;
}
// Pedestal down
if (!ctx->controls.count("pedestal_down"))
{
input::control* control = new input::control();
ctx->controls["pedestal_down"] = control;
}
// Move slow
if (!ctx->controls.count("move_slow"))
{
input::control* control = new input::control();
ctx->controls["move_slow"] = control;
}
// Move fast
if (!ctx->controls.count("move_fast"))
{
input::control* control = new input::control();
ctx->controls["move_fast"] = control;
}
// Mouse rotate
if (!ctx->controls.count("mouse_rotate"))
{
input::control* control = new input::control();
ctx->controls["mouse_rotate"] = control;
}
// Pan left
if (!ctx->controls.count("pan_left"))
{
input::control* control = new input::control();
ctx->controls["pan_left"] = control;
}
if (!ctx->controls.count("pan_left_mouse"))
{
input::control* control = new input::control();
ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::negative_x));
ctx->controls["pan_left_mouse"] = control;
}
// Pan right
if (!ctx->controls.count("pan_right"))
{
input::control* control = new input::control();
ctx->controls["pan_right"] = control;
}
if (!ctx->controls.count("pan_right_mouse"))
{
input::control* control = new input::control();
ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::positive_x));
ctx->controls["pan_right_mouse"] = control;
}
// Tilt up
if (!ctx->controls.count("tilt_up"))
{
input::control* control = new input::control();
ctx->controls["tilt_up"] = control;
}
if (!ctx->controls.count("tilt_up_mouse"))
{
input::control* control = new input::control();
ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::negative_y));
ctx->controls["tilt_up_mouse"] = control;
}
// Tilt down
if (!ctx->controls.count("tilt_down"))
{
input::control* control = new input::control();
ctx->controls["tilt_down"] = control;
}
if (!ctx->controls.count("tilt_down_mouse"))
{
input::control* control = new input::control();
ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::positive_y));
ctx->controls["tilt_down_mouse"] = control;
}
// Use tool
if (!ctx->controls.count("use_tool"))
{
input::control* control = new input::control();
ctx->controls["use_tool"] = control;
}
} }
void cosmogenesis(game::context* ctx) void cosmogenesis(game::context* ctx)

+ 1
- 1
src/resources/json-loader.cpp View File

@ -32,7 +32,7 @@ json* resource_loader::load(resource_manager* resource_manager, PHYSFS_Fil
PHYSFS_readBytes(file, &buffer[0], size); PHYSFS_readBytes(file, &buffer[0], size);
// Parse json from file buffer // Parse json from file buffer
json* data = new json(json::parse(buffer));
json* data = new json(json::parse(buffer, nullptr, true, true));
return data; return data;
} }

Loading…
Cancel
Save