Files
isaac-port/src/menu_controller_select.cpp
T

115 lines
4.4 KiB
C++

#include "menu_controller_select.h"
#include "fx_layers.h"
#include "kage_input.h"
#include "manager.h"
#include "menu_manager.h"
constexpr const char* CONTROLLER_SELECT_ANM2 = "gfx/ui/main menu/ControllerSelectMenu.anm2";
constexpr std::string_view NAME_ELLIPSIS = "..";
constexpr size_t NAME_MAX_LENGTH = 18;
constexpr size_t NAME_CUT_LENGTH = 16;
constexpr size_t ENTRY_LIMIT = 6;
constexpr int CURSOR_LAYER = 2;
constexpr int LANGUAGE_ENGLISH = 0;
constexpr float LIST_X = 265;
constexpr float LIST_Y = 60;
constexpr float ROW_HEIGHT = 20;
constexpr float ROW_SLANT = 4;
// Loads the menu, playing, and resets it, at its place on the menu sheet. Inlined in MenuManager::Init.
void Menu_ControllerSelect::Init(Vector menuPosition)
{
LoadGraphics();
reset();
position = menuPosition;
}
// Loads the menu again, playing. Inlined in MenuManager::LoadGraphics.
void Menu_ControllerSelect::LoadGraphics()
{
controllerSelectSprite.Load(CONTROLLER_SELECT_ANM2, GRAPHICS_LOAD);
controllerSelectSprite.Play(ANIMATION_IDLE);
}
// 008c9dd0 Puts the cursor on the first device of an empty list.
void Menu_ControllerSelect::reset()
{
selectedElement = 0;
entries.clear();
holdInputTimer = 0;
}
// Lists the first six devices, each name cut to 16 characters and an ellipsis when it is longer than 18.
void Menu_ControllerSelect::entries_load()
{
KAGE::Input::Manager& input = KAGE::Input::manager;
entries.clear();
for (int i = 0; i < input.get_device_count() && entries.size() < ENTRY_LIMIT; i++)
{
std::shared_ptr<KAGE::Input::InputDevice> device = input.get_device(i);
if (!device) continue;
std::string name = device->name;
if (name.size() > NAME_MAX_LENGTH) name = name.substr(0, NAME_CUT_LENGTH).append(NAME_ELLIPSIS);
entries.push_back({name, device->id});
}
}
// 008c9530 Steps the menu and lists the devices, keeping the cursor on the list. Confirm binds the selected device's
// keys with the hotkeys off, back leaves, and up and down move the cursor, wrapping and repeating while held.
// The original reads before an empty list on confirm. Confirm does nothing then.
void Menu_ControllerSelect::Update()
{
controllerSelectSprite.Update();
entries_load();
int count = (int)entries.size();
if ((size_t)selectedElement >= entries.size()) selectedElement = count - 1;
if (is_action(ACTION_MENU_CONFIRM, ACTION_TRIGGERED))
{
if (count == 0) return;
g_menuControllerIndex = entries[selectedElement].deviceId;
g_menuManager->push_menu(MENU_KEY_CONFIG);
g_manager->isHotkeysEnabled = false;
}
else if (is_action(ACTION_MENU_BACK, ACTION_TRIGGERED)) g_menuManager->pop_menu();
else if (is_action(ACTION_MENU_DOWN, ACTION_PRESSED))
{
if (is_key_repeat(holdInputTimer) && ++selectedElement >= count) selectedElement = 0;
}
else if (is_action(ACTION_MENU_UP, ACTION_PRESSED))
{
if (is_key_repeat(holdInputTimer) && --selectedElement < 0) selectedElement = count - 1;
}
bool isHeld = is_action(ACTION_MENU_UP, ACTION_PRESSED) || is_action(ACTION_MENU_DOWN, ACTION_PRESSED);
holdInputTimer = isHeld ? holdInputTimer + 1 : 0;
}
// 008c99b0 Draws the menu without its cursor, the device names down a slant, then the cursor by the selected one.
// Untranslated text uses the smaller font.
void Menu_ControllerSelect::Render()
{
SpriteDraw draw = g_menuManager->menu_draw_get(position);
Font& font = g_manager->stringTable.language == LANGUAGE_ENGLISH ? g_manager->font1_TeamMeatEx10
: g_manager->font2_TeamMeatEx12;
controllerSelectSprite.GetLayer(CURSOR_LAYER)->isVisible = false;
controllerSelectSprite.Render(draw);
for (size_t row = 0; row < entries.size(); row++)
{
FontSettings settings;
Vector textPosition = vector_make(draw.position.x + LIST_X - (float)row * ROW_SLANT,
draw.position.y + LIST_Y + (float)row * ROW_HEIGHT);
font.DrawString(entries[row].name, textPosition, vector_make(1, 1), kcolor_argb(TEXT_ARGB), settings);
}
SpriteDraw cursor;
cursor.position = vector_make(draw.position.x - (float)selectedElement * ROW_SLANT,
draw.position.y + (float)selectedElement * ROW_HEIGHT);
controllerSelectSprite.GetLayer(CURSOR_LAYER)->isVisible = true;
controllerSelectSprite.RenderLayer(CURSOR_LAYER, cursor);
}