refactor(PlayerList): height calculation code +horizontal scroll fix (#1629)

Fixes #752
This commit is contained in:
Andreas Maerten 2023-07-06 20:35:42 +02:00 committed by GitHub
parent c571f5a1e7
commit bae5d3adfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@
namespace big
{
bool has_scrollbar = false;
static void player_button(const player_ptr& plyr)
{
bool selected_player = plyr == g_player_service->get_selected();
@ -47,7 +48,11 @@ namespace big
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, {0.0, 0.5});
ImGui::PushID(plyr->id());
if (ImGui::Button(plyr->get_name(), {300.0f - ImGui::GetStyle().ScrollbarSize, 0.f}))
const auto style = ImGui::GetStyle();
// branchless conditional calculation
const auto plyr_btn_width = 300.f - (style.ItemInnerSpacing.x * 2) - (has_scrollbar * style.ScrollbarSize);
if (ImGui::Button(plyr->get_name(), { plyr_btn_width, 0.f}))
{
g_player_service->set_selected(plyr);
g_gui_service->set_selected(tabs::PLAYER);
@ -85,6 +90,7 @@ namespace big
void view::players()
{
// player count does not include ourself that's why +1
const auto player_count = g_player_service->players().size() + 1;
if (!*g_pointers->m_gta.m_is_session_started && player_count < 2)
@ -99,15 +105,24 @@ namespace big
if (ImGui::Begin("playerlist", nullptr, window_flags))
{
float window_height = (ImGui::CalcTextSize("A").y + ImGui::GetStyle().ItemInnerSpacing.y * 2 + 6.0f) * player_count + 10.0f;
window_height =
window_height + window_pos > (float)*g_pointers->m_gta.m_resolution_y - 10.f ? (float)*g_pointers->m_gta.m_resolution_y - (window_pos + 40.f) : window_height;
const auto style = ImGui::GetStyle();
float window_height = (
ImGui::CalcTextSize("A").y + style.FramePadding.y * 2.0f + style.ItemSpacing.y) // button size
* player_count // amount of players
+ (player_count > 1) * ((style.ItemSpacing.y * 2) + 1.f) // account for ImGui::Separator spacing
+ (player_count == 1) * 2.f; // some arbitrary height to make it fit
// used to account for scrollbar width
has_scrollbar = window_height + window_pos > (float)*g_pointers->m_gta.m_resolution_y - 10.f;
// basically whichever is smaller, the max available screenspace or the calculated window_height
window_height = has_scrollbar ? (float)*g_pointers->m_gta.m_resolution_y - (window_pos + 40.f) : window_height;
ImGui::PushStyleColor(ImGuiCol_FrameBg, {0.f, 0.f, 0.f, 0.f});
ImGui::PushStyleColor(ImGuiCol_ScrollbarBg, {0.f, 0.f, 0.f, 0.f});
if (ImGui::BeginListBox("##players", {ImGui::GetWindowSize().x - ImGui::GetStyle().WindowPadding.x * 2, window_height}))
{
ImGui::SetScrollX(0);
player_button(g_player_service->get_self());
if (player_count > 1)