Skip to content

Commit

Permalink
Editor : Minor tweaks & add WorldEditorDrawTool
Browse files Browse the repository at this point in the history
  • Loading branch information
P0ulpy committed Oct 27, 2024
1 parent 8fab9ad commit 585afc3
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 191 deletions.
9 changes: 9 additions & 0 deletions .idea/cmake.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/runConfigurations/Build___Run.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions ressources/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Collapsed=0

[Window][Camera]
Pos=1290,17
Size=430,116
Size=430,863
Collapsed=0
DockId=0x00000003,1

Expand All @@ -27,14 +27,14 @@ Collapsed=0
DockId=0x00000004,1

[Window][Rendering]
Pos=1290,135
Size=430,745
Pos=1290,283
Size=430,597
Collapsed=0
DockId=0x00000004,0

[Window][Sectors]
Pos=1290,17
Size=430,116
Size=430,863
Collapsed=0
DockId=0x00000003,0

Expand All @@ -48,6 +48,6 @@ DockId=0x00000001,1
DockSpace ID=0x7C6B3D9B Window=0xA87D555D Pos=0,17 Size=1720,863 Split=X Selected=0x8180B457
DockNode ID=0x00000001 Parent=0x7C6B3D9B SizeRef=1288,863 CentralNode=1 Selected=0xF981308D
DockNode ID=0x00000002 Parent=0x7C6B3D9B SizeRef=430,863 Split=Y Selected=0xB788DBD5
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=430,116 Selected=0xDD05356B
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=430,745 Selected=0x5B3C8097
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=430,264 Selected=0xDD05356B
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=430,597 Selected=0x5B3C8097

14 changes: 4 additions & 10 deletions src/Editor/RaycastingCameraViewport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
class RaycastingCameraViewport
{
public:
RaycastingCameraViewport(RaycastingCamera& camera, uint32_t RenderTextureWidth, uint32_t RenderTextureHeight)
: cam(camera)
, renderTexture(LoadRenderTexture(RenderTextureWidth, RenderTextureHeight))
RaycastingCameraViewport(int32_t RenderTextureWidth, int32_t RenderTextureHeight)
: renderTexture(LoadRenderTexture(RenderTextureWidth, RenderTextureHeight))
{}

~RaycastingCameraViewport()
Expand Down Expand Up @@ -112,13 +111,8 @@ class RaycastingCameraViewport
}

private:
RaycastingCamera& cam;
RenderTexture2D renderTexture;

bool mouseLocked = false;
bool mouseFocused = false;
bool autoResize = false;

ImGuiWindow* parentWindow = nullptr;
ImGuiID parentId = 0;
bool mouseFocused = false;
bool autoResize = false;
};
24 changes: 11 additions & 13 deletions src/Editor/RenderingOrchestrator.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#pragma once

#include <imgui.h>
#include <vector>
#include <raylib.h>
#include <imgui.h>

#include "RaycastingCameraViewport.hpp"
#include "Utils/DrawingHelper.hpp"

class RenderingOrchestrator
{
public:
RenderingOrchestrator(RaycastingCameraViewport& cameraViewport)
: cameraViewport(cameraViewport)
explicit RenderingOrchestrator(const RenderTexture2D& renderTexture)
: renderTexture(renderTexture)
{}

void Render(World &world, RaycastingCamera &cam)
{
auto& renderTexture = cameraViewport.GetRenderTexture();

if(play)
{
AllRenderItr(world, cam);
Expand All @@ -27,18 +25,19 @@ class RenderingOrchestrator
{
case StepInto: OneRenderItr(world, cam); break;
case StepOver: AllRenderItr(world, cam); break;
case None:
break;
}

invokeEvent = None;
}
}

void InitilizeFrame(World &world, RaycastingCamera &cam)
void InitializeFrame(World &world, RaycastingCamera &cam)
{
auto& renderTexture = cameraViewport.GetRenderTexture();
rasterizer.Reset(renderTexture.texture.width, renderTexture.texture.height, world, cam);

if(rasterizingItrsTextures.size() > 0)
if(!rasterizingItrsTextures.empty())
{
int width = rasterizingItrsTextures[0].texture.width;
int height = rasterizingItrsTextures[0].texture.height;
Expand Down Expand Up @@ -71,12 +70,11 @@ class RenderingOrchestrator
{
if(!rasterizer.IsRenderIterationRemains())
{
InitilizeFrame(world, cam);
InitializeFrame(world, cam);
}

assert(rasterizer.IsRenderIterationRemains());

auto& renderTexture = cameraViewport.GetRenderTexture();
auto& ctx = rasterizer.GetContext();

BeginTextureMode(renderTexture);
Expand Down Expand Up @@ -146,8 +144,8 @@ class RenderingOrchestrator
ImGui::End();
}

private:
RaycastingCameraViewport& cameraViewport;
private:
const RenderTexture2D& renderTexture;
WorldRasterizer rasterizer;

bool play = true;
Expand Down
158 changes: 40 additions & 118 deletions src/Editor/WorldEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ WorldEditor::WorldEditor(World& world, Vector2 target)
.rotation = 0.f,
.zoom = 1.f,
})
, drawTool(*this)
{}

WorldEditor::~WorldEditor()
Expand All @@ -37,36 +38,54 @@ void WorldEditor::DrawGUI()
RenderViewportGui();
}

