Demonstrate using the low-level ImDrawList to draw custom shapes.
| 4439 | |
| 4440 | // Demonstrate using the low-level ImDrawList to draw custom shapes. |
| 4441 | static void ShowExampleAppCustomRendering(bool* p_open) |
| 4442 | { |
| 4443 | if (!ImGui::Begin("Example: Custom rendering", p_open)) |
| 4444 | { |
| 4445 | ImGui::End(); |
| 4446 | return; |
| 4447 | } |
| 4448 | |
| 4449 | // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of overloaded operators, etc. |
| 4450 | // Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your types and ImVec2/ImVec4. |
| 4451 | // ImGui defines overloaded operators but they are internal to imgui.cpp and not exposed outside (to avoid messing with your types) |
| 4452 | // In this example we are not using the maths operators! |
| 4453 | ImDrawList* draw_list = ImGui::GetWindowDrawList(); |
| 4454 | |
| 4455 | if (ImGui::BeginTabBar("##TabBar")) |
| 4456 | { |
| 4457 | // Primitives |
| 4458 | if (ImGui::BeginTabItem("Primitives")) |
| 4459 | { |
| 4460 | static float sz = 36.0f; |
| 4461 | static float thickness = 3.0f; |
| 4462 | static int ngon_sides = 6; |
| 4463 | static bool circle_segments_override = false; |
| 4464 | static int circle_segments_override_v = 12; |
| 4465 | static ImVec4 colf = ImVec4(1.0f, 1.0f, 0.4f, 1.0f); |
| 4466 | ImGui::PushItemWidth(-ImGui::GetFontSize() * 10); |
| 4467 | ImGui::DragFloat("Size", &sz, 0.2f, 2.0f, 72.0f, "%.0f"); |
| 4468 | ImGui::DragFloat("Thickness", &thickness, 0.05f, 1.0f, 8.0f, "%.02f"); |
| 4469 | ImGui::SliderInt("N-gon sides", &ngon_sides, 3, 12); |
| 4470 | ImGui::Checkbox("##circlesegmentoverride", &circle_segments_override); |
| 4471 | ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); |
| 4472 | if (ImGui::SliderInt("Circle segments", &circle_segments_override_v, 3, 40)) |
| 4473 | circle_segments_override = true; |
| 4474 | ImGui::ColorEdit4("Color", &colf.x); |
| 4475 | const ImVec2 p = ImGui::GetCursorScreenPos(); |
| 4476 | const ImU32 col = ImColor(colf); |
| 4477 | const float spacing = 10.0f; |
| 4478 | const ImDrawCornerFlags corners_none = 0; |
| 4479 | const ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; |
| 4480 | const ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; |
| 4481 | const int circle_segments = circle_segments_override ? circle_segments_override_v : 0; |
| 4482 | float x = p.x + 4.0f, y = p.y + 4.0f; |
| 4483 | for (int n = 0; n < 2; n++) |
| 4484 | { |
| 4485 | // First line uses a thickness of 1.0f, second line uses the configurable thickness |
| 4486 | float th = (n == 0) ? 1.0f : thickness; |
| 4487 | draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon |
| 4488 | draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle |
| 4489 | draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square |
| 4490 | draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_all, th); x += sz + spacing; // Square with all rounded corners |
| 4491 | draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners |
| 4492 | draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th); x += sz + spacing; // Triangle |
| 4493 | draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th); x += sz*0.4f + spacing; // Thin triangle |
| 4494 | draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!) |
| 4495 | draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) |
| 4496 | draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line |
| 4497 | draw_list->AddBezierCurve(ImVec2(x, y), ImVec2(x + sz*1.3f, y + sz*0.3f), ImVec2(x + sz - sz*1.3f, y + sz - sz*0.3f), ImVec2(x + sz, y + sz), col, th); |
| 4498 | x = p.x + 4; |
no test coverage detected