Skip to content

Instantly share code, notes, and snippets.

@JeffM2501
Created April 21, 2021 15:14
Show Gist options
  • Save JeffM2501/4c3a7e8a85302f743f8bd9dc1aae00ae to your computer and use it in GitHub Desktop.
Save JeffM2501/4c3a7e8a85302f743f8bd9dc1aae00ae to your computer and use it in GitHub Desktop.
Example of how to do editor style docking in ImGui for raylib
/*******************************************************************************************
*
* raylib [core] example - Third Person Orbit Camera Example
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#include "imgui.h"
#include "rlImGui.h"
int main(int argc, char* argv[])
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 1900;
int screenHeight = 900;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE);
InitWindow(screenWidth, screenHeight, "raylib [ImGui] example - ImGui Demo");
SetTargetFPS(144);
SetupRLImGui(true);
// setup a background texture
Image img = GenImageChecked(64, 64, 16, 16, DARKGRAY, GRAY);
Texture bg = LoadTextureFromImage(img);
UnloadImage(img);
// flag so we can quit the game from the file menu
bool quit = false;
// Main game loop
while (!WindowShouldClose() && !quit) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(DARKGRAY);
// fill the background with something interesting
DrawTextureRec(bg, Rectangle{ 0, 0, float(GetScreenWidth()), float(GetScreenHeight()) }, Vector2Zero(), WHITE);
DrawRectanglePro(Rectangle{ GetScreenWidth() * 0.5f, GetScreenHeight() * 0.5f,200,200 }, Vector2{ 100,100 }, float(GetTime()) * 45, ColorAlpha(RED,0.75f));
// start the GUI
BeginRLImGui();
// create an ImGui window that covers the entire viewport, so that we can have a menu bar at the top of the applications
ImGui::SetNextWindowPos(ImVec2(0, 0)); // always at the window origin
ImGui::SetNextWindowSize(ImVec2(float(GetScreenWidth()), float(GetScreenHeight()))); // always at the window size
ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoBringToFrontOnFocus | // we just want to use this window as a host for the menubar and docking
ImGuiWindowFlags_NoNavFocus | // so turn off everything that would make it act like a window
ImGuiWindowFlags_NoDocking |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_MenuBar |
ImGuiWindowFlags_NoBackground; // we want our game content to show through this window, so turn off the background.
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // we don't want any padding for windows docked to this window frame
bool show = (ImGui::Begin("Main", NULL, windowFlags)); // show the "window"
ImGui::PopStyleVar(); // restore the style so inner windows have fames
// create a docking space inside our inner window that lets prevents anything from docking in the central node (so we can see our game content)
ImGui::DockSpace(ImGui::GetID("Dockspace"), ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_PassthruCentralNode);
if (show)
{
// Do a menu bar with an exit menu
if (ImGui::BeginMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Exit"))
quit = true;
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
}
ImGui::End();
// show any other windows that we want to be dockable
ImGui::ShowDemoWindow(NULL);
// end ImGui
EndRLImGui();
// Finish drawing
DrawFPS(0, GetScreenHeight() - 20);
EndDrawing();
//----------------------------------------------------------------------------------
}
ShutdownRLImGui();
UnloadTexture(bg);
CloseWindow();
return 0;
}
@EVWTRENTINI
Copy link

Any update on this? I am having the same problem with the docking + drawing stuff with Raylib.

No, I just abandoned docking.

@JeffM2501
Copy link
Author

Sorry, just saw this.
Your stuff is being drawn it's just drawn behind the dockspace central node that is created for the viewport.
You use specific flags to control that.
Set

ImGui::DockSpaceOverViewport(0,  NULL, ImGuiDockNodeFlags_PassthruCentralNode);

And that will remove the gray tint applied to the central node of the dockspace.
It works just fine.
image

Alternatively you can create an undecorated window with no background and attach a dockspace to it.

These are not issues with rlImGui, simply how docking branch works in ImGui.

@EVWTRENTINI
Copy link

Thanks for the answer @JeffM2501, I'll go back to using the docking branch now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment