| 859 | } |
| 860 | |
| 861 | static void ExampleImageViewer_DrawCanvas(ExampleImageViewerData* data, ImVec2 canvas_size, ImTextureRef image_tex_ref, int image_w, int image_h) |
| 862 | { |
| 863 | ImGuiIO& io = ImGui::GetIO(); |
| 864 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 865 | ImDrawList* draw_list = ImGui::GetWindowDrawList(); |
| 866 | IM_ASSERT(canvas_size.x >= 0.0f && canvas_size.y >= 0.0f); |
| 867 | |
| 868 | // Layout canvas |
| 869 | ImGui::InvisibleButton("##Canvas", canvas_size); |
| 870 | ImVec2 canvas_min = ImGui::GetItemRectMin(); |
| 871 | ImVec2 canvas_max = ImGui::GetItemRectMax(); |
| 872 | |
| 873 | if (data->ViewReset) |
| 874 | data->ViewOffset = ImVec2((canvas_size.x * 0.5f / data->Zoom) - 0.5f, (canvas_size.y * 0.5f / data->Zoom) - 0.5f); // Add half a pixel padding |
| 875 | data->ViewReset = false; |
| 876 | |
| 877 | // Handle inputs |
| 878 | if (ImGui::SetItemKeyOwner(ImGuiKey_MouseWheelY)) |
| 879 | if (io.MouseWheel != 0.0f) |
| 880 | data->Zoom = IM_CLAMP(data->Zoom * (1.0f + io.MouseWheel * 0.10f), data->ZoomMin, data->ZoomMax); |
| 881 | float zoom = data->Zoom; // (float)(int)ViewZoom; |
| 882 | if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) |
| 883 | { |
| 884 | data->ViewOffset.x -= io.MouseDelta.x / zoom; |
| 885 | data->ViewOffset.y -= io.MouseDelta.y / zoom; |
| 886 | } |
| 887 | |
| 888 | // Display image |
| 889 | ImVec2 image_min, image_max; |
| 890 | image_min.x = (float)(int)((canvas_min.x - (data->ViewOffset.x * zoom)) + (canvas_size.x * 0.5f)); |
| 891 | image_min.y = (float)(int)((canvas_min.y - (data->ViewOffset.y * zoom)) + (canvas_size.y * 0.5f)); |
| 892 | image_max.x = (float)(int)(image_min.x + image_w * zoom); |
| 893 | image_max.y = (float)(int)(image_min.y + image_h * zoom); |
| 894 | draw_list->AddRect(ImVec2(canvas_min.x - 1.0f, canvas_min.y - 1.0f), ImVec2(canvas_max.x + 1.0f, canvas_max.y + 1.0f), IM_COL32(255, 255, 255, 255)); |
| 895 | draw_list->PushClipRect(canvas_min, canvas_max, true); |
| 896 | draw_list->AddRectFilled(image_min, image_max, data->ImageBgColor); |
| 897 | if (platform_io.DrawCallback_SetSamplerNearest != NULL) |
| 898 | draw_list->AddCallback(platform_io.DrawCallback_SetSamplerNearest); |
| 899 | draw_list->AddImage(image_tex_ref, image_min, image_max); |
| 900 | if (platform_io.DrawCallback_SetSamplerLinear != NULL) |
| 901 | draw_list->AddCallback(ImGui::GetPlatformIO().DrawCallback_SetSamplerLinear); |
| 902 | |
| 903 | // Display grid lines for visible pixels |
| 904 | if (data->GridEnabled && zoom > 6.0f) |
| 905 | { |
| 906 | const float step = (float)zoom; |
| 907 | for (int px = (int)((canvas_min.x - image_min.x) / step); px <= (int)((canvas_max.x - image_min.x) / step); px++) |
| 908 | draw_list->AddLineV(image_min.x + px * step, canvas_min.y, canvas_max.y, data->GridColor, 1.0f); |
| 909 | for (int py = (int)((canvas_min.y - image_min.y) / step); py <= (int)((canvas_max.y - image_min.y) / step); py++) |
| 910 | draw_list->AddLineH(canvas_min.x, canvas_max.x, image_min.y + py * step, data->GridColor, 1.0f); |
| 911 | } |
| 912 | draw_list->PopClipRect(); |
| 913 | } |
| 914 | |
| 915 | //----------------------------------------------------------------------------- |
| 916 | // [SECTION] DemoWindowWidgetsBasic() |
no test coverage detected