Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replaced old spinner #2895

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 33 additions & 83 deletions source/main/gui/GUIUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,54 +125,47 @@ void RoR::ImTextFeeder::NextLine()
// Internal helper
inline void ColorToInts(ImVec4 v, int&r, int&g, int&b) { r=(int)(v.x*255); g=(int)(v.y*255); b=(int)(v.z*255); }

void RoR::DrawImGuiSpinner(float& counter, const ImVec2 size, const float spacing, const float step_sec)
// A nice spinner https://github.com/ocornut/imgui/issues/1901#issuecomment-444929973
void RoR::LoadingIndicatorCircle(const char* label, const float indicator_radius, const ImVec4& main_color, const ImVec4& backdrop_color, const int circle_count, const float speed)
{
// Hardcoded to 4 segments, counter is reset after full round (4 steps)
// --------------------------------------------------------------------

const ImU32 COLORS[] = { ImColor(255,255,255,255), ImColor(210,210,210,255), ImColor(120,120,120,255), ImColor(60,60,60,255) };

// Update counter, determine coloring
counter += ImGui::GetIO().DeltaTime;
int color_start = 0; // Index to GUI_SPINNER_COLORS array for the top middle segment (segment 0)
while (counter > (step_sec*4.f))
ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems)
{
counter -= (step_sec*4.f);
return;
}

if (counter > (step_sec*3.f))
{
color_start = 3;
}
else if (counter > (step_sec*2.f))
ImGuiContext& g = *GImGui;
const ImGuiID id = window->GetID(label);

const ImVec2 pos = window->DC.CursorPos;
const float circle_radius = indicator_radius / 10.0f;
const ImRect bb(pos, ImVec2(pos.x + indicator_radius * 2.0f, pos.y + indicator_radius * 2.0f));
ImGui::ItemSize(bb, ImGui::GetStyle().FramePadding.y);
if (!ImGui::ItemAdd(bb, id))
{
color_start = 2;
return;
}
else if (counter > (step_sec))

const float t = g.Time;
const auto degree_offset = 2.0f * IM_PI / circle_count;

for (int i = 0; i < circle_count; ++i)
{
color_start = 1;
}
const auto x = indicator_radius * std::sin(degree_offset * i);
const auto y = indicator_radius * std::cos(degree_offset * i);
const auto growth = std::max(0.0f, std::sin(t * speed - i * degree_offset));
ImVec4 color;
color.x = main_color.x * growth + backdrop_color.x * (1.0f - growth);
color.y = main_color.y * growth + backdrop_color.y * (1.0f - growth);
color.z = main_color.z * growth + backdrop_color.z * (1.0f - growth);
color.w = 1.0f;

// Draw segments
ImDrawList* draw_list = ImGui::GetWindowDrawList();
const ImVec2 pos = ImGui::GetCursorScreenPos();
const float left = pos.x;
const float top = pos.y;
const float right = pos.x + size.x;
const float bottom = pos.y + size.y;
const float mid_x = pos.x + (size.x / 2.f);
const float mid_y = pos.y + (size.y / 2.f);

// NOTE: Enter vertices in clockwise order, otherwise anti-aliasing doesn't work and polygon is rasterized larger! -- Observed under OpenGL2 / OGRE 1.9

// Top triangle, vertices: mid, left, right
draw_list->AddTriangleFilled(ImVec2(mid_x, mid_y-spacing), ImVec2(left + spacing, top), ImVec2(right - spacing, top), COLORS[color_start]);
// Right triangle, vertices: mid, top, bottom
draw_list->AddTriangleFilled(ImVec2(mid_x+spacing, mid_y), ImVec2(right, top + spacing), ImVec2(right, bottom - spacing), COLORS[(color_start+3)%4]);
// Bottom triangle, vertices: mid, right, left
draw_list->AddTriangleFilled(ImVec2(mid_x, mid_y+spacing), ImVec2(right - spacing, bottom), ImVec2(left + spacing, bottom), COLORS[(color_start+2)%4]);
// Left triangle, vertices: mid, bottom, top
draw_list->AddTriangleFilled(ImVec2(mid_x-spacing, mid_y), ImVec2(left, bottom - spacing), ImVec2(left, top + spacing), COLORS[(color_start+1)%4]);
window->DrawList->AddCircleFilled(ImVec2(pos.x + indicator_radius + x,
pos.y + indicator_radius - y),
circle_radius + growth * circle_radius,
ImGui::GetColorU32(color));

}
}

//source: https://github.com/ocornut/imgui/issues/1982#issuecomment-408834301
Expand Down Expand Up @@ -367,46 +360,3 @@ ImDrawList* RoR::GetImDummyFullscreenWindow()

return drawlist;
}

// A nice spinner https://github.com/ocornut/imgui/issues/1901#issuecomment-444929973
void RoR::LoadingIndicatorCircle(const char* label, const float indicator_radius, const ImVec4& main_color, const ImVec4& backdrop_color, const int circle_count, const float speed)
{
ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems)
{
return;
}

ImGuiContext& g = *GImGui;
const ImGuiID id = window->GetID(label);

const ImVec2 pos = window->DC.CursorPos;
const float circle_radius = indicator_radius / 10.0f;
const ImRect bb(pos, ImVec2(pos.x + indicator_radius * 2.0f, pos.y + indicator_radius * 2.0f));
ImGui::ItemSize(bb, ImGui::GetStyle().FramePadding.y);
if (!ImGui::ItemAdd(bb, id))
{
return;
}

const float t = g.Time;
const auto degree_offset = 2.0f * IM_PI / circle_count;

