@@ -16,6 +16,7 @@ WorldEditor::WorldEditor(World& world, Vector2 target)
16
16
.rotation = 0 .f ,
17
17
.zoom = 1 .f ,
18
18
})
19
+ , drawTool(*this )
19
20
{}
20
21
21
22
WorldEditor::~WorldEditor ()
@@ -37,36 +38,54 @@ void WorldEditor::DrawGUI()
37
38
RenderViewportGui ();
38
39
}
39
40
40
- Vector2 WorldEditor::GetMouseViewportPosition ( ) const
41
+ Vector2 WorldEditor::ScreenToViewportPosition (Vector2 pos ) const
41
42
{
42
- Vector2 mousePos = GetMousePosition ();
43
- Vector2 mousePosInViewport = Vector2Subtract (mousePos, viewportWindowOffset);
43
+ Vector2 posInViewport = Vector2Subtract (pos, viewportWindowOffset);
44
44
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 ),
48
48
};
49
49
50
- return mousePosInRenderTexture ;
50
+ return posInRenderTexture ;
51
51
}
52
52
53
- Vector2 WorldEditor::GetMouseWorldPosition ( ) const
53
+ Vector2 WorldEditor::ScreenToWorldPosition (Vector2 viewportPos ) const
54
54
{
55
- Vector2 mousePos = GetMouseViewportPosition ();
56
- Vector2 mouseWorldPos = GetScreenToWorld2D (mousePos, camera);
57
-
58
- return mouseWorldPos;
55
+ Vector2 pos = ScreenToViewportPosition (viewportPos);
56
+ return GetScreenToWorld2D (pos, camera);
59
57
}
60
58
61
59
void WorldEditor::Update (float dt)
62
60
{
63
61
if (isViewportFocused)
64
62
{
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);
66
85
}
67
86
}
68
87
69
- void WorldEditor::Render (RaycastingCamera& cam)
88
+ void WorldEditor::Render (RaycastingCamera& cam) const
70
89
{
71
90
BeginTextureMode (renderTexture);
72
91
@@ -90,74 +109,13 @@ void WorldEditor::Render(RaycastingCamera& cam)
90
109
}
91
110
}
92
111
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
-
108
112
EndMode2D ();
109
113
110
114
DrawUI ();
111
115
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);
141
117
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 ();
161
119
}
162
120
163
121
void WorldEditor::DrawCam (const RaycastingCamera& cam)
@@ -172,42 +130,6 @@ void WorldEditor::DrawCam(const RaycastingCamera& cam)
172
130
173
131
void WorldEditor::DrawBackgroundGrid () const
174
132
{
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
-
211
133
int32_t cellSize = GetGridCellSize ();
212
134
213
135
Vector2 center = {
@@ -217,8 +139,8 @@ void WorldEditor::DrawBackgroundGrid() const
217
139
center = GetScreenToWorld2D (center, camera);
218
140
219
141
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
222
144
};
223
145
224
146
int32_t xStart = ((int32_t )offset.x - GridSize);
@@ -271,7 +193,7 @@ void WorldEditor::DrawWall(const Wall& wall, bool noSectorSelected, bool thisSec
271
193
DrawLineEx (tikPositionA, tikPositionB, thickness, tikColor);
272
194
}
273
195
274
- void WorldEditor::DrawUI ()
196
+ void WorldEditor::DrawUI () const
275
197
{
276
198
// Top Right Axis
277
199
{
@@ -292,7 +214,7 @@ void WorldEditor::DrawUI()
292
214
40 ,
293
215
};
294
216
295
- Vector2 mousePos = GetMouseViewportPosition ( );
217
+ Vector2 mousePos = ScreenToViewportPosition ( GetMousePosition () );
296
218
297
219
std::stringstream posStr;
298
220
posStr << " Cursor pos : [ " << (uint32_t )mousePos.x << " , " << (uint32_t )mousePos.y << " ]" ;
@@ -408,7 +330,7 @@ int32_t WorldEditor::GetGridCellSize() const
408
330
409
331
if (camera.zoom <= 1 .f )
410
332
cellSize = 100 ;
411
- else if (camera.zoom <= 5 .f )
333
+ else if (camera.zoom <= 3 .f )
412
334
cellSize = 50 ;
413
335
else if (camera.zoom <= 10 .f )
414
336
cellSize = 25 ;
0 commit comments