Vector2 WorldEditor::GetMouseViewportPosition() const
Vector2 WorldEditor::ScreenToViewportPosition(Vector2 pos) const
{
Vector2 mousePos = GetMousePosition();
Vector2 mousePosInViewport = Vector2Subtract(mousePos, viewportWindowOffset);
Vector2 posInViewport = Vector2Subtract(pos, viewportWindowOffset);

Vector2 mousePosInRenderTexture = {
mousePosInViewport.x * ((float)renderTexture.texture.width / viewportWindowSize.x),
mousePosInViewport.y * ((float)renderTexture.texture.height / viewportWindowSize.y),
Vector2 posInRenderTexture = {
posInViewport.x * ((float)renderTexture.texture.width / viewportWindowSize.x),
posInViewport.y * ((float)renderTexture.texture.height / viewportWindowSize.y),
};

return mousePosInRenderTexture;
return posInRenderTexture;
}

Vector2 WorldEditor::GetMouseWorldPosition() const
Vector2 WorldEditor::ScreenToWorldPosition(Vector2 viewportPos) const
{
Vector2 mousePos = GetMouseViewportPosition();
Vector2 mouseWorldPos = GetScreenToWorld2D(mousePos, camera);

return mouseWorldPos;
Vector2 pos = ScreenToViewportPosition(viewportPos);
return GetScreenToWorld2D(pos, camera);
}

void WorldEditor::Update(float dt)
{
if(isViewportFocused)
{
HandleInputs();
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
Vector2 delta = GetMouseDelta();
delta = Vector2Scale(delta, - MouseDragSensitivity / camera.zoom);
camera.target = Vector2Add(camera.target, delta);
}

Vector2 mouseWorldPos = ScreenToWorldPosition(GetMousePosition());

float wheel = GetMouseWheelMove();
if (wheel != 0)
{
camera.offset = ScreenToViewportPosition(GetMousePosition());
camera.target = mouseWorldPos;

float scaleFactor = MouseZoomSensitivity + (0.25f * fabsf(wheel));
if (wheel < 0) scaleFactor = 1.0f / scaleFactor;

camera.zoom = Clamp(camera.zoom * scaleFactor, MinZoom, MaxZoom);
}

drawTool.Update(dt);
}
}

void WorldEditor::Render(RaycastingCamera& cam)
void WorldEditor::Render(RaycastingCamera& cam) const
{
BeginTextureMode(renderTexture);

Expand All @@ -90,74 +109,13 @@ void WorldEditor::Render(RaycastingCamera& cam)
}
}

// Wall Drawing Render
if(isDragging)
{
EndMode2D();
Vector2 mousePos = GetMouseViewportPosition();
DrawLine(mousePos.x, 0, mousePos.x, renderTexture.texture.height, GRAY);
DrawLine(0, mousePos.y, renderTexture.texture.width, mousePos.y, GRAY);

BeginMode2D(camera);

DrawLineV(dragStartPosition, dragEndPosition, ORANGE);
DrawCircleV(dragStartPosition, 1, RED);
DrawCircleV(dragEndPosition, 1, BLUE);
}

EndMode2D();

DrawUI();

EndTextureMode();
}

void WorldEditor::HandleInputs()
{
constexpr float MOUSE_DRAG_SENSITIVITY = 1.f;

if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
Vector2 delta = GetMouseDelta();
delta = Vector2Scale(delta, -MOUSE_DRAG_SENSITIVITY / camera.zoom);
camera.target = Vector2Add(camera.target, delta);
}

