Browse Source

add function to draw a render texture fit to the content area

Jeffery Myers 1 year ago
parent
commit
b202e0f4e2
3 changed files with 38 additions and 1 deletions
  1. 2 1
      examples/editor.cpp
  2. 28 0
      rlImGui.cpp
  3. 8 0
      rlImGui.h

+ 2 - 1
examples/editor.cpp

@@ -256,7 +256,8 @@ public:
 			viewRect.height = -size.y;
 			viewRect.height = -size.y;
 
 
 			// draw the view
 			// draw the view
-			rlImGuiImageRect(&ViewTexture.texture, (int)size.x, (int)size.y, viewRect);
+			rlImGuiImageRenderTextureFit(&ViewTexture, true);
+			//rlImGuiImageRect(&ViewTexture.texture, (int)size.x, (int)size.y, viewRect);
 		}
 		}
 		ImGui::End();
 		ImGui::End();
 		ImGui::PopStyleVar();
 		ImGui::PopStyleVar();

+ 28 - 0
rlImGui.cpp

@@ -596,3 +596,31 @@ void rlImGuiImageRenderTexture(const RenderTexture* image)
 
 
     rlImGuiImageRect(&image->texture, image->texture.width, image->texture.height, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
     rlImGuiImageRect(&image->texture, image->texture.width, image->texture.height, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
 }
 }
+
+void rlImGuiImageRenderTextureFit(const RenderTexture* image, bool center)
+{
+	if (!image)
+		return;
+
+    ImVec2 area = ImGui::GetContentRegionAvail();
+
+    float scale =  area.x / image->texture.width;
+
+    float y = image->texture.height * scale;
+    if (y > area.y)
+    {
+        scale = area.y / image->texture.height;
+    }
+
+    int sizeX = int(image->texture.width * scale);
+    int sizeY = int(image->texture.height * scale);
+
+    if (center)
+    {
+        ImGui::SetCursorPosX(0);
+        ImGui::SetCursorPosX(area.x/2 - sizeX/2);
+        ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (area.y / 2 - sizeY / 2));
+    }
+
+    rlImGuiImageRect(&image->texture, sizeX, sizeY, Rectangle{ 0,0, float(image->texture.width), -float(image->texture.height) });
+}

+ 8 - 0
rlImGui.h

@@ -137,6 +137,14 @@ void rlImGuiImageRect(const Texture* image, int destWidth, int destHeight, Recta
 /// <param name="image">The render texture to draw</param>
 /// <param name="image">The render texture to draw</param>
 void rlImGuiImageRenderTexture(const RenderTexture* image);
 void rlImGuiImageRenderTexture(const RenderTexture* image);
 
 
+/// <summary>
+/// Draws a render texture as an image an ImGui Context, automatically flipping the Y axis so it will show correctly on screen
+/// Fits the render texture to the available content area
+/// </summary>
+/// <param name="image">The render texture to draw</param>
+/// <param name="center">When true the image will be centered in the content area</param>
+void rlImGuiImageRenderTextureFit(const RenderTexture* image, bool center);
+
 
 
 /// <summary>
 /// <summary>
 /// Draws a texture as an image button in an ImGui context. Uses the current ImGui cursor position and the full size of the texture
 /// Draws a texture as an image button in an ImGui context. Uses the current ImGui cursor position and the full size of the texture