Add cimgui

This commit is contained in:
2023-11-10 09:48:29 +01:00
parent fb7c0831da
commit 5c9b80a598
271 changed files with 222188 additions and 0 deletions

View File

@@ -0,0 +1,392 @@
/*******************************************************************************************
*
* raylib-extras [ImGui] example - editor
*
* This is a more complex ImGui Integration
* It shows how to build windows on top of 2d and 3d views using a render texture
*
* Copyright (c) 2021 Jeffery Myers
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#include "imgui.h"
#include "rlImGui.h"
#include "rlImGuiColors.h"
bool Quit = false;
bool ImGuiDemoOpen = false;
class DocumentWindow
{
public:
bool Open = false;
RenderTexture ViewTexture;
virtual void Setup() = 0;
virtual void Shutdown() = 0;
virtual void Show() = 0;
virtual void Update() = 0;
bool Focused = false;
Rectangle ContentRect = { 0 };
};
class ImageViewerWindow : public DocumentWindow
{
public:
void Setup() override
{
Camera.zoom = 1;
Camera.target.x = 0;
Camera.target.y = 0;
Camera.rotation = 0;
Camera.offset.x = GetScreenWidth() / 2.0f;
Camera.offset.y = GetScreenHeight() / 2.0f;
ViewTexture = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
ImageTexture = LoadTexture("resources/parrots.png");
UpdateRenderTexture();
}
void Show() override
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::SetNextWindowSizeConstraints(ImVec2(400, 400), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));
Focused = false;
if (ImGui::Begin("Image Viewer", &Open, ImGuiWindowFlags_NoScrollbar))
{
// save off the screen space content rectangle
ContentRect = { ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMin().x, ImGui::GetWindowPos().y + ImGui::GetWindowContentRegionMin().y, ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y };
Focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
ImVec2 size = ImGui::GetContentRegionAvail();
// center the scratch pad in the view
Rectangle viewRect = { 0 };
viewRect.x = ViewTexture.texture.width / 2 - size.x / 2;
viewRect.y = ViewTexture.texture.height / 2 - size.y / 2;
viewRect.width = size.x;
viewRect.height = -size.y;
if (ImGui::BeginChild("Toolbar", ImVec2(ImGui::GetContentRegionAvail().x, 25)))
{
ImGui::SetCursorPosX(2);
ImGui::SetCursorPosY(3);
if (ImGui::Button("None"))
{
CurrentToolMode = ToolMode::None;
}
ImGui::SameLine();
if (ImGui::Button("Move"))
{
CurrentToolMode = ToolMode::Move;
}
ImGui::SameLine();
switch (CurrentToolMode)
{
case ToolMode::None:
ImGui::TextUnformatted("No Tool");
break;
case ToolMode::Move:
ImGui::TextUnformatted("Move Tool");
break;
default:
break;
}
ImGui::SameLine();
ImGui::TextUnformatted(TextFormat("camera target X%f Y%f", Camera.target.x, Camera.target.y));
ImGui::EndChild();
}
rlImGuiImageRect(&ViewTexture.texture, (int)size.x, (int)size.y, viewRect);
}
ImGui::End();
ImGui::PopStyleVar();
}
void Update() override
{
if (!Open)
return;
if (IsWindowResized())
{
UnloadRenderTexture(ViewTexture);
ViewTexture = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
Camera.offset.x = GetScreenWidth() / 2.0f;
Camera.offset.y = GetScreenHeight() / 2.0f;
}
Vector2 mousePos = GetMousePosition();
if (Focused)
{
if (CurrentToolMode == ToolMode::Move)
{
// only do this tool when the mouse is in the content area of the window
if (IsMouseButtonDown(0) && CheckCollisionPointRec(mousePos, ContentRect))
{
if (!Dragging)
{
LastMousePos = mousePos;
LastTarget = Camera.target;
}
Dragging = true;
Vector2 mouseDelta = Vector2Subtract(LastMousePos, mousePos);
mouseDelta.x /= Camera.zoom;
mouseDelta.y /= Camera.zoom;
Camera.target = Vector2Add(LastTarget, mouseDelta);
DirtyScene = true;
}
else
{
Dragging = false;
}
}
}
else
{
Dragging = false;
}
if (DirtyScene)
{
DirtyScene = false;
UpdateRenderTexture();
}
}
Texture ImageTexture;
Camera2D Camera = { 0 };
Vector2 LastMousePos = { 0 };
Vector2 LastTarget = { 0 };
bool Dragging = false;
bool DirtyScene = false;
enum class ToolMode
{
None,
Move,
};
ToolMode CurrentToolMode = ToolMode::None;
void UpdateRenderTexture()
{
BeginTextureMode(ViewTexture);
ClearBackground(BLUE);
// camera with our view offset with a world origin of 0,0
BeginMode2D(Camera);
// center the image at 0,0
DrawTexture(ImageTexture, ImageTexture.width / -2, ImageTexture.height / -2, WHITE);
EndMode2D();
EndTextureMode();
}
void Shutdown() override
{
UnloadRenderTexture(ViewTexture);
UnloadTexture(ImageTexture);
}
};
class SceneViewWindow : public DocumentWindow
{
public:
Camera3D Camera = { 0 };
void Setup() override
{
ViewTexture = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
Camera.fovy = 45;
Camera.up.y = 1;
Camera.position.y = 3;
Camera.position.z = -25;
Image img = GenImageChecked(256, 256, 32, 32, DARKGRAY, WHITE);
GridTexture = LoadTextureFromImage(img);
UnloadImage(img);
GenTextureMipmaps(&GridTexture);
SetTextureFilter(GridTexture, TEXTURE_FILTER_ANISOTROPIC_16X);
SetTextureWrap(GridTexture, TEXTURE_WRAP_CLAMP);
}
void Shutdown() override
{
UnloadRenderTexture(ViewTexture);
UnloadTexture(GridTexture);
}
void Show() override
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::SetNextWindowSizeConstraints(ImVec2(400, 400), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));
if (ImGui::Begin("3D View", &Open, ImGuiWindowFlags_NoScrollbar))
{
Focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows);
// draw the view
rlImGuiImageRenderTextureFit(&ViewTexture, true);
}
ImGui::End();
ImGui::PopStyleVar();
}
void Update() override
{
if (!Open)
return;
if (IsWindowResized())
{
UnloadRenderTexture(ViewTexture);
ViewTexture = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
}
float period = 10;
float magnitude = 25;
Camera.position.x = (float)(sinf((float)GetTime() / period) * magnitude);
BeginTextureMode(ViewTexture);
ClearBackground(SKYBLUE);
BeginMode3D(Camera);
// grid of cube trees on a plane to make a "world"
DrawPlane(Vector3{ 0, 0, 0 }, Vector2{ 50, 50 }, BEIGE); // simple world plane
float spacing = 4;
int count = 5;
for (float x = -count * spacing; x <= count * spacing; x += spacing)
{
for (float z = -count * spacing; z <= count * spacing; z += spacing)
{
Vector3 pos = { x, 0.5f, z };
Vector3 min = { x - 0.5f,0,z - 0.5f };
Vector3 max = { x + 0.5f,1,z + 0.5f };
DrawCube(Vector3{ x, 1.5f, z }, 1, 1, 1, GREEN);
DrawCube(Vector3{ x, 0.5f, z }, 0.25f, 1, 0.25f, BROWN);
}
}
EndMode3D();
EndTextureMode();
}
Texture2D GridTexture = { 0 };
};
ImageViewerWindow ImageViewer;
SceneViewWindow SceneView;
void DoMainMenu()
{
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Exit"))
Quit = true;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Window"))
{
ImGui::MenuItem("ImGui Demo", nullptr, &ImGuiDemoOpen);
ImGui::MenuItem("Image Viewer", nullptr, &ImageViewer.Open);
ImGui::MenuItem("3D View", nullptr, &SceneView.Open);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
int main(int argc, char* argv[])
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 1900;
int screenHeight = 900;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "raylib-Extras [ImGui] example - ImGui Demo");
SetTargetFPS(144);
rlImGuiSetup(true);
ImGui::GetIO().ConfigWindowsMoveFromTitleBarOnly = true;
ImageViewer.Setup();
ImageViewer.Open = true;
SceneView.Setup();
SceneView.Open = true;
// Main game loop
while (!WindowShouldClose() && !Quit) // Detect window close button or ESC key
{
ImageViewer.Update();
SceneView.Update();
BeginDrawing();
ClearBackground(DARKGRAY);
rlImGuiBegin();
DoMainMenu();
if (ImGuiDemoOpen)
ImGui::ShowDemoWindow(&ImGuiDemoOpen);
if (ImageViewer.Open)
ImageViewer.Show();
if (SceneView.Open)
SceneView.Show();
rlImGuiEnd();
EndDrawing();
//----------------------------------------------------------------------------------
}
rlImGuiShutdown();
ImageViewer.Shutdown();
SceneView.Shutdown();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View File

@@ -0,0 +1,124 @@
// Dear ImGui: standalone example application for Raylib with OpenGL
// (Raylib is a simple learning library for game development)
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
// - Getting Started https://dearimgui.com/getting-started
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
// - Introduction, links and more at the top of imgui.cpp
#include "imgui.h"
#include "imgui_impl_raylib.h"
#include "raylib.h"
// Main code
int main(int, char**)
{
// Setup raylib window
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI);
InitWindow(1280, 720, "Dear ImGui Raylib(OpenGL) example");
// Setup Dear ImGui context
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplRaylib_Init();
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// Main loop
bool done = false;
while (!done)
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
ImGui_ImplRaylib_ProcessEvents();
// Start the Dear ImGui frame
ImGui_ImplRaylib_NewFrame();
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
// 3. Show another simple window.
if (show_another_window)
{
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
show_another_window = false;
ImGui::End();
}
// Rendering
ImGui::Render();
BeginDrawing();
ClearBackground(Color{ (unsigned char)(clear_color.x * 255), (unsigned char)(clear_color.y * 255),(unsigned char)(clear_color.z * 255),(unsigned char)(clear_color.w * 255) });
ImGui_ImplRaylib_RenderDrawData(ImGui::GetDrawData());
EndDrawing();
done = WindowShouldClose();
}
// Cleanup
ImGui_ImplRaylib_Shutdown();
ImGui::DestroyContext();
CloseWindow();
return 0;
}

View File

@@ -0,0 +1,59 @@
/*******************************************************************************************
*
* raylib-extras [ImGui] example - Simple Integration
*
* This is a simple ImGui Integration
* It is done using C++ but with C style code
* It can be done in C as well if you use the C ImGui wrapper
* https://github.com/cimgui/cimgui
*
* Copyright (c) 2021 Jeffery Myers
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#include "imgui.h"
#include "rlImGui.h"
int main(int argc, char* argv[])
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 1280;
int screenHeight = 800;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "raylib-Extras [ImGui] example - simple ImGui Demo");
SetTargetFPS(144);
rlImGuiSetup(true);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(DARKGRAY);
// start ImGui Conent
rlImGuiBegin();
// show ImGui Content
bool open = true;
ImGui::ShowDemoWindow(&open);
// end ImGui Content
rlImGuiEnd();
EndDrawing();
//----------------------------------------------------------------------------------
}
rlImGuiShutdown();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}