Skip to content

Commit 585afc3

Browse files
committed
Editor : Minor tweaks & add WorldEditorDrawTool
1 parent 8fab9ad commit 585afc3

File tree

12 files changed

+229
-191
lines changed

12 files changed

+229
-191
lines changed

.idea/cmake.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/Build___Run.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ressources/imgui.ini

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Collapsed=0
1010

1111
[Window][Camera]
1212
Pos=1290,17
13-
Size=430,116
13+
Size=430,863
1414
Collapsed=0
1515
DockId=0x00000003,1
1616

@@ -27,14 +27,14 @@ Collapsed=0
2727
DockId=0x00000004,1
2828

2929
[Window][Rendering]
30-
Pos=1290,135
31-
Size=430,745
30+
Pos=1290,283
31+
Size=430,597
3232
Collapsed=0
3333
DockId=0x00000004,0
3434

3535
[Window][Sectors]
3636
Pos=1290,17
37-
Size=430,116
37+
Size=430,863
3838
Collapsed=0
3939
DockId=0x00000003,0
4040

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

src/Editor/RaycastingCameraViewport.hpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
class RaycastingCameraViewport
1010
{
1111
public:
12-
RaycastingCameraViewport(RaycastingCamera& camera, uint32_t RenderTextureWidth, uint32_t RenderTextureHeight)
13-
: cam(camera)
14-
, renderTexture(LoadRenderTexture(RenderTextureWidth, RenderTextureHeight))
12+
RaycastingCameraViewport(int32_t RenderTextureWidth, int32_t RenderTextureHeight)
13+
: renderTexture(LoadRenderTexture(RenderTextureWidth, RenderTextureHeight))
1514
{}
1615

1716
~RaycastingCameraViewport()
@@ -112,13 +111,8 @@ class RaycastingCameraViewport
112111
}
113112

114113
private:
115-
RaycastingCamera& cam;
116114
RenderTexture2D renderTexture;
117115

118-
bool mouseLocked = false;
119-
bool mouseFocused = false;
120-
bool autoResize = false;
121-
122-
ImGuiWindow* parentWindow = nullptr;
123-
ImGuiID parentId = 0;
116+
bool mouseFocused = false;
117+
bool autoResize = false;
124118
};

src/Editor/RenderingOrchestrator.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
#pragma once
22

3-
#include <imgui.h>
43
#include <vector>
4+
#include <raylib.h>
5+
#include <imgui.h>
56

6-
#include "RaycastingCameraViewport.hpp"
77
#include "Utils/DrawingHelper.hpp"
88

99
class RenderingOrchestrator
1010
{
1111
public:
12-
RenderingOrchestrator(RaycastingCameraViewport& cameraViewport)
13-
: cameraViewport(cameraViewport)
12+
explicit RenderingOrchestrator(const RenderTexture2D& renderTexture)
13+
: renderTexture(renderTexture)
1414
{}
1515

1616
void Render(World &world, RaycastingCamera &cam)
1717
{
18-
auto& renderTexture = cameraViewport.GetRenderTexture();
19-
2018
if(play)
2119
{
2220
AllRenderItr(world, cam);
@@ -27,18 +25,19 @@ class RenderingOrchestrator
2725
{
2826
case StepInto: OneRenderItr(world, cam); break;
2927
case StepOver: AllRenderItr(world, cam); break;
28+
case None:
29+
break;
3030
}
3131

3232
invokeEvent = None;
3333
}
3434
}
3535

36-
void InitilizeFrame(World &world, RaycastingCamera &cam)
36+
void InitializeFrame(World &world, RaycastingCamera &cam)
3737
{
38-
auto& renderTexture = cameraViewport.GetRenderTexture();
3938
rasterizer.Reset(renderTexture.texture.width, renderTexture.texture.height, world, cam);
4039

41-
if(rasterizingItrsTextures.size() > 0)
40+
if(!rasterizingItrsTextures.empty())
4241
{
4342
int width = rasterizingItrsTextures[0].texture.width;
4443
int height = rasterizingItrsTextures[0].texture.height;
@@ -71,12 +70,11 @@ class RenderingOrchestrator
7170
{
7271
if(!rasterizer.IsRenderIterationRemains())
7372
{
74-
InitilizeFrame(world, cam);
73+
InitializeFrame(world, cam);
7574
}
7675

7776
assert(rasterizer.IsRenderIterationRemains());
7877

79-
auto& renderTexture = cameraViewport.GetRenderTexture();
8078
auto& ctx = rasterizer.GetContext();
8179

8280
BeginTextureMode(renderTexture);
@@ -146,8 +144,8 @@ class RenderingOrchestrator
146144
ImGui::End();
147145
}
148146

149-
private:
150-
RaycastingCameraViewport& cameraViewport;
147+
private:
148+
const RenderTexture2D& renderTexture;
151149
WorldRasterizer rasterizer;
152150

153151
bool play = true;

src/Editor/WorldEditor.cpp

Lines changed: 40 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ WorldEditor::WorldEditor(World& world, Vector2 target)
1616
.rotation = 0.f,
1717
.zoom = 1.f,
1818
})
19+
, drawTool(*this)
1920
{}
2021

2122
WorldEditor::~WorldEditor()
@@ -37,36 +38,54 @@ void WorldEditor::DrawGUI()
3738
RenderViewportGui();
3839
}
3940

