InputCommon: fix dynamic input textures being not generated when the key or device isn't mapped

This commit is contained in:
iwubcode 2020-12-05 13:50:11 -06:00
parent a34823df61
commit 39e78ce873
1 changed files with 28 additions and 8 deletions

View File

@ -300,24 +300,44 @@ bool DynamicInputTextureConfiguration::GenerateTexture(
} }
} }
// Load image copy // Two copies of the loaded texture
auto base_image = LoadImage(m_base_path + texture_data.m_image_name); // The first one is used as a fallback if a key or device isn't mapped
bool dirty = false; // the second one is used as the final image to write to the textures directory
const auto original_image = LoadImage(m_base_path + texture_data.m_image_name);
auto image_to_write = original_image;
bool dirty = false;
for (auto& [emulated_key, rects] : emulated_controls_iter->second) for (auto& [emulated_key, rects] : emulated_controls_iter->second)
{ {
std::string host_key = ""; // TODO: Remove this line when we move to C++20
sec->Get(emulated_key, &host_key); auto& rects_ref = rects;
auto apply_original = [&] {
for (const auto& rect : rects_ref)
{
CopyImageRegion(*original_image, *image_to_write, rect, rect);
dirty = true;
}
};
if (!device_found) if (!device_found)
{ {
// If we get here, that means the controller is set to a // If we get here, that means the controller is set to a
// device not exposed to the pack // device not exposed to the pack
// We still apply the original image, in case the user
// switched devices and wants to see the changes
apply_original();
continue; continue;
} }
std::string host_key;
sec->Get(emulated_key, &host_key);
const auto input_image_iter = host_devices_iter->second.find(host_key); const auto input_image_iter = host_devices_iter->second.find(host_key);
if (input_image_iter != host_devices_iter->second.end()) if (input_image_iter == host_devices_iter->second.end())
{
apply_original();
}
else
{ {
const auto host_key_image = LoadImage(m_base_path + input_image_iter->second); const auto host_key_image = LoadImage(m_base_path + input_image_iter->second);
@ -339,7 +359,7 @@ bool DynamicInputTextureConfiguration::GenerateTexture(
Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight()); Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight());
} }
CopyImageRegion(pixel_data, *base_image, Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, CopyImageRegion(pixel_data, *image_to_write, Rect{0, 0, rect.GetWidth(), rect.GetHeight()},
rect); rect);
dirty = true; dirty = true;
} }
@ -355,7 +375,7 @@ bool DynamicInputTextureConfiguration::GenerateTexture(
{ {
File::CreateDir(hi_res_folder); File::CreateDir(hi_res_folder);
} }
WriteImage(hi_res_folder + DIR_SEP + texture_data.m_hires_texture_name, *base_image); WriteImage(hi_res_folder + DIR_SEP + texture_data.m_hires_texture_name, *image_to_write);
const auto game_id_folder = hi_res_folder + DIR_SEP + "gameids"; const auto game_id_folder = hi_res_folder + DIR_SEP + "gameids";
if (!File::IsDirectory(game_id_folder)) if (!File::IsDirectory(game_id_folder))