for (int i = 0; i < circle_count; ++i)
{
const auto x = indicator_radius * std::sin(degree_offset * i);
const auto y = indicator_radius * std::cos(degree_offset * i);
const auto growth = std::max(0.0f, std::sin(t * speed - i * degree_offset));
ImVec4 color;
color.x = main_color.x * growth + backdrop_color.x * (1.0f - growth);
color.y = main_color.y * growth + backdrop_color.y * (1.0f - growth);
color.z = main_color.z * growth + backdrop_color.z * (1.0f - growth);
color.w = 1.0f;

window->DrawList->AddCircleFilled(ImVec2(pos.x + indicator_radius + x,
pos.y + indicator_radius - y),
circle_radius + growth * circle_radius,
ImGui::GetColorU32(color));

}
}
6 changes: 1 addition & 5 deletions source/main/gui/GUIUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ struct ImTextFeeder /// Helper for drawing multiline wrapped & colored text.
};

/// Draws animated loading spinner
void DrawImGuiSpinner(
float& counter, const ImVec2 size = ImVec2(16.f, 16.f),
const float spacing = 2.f, const float step_sec = 0.15f);
void LoadingIndicatorCircle(const char* label, const float indicator_radius, const ImVec4& main_color, const ImVec4& backdrop_color, const int circle_count, const float speed);

/// Add rotated textured quad to ImDrawList, source: https://github.com/ocornut/imgui/issues/1982#issuecomment-408834301
void DrawImageRotated(ImTextureID tex_id, ImVec2 center, ImVec2 size, float angle);
Expand Down Expand Up @@ -76,6 +74,4 @@ Ogre::TexturePtr FetchIcon(const char* name);

ImDrawList* GetImDummyFullscreenWindow();

void LoadingIndicatorCircle(const char* label, const float indicator_radius, const ImVec4& main_color, const ImVec4& backdrop_color, const int circle_count, const float speed);

} // namespace RoR
5 changes: 4 additions & 1 deletion source/main/gui/panels/GUI_LoadingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ void LoadingWindow::SetProgressNetConnect(const std::string& net_status)

void LoadingWindow::Draw()
{
GUIManager::GuiTheme const& theme = App::GetGuiManager()->GetTheme();

// Height calc
float text_h = ImGui::CalcTextSize("A").y;
float statusbar_h = text_h + (ImGui::GetStyle().FramePadding.y * 2);
Expand All @@ -98,7 +100,8 @@ void LoadingWindow::Draw()

if (m_percent == PERC_SHOW_SPINNER)
{
DrawImGuiSpinner(m_spinner_counter, ImVec2(ImGui::GetTextLineHeight(), ImGui::GetTextLineHeight()));
float spinner_size = 8.f;
LoadingIndicatorCircle("spinner", spinner_size, theme.value_blue_text_color, theme.value_blue_text_color, 10, 10);
}
else if (m_percent == PERC_HIDE_PROGRESSBAR)
{
Expand Down
16 changes: 14 additions & 2 deletions source/main/gui/panels/GUI_MultiplayerSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ void MultiplayerSelector::DrawDirectTab()

void MultiplayerSelector::DrawServerlistTab()
{
GUIManager::GuiTheme const& theme = App::GetGuiManager()->GetTheme();

const char* draw_label_text = nullptr;
ImVec4 draw_label_color;

Expand All @@ -252,6 +254,14 @@ void MultiplayerSelector::DrawServerlistTab()
draw_label_color = m_serverlist_msg_color;
}

if (m_show_spinner)
{
float spinner_size = 25.f;
ImGui::SetCursorPosX((ImGui::GetWindowSize().x / 2.f) - spinner_size);
ImGui::SetCursorPosY((ImGui::GetWindowSize().y / 2.f) - spinner_size);
LoadingIndicatorCircle("spinner", spinner_size, theme.value_blue_text_color, theme.value_blue_text_color, 10, 10);
}

// DRAW SERVERLIST TABLE
if (m_draw_table)
{
Expand Down Expand Up @@ -355,11 +365,11 @@ void MultiplayerSelector::DrawServerlistTab()
void MultiplayerSelector::StartAsyncRefresh()
{
#if defined(USE_CURL)
m_show_spinner = true;
m_draw_table = false;
m_serverlist_data.clear();
m_selected_item = -1;
m_serverlist_msg = _LC("MultiplayerSelector", "... refreshing ...");
m_serverlist_msg_color = App::GetGuiManager()->GetTheme().in_progress_text_color;
m_serverlist_msg = "";
std::packaged_task<void(std::string)> task(FetchServerlist);
std::thread(std::move(task), App::mp_api_url->getStr()).detach(); // launch on a thread
#endif // defined(USE_CURL)
Expand All @@ -381,13 +391,15 @@ void MultiplayerSelector::SetVisible(bool visible)

void MultiplayerSelector::DisplayRefreshFailed(std::string const& msg)
{
m_show_spinner = false;
m_serverlist_msg = msg;
m_serverlist_msg_color = App::GetGuiManager()->GetTheme().error_text_color;
m_draw_table = false;
}

void MultiplayerSelector::UpdateServerlist(MpServerInfoVec* data)
{
m_show_spinner = false;
m_serverlist_data = *data;
m_draw_table = true;
if (m_serverlist_data.empty())
Expand Down
1 change: 1 addition & 0 deletions source/main/gui/panels/GUI_MultiplayerSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class MultiplayerSelector
Str<1000> m_password_buf;
Str<1000> m_server_host_buf;
Ogre::TexturePtr m_lock_icon;
bool m_show_spinner = false;
};

} // namespace GUI
Expand Down