图示
我的测试
原理
绘制数个大小不一的圆形排列的圆形图案
代码
#include "imgui.h"
#include "imgui_internal.h"
#include <algorithm>
#include <cmath>
//... 不同于原代码
void LoadingIndicatorCircle(const char* label, const float indicator_radius,
const ImVec4& main_color, const ImVec4& backdrop_color,
const int circle_count, const float speed) {
using namespace ImGui;
ImGuiWindow* window = 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));
ItemSize(bb, ImGui::GetStyle().FramePadding.y);
if (!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);
auto growth = std::sin(t * speed - i * degree_offset) <0;
if (growth < 0)
{
growth = 0.0f;
}
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,
GetColorU32(color));
}
}
调用示例,效果见本文第二张图
LoadingIndicatorCircle("cce",
20.f,
ImVec4(144, 0, 0, 1.0),// 颜色 此表达类似于 RGBA(255,255,255,1.0)
ImVec4(0, 255, 0, 1.0),//一处不合理的地方 这两个参数对应构造参数之和
//不能大于255 ,具体原因去看上面的代码
12,
7);