Save State UI: Cache legend strings

This commit is contained in:
Silent 2021-02-22 19:34:51 +01:00
parent e361b9f012
commit 78f9136fb8
No known key found for this signature in database
GPG key ID: AE53149BB0C45AF1
3 changed files with 42 additions and 33 deletions

View file

@ -1292,6 +1292,8 @@ void CommonHostInterface::UpdateHotkeyInputMap(SettingsInterface& si)
AddButtonToInputMap(binding, device, button, hi.handler);
}
}
m_save_state_selector_ui->RefreshHotkeyLegend();
}
bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const std::string_view& device,

View file

@ -1,5 +1,6 @@
#include "save_state_selector_ui.h"
#include "common/log.h"
#include "common/string_util.h"
#include "common/timestamp.h"
#include "core/host_display.h"
#include "core/system.h"
@ -23,6 +24,7 @@ void SaveStateSelectorUI::Open(float open_time /* = DEFAULT_OPEN_TIME */)
m_open = true;
RefreshList();
RefreshHotkeyLegend();
}
void SaveStateSelectorUI::Close()
@ -78,6 +80,32 @@ void SaveStateSelectorUI::RefreshList()
m_current_selection = 0;
}
void SaveStateSelectorUI::RefreshHotkeyLegend()
{
if (!m_open)
return;
auto format_legend_entry = [](std::string_view setting, std::string_view caption) {
auto slash_pos = setting.find_first_of('/');
if (slash_pos != setting.npos)
{
setting = setting.substr(slash_pos + 1);
}
return StringUtil::StdStringFromFormat("%.*s - %.*s", static_cast<int>(setting.size()), setting.data(),
static_cast<int>(caption.size()), caption.data());
};
m_load_legend = format_legend_entry(m_host_interface->GetStringSettingValue("Hotkeys", "LoadSelectedSaveState"),
m_host_interface->TranslateStdString("SaveStateSelectorUI", "Load"));
m_save_legend = format_legend_entry(m_host_interface->GetStringSettingValue("Hotkeys", "SaveSelectedSaveState"),
m_host_interface->TranslateStdString("SaveStateSelectorUI", "Save"));
m_prev_legend = format_legend_entry(m_host_interface->GetStringSettingValue("Hotkeys", "SelectPreviousSaveStateSlot"),
m_host_interface->TranslateStdString("SaveStateSelectorUI", "Select Previous"));
m_next_legend = format_legend_entry(m_host_interface->GetStringSettingValue("Hotkeys", "SelectNextSaveStateSlot"),
m_host_interface->TranslateStdString("SaveStateSelectorUI", "Select Next"));
}
const char* SaveStateSelectorUI::GetSelectedStatePath() const
{
if (m_slots.empty() || m_slots[m_current_selection].path.empty())
@ -256,42 +284,14 @@ void SaveStateSelectorUI::Draw()
ImGui::SetCursorPosX(padding);
ImGui::BeginTable("table", 2);
auto strip_device_name = [](std::string str) {
std::string result;
auto slash_pos = str.find_first_of('/');
if (slash_pos != str.npos)
{
result = str.substr(slash_pos + 1);
}
else
{
result = std::move(str);
}
return result;
};
const std::string load_savestate_hotkey =
strip_device_name(m_host_interface->GetStringSettingValue("Hotkeys", "LoadSelectedSaveState"));
const std::string save_savestate_hotkey =
strip_device_name(m_host_interface->GetStringSettingValue("Hotkeys", "SaveSelectedSaveState"));
const std::string next_savestate_hotkey =
strip_device_name(m_host_interface->GetStringSettingValue("Hotkeys", "SelectNextSaveStateSlot"));
const std::string prev_savestate_hotkey =
strip_device_name(m_host_interface->GetStringSettingValue("Hotkeys", "SelectPreviousSaveStateSlot"));
ImGui::TableNextColumn();
ImGui::Text("%s - %s", load_savestate_hotkey.c_str(),
m_host_interface->TranslateString("SaveStateSelectorUI", "Load").GetCharArray());
ImGui::TextUnformatted(m_load_legend.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s - %s", prev_savestate_hotkey.c_str(),
m_host_interface->TranslateString("SaveStateSelectorUI", "Select Previous").GetCharArray());
ImGui::TextUnformatted(m_prev_legend.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s - %s", save_savestate_hotkey.c_str(),
m_host_interface->TranslateString("SaveStateSelectorUI", "Save").GetCharArray());
ImGui::TextUnformatted(m_save_legend.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s - %s", next_savestate_hotkey.c_str(),
m_host_interface->TranslateString("SaveStateSelectorUI", "Select Next").GetCharArray());
ImGui::TextUnformatted(m_next_legend.c_str());
ImGui::EndTable();
}

View file

@ -1,6 +1,6 @@
#pragma once
#include "common_host_interface.h"
#include "common/timer.h"
#include "common_host_interface.h"
#include <memory>
class HostDisplayTexture;
@ -24,6 +24,8 @@ public:
void ClearList();
void RefreshList();
void RefreshHotkeyLegend();
const char* GetSelectedStatePath() const;
s32 GetSelectedStateSlot() const;
@ -51,6 +53,11 @@ private:
void InitializeListEntry(ListEntry* li, CommonHostInterface::ExtendedSaveStateInfo* ssi);
std::pair<s32, bool> GetSlotTypeFromSelection(u32 selection) const;
std::string m_load_legend;
std::string m_save_legend;
std::string m_prev_legend;
std::string m_next_legend;
CommonHostInterface* m_host_interface;
std::vector<ListEntry> m_slots;
u32 m_current_selection = 0;