2021-12-31 01:10:06 +01:00
# include <hex/ui/imgui_imhex_extensions.h>
2021-01-27 00:00:20 +01:00
# include <imgui.h>
2021-01-27 12:04:42 +01:00
# include <imgui_freetype.h>
2021-01-27 00:00:20 +01:00
# define IMGUI_DEFINE_MATH_OPERATORS
# include <imgui_internal.h>
# undef IMGUI_DEFINE_MATH_OPERATORS
2021-04-17 15:46:26 +02:00
# define STB_IMAGE_IMPLEMENTATION
# include <stb_image.h>
2021-01-27 12:04:42 +01:00
# include <string>
2021-08-21 15:03:44 +02:00
# include <imgui_impl_opengl3_loader.h>
2021-04-17 15:46:26 +02:00
2021-01-27 00:00:20 +01:00
namespace ImGui {
2021-12-07 22:47:41 +01:00
int UpdateStringSizeCallback ( ImGuiInputTextCallbackData * data ) {
2022-01-24 20:53:17 +01:00
auto & mathInput = * static_cast < std : : string * > ( data - > UserData ) ;
2021-12-07 22:47:41 +01:00
mathInput . resize ( data - > BufTextLen ) ;
return 0 ;
}
2022-01-24 20:53:17 +01:00
bool IconHyperlink ( const char * icon , const char * label , const ImVec2 & size_arg , ImGuiButtonFlags flags ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-08-04 14:01:24 +02:00
if ( window - > SkipItems )
return false ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( label ) ;
ImVec2 label_size = CalcTextSize ( icon , NULL , false ) ;
2021-08-16 23:55:06 +02:00
label_size . x + = CalcTextSize ( " " , NULL , false ) . x + CalcTextSize ( label , NULL , false ) . x ;
2021-08-04 14:01:24 +02:00
2022-02-01 22:09:44 +01:00
ImVec2 pos = window - > DC . CursorPos ;
2021-08-04 14:01:24 +02:00
ImVec2 size = CalcItemSize ( size_arg , label_size . x , label_size . y ) ;
const ImRect bb ( pos , pos + size ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
2021-08-21 00:51:50 +02:00
if ( g . LastItemData . InFlags & ImGuiItemFlags_ButtonRepeat )
2021-08-04 14:01:24 +02:00
flags | = ImGuiButtonFlags_Repeat ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , flags ) ;
// Render
const ImU32 col = hovered ? GetColorU32 ( ImGuiCol_ButtonHovered ) : GetColorU32 ( ImGuiCol_ButtonActive ) ;
PushStyleColor ( ImGuiCol_Text , ImU32 ( col ) ) ;
Text ( " %s %s " , icon , label ) ;
GetWindowDrawList ( ) - > AddLine ( ImVec2 ( pos . x , pos . y + size . y ) , pos + size , ImU32 ( col ) ) ;
PopStyleColor ( ) ;
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-01-24 20:53:17 +01:00
bool Hyperlink ( const char * label , const ImVec2 & size_arg , ImGuiButtonFlags flags ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-01-27 00:00:20 +01:00
if ( window - > SkipItems )
return false ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( label ) ;
2021-01-27 00:00:20 +01:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2022-02-01 22:09:44 +01:00
ImVec2 pos = window - > DC . CursorPos ;
2021-01-27 00:00:20 +01:00
ImVec2 size = CalcItemSize ( size_arg , label_size . x , label_size . y ) ;
const ImRect bb ( pos , pos + size ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
2021-08-21 00:51:50 +02:00
if ( g . LastItemData . InFlags & ImGuiItemFlags_ButtonRepeat )
2021-01-27 00:00:20 +01:00
flags | = ImGuiButtonFlags_Repeat ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , flags ) ;
// Render
2021-01-27 12:04:42 +01:00
const ImU32 col = hovered ? GetColorU32 ( ImGuiCol_ButtonHovered ) : GetColorU32 ( ImGuiCol_ButtonActive ) ;
2021-01-27 00:00:20 +01:00
PushStyleColor ( ImGuiCol_Text , ImU32 ( col ) ) ;
2022-01-24 20:53:17 +01:00
TextEx ( label , NULL , ImGuiTextFlags_NoWidthForLargeClippedText ) ; // Skip formatting
2021-01-27 14:26:24 +01:00
GetWindowDrawList ( ) - > AddLine ( ImVec2 ( pos . x , pos . y + size . y ) , pos + size , ImU32 ( col ) ) ;
2021-01-27 00:00:20 +01:00
PopStyleColor ( ) ;
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-01-24 20:53:17 +01:00
bool BulletHyperlink ( const char * label , const ImVec2 & size_arg , ImGuiButtonFlags flags ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-01-27 12:04:42 +01:00
if ( window - > SkipItems )
return false ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( label ) ;
2021-01-27 12:04:42 +01:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2022-02-01 22:09:44 +01:00
ImVec2 pos = window - > DC . CursorPos ;
2021-01-30 23:02:03 +01:00
ImVec2 size = CalcItemSize ( size_arg , label_size . x , label_size . y ) + ImVec2 ( g . FontSize + style . FramePadding . x * 2 , 0.0f ) ;
2021-01-27 12:04:42 +01:00
const ImRect bb ( pos , pos + size ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
2021-08-21 00:51:50 +02:00
if ( g . LastItemData . InFlags & ImGuiItemFlags_ButtonRepeat )
2021-01-27 12:04:42 +01:00
flags | = ImGuiButtonFlags_Repeat ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , flags ) ;
// Render
const ImU32 col = hovered ? GetColorU32 ( ImGuiCol_ButtonHovered ) : GetColorU32 ( ImGuiCol_ButtonActive ) ;
PushStyleColor ( ImGuiCol_Text , ImU32 ( col ) ) ;
RenderBullet ( window - > DrawList , bb . Min + ImVec2 ( style . FramePadding . x + g . FontSize * 0.5f , g . FontSize * 0.5f ) , col ) ;
RenderText ( bb . Min + ImVec2 ( g . FontSize + style . FramePadding . x * 2 , 0.0f ) , label , nullptr , false ) ;
2021-01-30 23:02:03 +01:00
GetWindowDrawList ( ) - > AddLine ( bb . Min + ImVec2 ( style . FramePadding . x , size . y ) , pos + size , ImU32 ( col ) ) ;
2021-01-27 12:04:42 +01:00
ImGui : : NewLine ( ) ;
PopStyleColor ( ) ;
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-01-24 20:53:17 +01:00
bool DescriptionButton ( const char * label , const char * description , const ImVec2 & size_arg , ImGuiButtonFlags flags ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-01-27 12:04:42 +01:00
if ( window - > SkipItems )
return false ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( label ) ;
const ImVec2 text_size = CalcTextSize ( ( std : : string ( label ) + " \n " + std : : string ( description ) ) . c_str ( ) , NULL , true ) ;
2021-01-27 12:04:42 +01:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
ImVec2 pos = window - > DC . CursorPos ;
2022-01-24 20:53:17 +01:00
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)
2021-01-27 12:04:42 +01:00
pos . y + = window - > DC . CurrLineTextBaseOffset - style . FramePadding . y ;
ImVec2 size = CalcItemSize ( size_arg , text_size . x + style . FramePadding . x * 4.0f , text_size . y + style . FramePadding . y * 4.0f ) ;
const ImRect bb ( pos , pos + size ) ;
ItemSize ( size , style . FramePadding . y ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
2021-08-21 00:51:50 +02:00
if ( g . LastItemData . InFlags & ImGuiItemFlags_ButtonRepeat )
2021-01-27 12:04:42 +01:00
flags | = ImGuiButtonFlags_Repeat ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held , flags ) ;
ImGui : : PushStyleVar ( ImGuiStyleVar_ButtonTextAlign , ImVec2 ( 0.0 , 0.5 ) ) ;
// Render
2022-01-24 20:53:17 +01:00
const ImU32 col = GetCustomColorU32 ( ( held & & hovered ) ? ImGuiCustomCol_DescButtonActive : hovered ? ImGuiCustomCol_DescButtonHovered
: ImGuiCustomCol_DescButton ) ;
2021-01-27 12:04:42 +01:00
RenderNavHighlight ( bb , id ) ;
RenderFrame ( bb . Min , bb . Max , col , true , style . FrameRounding ) ;
PushStyleColor ( ImGuiCol_Text , GetColorU32 ( ImGuiCol_ButtonActive ) ) ;
2021-02-25 00:17:41 +01:00
RenderTextWrapped ( bb . Min + style . FramePadding * 2 , label , nullptr , CalcWrapWidthForPos ( window - > DC . CursorPos , window - > DC . TextWrapPos ) ) ;
2021-01-27 12:04:42 +01:00
PopStyleColor ( ) ;
PushStyleColor ( ImGuiCol_Text , GetColorU32 ( ImGuiCol_Text ) ) ;
RenderTextClipped ( bb . Min + style . FramePadding * 2 + ImVec2 ( style . FramePadding . x * 2 , label_size . y ) , bb . Max - style . FramePadding , description , NULL , & text_size , style . ButtonTextAlign , & bb ) ;
PopStyleColor ( ) ;
ImGui : : PopStyleVar ( ) ;
// Automatically close popups
2022-01-24 20:53:17 +01:00
// if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
2021-01-27 12:04:42 +01:00
// CloseCurrentPopup();
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-01-24 20:53:17 +01:00
void UnderlinedText ( const char * label , ImColor color , const ImVec2 & size_arg ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-01-27 12:04:42 +01:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
2022-02-01 22:09:44 +01:00
ImVec2 pos = window - > DC . CursorPos ;
2021-01-27 12:04:42 +01:00
ImVec2 size = CalcItemSize ( size_arg , label_size . x , label_size . y ) ;
PushStyleColor ( ImGuiCol_Text , ImU32 ( color ) ) ;
2022-01-24 20:53:17 +01:00
TextEx ( label , NULL , ImGuiTextFlags_NoWidthForLargeClippedText ) ; // Skip formatting
2021-01-27 14:26:24 +01:00
GetWindowDrawList ( ) - > AddLine ( ImVec2 ( pos . x , pos . y + size . y ) , pos + size , ImU32 ( color ) ) ;
2021-01-27 12:04:42 +01:00
PopStyleColor ( ) ;
}
2021-02-25 00:17:41 +01:00
void Disabled ( const std : : function < void ( ) > & widgets , bool disabled ) {
2021-02-20 22:38:31 +01:00
if ( disabled ) {
2021-08-21 13:55:21 +02:00
BeginDisabled ( ) ;
2021-02-20 22:38:31 +01:00
widgets ( ) ;
2021-08-21 13:55:21 +02:00
EndDisabled ( ) ;
2021-02-20 22:38:31 +01:00
} else {
widgets ( ) ;
}
}
2022-01-24 20:53:17 +01:00
void TextSpinner ( const char * label ) {
2021-02-22 13:06:53 +01:00
ImGui : : Text ( " [%c] %s " , " |/- \\ " [ ImU32 ( ImGui : : GetTime ( ) * 20 ) % 4 ] , label ) ;
}
2021-03-02 13:49:45 +01:00
void Header ( const char * label , bool firstEntry ) {
if ( ! firstEntry )
ImGui : : NewLine ( ) ;
ImGui : : TextUnformatted ( label ) ;
ImGui : : Separator ( ) ;
}
2021-08-28 16:02:53 +02:00
void HeaderColored ( const char * label , ImColor color , bool firstEntry ) {
if ( ! firstEntry )
ImGui : : NewLine ( ) ;
2022-01-15 14:14:53 +01:00
ImGui : : TextFormattedColored ( color , " {} " , label ) ;
2021-08-28 16:02:53 +02:00
ImGui : : Separator ( ) ;
}
2021-08-18 23:12:27 +02:00
void InfoTooltip ( const char * text ) {
2021-08-21 00:51:50 +02:00
static double lastMoveTime ;
static ImGuiID lastHoveredID ;
2022-02-01 22:09:44 +01:00
double currTime = ImGui : : GetTime ( ) ;
2021-08-21 00:51:50 +02:00
ImGuiID hoveredID = ImGui : : GetHoveredID ( ) ;
if ( IsItemHovered ( ) & & ( currTime - lastMoveTime ) > = 0.5 & & hoveredID = = lastHoveredID ) {
2021-08-18 23:12:27 +02:00
BeginTooltip ( ) ;
TextUnformatted ( text ) ;
EndTooltip ( ) ;
}
2021-08-21 00:51:50 +02:00
if ( hoveredID ! = lastHoveredID ) {
lastMoveTime = currTime ;
}
lastHoveredID = hoveredID ;
2021-08-18 23:12:27 +02:00
}
2021-02-25 00:17:41 +01:00
ImU32 GetCustomColorU32 ( ImGuiCustomCol idx , float alpha_mul ) {
2022-01-24 20:53:17 +01:00
auto & customData = * static_cast < ImHexCustomData * > ( GImGui - > IO . UserData ) ;
2022-02-01 22:09:44 +01:00
ImVec4 c = customData . Colors [ idx ] ;
2021-02-25 00:17:41 +01:00
c . w * = GImGui - > Style . Alpha * alpha_mul ;
return ColorConvertFloat4ToU32 ( c ) ;
}
2021-08-21 00:51:50 +02:00
ImVec4 GetCustomColorVec4 ( ImGuiCustomCol idx , float alpha_mul ) {
2022-01-24 20:53:17 +01:00
auto & customData = * static_cast < ImHexCustomData * > ( GImGui - > IO . UserData ) ;
2022-02-01 22:09:44 +01:00
ImVec4 c = customData . Colors [ idx ] ;
2021-08-21 00:51:50 +02:00
c . w * = GImGui - > Style . Alpha * alpha_mul ;
return c ;
}
2021-02-25 00:17:41 +01:00
void StyleCustomColorsDark ( ) {
2022-01-24 20:53:17 +01:00
auto & colors = static_cast < ImHexCustomData * > ( GImGui - > IO . UserData ) - > Colors ;
2021-02-25 00:17:41 +01:00
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_DescButton ] = ImColor ( 20 , 20 , 20 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_DescButtonHovered ] = ImColor ( 40 , 40 , 40 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_DescButtonActive ] = ImColor ( 60 , 60 , 60 ) ;
2021-08-21 00:51:50 +02:00
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarGray ] = ImColor ( 230 , 230 , 230 ) ;
colors [ ImGuiCustomCol_ToolbarRed ] = ImColor ( 231 , 76 , 60 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_ToolbarYellow ] = ImColor ( 241 , 196 , 15 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarGreen ] = ImColor ( 56 , 139 , 66 ) ;
colors [ ImGuiCustomCol_ToolbarBlue ] = ImColor ( 6 , 83 , 155 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_ToolbarPurple ] = ImColor ( 103 , 42 , 120 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarBrown ] = ImColor ( 219 , 179 , 119 ) ;
2021-08-28 16:02:53 +02:00
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_Highlight ] = ImColor ( 77 , 198 , 155 ) ;
2021-02-25 00:17:41 +01:00
}
void StyleCustomColorsLight ( ) {
2022-01-24 20:53:17 +01:00
auto & colors = static_cast < ImHexCustomData * > ( GImGui - > IO . UserData ) - > Colors ;
2021-02-25 00:17:41 +01:00
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_DescButton ] = ImColor ( 230 , 230 , 230 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_DescButtonHovered ] = ImColor ( 210 , 210 , 210 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_DescButtonActive ] = ImColor ( 190 , 190 , 190 ) ;
2021-08-21 00:51:50 +02:00
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarGray ] = ImColor ( 25 , 25 , 25 ) ;
colors [ ImGuiCustomCol_ToolbarRed ] = ImColor ( 231 , 76 , 60 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_ToolbarYellow ] = ImColor ( 241 , 196 , 15 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarGreen ] = ImColor ( 56 , 139 , 66 ) ;
colors [ ImGuiCustomCol_ToolbarBlue ] = ImColor ( 6 , 83 , 155 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_ToolbarPurple ] = ImColor ( 103 , 42 , 120 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarBrown ] = ImColor ( 219 , 179 , 119 ) ;
2021-08-28 16:02:53 +02:00
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_Highlight ] = ImColor ( 41 , 151 , 112 ) ;
2021-02-25 00:17:41 +01:00
}
void StyleCustomColorsClassic ( ) {
2022-01-24 20:53:17 +01:00
auto & colors = static_cast < ImHexCustomData * > ( GImGui - > IO . UserData ) - > Colors ;
2021-02-25 00:17:41 +01:00
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_DescButton ] = ImColor ( 40 , 40 , 80 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_DescButtonHovered ] = ImColor ( 60 , 60 , 100 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_DescButtonActive ] = ImColor ( 80 , 80 , 120 ) ;
2021-08-21 00:51:50 +02:00
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarGray ] = ImColor ( 230 , 230 , 230 ) ;
colors [ ImGuiCustomCol_ToolbarRed ] = ImColor ( 231 , 76 , 60 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_ToolbarYellow ] = ImColor ( 241 , 196 , 15 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarGreen ] = ImColor ( 56 , 139 , 66 ) ;
colors [ ImGuiCustomCol_ToolbarBlue ] = ImColor ( 6 , 83 , 155 ) ;
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_ToolbarPurple ] = ImColor ( 103 , 42 , 120 ) ;
2022-02-01 22:09:44 +01:00
colors [ ImGuiCustomCol_ToolbarBrown ] = ImColor ( 219 , 179 , 119 ) ;
2021-08-28 16:02:53 +02:00
2022-01-24 20:53:17 +01:00
colors [ ImGuiCustomCol_Highlight ] = ImColor ( 77 , 198 , 155 ) ;
2021-02-25 00:17:41 +01:00
}
2021-08-18 22:36:46 +02:00
Texture LoadImageFromPath ( const char * path ) {
2022-02-01 22:09:44 +01:00
int imageWidth = 0 ;
2021-04-17 15:46:26 +02:00
int imageHeight = 0 ;
2022-01-24 20:53:17 +01:00
unsigned char * imageData = stbi_load ( path , & imageWidth , & imageHeight , nullptr , 4 ) ;
2021-04-17 15:46:26 +02:00
if ( imageData = = nullptr )
return { nullptr , - 1 , - 1 } ;
2021-08-21 13:55:21 +02:00
2021-04-17 15:46:26 +02:00
GLuint texture ;
glGenTextures ( 1 , & texture ) ;
glBindTexture ( GL_TEXTURE_2D , texture ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ;
2022-01-24 20:53:17 +01:00
# if defined(GL_UNPACK_ROW_LENGTH)
glPixelStorei ( GL_UNPACK_ROW_LENGTH , 0 ) ;
# endif
2021-04-17 15:46:26 +02:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , imageWidth , imageHeight , 0 , GL_RGBA , GL_UNSIGNED_BYTE , imageData ) ;
stbi_image_free ( imageData ) ;
return { reinterpret_cast < ImTextureID > ( static_cast < intptr_t > ( texture ) ) , imageWidth , imageHeight } ;
}
2021-12-22 15:06:16 +01:00
Texture LoadImageFromMemory ( const ImU8 * buffer , int size ) {
2022-02-01 22:09:44 +01:00
int imageWidth = 0 ;
2021-07-31 17:10:19 +02:00
int imageHeight = 0 ;
2022-01-24 20:53:17 +01:00
unsigned char * imageData = stbi_load_from_memory ( buffer , size , & imageWidth , & imageHeight , nullptr , 4 ) ;
2021-07-31 17:10:19 +02:00
if ( imageData = = nullptr )
return { nullptr , - 1 , - 1 } ;
GLuint texture ;
glGenTextures ( 1 , & texture ) ;
glBindTexture ( GL_TEXTURE_2D , texture ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ;
2022-01-24 20:53:17 +01:00
# if defined(GL_UNPACK_ROW_LENGTH)
2021-07-31 17:10:19 +02:00
glPixelStorei ( GL_UNPACK_ROW_LENGTH , 0 ) ;
2022-01-24 20:53:17 +01:00
# endif
2021-07-31 17:10:19 +02:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , imageWidth , imageHeight , 0 , GL_RGBA , GL_UNSIGNED_BYTE , imageData ) ;
stbi_image_free ( imageData ) ;
return { reinterpret_cast < ImTextureID > ( static_cast < intptr_t > ( texture ) ) , imageWidth , imageHeight } ;
}
2021-08-18 22:36:46 +02:00
void UnloadImage ( Texture & texture ) {
if ( texture . textureId = = nullptr )
2021-07-31 17:10:19 +02:00
return ;
2021-08-18 22:36:46 +02:00
auto glTextureId = static_cast < GLuint > ( reinterpret_cast < intptr_t > ( texture . textureId ) ) ;
2021-04-17 15:46:26 +02:00
glDeleteTextures ( 1 , & glTextureId ) ;
2021-08-18 22:36:46 +02:00
texture = { nullptr , 0 , 0 } ;
2021-04-17 15:46:26 +02:00
}
2021-09-28 12:34:55 +02:00
void OpenPopupInWindow ( const char * window_name , const char * popup_name ) {
if ( ImGui : : Begin ( window_name ) ) {
ImGui : : OpenPopup ( popup_name ) ;
}
ImGui : : End ( ) ;
}
2021-08-21 00:51:50 +02:00
2022-01-24 20:53:17 +01:00
bool TitleBarButton ( const char * label , ImVec2 size_arg ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-08-21 00:51:50 +02:00
if ( window - > SkipItems )
return false ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( label ) ;
2021-08-21 00:51:50 +02:00
const ImVec2 label_size = CalcTextSize ( label , NULL , true ) ;
ImVec2 pos = window - > DC . CursorPos ;
ImVec2 size = CalcItemSize ( size_arg , label_size . x + style . FramePadding . x * 2.0f , label_size . y + style . FramePadding . y * 2.0f ) ;
const ImRect bb ( pos , pos + size ) ;
ItemSize ( size , style . FramePadding . y ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held ) ;
// Render
2022-01-24 20:53:17 +01:00
const ImU32 col = GetColorU32 ( ( held & & hovered ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered
: ImGuiCol_Button ) ;
2021-08-21 00:51:50 +02:00
RenderNavHighlight ( bb , id ) ;
RenderFrame ( bb . Min , bb . Max , col , true , style . FrameRounding ) ;
RenderTextClipped ( bb . Min + style . FramePadding * ImVec2 ( 1 , 2 ) , bb . Max - style . FramePadding , label , NULL , & label_size , style . ButtonTextAlign , & bb ) ;
// Automatically close popups
2022-01-24 20:53:17 +01:00
// if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
2021-08-21 00:51:50 +02:00
// CloseCurrentPopup();
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-01-24 20:53:17 +01:00
bool ToolBarButton ( const char * symbol , ImVec4 color ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-08-21 00:51:50 +02:00
if ( window - > SkipItems )
return false ;
color . w = 1.0F ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( symbol ) ;
2021-08-21 00:51:50 +02:00
const ImVec2 label_size = CalcTextSize ( symbol , NULL , true ) ;
ImVec2 pos = window - > DC . CursorPos ;
2021-10-07 11:34:46 +02:00
ImVec2 size = CalcItemSize ( ImVec2 ( 1 , 1 ) * ImGui : : GetCurrentWindow ( ) - > MenuBarHeight ( ) , label_size . x + style . FramePadding . x * 2.0f , label_size . y + style . FramePadding . y * 2.0f ) ;
2021-08-21 00:51:50 +02:00
const ImRect bb ( pos , pos + size ) ;
ItemSize ( size , style . FramePadding . y ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held ) ;
PushStyleColor ( ImGuiCol_Text , color ) ;
// Render
2022-01-24 20:53:17 +01:00
const ImU32 col = GetColorU32 ( ( held & & hovered ) ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered
: ImGuiCol_MenuBarBg ) ;
2021-08-21 00:51:50 +02:00
RenderNavHighlight ( bb , id ) ;
RenderFrame ( bb . Min , bb . Max , col , false , style . FrameRounding ) ;
RenderTextClipped ( bb . Min + style . FramePadding * ImVec2 ( 1 , 2 ) , bb . Max - style . FramePadding , symbol , NULL , & label_size , style . ButtonTextAlign , & bb ) ;
PopStyleColor ( ) ;
// Automatically close popups
2022-01-24 20:53:17 +01:00
// if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
2021-08-21 00:51:50 +02:00
// CloseCurrentPopup();
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-01-24 20:53:17 +01:00
bool IconButton ( const char * symbol , ImVec4 color , ImVec2 size_arg ) {
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-10-07 11:34:46 +02:00
if ( window - > SkipItems )
return false ;
color . w = 1.0F ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2022-02-01 22:09:44 +01:00
const ImGuiID id = window - > GetID ( symbol ) ;
2021-10-07 11:34:46 +02:00
const ImVec2 label_size = CalcTextSize ( symbol , NULL , true ) ;
ImVec2 pos = window - > DC . CursorPos ;
ImVec2 size = CalcItemSize ( size_arg , label_size . x + style . FramePadding . x * 2.0f , label_size . y + style . FramePadding . y * 2.0f ) ;
const ImRect bb ( pos , pos + size ) ;
ItemSize ( size , style . FramePadding . y ) ;
if ( ! ItemAdd ( bb , id ) )
return false ;
bool hovered , held ;
bool pressed = ButtonBehavior ( bb , id , & hovered , & held ) ;
PushStyleColor ( ImGuiCol_Text , color ) ;
// Render
2022-01-24 20:53:17 +01:00
const ImU32 col = GetColorU32 ( ( held & & hovered ) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered
: ImGuiCol_Button ) ;
2021-10-07 11:34:46 +02:00
RenderNavHighlight ( bb , id ) ;
RenderFrame ( bb . Min , bb . Max , col , false , style . FrameRounding ) ;
RenderTextClipped ( bb . Min + style . FramePadding * ImVec2 ( 1 , 2 ) , bb . Max - style . FramePadding , symbol , NULL , & label_size , style . ButtonTextAlign , & bb ) ;
PopStyleColor ( ) ;
// Automatically close popups
2022-01-24 20:53:17 +01:00
// if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
2021-10-07 11:34:46 +02:00
// CloseCurrentPopup();
IMGUI_TEST_ENGINE_ITEM_INFO ( id , label , window - > DC . LastItemStatusFlags ) ;
return pressed ;
}
2022-02-08 22:25:25 +01:00
bool InputIntegerPrefix ( const char * label , const char * prefix , u64 * value , ImGuiInputTextFlags flags ) {
2022-02-08 21:10:48 +01:00
auto window = ImGui : : GetCurrentWindow ( ) ;
const ImGuiID id = window - > GetID ( label ) ;
const ImGuiStyle & style = GImGui - > Style ;
const ImVec2 label_size = CalcTextSize ( label , nullptr , true ) ;
const ImVec2 frame_size = CalcItemSize ( ImVec2 ( 0 , 0 ) , CalcTextSize ( prefix ) . x , label_size . y + style . FramePadding . y * 2.0f ) ;
const ImRect frame_bb ( window - > DC . CursorPos , window - > DC . CursorPos + frame_size ) ;
2022-02-08 21:51:09 +01:00
ImGui : : SetCursorPosX ( ImGui : : GetCursorPosX ( ) + frame_size . x ) ;
char buf [ 64 ] ;
DataTypeFormatString ( buf , IM_ARRAYSIZE ( buf ) , ImGuiDataType_U64 , value , " %llX " ) ;
bool value_changed = false ;
if ( InputTextEx ( label , nullptr , buf , IM_ARRAYSIZE ( buf ) , ImVec2 ( CalcItemWidth ( ) - frame_size . x , label_size . y + style . FramePadding . y * 2.0f ) , flags ) )
value_changed = DataTypeApplyOpFromText ( buf , GImGui - > InputTextState . InitialTextA . Data , ImGuiDataType_U64 , value , " %llX " ) ;
if ( value_changed )
MarkItemEdited ( GImGui - > LastItemData . ID ) ;
2022-02-08 21:10:48 +01:00
RenderNavHighlight ( frame_bb , id ) ;
RenderFrame ( frame_bb . Min , frame_bb . Max , GetColorU32 ( ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
2022-02-08 22:19:54 +01:00
ImGui : : PushStyleVar ( ImGuiStyleVar_Alpha , 0.6F ) ;
2022-02-08 21:10:48 +01:00
RenderText ( ImVec2 ( frame_bb . Min . x + style . FramePadding . x , frame_bb . Min . y + style . FramePadding . y ) , prefix ) ;
2022-02-08 22:19:54 +01:00
ImGui : : PopStyleVar ( ) ;
2022-02-08 21:10:48 +01:00
2022-02-08 21:51:09 +01:00
return value_changed ;
2022-02-08 21:10:48 +01:00
}
2022-02-08 22:25:25 +01:00
bool InputHexadecimal ( const char * label , u64 * value , ImGuiInputTextFlags flags ) {
2022-02-08 21:10:48 +01:00
return InputIntegerPrefix ( label , " 0x " , value , flags | ImGuiInputTextFlags_CharsHexadecimal ) ;
}
2021-12-16 23:48:52 +01:00
void SmallProgressBar ( float fraction , float yOffset ) {
2022-01-24 20:53:17 +01:00
ImGuiWindow * window = GetCurrentWindow ( ) ;
2021-12-16 23:48:52 +01:00
if ( window - > SkipItems )
return ;
2022-02-01 22:09:44 +01:00
ImGuiContext & g = * GImGui ;
2022-01-24 20:53:17 +01:00
const ImGuiStyle & style = g . Style ;
2021-12-16 23:48:52 +01:00
2022-02-01 22:09:44 +01:00
ImVec2 pos = window - > DC . CursorPos + ImVec2 ( 0 , yOffset ) ;
2021-12-16 23:48:52 +01:00
ImVec2 size = CalcItemSize ( ImVec2 ( 100 , 5 ) , 100 , g . FontSize + style . FramePadding . y * 2.0f ) ;
ImRect bb ( pos , pos + size ) ;
ItemSize ( size , 0 ) ;
if ( ! ItemAdd ( bb , 0 ) )
return ;
// Render
fraction = ImSaturate ( fraction ) ;
RenderFrame ( bb . Min , bb . Max , GetColorU32 ( ImGuiCol_FrameBg ) , true , style . FrameRounding ) ;
bb . Expand ( ImVec2 ( - style . FrameBorderSize , - style . FrameBorderSize ) ) ;
const ImVec2 fill_br = ImVec2 ( ImLerp ( bb . Min . x , bb . Max . x , fraction ) , bb . Max . y ) ;
RenderRectFilledRangeH ( window - > DrawList , bb , GetColorU32 ( ImGuiCol_PlotHistogram ) , 0.0f , fraction , style . FrameRounding ) ;
}
2021-01-27 00:00:20 +01:00
}