40-
Vector2 WorldEditor::GetMouseViewportPosition() const
41+
Vector2 WorldEditor::ScreenToViewportPosition(Vector2 pos) const
4142
{
42-
Vector2 mousePos = GetMousePosition();
43-
Vector2 mousePosInViewport = Vector2Subtract(mousePos, viewportWindowOffset);
43+
Vector2 posInViewport = Vector2Subtract(pos, viewportWindowOffset);
4444

45-
Vector2 mousePosInRenderTexture = {
46-
mousePosInViewport.x * ((float)renderTexture.texture.width / viewportWindowSize.x),
47-
mousePosInViewport.y * ((float)renderTexture.texture.height / viewportWindowSize.y),
45+
Vector2 posInRenderTexture = {
46+
posInViewport.x * ((float)renderTexture.texture.width / viewportWindowSize.x),
47+
posInViewport.y * ((float)renderTexture.texture.height / viewportWindowSize.y),
4848
};
4949

50-
return mousePosInRenderTexture;
50+
return posInRenderTexture;
5151
}
5252

53-
Vector2 WorldEditor::GetMouseWorldPosition() const
53+
Vector2 WorldEditor::ScreenToWorldPosition(Vector2 viewportPos) const
5454
{
55-
Vector2 mousePos = GetMouseViewportPosition();
56-
Vector2 mouseWorldPos = GetScreenToWorld2D(mousePos, camera);
57-
58-
return mouseWorldPos;
55+
Vector2 pos = ScreenToViewportPosition(viewportPos);
56+
return GetScreenToWorld2D(pos, camera);
5957
}
6058

6159
void WorldEditor::Update(float dt)
6260
{
6361
if(isViewportFocused)
6462
{
65-
HandleInputs();
63+
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
64+
{
65+
Vector2 delta = GetMouseDelta();
66+
delta = Vector2Scale(delta, - MouseDragSensitivity / camera.zoom);
67+
camera.target = Vector2Add(camera.target, delta);
68+
}
69+
70+
Vector2 mouseWorldPos = ScreenToWorldPosition(GetMousePosition());
71+
72+
float wheel = GetMouseWheelMove();
73+
if (wheel != 0)
74+
{
75+
camera.offset = ScreenToViewportPosition(GetMousePosition());
76+
camera.target = mouseWorldPos;
77+
78+
float scaleFactor = MouseZoomSensitivity + (0.25f * fabsf(wheel));
79+
if (wheel < 0) scaleFactor = 1.0f / scaleFactor;
80+
81+
camera.zoom = Clamp(camera.zoom * scaleFactor, MinZoom, MaxZoom);
82+
}
83+
84+
drawTool.Update(dt);
6685
}
6786
}
6887

69-
void WorldEditor::Render(RaycastingCamera& cam)
88+
void WorldEditor::Render(RaycastingCamera& cam) const
7089
{
7190
BeginTextureMode(renderTexture);
7291

@@ -90,74 +109,13 @@ void WorldEditor::Render(RaycastingCamera& cam)
90109
}
91110
}
92111

93-
// Wall Drawing Render
94-
if(isDragging)
95-
{
96-
EndMode2D();
97-
Vector2 mousePos = GetMouseViewportPosition();
98-
DrawLine(mousePos.x, 0, mousePos.x, renderTexture.texture.height, GRAY);
99-
DrawLine(0, mousePos.y, renderTexture.texture.width, mousePos.y, GRAY);
100-
101-
BeginMode2D(camera);
102-
103-
DrawLineV(dragStartPosition, dragEndPosition, ORANGE);
104-
DrawCircleV(dragStartPosition, 1, RED);
105-
DrawCircleV(dragEndPosition, 1, BLUE);
106-
}
107-
108112
EndMode2D();
109113

110114
DrawUI();
111115

