Skip to content

Commit

Permalink
Better progress indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Aug 18, 2024
1 parent c8f44bb commit 3e0e5dc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7489,7 +7489,7 @@ static void frame(void) {
retro_achievements_draw_notifications(left+padding,top+padding,screen_width/se_dpi_scale(),gui_state.settings.only_one_notification);

if (gui_state.settings.draw_progress_indicators)
retro_achievements_draw_progress_indicator(right-padding,top+padding);
retro_achievements_draw_progress_indicator(right-padding,top+padding,screen_width/se_dpi_scale());

if (gui_state.settings.draw_leaderboard_trackers)
retro_achievements_draw_leaderboard_trackers(left+padding,bottom-padding);
Expand Down
117 changes: 67 additions & 50 deletions src/retro_achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct ra_progress_indicator_t
atlas_tile_t* tile = nullptr;
std::string title{};
std::string measured_progress{};
float measured_percent = 0;
bool show = false;
};

Expand Down Expand Up @@ -318,6 +319,7 @@ namespace
{
game_state->progress_indicator.title = std::string("Progress: ") + rc_achievement->title;
game_state->progress_indicator.measured_progress = rc_achievement->measured_progress;
game_state->progress_indicator.measured_percent = rc_achievement->measured_percent;

std::string url;
url.resize(256);
Expand Down Expand Up @@ -1175,7 +1177,7 @@ void retro_achievements_draw_notifications(float left, float top, float screen_w
}
}

