Skip to content

Commit

Permalink
gui/home screen: refactor App filtered, moving to string. (Vita3K#3452)
Browse files Browse the repository at this point in the history
- Is better code more cleaner.
  • Loading branch information
Zangetsu38 authored Dec 13, 2024
1 parent 2e202ac commit ad1ba0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
2 changes: 1 addition & 1 deletion vita3k/gui/include/gui/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void browse_live_area_apps_list(GuiState &gui, EmuEnvState &emuenv, const uint32
void browse_pages_manual(GuiState &gui, EmuEnvState &emuenv, const uint32_t button);
void browse_save_data_dialog(GuiState &gui, EmuEnvState &emuenv, const uint32_t button);
void browse_users_management(GuiState &gui, EmuEnvState &emuenv, const uint32_t button);
void close_and_run_new_app(GuiState &gui, EmuEnvState &emuenv, const std::string &app_path);
void close_and_run_new_app(EmuEnvState &emuenv, const std::string &app_path);
void close_live_area_app(GuiState &gui, EmuEnvState &emuenv, const std::string &app_path);
void close_system_app(GuiState &gui, EmuEnvState &emuenv);
void delete_app(GuiState &gui, EmuEnvState &emuenv, const std::string &app_path);
Expand Down
98 changes: 46 additions & 52 deletions vita3k/gui/src/home_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void init_user_apps(GuiState &gui, EmuEnvState &emuenv) {
gui.app_selector.icon_async_loader->quit = true;

std::thread init_apps([&gui, &emuenv]() {
auto app_list_size = gui.app_selector.user_apps.size();
auto apps_list_size = gui.app_selector.user_apps.size();
gui.app_selector.user_apps.clear();
get_user_apps_title(gui, emuenv);

Expand All @@ -57,19 +57,20 @@ void init_user_apps(GuiState &gui, EmuEnvState &emuenv) {
init_last_time_apps(gui, emuenv);
load_and_update_compat_user_apps(gui, emuenv);

const auto new_apps_list_size = gui.app_selector.user_apps.size();
init_apps_icon(gui, emuenv, gui.app_selector.user_apps);

if (app_list_size == gui.app_selector.user_apps.size())
if (apps_list_size == new_apps_list_size)
return false;

std::string change_app_list = "new application(s) added";
if (app_list_size > gui.app_selector.user_apps.size()) {
if (apps_list_size > new_apps_list_size) {
change_app_list = "application(s) removed";
app_list_size -= gui.app_selector.user_apps.size();
apps_list_size -= new_apps_list_size;
} else
app_list_size = gui.app_selector.user_apps.size() - app_list_size;
apps_list_size = new_apps_list_size - apps_list_size;

LOG_INFO("{} {}", app_list_size, change_app_list);
LOG_INFO("{} {}", apps_list_size, change_app_list);

return true;
});
Expand Down Expand Up @@ -215,7 +216,7 @@ void close_system_app(GuiState &gui, EmuEnvState &emuenv) {
}
}

void close_and_run_new_app(GuiState &gui, EmuEnvState &emuenv, const std::string &app_path) {
void close_and_run_new_app(EmuEnvState &emuenv, const std::string &app_path) {
emuenv.kernel.exit_delete_all_threads();
emuenv.load_app_path = app_path;
emuenv.load_exec = true;
Expand Down Expand Up @@ -258,7 +259,7 @@ void draw_app_close(GuiState &gui, EmuEnvState &emuenv) {
ImGui::SameLine(0, 20.f * SCALE.x);
if (ImGui::Button(common["ok"].c_str(), BUTTON_SIZE)) {
const auto &app_path = gui.vita_area.live_area_screen ? gui.live_area_current_open_apps_list[gui.live_area_app_current_open] : emuenv.app_path;
close_and_run_new_app(gui, emuenv, app_path);
close_and_run_new_app(emuenv, app_path);
}
ImGui::PopStyleVar();
ImGui::EndChild();
Expand Down Expand Up @@ -419,10 +420,9 @@ static std::string get_label_name(GuiState &gui, const SortType &type) {
return label;
}

static constexpr int32_t INVALID_APP_INDEX = -5;
static int32_t first_visible_app_index = INVALID_APP_INDEX, current_selected_app_index = INVALID_APP_INDEX;
static std::string first_visible_app, current_selected_app;

static std::vector<int32_t> apps_list_filtered;
static std::vector<std::string> apps_list_filtered;
void browse_home_apps_list(GuiState &gui, EmuEnvState &emuenv, const uint32_t button) {
if (apps_list_filtered.empty())
return;
Expand All @@ -432,23 +432,23 @@ void browse_home_apps_list(GuiState &gui, EmuEnvState &emuenv, const uint32_t bu
gui.is_nav_button = true;

// When the current selected app index have not any selected, set it to the first visible app index
if (current_selected_app_index <= INVALID_APP_INDEX)
current_selected_app_index = first_visible_app_index;
if (current_selected_app.empty())
current_selected_app = first_visible_app;

return;
}

const auto apps_list_filtered_size = static_cast<int32_t>(apps_list_filtered.size() - 1);

// Find current selected app index in apps list filtered
const int32_t filtered_index = vector_utils::find_index(apps_list_filtered, current_selected_app_index);
if (filtered_index == -1) {
current_selected_app_index = first_visible_app_index;
const int32_t current_selected_app_index = vector_utils::find_index(apps_list_filtered, current_selected_app);
if (current_selected_app_index == -1) {
current_selected_app = first_visible_app;
return;
}

const auto prev_filtered_index = apps_list_filtered[std::max(filtered_index - 1, 0)];
const auto next_filtered_index = apps_list_filtered[std::min(filtered_index + 1, apps_list_filtered_size)];
const auto prev_filtered_app = apps_list_filtered[std::max(current_selected_app_index - 1, 0)];
const auto next_filtered_app = apps_list_filtered[std::min(current_selected_app_index + 1, apps_list_filtered_size)];

const auto switch_to_first_live_area_open_app = [&gui]() {
if (!gui.live_area_current_open_apps_list.empty()) {
Expand All @@ -461,53 +461,49 @@ void browse_home_apps_list(GuiState &gui, EmuEnvState &emuenv, const uint32_t bu
switch (button) {
case SCE_CTRL_UP:
if (emuenv.cfg.apps_list_grid) {
if (filtered_index >= 4)
current_selected_app_index = apps_list_filtered[filtered_index - 4];
if (current_selected_app_index >= 4)
current_selected_app = apps_list_filtered[current_selected_app_index - 4];
} else
current_selected_app_index = prev_filtered_index;
current_selected_app = prev_filtered_app;
break;
case SCE_CTRL_RIGHT:
if (emuenv.cfg.apps_list_grid) {
if (((filtered_index + 1) % 4 == 0) || (filtered_index == apps_list_filtered_size))
if (((current_selected_app_index + 1) % 4 == 0) || (current_selected_app_index == apps_list_filtered_size))
switch_to_first_live_area_open_app();
else
current_selected_app_index = next_filtered_index;
current_selected_app = next_filtered_app;
} else
switch_to_first_live_area_open_app();
break;
case SCE_CTRL_DOWN:
if (emuenv.cfg.apps_list_grid) {
if ((filtered_index + 4) <= apps_list_filtered_size)
current_selected_app_index = apps_list_filtered[filtered_index + 4];
if ((current_selected_app_index + 4) <= apps_list_filtered_size)
current_selected_app = apps_list_filtered[current_selected_app_index + 4];
} else
current_selected_app_index = next_filtered_index;
current_selected_app = next_filtered_app;
break;
case SCE_CTRL_LEFT:
if (emuenv.cfg.apps_list_grid && (filtered_index % 4 != 0))
current_selected_app_index = prev_filtered_index;
if (emuenv.cfg.apps_list_grid && (current_selected_app_index % 4 != 0))
current_selected_app = prev_filtered_app;
break;
case SCE_CTRL_R1:
gui.live_area_app_current_open = std::min(gui.live_area_app_current_open + 1, static_cast<int32_t>(gui.live_area_current_open_apps_list.size() - 1));
gui.vita_area.live_area_screen = gui.live_area_app_current_open >= 0;
gui.vita_area.home_screen = !gui.vita_area.live_area_screen;
break;
case SCE_CTRL_CIRCLE:
if (emuenv.cfg.sys_button == 0) {
const auto &selected_app = current_selected_app_index < 0 ? gui.app_selector.sys_apps[current_selected_app_index + 4] : gui.app_selector.user_apps[current_selected_app_index];
pre_load_app(gui, emuenv, emuenv.cfg.show_live_area_screen, selected_app.path);
}
if (emuenv.cfg.sys_button == 0)
pre_load_app(gui, emuenv, emuenv.cfg.show_live_area_screen, current_selected_app);
break;
case SCE_CTRL_CROSS:
if (emuenv.cfg.sys_button == 1) {
const auto &selected_app = current_selected_app_index < 0 ? gui.app_selector.sys_apps[current_selected_app_index + 4] : gui.app_selector.user_apps[current_selected_app_index];
pre_load_app(gui, emuenv, emuenv.cfg.show_live_area_screen, selected_app.path);
}
if (emuenv.cfg.sys_button == 1)
pre_load_app(gui, emuenv, emuenv.cfg.show_live_area_screen, current_selected_app);
break;
default: break;
}
}

static int selected_app_index = INVALID_APP_INDEX;
static std::string selected_app;
void select_app(GuiState &gui, const std::string &title_id) {
// Find the app in the user apps list
auto app_it = std::find_if(gui.app_selector.user_apps.begin(), gui.app_selector.user_apps.end(), [&](const App &app) {
Expand All @@ -516,8 +512,8 @@ void select_app(GuiState &gui, const std::string &title_id) {

// Check if the app was found
if (app_it != gui.app_selector.user_apps.end()) {
// Set the selected app index
current_selected_app_index = selected_app_index = std::distance(gui.app_selector.user_apps.begin(), app_it);
// Set the selected app
current_selected_app = selected_app = title_id;
gui.is_nav_button = true;
} else
LOG_ERROR("App with title id {} not found", title_id);
Expand Down Expand Up @@ -771,7 +767,7 @@ void draw_home_screen(GuiState &gui, EmuEnvState &emuenv) {
ImGui::SetWindowFontScale(1.1f);
ImGui::PushStyleColor(ImGuiCol_Text, GUI_COLOR_TEXT);

std::vector<int32_t> visible_apps{};
std::vector<std::string> visible_apps{};

const auto display_app = [&](const std::vector<gui::App> &apps_list, std::map<std::string, ImGui_Texture> &apps_icon) {
for (const auto &app : apps_list) {
Expand Down Expand Up @@ -801,15 +797,13 @@ void draw_home_screen(GuiState &gui, EmuEnvState &emuenv) {
ImGui::SetCursorPosX(GRID_ICON_POS);

// Get the current app index off the apps list.
const auto app_index = static_cast<int>(&app - apps_list.data());
const auto current_app_index = !is_sys ? app_index : app_index - 4;
apps_list_filtered.push_back(current_app_index);
apps_list_filtered.push_back(app.path);

// Check if the current app is selected.
const auto is_current_app_selected = gui.is_nav_button && (current_selected_app_index == current_app_index);
const auto is_current_app_selected = gui.is_nav_button && (current_selected_app == app.path);
const auto icon_flags = emuenv.cfg.apps_list_grid ? ImGuiSelectableFlags_None : ImGuiSelectableFlags_SpanAllColumns;
if (ImGui::Selectable("##icon", is_current_app_selected || (selected_app_index == current_app_index), icon_flags, SELECTABLE_APP_SIZE)) {
selected_app_index = INVALID_APP_INDEX;
if (ImGui::Selectable("##icon", is_current_app_selected || (selected_app == app.path), icon_flags, SELECTABLE_APP_SIZE)) {
selected_app.clear();
pre_load_app(gui, emuenv, emuenv.cfg.show_live_area_screen, app.path);
}

Expand Down Expand Up @@ -848,11 +842,11 @@ void draw_home_screen(GuiState &gui, EmuEnvState &emuenv) {
// Draw the app icons and custom config button only when they are within the visible area.
if (element_is_within_visible_area) {
// Add the current app index to the visible apps list.
visible_apps.push_back(current_app_index);
visible_apps.push_back(app.path);

// Set the current selected app index to the current app index when the app is hovered.
if (!gui.is_nav_button && ImGui::IsItemHovered())
current_selected_app_index = current_app_index;
current_selected_app = app.path;

// Draw the app icon
if (apps_icon.contains(app.path)) {
Expand All @@ -875,9 +869,9 @@ void draw_home_screen(GuiState &gui, EmuEnvState &emuenv) {
ImGui::Button("CC", ImVec2(40.f * VIEWPORT_SCALE.x, 0.f));
ImGui::PopStyleColor();
}
} else if (!gui.is_nav_button && (current_selected_app_index == current_app_index)) {
} else if (!gui.is_nav_button && (current_selected_app == app.path)) {
// When the app is selected but not visible, reset the current selected app index.
current_selected_app_index = INVALID_APP_INDEX;
current_selected_app.clear();
}

if (!emuenv.cfg.apps_list_grid)
Expand Down Expand Up @@ -946,9 +940,9 @@ void draw_home_screen(GuiState &gui, EmuEnvState &emuenv) {
ImGui::SetWindowFontScale(1.f);
ImGui::EndChild();

// When visible apps list is not empty, set first visible app index to 0
// When visible apps list is not empty, set first visible app
if (!visible_apps.empty())
first_visible_app_index = visible_apps.front();
first_visible_app = visible_apps.front();

const auto SELECTABLE_SIZE = ImVec2(50.f * VIEWPORT_SCALE.x, 60.f * VIEWPORT_SCALE.y);

Expand Down
2 changes: 1 addition & 1 deletion vita3k/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ bool handle_events(EmuEnvState &emuenv, GuiState &gui) {
};
const auto confirm = [&gui, &emuenv]() {
const auto app_path = gui.vita_area.live_area_screen ? gui.live_area_current_open_apps_list[gui.live_area_app_current_open] : emuenv.app_path;
gui::close_and_run_new_app(gui, emuenv, app_path);
gui::close_and_run_new_app(emuenv, app_path);
};
switch (sce_ctrl_btn) {
case SCE_CTRL_CIRCLE:
Expand Down

0 comments on commit ad1ba0c

Please sign in to comment.