/*
* Copyright (C) 2023 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see .
*/
#include "game/state/gamepad-config-menu.hpp"
#include "game/state/controls-menu.hpp"
#include "game/context.hpp"
#include "scene/text.hpp"
#include "debug/log.hpp"
#include "resources/resource-manager.hpp"
#include "game/menu.hpp"
#include "game/controls.hpp"
#include "game/strings.hpp"
#include "utility/hash/fnv1a.hpp"
using namespace hash::literals;
namespace game {
namespace state {
gamepad_config_menu::gamepad_config_menu(game::context& ctx):
game::state::base(ctx)
{
debug::log::trace("Entering gamepad 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");
// Construct menu item texts
scene::text* back_text = new scene::text();
// Build list of menu item texts
ctx.menu_item_texts.push_back({back_text, nullptr});
// Set content of menu item texts
back_text->set_content(get_string(ctx, "back"_fnv1a32));
// Init menu item index
game::menu::init_menu_item_index(ctx, "gamepad_config");
game::menu::update_text_color(ctx);
game::menu::update_text_font(ctx);
game::menu::align_text(ctx);
game::menu::update_text_tweens(ctx);
game::menu::add_text_to_ui(ctx);
game::menu::setup_animations(ctx);
// Construct menu item callbacks
auto select_back_callback = [&ctx]()
{
// Disable controls
game::menu::clear_controls(ctx);
game::menu::fade_out
(
ctx,
[&ctx]()
{
// Queue change to controls menu state
ctx.function_queue.push
(
[&ctx]()
{
ctx.state_machine.pop();
ctx.state_machine.emplace(new game::state::controls_menu(ctx));
}
);
}
);
};
// Build list of menu select callbacks
ctx.menu_select_callbacks.push_back(select_back_callback);
// Build list of menu left callbacks
ctx.menu_left_callbacks.push_back(nullptr);
// Build list of menu right callbacks
ctx.menu_right_callbacks.push_back(nullptr);
// Set menu back callback
ctx.menu_back_callback = select_back_callback;
// Queue menu control setup
ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
// Fade in menu
game::menu::fade_in(ctx, nullptr);
debug::log::trace("Entered gamepad config menu state");
}
gamepad_config_menu::~gamepad_config_menu()
{
debug::log::trace("Exiting gamepad config menu state...");
// Destruct menu
game::menu::clear_controls(ctx);
game::menu::clear_callbacks(ctx);
game::menu::delete_animations(ctx);
game::menu::remove_text_from_ui(ctx);
game::menu::delete_text(ctx);
// Save control profile
game::save_control_profile(ctx);
debug::log::trace("Exited gamepad config menu state");
}
std::string gamepad_config_menu::get_binding_string(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;
switch (mapping->get_type())
{
case input::mapping_type::gamepad_axis:
{
const input::gamepad_axis_mapping* axis_mapping = static_cast(mapping);
switch (axis_mapping->axis)
{
case input::gamepad_axis::left_x:
if (axis_mapping->negative)
mapping_string = (*ctx.strings)["gamepad_left_stick_left"];
else
mapping_string = (*ctx.strings)["gamepad_left_stick_right"];
break;
case input::gamepad_axis::left_y:
if (axis_mapping->negative)
mapping_string = (*ctx.strings)["gamepad_left_stick_up"];
else
mapping_string = (*ctx.strings)["gamepad_left_stick_down"];
break;
case input::gamepad_axis::right_x:
if (axis_mapping->negative)
mapping_string = (*ctx.strings)["gamepad_right_stick_left"];
else
mapping_string = (*ctx.strings)["gamepad_right_stick_right"];
break;
case input::gamepad_axis::right_y:
if (axis_mapping->negative)
mapping_string = (*ctx.strings)["gamepad_right_stick_up"];
else
mapping_string = (*ctx.strings)["gamepad_right_stick_down"];
break;
case input::gamepad_axis::left_trigger:
mapping_string = (*ctx.strings)["gamepad_left_trigger"];
break;
case input::gamepad_axis::right_trigger:
mapping_string = (*ctx.strings)["gamepad_right_trigger"];
break;
default:
break;
}
break;
}
case input::mapping_type::gamepad_button:
{
const input::gamepad_button_mapping* button_mapping = static_cast(mapping);
switch (button_mapping->button)
{
case input::gamepad_button::a:
mapping_string = (*ctx.strings)["gamepad_button_a"];
break;
case input::gamepad_button::b:
mapping_string = (*ctx.strings)["gamepad_button_b"];
break;
case input::gamepad_button::x:
mapping_string = (*ctx.strings)["gamepad_button_x"];
break;
case input::gamepad_button::y:
mapping_string = (*ctx.strings)["gamepad_button_y"];
break;
case input::gamepad_button::back:
mapping_string = (*ctx.strings)["gamepad_button_back"];
break;
case input::gamepad_button::guide:
mapping_string = (*ctx.strings)["gamepad_button_guide"];
break;
case input::gamepad_button::start:
mapping_string = (*ctx.strings)["gamepad_button_start"];
break;
case input::gamepad_button::left_stick:
mapping_string = (*ctx.strings)["gamepad_button_left_stick"];
break;
case input::gamepad_button::right_stick:
mapping_string = (*ctx.strings)["gamepad_button_right_stick"];
break;
case input::gamepad_button::left_shoulder:
mapping_string = (*ctx.strings)["gamepad_button_left_shoulder"];
break;
case input::gamepad_button::right_shoulder:
mapping_string = (*ctx.strings)["gamepad_button_right_shoulder"];
break;
case input::gamepad_button::dpad_up:
mapping_string = (*ctx.strings)["gamepad_button_dpad_up"];
break;
case input::gamepad_button::dpad_down:
mapping_string = (*ctx.strings)["gamepad_button_dpad_down"];
break;
case input::gamepad_button::dpad_left:
mapping_string = (*ctx.strings)["gamepad_button_dpad_left"];
break;
case input::gamepad_button::dpad_right:
mapping_string = (*ctx.strings)["gamepad_button_dpad_right"];
break;
default:
break;
}
break;
}
default:
break;
}
if (!mapping_string.empty())
{
if (binding_string.empty())
{
binding_string = mapping_string;
}
else
{
binding_string += " " + mapping_string;
}
}
}
*/
return binding_string;
}
void gamepad_config_menu::add_control_item(const std::string& control_name)
{
// 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();
// 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 content of value text
//value_text->set_content(get_binding_string(control));
auto select_callback = [this, &ctx = this->ctx, value_text]()
{
// Clear binding string from value text
value_text->set_content(get_string(ctx, "ellipsis"_fnv1a32));
game::menu::align_text(ctx);
game::menu::update_text_tweens(ctx);
// Disable controls
game::menu::clear_controls(ctx);
/*
// Remove gamepad event mappings from control
ctx.input_event_router->remove_mappings(control, input::mapping_type::gamepad_axis);
ctx.input_event_router->remove_mappings(control, input::mapping_type::gamepad_button);
// Setup input binding listener
ctx.input_listener->set_callback
(
[this, &ctx, control, value_text](const event_base& event)
{
auto id = event.get_event_type_id();
if (id == gamepad_axis_moved_event::event_type_id)
{
// Map gamepad axis event to control
const gamepad_axis_moved_event& axis_event = static_cast(event);
if (std::abs(axis_event.value) < 0.5f)
return;
ctx.input_event_router->add_mapping(input::gamepad_axis_mapping(control, nullptr, axis_event.axis, (axis_event.value < 0)));
}
else if (id == gamepad_button_pressed_event::event_type_id)
{
// Map gamepad button event to control
const gamepad_button_pressed_event& button_event = static_cast(event);
ctx.input_event_router->add_mapping(input::gamepad_button_mapping(control, nullptr, button_event.button));
}
else if (id == key_pressed_event::event_type_id)
{
// Map key pressed event to control
const key_pressed_event& key_event = static_cast(event);
if (key_event.scancode != input::scancode::escape && key_event.scancode != input::scancode::backspace)
return;
}
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)));
}
);
ctx.input_listener->set_enabled(true);
*/
};
// Register menu item callbacks
ctx.menu_select_callbacks.push_back(select_callback);
ctx.menu_left_callbacks.push_back(nullptr);
ctx.menu_right_callbacks.push_back(nullptr);
}
} // namespace state
} // namespace game