void retro_achievements_draw_progress_indicator(float right, float top)
void retro_achievements_draw_progress_indicator(float right, float top, float screen_width)
{
ra_game_state_ptr game_state = ra_state->game_state;

Expand All @@ -1187,61 +1189,76 @@ void retro_achievements_draw_progress_indicator(float right, float top)

ra_progress_indicator_t& indicator = game_state->progress_indicator;

float image_width = 64;
float image_height = 64;
float wrap_width = 200;
float placard_width = padding + image_width + padding + wrap_width + padding;
float x = right - placard_width;
float y = top;
float image_size = screen_width * 0.04f;
float font_size = 0.015f * screen_width;

ImVec2 out;
ImFont* font = igGetFont();
ImFont_CalcTextSizeA(&out, font, 22.0f, std::numeric_limits<float>::max(), wrap_width,
indicator.measured_progress.c_str(), NULL, NULL);
float title_height = out.y;
ImFont_CalcTextSizeA(&out, font, 18.0f, std::numeric_limits<float>::max(), wrap_width,
indicator.measured_progress.c_str(), NULL, NULL);
float progress_height = out.y;

float placard_height = std::max(padding + title_height + padding + progress_height + padding,
padding + image_height + padding);

ImDrawList_AddRectFilled(igGetWindowDrawList(), ImVec2{x, y},
ImVec2{x + placard_width, y + placard_height}, 0x80515151, 8.0f,
ImDrawCornerFlags_All);

ImDrawList_AddRect(
igGetWindowDrawList(), ImVec2{x + (padding / 2), y + (padding / 2)},
ImVec2{x + placard_width - (padding / 2), y + placard_height - (padding / 2)}, 0x80000000,
8.0f, ImDrawCornerFlags_All, 2.0f);
if (font_size < 9.0f) {
font_size = 9.0f;
}

uint32_t id = atlas_get_tile_id(indicator.tile);
if (indicator.tile && id != SG_INVALID_ID)
{
atlas_uvs_t uvs = atlas_get_tile_uvs(indicator.tile);
ImVec2 uv0 = {uvs.x1, uvs.y1};
ImVec2 uv1 = {uvs.x2, uvs.y2};
ImDrawList_AddImageRounded(igGetWindowDrawList(),
(ImTextureID)(intptr_t)id,
ImVec2{x + padding, y + padding},
ImVec2{x + padding + image_width, y + padding + image_height},
uv0, uv1, 0x80ffffff, 8.0f, ImDrawCornerFlags_All);
int numerator, denominator;
bool is_percentage = false;

if (strchr(indicator.measured_progress.c_str(), '/') != NULL) {
int res = sscanf(indicator.measured_progress.c_str(), "%d/%d", &numerator, &denominator);
if (res != 2) {
is_percentage = true;
}
} else {
is_percentage = true;
}
else
{
ImDrawList_AddRectFilled(igGetWindowDrawList(), ImVec2{x + 15, y + 15},
ImVec2{x + padding + image_width, y + padding + image_height},
0x80242424, 8.0f, ImDrawCornerFlags_All);

std::string nums = std::to_string(numerator);
std::string dens = std::to_string(denominator);
ImVec2 numout, denout;
if (!is_percentage) {
ImFont_CalcTextSizeA(&numout, igGetFont(), font_size, std::numeric_limits<float>::max(), 0, nums.c_str(), NULL, NULL);
ImFont_CalcTextSizeA(&denout, igGetFont(), font_size, std::numeric_limits<float>::max(), 0, dens.c_str(), NULL, NULL);

image_size = std::max(std::max(image_size, numout.x + 8), denout.x + 8);
}

ImDrawList_AddTextFontPtr(igGetWindowDrawList(), igGetFont(), 22.0f,
ImVec2{x + padding + image_width + padding, y + padding}, 0xffffffff,
indicator.title.c_str(), NULL, wrap_width, NULL);
uint32_t id = atlas_get_tile_id(indicator.tile);
if (indicator.tile && id != SG_INVALID_ID) {
atlas_uvs_t uvs = atlas_get_tile_uvs(indicator.tile);
ImDrawList_AddImage(igGetWindowDrawList(),
(ImTextureID)(intptr_t)id, ImVec2{right-image_size, top},
ImVec2{right, top+image_size},
ImVec2{uvs.x1, uvs.y1},
ImVec2{uvs.x2, uvs.y2}, 0x80ffffff);
} else {
ImDrawList_AddRectFilled(igGetWindowDrawList(), ImVec2{right-image_size, top},
ImVec2{right, top+image_size}, 0x80242424, 0, 0);
}

ImDrawList_AddTextFontPtr(
igGetWindowDrawList(), igGetFont(), 18.0f,
ImVec2{x + padding + image_width + padding, y + placard_height - padding - progress_height},
0xff00aaaa, indicator.measured_progress.c_str(), NULL, wrap_width, NULL);
if (is_percentage) {
char percentage[8];
snprintf(percentage, sizeof(percentage), "%.2f%%", indicator.measured_percent);
ImVec2 out;
ImFont_CalcTextSizeA(&out, igGetFont(), font_size, std::numeric_limits<float>::max(), 0, percentage, NULL, NULL);
float text_height = out.y;
float start_of_rectangle = top + image_size / 2 - text_height / 2;
float start_of_text = right - image_size / 2 - out.x / 2;
ImDrawList_AddRectFilled(igGetWindowDrawList(), ImVec2{start_of_text - 4, start_of_rectangle - 4},
ImVec2{right, start_of_rectangle + text_height + 4}, 0x80515151, 0, 0);
ImDrawList_AddTextFontPtr(igGetWindowDrawList(), igGetFont(), font_size, ImVec2{start_of_text, start_of_rectangle},
0xffffffff, percentage, NULL, std::numeric_limits<float>::max(), NULL);
} else {
float text_height = numout.y;
float start_of_rectangle = top + image_size / 2 - text_height - 8;
float start_of_text_num = right - image_size / 2 - numout.x / 2;
float start_of_text_den = right - image_size / 2 - denout.x / 2;
float start_of_text_num_y = start_of_rectangle + 4;
float start_of_text_den_y = start_of_rectangle + 4 + text_height + 4;
float start_of_line = start_of_text_den - 2;
float start_of_line_y = start_of_text_num_y + text_height + 2;
ImDrawList_AddTextFontPtr(igGetWindowDrawList(), igGetFont(), font_size, ImVec2{start_of_text_num, start_of_text_num_y},
0xffffffff, nums.c_str(), NULL, std::numeric_limits<float>::max(), NULL);
ImDrawList_AddLine(igGetWindowDrawList(), ImVec2{start_of_line, start_of_line_y},
ImVec2{start_of_line + denout.x + 2, start_of_line_y}, 0xffffffff, 2.0f);
ImDrawList_AddTextFontPtr(igGetWindowDrawList(), igGetFont(), font_size, ImVec2{start_of_text_den, start_of_text_den_y},
0xffffffff, dens.c_str(), NULL, std::numeric_limits<float>::max(), NULL);
}
}

void retro_achievements_draw_leaderboard_trackers(float left, float bottom)
Expand Down
2 changes: 1 addition & 1 deletion src/retro_achievements.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void retro_achievements_keep_alive();

void retro_achievements_draw_notifications(float left, float top, float screen_width, bool only_one_notification);

void retro_achievements_draw_progress_indicator(float right, float top);
void retro_achievements_draw_progress_indicator(float right, float top, float screen_width);

void retro_achievements_draw_leaderboard_trackers(float left, float bottom);

Expand Down

0 comments on commit 3e0e5dc

Please sign in to comment.