Skip to content

Commit

Permalink
Editor : Sectors visualisation & zFloor / zCeiling edition
Browse files Browse the repository at this point in the history
  • Loading branch information
P0ulpy committed Jul 6, 2024
1 parent 67d66b4 commit 0131766
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 79 deletions.
211 changes: 151 additions & 60 deletions src/Editor/WorldEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,90 +29,181 @@ class WorldEditor

void DrawGUI()
{
ImGui::Begin("World Editor");
ImGuiID worldEditorDockerSpace = ImGui::GetID("worldEditorDockerSpace");
ImGui::DockSpace(worldEditorDockerSpace);
ImGui::End();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
RenderSectorsGui();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
RenderViewportGui();
}

void Update(float dt)
{

}

void Render(RaycastingCamera& cam)
{
Camera2D minimapCamera = { 0 };
minimapCamera.target = cam.position;
minimapCamera.offset = { (float)renderTexture.texture.width / 2, (float)renderTexture.texture.height / 2 };
minimapCamera.rotation = 0.0f;
minimapCamera.zoom = 1.f;//zoom;

BeginTextureMode(renderTexture);

BeginMode2D(minimapCamera);

ClearBackground(LIGHTGRAY);

Vector2 camHeadingDirectionPoint = Vector2Add(cam.position, Vector2Scale(cam.Forward(), 50));
DrawLineV(cam.position, camHeadingDirectionPoint, RED);
DrawCircleV(cam.position, 10, GREEN);

for(const auto& [ sectorId, sector ] : world.Sectors)
{
bool sectorSelected = sectorId == currentSelectedSector;

Vector2 selectionMin = { (float)renderTexture.texture.width, (float)renderTexture.texture.height };
Vector2 selectionMax = { 0, 0 };

for(const auto& wall : sector.walls)
{
Color color = WHITE;
if(sectorSelected)
{
color = RED;
if(wall.toSector != NULL_SECTOR)
{
color = BLUE;
}
}
else if(wall.toSector != NULL_SECTOR) color = PURPLE;

DrawLineV(wall.segment.a, wall.segment.b, color);

if(sectorSelected)
{
if(wall.segment.a.x < selectionMin.x)
selectionMin.x = wall.segment.a.x;
if(wall.segment.b.x < selectionMin.x)
selectionMin.x = wall.segment.b.x;

if(wall.segment.a.y < selectionMin.y)
selectionMin.y = wall.segment.a.y;
if(wall.segment.b.y < selectionMin.y)
selectionMin.y = wall.segment.b.y;

if(wall.segment.a.x > selectionMax.x)
selectionMax.x = wall.segment.a.x;
if(wall.segment.b.x > selectionMax.x)
selectionMax.x = wall.segment.b.x;

if(wall.segment.a.y > selectionMax.y)
selectionMax.y = wall.segment.a.y;
if(wall.segment.b.y > selectionMax.y)
selectionMax.y = wall.segment.b.y;
}
}

if(sectorSelected)
{
static const float padding = 10;

selectionMin.x -= padding;
selectionMin.y -= padding;
selectionMax.x += padding;
selectionMax.y += padding;

int width = selectionMax.x - selectionMin.x;
int height = selectionMax.y - selectionMin.y;

DrawRectangleLines(selectionMin.x, selectionMin.y, width, height, ColorAlpha(RED, .5f));
}
}

EndMode2D();

EndTextureMode();
}

private:
void RenderViewportGui()
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::SetNextWindowSizeConstraints(ImVec2(renderTexture.texture.width, renderTexture.texture.height), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));





if (ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar))
{
ImGui::Begin("World Editor");
ImGuiID worldEditorDockerSpace = ImGui::GetID("worldEditorDockerSpace");
ImGui::DockSpace(worldEditorDockerSpace);
ImGui::End();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
ImGui::Begin("Sectors");

ImGui::Text("salut");

ImGui::End();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
// ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
// ImGui::SetNextWindowSizeConstraints(ImVec2(renderTexture.texture.width, renderTexture.texture.height), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));

if (ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar))
ImVec2 windowSize = ImGui::GetWindowSize();
if(windowSize.x != renderTexture.texture.width || windowSize.y != renderTexture.texture.height)
{
// ImVec2 windowSize = ImGui::GetWindowSize();
// if(windowSize.x != renderTexture.texture.width || windowSize.y != renderTexture.texture.height)
// {
// ResizeRenderTextureSize(windowSize.x, windowSize.y);
// }

// rlImGuiImageRenderTextureFit(&renderTexture, true);
ResizeRenderTextureSize(windowSize.x, windowSize.y);
}
ImGui::End();

// ImGui::PopStyleVar();
rlImGuiImageRenderTextureFit(&renderTexture, true);
}
ImGui::End();