constexpr float MOUSE_ZOOM_SENSITIVITY = 1.f;

Vector2 mouseWorldPos = GetMouseWorldPosition();

float wheel = GetMouseWheelMove();
if (wheel != 0)
{
camera.offset = GetMouseViewportPosition();
camera.target = mouseWorldPos;

float scaleFactor = MOUSE_ZOOM_SENSITIVITY + ( 0.25f * fabsf(wheel));
if (wheel < 0) scaleFactor = 1.0f / scaleFactor;

camera.zoom = Clamp(camera.zoom * scaleFactor, MinZoom, MaxZoom);
}
drawTool.Render(cam);

// Wall Drawing Drag
{
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
dragStartPosition = mouseWorldPos;
isDragging = true;
}
else if(IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
dragEndPosition = mouseWorldPos;
}
//else if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
else if(isDragging)
{
float deltaLength = Vector2Length(Vector2Subtract(dragStartPosition, dragEndPosition));
std::cout << "Delta Length : " << deltaLength << std::endl;
isDragging = false;
}
}
EndTextureMode();
}

void WorldEditor::DrawCam(const RaycastingCamera& cam)
Expand All @@ -172,42 +130,6 @@ void WorldEditor::DrawCam(const RaycastingCamera& cam)

void WorldEditor::DrawBackgroundGrid() const
{
// int32_t cellSize = GetGridCellSize();
// int32_t gridSize = cellSize * 100;
//
// // Compute Grid Offset based on camera position
//
// const int32_t rowOffset = (int32_t)camera.target.x / cellSize;
// int32_t startingRow = -(gridSize - rowOffset);
// int32_t endingRow = (gridSize + rowOffset);
//
// const int32_t colOffset = (int32_t)camera.target.y / cellSize;
// int32_t startingCol = -(gridSize - colOffset);
// int32_t endingCol = (gridSize + colOffset);
//
// int32_t yRowBegin = startingCol * cellSize;
// int32_t yRowEnd = endingCol * cellSize;
//
// for (int32_t row = startingRow; row <= endingRow; ++row)
// {
// int32_t x = cellSize * row;
// DrawLine(x, yRowBegin, x, yRowEnd, GridColor);
// }
//
// int32_t xColBegin = startingRow * cellSize;
// int32_t xColEnd = endingRow * cellSize;
//
// for (int32_t col = startingCol; col <= endingCol; ++col)
// {
// int32_t y = cellSize * col;
// DrawLine(xColBegin, y, xColEnd, y, GridColor);
// }
// for (int32_t row = startingRow; row <= endingRow; ++row)
// {
// int32_t x = cellSize * row;
// DrawLine(x, yRowBegin, x, yRowEnd, GridColor);
// }

int32_t cellSize = GetGridCellSize();

Vector2 center = {
Expand All @@ -217,8 +139,8 @@ void WorldEditor::DrawBackgroundGrid() const
center = GetScreenToWorld2D(center, camera);

Vector2 offset = {
std::round(center.x / cellSize) * cellSize,
std::round(center.y / cellSize) * cellSize
std::round(center.x / (float)cellSize) * (float)cellSize,
std::round(center.y / (float)cellSize) * (float)cellSize
};

int32_t xStart = ((int32_t)offset.x - GridSize);
Expand Down Expand Up @@ -271,7 +193,7 @@ void WorldEditor::DrawWall(const Wall& wall, bool noSectorSelected, bool thisSec
DrawLineEx(tikPositionA, tikPositionB, thickness, tikColor);
}

void WorldEditor::DrawUI()
void WorldEditor::DrawUI() const
{
// Top Right Axis
{
Expand All @@ -292,7 +214,7 @@ void WorldEditor::DrawUI()
40,
};

Vector2 mousePos = GetMouseViewportPosition();
Vector2 mousePos = ScreenToViewportPosition(GetMousePosition());

std::stringstream posStr;
posStr << "Cursor pos : [ " << (uint32_t)mousePos.x << ", " << (uint32_t)mousePos.y << " ]";
Expand Down Expand Up @@ -408,7 +330,7 @@ int32_t WorldEditor::GetGridCellSize() const

if(camera.zoom <= 1.f)
cellSize = 100;
else if (camera.zoom <= 5.f)
else if (camera.zoom <= 3.f)
cellSize = 50;
else if (camera.zoom <= 10.f)
cellSize = 25;
Expand Down
Loading

0 comments on commit 585afc3

Please sign in to comment.