112-
EndTextureMode();
113-
}
114-
115-
void WorldEditor::HandleInputs()
116-
{
117-
constexpr float MOUSE_DRAG_SENSITIVITY = 1.f;
118-
119-
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
120-
{
121-
Vector2 delta = GetMouseDelta();
122-
delta = Vector2Scale(delta, -MOUSE_DRAG_SENSITIVITY / camera.zoom);
123-
camera.target = Vector2Add(camera.target, delta);
124-
}
125-
126-
constexpr float MOUSE_ZOOM_SENSITIVITY = 1.f;
127-
128-
Vector2 mouseWorldPos = GetMouseWorldPosition();
129-
130-
float wheel = GetMouseWheelMove();
131-
if (wheel != 0)
132-
{
133-
camera.offset = GetMouseViewportPosition();
134-
camera.target = mouseWorldPos;
135-
136-
float scaleFactor = MOUSE_ZOOM_SENSITIVITY + ( 0.25f * fabsf(wheel));
137-
if (wheel < 0) scaleFactor = 1.0f / scaleFactor;
138-
139-
camera.zoom = Clamp(camera.zoom * scaleFactor, MinZoom, MaxZoom);
140-
}
116+
drawTool.Render(cam);
141117

142-
// Wall Drawing Drag
143-
{
144-
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
145-
{
146-
dragStartPosition = mouseWorldPos;
147-
isDragging = true;
148-
}
149-
else if(IsMouseButtonDown(MOUSE_BUTTON_LEFT))
150-
{
151-
dragEndPosition = mouseWorldPos;
152-
}
153-
//else if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
154-
else if(isDragging)
155-
{
156-
float deltaLength = Vector2Length(Vector2Subtract(dragStartPosition, dragEndPosition));
157-
std::cout << "Delta Length : " << deltaLength << std::endl;
158-
isDragging = false;
159-
}
160-
}
118+
EndTextureMode();
161119
}
162120

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

173131
void WorldEditor::DrawBackgroundGrid() const
174132
{
175-
// int32_t cellSize = GetGridCellSize();
176-
// int32_t gridSize = cellSize * 100;
177-
//
178-
// // Compute Grid Offset based on camera position
179-
//
180-
// const int32_t rowOffset = (int32_t)camera.target.x / cellSize;
181-
// int32_t startingRow = -(gridSize - rowOffset);
182-
// int32_t endingRow = (gridSize + rowOffset);
183-
//
184-
// const int32_t colOffset = (int32_t)camera.target.y / cellSize;
185-
// int32_t startingCol = -(gridSize - colOffset);
186-
// int32_t endingCol = (gridSize + colOffset);
187-
//
188-
// int32_t yRowBegin = startingCol * cellSize;
189-
// int32_t yRowEnd = endingCol * cellSize;
190-
//
191-
// for (int32_t row = startingRow; row <= endingRow; ++row)
192-
// {
193-
// int32_t x = cellSize * row;
194-
// DrawLine(x, yRowBegin, x, yRowEnd, GridColor);
195-
// }
196-
//
197-
// int32_t xColBegin = startingRow * cellSize;
198-
// int32_t xColEnd = endingRow * cellSize;
199-
//
200-
// for (int32_t col = startingCol; col <= endingCol; ++col)
201-
// {
202-
// int32_t y = cellSize * col;
203-
// DrawLine(xColBegin, y, xColEnd, y, GridColor);
204-
// }
205-
// for (int32_t row = startingRow; row <= endingRow; ++row)
206-
// {
207-
// int32_t x = cellSize * row;
208-
// DrawLine(x, yRowBegin, x, yRowEnd, GridColor);
209-
// }
210-
211133
int32_t cellSize = GetGridCellSize();
212134

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

219141
Vector2 offset = {
220-
std::round(center.x / cellSize) * cellSize,
221-
std::round(center.y / cellSize) * cellSize
142+
std::round(center.x / (float)cellSize) * (float)cellSize,
143+
std::round(center.y / (float)cellSize) * (float)cellSize
222144
};
223145

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

274-
void WorldEditor::DrawUI()
196+
void WorldEditor::DrawUI() const
275197
{
276198
// Top Right Axis
277199
{
@@ -292,7 +214,7 @@ void WorldEditor::DrawUI()
292214
40,
293215
};
294216

295-
Vector2 mousePos = GetMouseViewportPosition();
217+
Vector2 mousePos = ScreenToViewportPosition(GetMousePosition());
296218

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

409331
if(camera.zoom <= 1.f)
410332
cellSize = 100;
411-
else if (camera.zoom <= 5.f)
333+
else if (camera.zoom <= 3.f)
412334
cellSize = 50;
413335
else if (camera.zoom <= 10.f)
414336
cellSize = 25;

0 commit comments

Comments
 (0)