// ImGui::Begin("World Editor", nullptr, ImGuiWindowFlags_DockNodeHost);

// ImGui::Begin("Sectors");
// ImGui::Text("salut");
// ImGui::End();

// ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
// ImGui::SetNextWindowSizeConstraints(ImVec2(renderTexture.texture.width, renderTexture.texture.height), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));

// if (ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar))
// {
// ImVec2 windowSize = ImGui::GetWindowSize();
// if(windowSize.x != renderTexture.texture.width || windowSize.y != renderTexture.texture.height)
// {
// ResizeRenderTextureSize(windowSize.x, windowSize.y);
// }

// rlImGuiImageRenderTextureFit(&renderTexture, true);
// }
// ImGui::End();

// ImGui::PopStyleVar();

// ImGui::End();
ImGui::PopStyleVar();
}

void Render(RaycastingCamera& cam)
void RenderSectorsGui()
{
using namespace std::string_literals;

ImGui::Begin("Sectors");

for(auto& [ sectorId, sector ] : world.Sectors)
{
static const ImGuiTreeNodeFlags BaseSectorFlags =
ImGuiTreeNodeFlags_OpenOnArrow
| ImGuiTreeNodeFlags_OpenOnDoubleClick
| ImGuiTreeNodeFlags_SpanAvailWidth;

ImGuiTreeNodeFlags sectorFlags = BaseSectorFlags;
if (currentSelectedSector == sectorId)
{
sectorFlags |= ImGuiTreeNodeFlags_Selected;
}

std::string label = "Sector - "s + std::to_string(sectorId);
bool sectorOpen = ImGui::TreeNodeEx(label.c_str(), sectorFlags);

if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
{
currentSelectedSector = sectorId;
}

if (sectorOpen)
{
RenderSectorContentGui(sector);
ImGui::TreePop();
}
}

ImGui::End();
}

void Update(float dt)
void RenderSectorContentGui(Sector& sector)
{
ImGui::SliderFloat("zCeiling", &sector.zCeiling, -10, 10);
ImGui::SliderFloat("zFloor", &sector.zFloor, -10, 10);

for(size_t i = 0; i < sector.walls.size(); ++i)
{
ImGui::Text("[%d] => %d", i, sector.walls[i].toSector);

Check warning on line 199 in src/Editor/WorldEditor.hpp

View workflow job for this annotation

GitHub Actions / macos-latest-gcc-Release-Build

format specifies type 'int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]

Check warning on line 199 in src/Editor/WorldEditor.hpp

View workflow job for this annotation

GitHub Actions / macos-latest-gcc-Debug-Build

format specifies type 'int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
}
}

private:
World& world;
RenderTexture2D renderTexture;

// State
SectorID currentSelectedSector = NULL_SECTOR;
};
15 changes: 14 additions & 1 deletion src/RaycastingCameraViewport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ class RaycastingCameraViewport
}
}

focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows);
bool isFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows);
if(isFocused && !focused)
{
SetMousePosition(GetScreenWidth() / 2, GetScreenHeight() / 2);
}

focused = isFocused;

// draw the view
rlImGuiImageRenderTextureFit(&renderTexture, true);

if(IsFocused() && IsKeyPressed(KEY_ESCAPE))
{
ImGui::SetFocusID(ImGui::GetID("Game Player"), ImGui::GetCurrentWindow());
focused = false;
}
}
ImGui::End();

Expand Down
26 changes: 8 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <rlImGui.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_stdlib.h>
#include "ImGuiStyle.hpp"

Expand Down Expand Up @@ -71,8 +72,6 @@ int main()
WorldEditor worldEditor(world);

RenderingOrchestrator renderingOrchestrator(cameraViewport);

bool lockCursor = false;

while (!WindowShouldClose())
{
Expand All @@ -82,12 +81,6 @@ int main()

// Inputs

if(IsKeyPressed(KEY_ESCAPE))
{
lockCursor = !lockCursor;
SetMousePosition(GetScreenWidth() / 2, GetScreenHeight() / 2);
}

// Update

{
Expand All @@ -107,25 +100,22 @@ int main()
}
}

if(cameraViewport.IsFocused() && lockCursor)
if(cameraViewport.IsFocused())
{
cam.Update(deltaTime);
SetMousePosition(GetScreenWidth() / 2, GetScreenHeight() / 2);
HideCursor();
}
else
{
ShowCursor();
}

worldEditor.Update(deltaTime);

// Draw

BeginDrawing();

if(cameraViewport.IsFocused() && lockCursor)
{
HideCursor();
}
else
{
ShowCursor();
}

miniMapViewport.Render(world, cam);
worldEditor.Render(cam);
Expand Down

0 comments on commit 0131766

Please sign in to comment.