Skip to content

Commit

Permalink
Add SetImageBackground
Browse files Browse the repository at this point in the history
  • Loading branch information
soufianekhiat committed Jun 11, 2024
1 parent 722426f commit a574801
Showing 7 changed files with 90 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -10,3 +10,6 @@
[submodule "extern/ImPlatform"]
path = extern/ImPlatform
url = https://github.com/soufianekhiat/ImPlatform.git
[submodule "extern/stb"]
path = extern/stb
url = https://github.com/nothings/stb.git
1 change: 1 addition & 0 deletions extern/stb
Submodule stb added at 013ac3
1 change: 1 addition & 0 deletions sharpmakes/DemoProject.cs
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ public void ConfigureDemo(Configuration conf, DearTarget target)
conf.TargetPath = RootPath + "/WorkingDir";

conf.IncludePaths.Add(@"[project.RootPath]/src/demo/");
conf.IncludePaths.Add(@"[project.RootPath]/extern/stb/");
conf.IncludePaths.Add(@"[project.RootPath]/extern/imgui/");
conf.IncludePaths.Add(@"[project.RootPath]/extern/imgui/examples/libs/glfw/include/");
conf.LibraryPaths.Add(@"[project.RootPath]/extern/imgui/examples/libs/glfw/");
53 changes: 53 additions & 0 deletions src/api/dear_widgets.cpp
Original file line number Diff line number Diff line change
@@ -2662,4 +2662,57 @@ namespace ImWidgets {

return bModified;
}

//////////////////////////////////////////////////////////////////////////
// Window Customization
//////////////////////////////////////////////////////////////////////////
bool SetImageBackground( ImTextureID id, ImVec2 imgSize, bool fixedSize, ImU32 col )
{
float ar = imgSize.x / imgSize.y;
ImGuiWindow* window = ImGui::GetCurrentWindow();
//ImDrawList* drawList = window->DrawList;
ImDrawList* drawList = ImGui::GetBackgroundDrawList();
ImVec2 cur = window->DC.CursorStartPos - window->WindowPadding - ImVec2( 0.0f, window->TitleBarHeight + window->MenuBarHeight );
ImVec2 uv;
ImVec2 winSize = ImGui::GetWindowSize();

if ( fixedSize )
{
uv.x = winSize.x / imgSize.x;
uv.y = winSize.y / imgSize.y;
}
else
{
if ( winSize.x > winSize.y )
{
if ( imgSize.x > imgSize.y )
{
uv.x = 1.0f;
uv.y = winSize.y / winSize.x;
}
else
{
uv.x = winSize.y / winSize.x;
uv.y = 1.0f;
}
}
else
{
if ( imgSize.x > imgSize.y )
{
uv.x = winSize.x / winSize.y;
uv.y = 1.0f;
}
else
{
uv.x = 1.0f;
uv.y = winSize.y / winSize.x;
}
}
}

drawList->AddImage( id, cur, cur + winSize, ImVec2( 0.0f, 0.0f ), uv, col );

return true;
}
}
6 changes: 6 additions & 0 deletions src/api/dear_widgets.h
Original file line number Diff line number Diff line change
@@ -607,4 +607,10 @@ namespace ImWidgets{
//////////////////////////////////////////////////////////////////////////
IMGUI_API bool HueSelector( char const* label, float hueHeight, float cursorHeight, float* hueCenter, float* hueWidth, float* featherLeft, float* featherRight, int division = 32, float alpha = 1.0f, float hideHueAlpha = 0.75f, float offset = 0.0f );
IMGUI_API bool Slider2DScalar( char const* pLabel, ImGuiDataType data_type, void* pValueX, void* pValueY, void* p_minX, void* p_maxX, void* p_minY, void* p_maxY, float const fScale = 1.0f );

//////////////////////////////////////////////////////////////////////////
// Window Customization
//////////////////////////////////////////////////////////////////////////
// Note: it will break the rounding.
IMGUI_API bool SetImageBackground( ImTextureID id, ImVec2 imgSize, bool fixedSize = false, ImU32 col = IM_COL32( 255, 255, 255, 255 ) );
}
27 changes: 26 additions & 1 deletion src/demo/demo.cpp
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@
#include <vector>
#include <random>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

static int grid_rows = 8;
static int grid_columns = 8;
static ImVector<float> grid_values;
@@ -127,6 +130,8 @@ ImU32 sdHorseshoeColor(ImVec2 p, float fTime)
}
#pragma endregion ShaderToyHelper

ImTextureID background;
ImVec2 background_size;
int main()
{
if ( !ImPlatform::ImSimpleStart( "Dear Widgets Demo", ImVec2( 0.0f, 0.0f ), 1024 * 2, 764 * 2) )
@@ -165,6 +170,21 @@ int main()
// style.Colors[ImGuiCol_WindowBg].w = 1.0f;
//}

int width;
int height;
// Image from: https://www.pexels.com/fr-fr/photo/deux-chaises-avec-table-en-verre-sur-le-salon-pres-de-la-fenetre-1571453/
stbi_uc* data = stbi_load( "pexels-fotoaibe-1571453.jpg", &width, &height, NULL, 4 );
background = ImPlatform::ImCreateTexture2D( ( char* )data, width, height,
{
ImPlatform::IM_RGBA,
ImPlatform::IM_TYPE_UINT8,
ImPlatform::IM_FILTERING_LINEAR,
ImPlatform::IM_BOUNDARY_CLAMP,
ImPlatform::IM_BOUNDARY_CLAMP
} );
background_size = ImVec2( ( float )width, ( float )height );
STBI_FREE( data );

ImVec4 clear_color = ImVec4( 0.461f, 0.461f, 0.461f, 1.0f );
while ( ImPlatform::ImPlatformContinue() )
{
@@ -186,6 +206,8 @@ int main()

ImPlatform::ImSimpleFinish();

ImPlatform::ImReleaseTexture2D( background );

return 0;
}

@@ -196,7 +218,10 @@ namespace ImWidgets {
static float f = 0.0f;
static int counter = 0;

ImGui::Begin("Dear Widgets");
ImGui::SetNextWindowBgAlpha( 0.75f );
ImGui::Begin( "Dear Widgets", NULL, ImGuiWindowFlags_NoTitleBar );

ImWidgets::SetImageBackground( background, background_size, false );

if (ImGui::TreeNode("Draw"))
{
Binary file added workingdir/pexels-fotoaibe-1571453.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a574801

Please sign in to comment.