Created
May 6, 2020 05:48
-
-
Save moebiussurfing/c1110be8bb3f6776311512b63523a0a3 to your computer and use it in GitHub Desktop.
openFrameworks / ImGui toggle button. styled and with animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//2 snippets from here: | |
//https://github.com/ocornut/imgui/issues/1537 | |
//1. styled rounded toggle button with animation | |
//add into ofApp.cpp | |
namespace ImGui { | |
void ToggleButton(const char* str_id, bool* v) | |
{ | |
ImVec2 p = ImGui::GetCursorScreenPos(); | |
ImDrawList* draw_list = ImGui::GetWindowDrawList(); | |
float height = ImGui::GetFrameHeight(); | |
float width = height * 1.55f; | |
float radius = height * 0.50f; | |
ImGui::InvisibleButton(str_id, ImVec2(width, height)); | |
if (ImGui::IsItemClicked()) | |
*v = !*v; | |
float t = *v ? 1.0f : 0.0f; | |
ImGuiContext& g = *GImGui; | |
float ANIM_SPEED = 0.08f; | |
if (g.LastActiveId == g.CurrentWindow->GetID(str_id))// && g.LastActiveIdTimer < ANIM_SPEED) | |
{ | |
float t_anim = ImSaturate(g.LastActiveIdTimer / ANIM_SPEED); | |
t = *v ? (t_anim) : (1.0f - t_anim); | |
} | |
ImU32 col_bg; | |
if (ImGui::IsItemHovered()) | |
col_bg = ImGui::GetColorU32(ImLerp(ImVec4(0.78f, 0.78f, 0.78f, 1.0f), ImVec4(0.64f, 0.83f, 0.34f, 1.0f), t)); | |
else | |
col_bg = ImGui::GetColorU32(ImLerp(ImVec4(0.85f, 0.85f, 0.85f, 1.0f), ImVec4(0.56f, 0.83f, 0.26f, 1.0f), t)); | |
draw_list->AddRectFilled(p, ImVec2(p.x + width, p.y + height), col_bg, height * 0.5f); | |
draw_list->AddCircleFilled(ImVec2(p.x + radius + t * (width - radius * 2.0f), p.y + radius), radius - 1.5f, IM_COL32(255, 255, 255, 255)); | |
} | |
} | |
//-- | |
//2. rectangle box button with colors | |
static bool enable_7m = false; // default value, the button is disabled | |
static float b = 1.0f; // test whatever color you need from imgui_demo.cpp e.g. | |
static float c = 0.5f; // | |
static int i = 3; | |
( some code... ) | |
if (enable_7m == true) | |
{ | |
ImGui::PushID(" 7m "); | |
ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(i/7.0f, b, b)); | |
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(i/7.0f, b, b)); | |
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i/7.0f, c, c)); | |
ImGui::Button(" 7m "); | |
if (ImGui::IsItemClicked(0)) | |
{ | |
enable_7m = !enable_7m; | |
} | |
ImGui::PopStyleColor(3); | |
ImGui::PopID(); | |
} | |
else | |
{ | |
if (ImGui::Button(" 7m ")) | |
enable_7m = true; | |
} | |
+ add wherever you need in the code (e.g. after some action): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment