From 8d1583d61147f6ea597db2e18a516cf0ea36f933 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 31 Jan 2015 16:44:55 +0000 Subject: [PATCH] Added ImColor() inline helper. Image/ImageButton() now takes ImVec4 for consistency and honor window alpha. --- imgui.cpp | 22 +++++++++++----------- imgui.h | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 50a454eb..b404ac08 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3679,27 +3679,27 @@ static bool CloseWindowButton(bool* p_opened) return pressed; } -void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, ImU32 tint_col, ImU32 border_col) +void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + size); - if (border_col != 0) + if (border_col.w > 0.0f) bb.Max += ImVec2(2,2); ItemSize(bb); if (!ItemAdd(bb, NULL)) return; - if (border_col != 0) + if (border_col.w > 0.0f) { - window->DrawList->AddRect(bb.Min, bb.Max, border_col, 0.0f); - window->DrawList->AddImage(user_texture_id, bb.Min+ImVec2(1,1), bb.Max-ImVec2(1,1), uv0, uv1, tint_col); + window->DrawList->AddRect(bb.Min, bb.Max, window->Color(border_col), 0.0f); + window->DrawList->AddImage(user_texture_id, bb.Min+ImVec2(1,1), bb.Max-ImVec2(1,1), uv0, uv1, window->Color(tint_col)); } else { - window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, tint_col); + window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, window->Color(tint_col)); } } @@ -3707,7 +3707,7 @@ void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& // frame_padding = 0: no framing // frame_padding > 0: set framing size // The color used are the button colors. -bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, ImU32 bg_col) +bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col) { ImGuiState& g = GImGui; ImGuiWindow* window = GetCurrentWindow(); @@ -3736,8 +3736,8 @@ bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const I const ImU32 col = window->Color((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); if (padding.x > 0.0f || padding.y > 0.0f) RenderFrame(bb.Min, bb.Max, col); - if ((bg_col >> 24) != 0) - window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, bg_col); + if (bg_col.w > 0.0f) + window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, window->Color(bg_col)); window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1); return pressed; @@ -7770,7 +7770,7 @@ void ImGui::ShowTestWindow(bool* opened) float tex_w = (float)ImGui::GetIO().Fonts->TexWidth; float tex_h = (float)ImGui::GetIO().Fonts->TexHeight; ImTextureID tex_id = ImGui::GetIO().Fonts->TexID; - ImGui::Image(tex_id, ImVec2(tex_w, tex_h), ImVec2(0,0), ImVec2(1,1), 0xFFFFFFFF, 0x80808080); + ImGui::Image(tex_id, ImVec2(tex_w, tex_h), ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255), ImColor(255,255,255,128)); if (ImGui::IsItemHovered()) { ImGui::BeginTooltip(); @@ -7781,7 +7781,7 @@ void ImGui::ShowTestWindow(bool* opened) ImGui::Text("Max: (%.2f, %.2f)", focus_x + focus_sz, focus_y + focus_sz); ImVec2 uv0 = ImVec2((focus_x) / tex_w, (focus_y) / tex_h); ImVec2 uv1 = ImVec2((focus_x + focus_sz) / tex_w, (focus_y + focus_sz) / tex_h); - ImGui::Image(tex_id, ImVec2(128,128), uv0, uv1, 0xFFFFFFFF, 0x80808080); + ImGui::Image(tex_id, ImVec2(128,128), uv0, uv1, ImColor(255,255,255,255), ImColor(255,255,255,128)); ImGui::EndTooltip(); } ImGui::TextWrapped("And now some textured buttons.."); diff --git a/imgui.h b/imgui.h index 1002e47f..0e4ade86 100644 --- a/imgui.h +++ b/imgui.h @@ -245,8 +245,8 @@ namespace ImGui IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0,0), bool repeat_when_held = false); IMGUI_API bool SmallButton(const char* label); IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size); - IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), ImU32 tint_col = 0xFFFFFFFF, ImU32 border_col = 0x00000000); - IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, ImU32 bg_col = 0xFF000000); // <0 frame_padding uses default frame padding settings. 0 for no paddnig. + IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); + IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,1)); // <0 frame_padding uses default frame padding settings. 0 for no paddnig. IMGUI_API bool CollapsingHeader(const char* label, const char* str_id = NULL, const bool display_frame = true, const bool default_open = false); IMGUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f); // adjust display_format to decorate the value with a prefix or a suffix. Use power!=1.0 for logarithmic sliders. IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f); @@ -697,6 +697,19 @@ struct ImGuiTextEditCallbackData void InsertChars(int pos, const char* text, const char* text_end = NULL); }; +// ImColor() is just a helper that implicity converts to either ImU32 (packed 4x1 byte) or ImVec4 (4x1 float) +// None of the ImGui API are using ImColor directly but you can use it as a convenience to pass colors in either formats. +struct ImColor +{ + ImVec4 Value; + + ImColor(int r, int g, int b, int a = 255) { Value.x = r / 255.0f; Value.y = g / 255.0f; Value.z = b / 255.0f; Value.w = a / 255.0f; } + ImColor(const ImVec4& col) { Value = col; } + ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } + operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } + operator ImVec4() const { return Value; } +}; + //----------------------------------------------------------------------------- // Draw List // Hold a series of drawing commands. The user provides a renderer for ImDrawList.