├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md └── src ├── imgui_animated.cpp └── imgui_animated.h /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.* 2 | !src/imgui_animated.* 3 | !.gitignore 4 | !.gitattributes 5 | !*.md -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 bludeck 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imgui-animated 2 | 3 | imgui-animated is a collection of templates for animating imgui functions or adding a new one 4 | 5 | At the moment it has in itself: 6 | 7 | * [ButtonScrollable(Ex)](#buttonscrollableex-usage) 8 | * [Toggle](#toggle-usage) 9 | 10 | # ButtonScrollable(Ex) usage: 11 | https://user-images.githubusercontent.com/47151102/132950549-46cd755a-4104-4d39-b175-2f5bfb8f7dca.mp4 12 | ```cpp 13 | ... 14 | ImGui::ButtonScrollable("Button Scrollable", ImVec2(100.f, 0.f)); 15 | ImGui::ButtonScrollable("Button Scrollable that fits in button size", ImVec2(350.f, 0.f)); 16 | ImGui::ButtonScrollableEx("Button Scrollable (Right-click only!)", ImVec2(100.f, 0.f), ImGuiButtonFlags_MouseButtonRight); 17 | ... 18 | ``` 19 | 20 | # Toggle usage: 21 | https://user-images.githubusercontent.com/47151102/132950560-aeb77c0d-3c1f-46bc-9cb3-c0a974f92600.mp4 22 | ```cpp 23 | ImGui::Toggle( "Toggle button", &toggle_button ); 24 | ``` 25 | -------------------------------------------------------------------------------- /src/imgui_animated.cpp: -------------------------------------------------------------------------------- 1 | #define IMGUI_DEFINE_MATH_OPERATORS 2 | #include "imgui.h" 3 | #include "imgui_internal.h" 4 | #include "imgui_animated.h" 5 | 6 | bool ImGui::Toggle( const char* label, bool* v ) 7 | { 8 | ImGuiWindow* window = GetCurrentWindow( ); 9 | if ( window->SkipItems ) 10 | return false; 11 | 12 | ImGuiContext& g = *GImGui; 13 | const ImGuiStyle& style = g.Style; 14 | const ImGuiID id = window->GetID( label ); 15 | const ImVec2 label_size = CalcTextSize( label, NULL, true ); 16 | 17 | float height = ImGui::GetFrameHeight( ); 18 | const ImVec2 pos = window->DC.CursorPos; 19 | 20 | float width = height * 2.f; 21 | float radius = height * 0.50f; 22 | 23 | const ImRect total_bb( pos, pos + ImVec2( width + ( label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f ), label_size.y + style.FramePadding.y * 2.0f ) ); 24 | 25 | ItemSize( total_bb, style.FramePadding.y ); 26 | if ( !ItemAdd( total_bb, id ) ) 27 | return false; 28 | 29 | float last_active_id_timer = g.LastActiveIdTimer; 30 | 31 | bool hovered, held; 32 | bool pressed = ButtonBehavior( total_bb, id, &hovered, &held ); 33 | if ( pressed ) 34 | { 35 | *v = !( *v ); 36 | MarkItemEdited( id ); 37 | g.LastActiveIdTimer = 0.f; 38 | } 39 | 40 | if ( g.LastActiveIdTimer == 0.f && g.LastActiveId == id && !pressed ) 41 | g.LastActiveIdTimer = last_active_id_timer; 42 | 43 | float t = *v ? 1.0f : 0.0f; 44 | 45 | if ( g.LastActiveId == id ) 46 | { 47 | float t_anim = ImSaturate( g.LastActiveIdTimer / 0.16f ); 48 | t = *v ? ( t_anim ) : ( 1.0f - t_anim ); 49 | } 50 | 51 | ImU32 col_bg = GetColorU32( ( held && hovered ) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg ); 52 | 53 | const ImRect frame_bb( pos, pos + ImVec2( width, height ) ); 54 | 55 | RenderFrame( frame_bb.Min, frame_bb.Max, col_bg, true, height * 0.5f ); 56 | RenderNavHighlight( total_bb, id ); 57 | 58 | ImVec2 label_pos = ImVec2( frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y ); 59 | RenderText( label_pos, label ); 60 | window->DrawList->AddCircleFilled( ImVec2( pos.x + radius + t * ( width - radius * 2.0f ), pos.y + radius ), radius - 1.5f, ImGui::GetColorU32( ImGuiCol_CheckMark ), 36 ); 61 | 62 | return pressed; 63 | } 64 | 65 | bool ImGui::ButtonScrollableEx( const char* label, const ImVec2& size_arg, ImGuiButtonFlags flags ) 66 | { 67 | ImGuiWindow* window = GetCurrentWindow( ); 68 | if ( window->SkipItems ) 69 | return false; 70 | 71 | ImGuiContext& g = *GImGui; 72 | const ImGuiStyle& style = g.Style; 73 | const ImGuiID id = window->GetID( label ); 74 | const ImVec2 label_size = CalcTextSize( label, NULL, true ); 75 | 76 | ImVec2 pos = window->DC.CursorPos; 77 | if ( ( flags & ImGuiButtonFlags_AlignTextBaseLine ) && style.FramePadding.y < window->DC.CurrLineTextBaseOffset ) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag) 78 | pos.y += window->DC.CurrLineTextBaseOffset - style.FramePadding.y; 79 | ImVec2 size = CalcItemSize( size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f ); 80 | 81 | const ImRect bb( pos, pos + size ); 82 | ItemSize( size, style.FramePadding.y ); 83 | if ( !ItemAdd( bb, id ) ) 84 | return false; 85 | 86 | if ( g.LastItemData.InFlags & ImGuiItemFlags_ButtonRepeat ) 87 | flags |= ImGuiButtonFlags_Repeat; 88 | 89 | bool hovered, held; 90 | bool pressed = ButtonBehavior( bb, id, &hovered, &held, flags ); 91 | 92 | const ImU32 col = GetColorU32( ( held && hovered ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button ); 93 | RenderNavHighlight( bb, id ); 94 | RenderFrame( bb.Min, bb.Max, col, true, style.FrameRounding ); 95 | 96 | const float offset = size.x >= label_size.x + style.FramePadding.x * 2.0f ? size.x + style.FramePadding.x : static_cast< int >( g.Time * 60.f ) % static_cast< int >( label_size.x + size.x + style.FramePadding.x * 2.f + 4 ); 97 | const ImRect text = ImRect( ImVec2( bb.Min.x + size.x - offset + style.FramePadding.x * 2.f, bb.Min.y + style.FramePadding.y ), bb.Max - style.FramePadding ); 98 | 99 | RenderTextClipped( text.Min, text.Max, label, NULL, &label_size, size.x >= label_size.x + style.FramePadding.x * 2.0f ? g.Style.ButtonTextAlign : ImVec2( 0, 0 ), &bb ); 100 | return pressed; 101 | } 102 | 103 | bool ImGui::ButtonScrollable( const char* label, const ImVec2& size_arg ) 104 | { 105 | return ButtonScrollableEx( label, size_arg, ImGuiButtonFlags_None ); 106 | } -------------------------------------------------------------------------------- /src/imgui_animated.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "imgui.h" 4 | 5 | namespace ImGui 6 | { 7 | IMGUI_API bool Toggle( const char* label, bool* v ); 8 | 9 | IMGUI_API bool ButtonScrollableEx( const char* label, const ImVec2& size_arg = ImVec2( 0, 0 ), ImGuiButtonFlags flags = 0 ); 10 | IMGUI_API bool ButtonScrollable( const char* label, const ImVec2& size_arg = ImVec2( 0, 0 ) ); 11 | } --------------------------------------------------------------------------------