Skip to content

Commit 328c2e3

Browse files
committed
Fix things and little refacto
1 parent d1c517e commit 328c2e3

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed

include/editor/colony_creator/colony_tool.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,19 @@ struct ColonyTool : GUI::Container
4545

4646
auto set_position_button = create<GUI::Button>("Set Position", [this](){
4747
control_state.requestEditModeOff();
48+
// Preview callbacks
49+
control_state.draw_action = [this](sf::RenderTarget& target, const ViewportHandler& vp_handler) {
50+
sf::CircleShape c(colony->base.radius);
51+
c.setOrigin(c.getRadius(), c.getRadius());
52+
c.setPosition(vp_handler.getMouseWorldPosition());
53+
const sf::Color colony_color = colony->ants_color;
54+
c.setFillColor({colony_color.r, colony_color.g, colony_color.b, 100});
55+
target.draw(c, vp_handler.getRenderState());
56+
};
4857
control_state.view_action = [this](sf::Vector2f world_position) {
4958
colony->setPosition(world_position);
59+
control_state.draw_action = nullptr;
60+
control_state.view_action = nullptr;
5061
};
5162
});
5263
set_position_button->setHeight(20.0f);

include/editor/control_state.hpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#pragma once
22
#include <functional>
3-
#include <SFML/System/Vector2.hpp>
3+
#include <SFML/Graphics.hpp>
44
#include "editor/transition.hpp"
55

66

77
struct ControlState
88
{
99
using ViewAction = std::function<void(sf::Vector2f)>;
1010
using Action = std::function<void(void)>;
11+
using DrawAction = std::function<void(sf::RenderTarget&, const ViewportHandler&)>;
12+
1113
// Actions
12-
Action action = nullptr;
13-
ViewAction view_action = nullptr;
14-
Action view_action_end = nullptr;
14+
Action action = nullptr;
15+
ViewAction view_action = nullptr;
16+
Action view_action_end = nullptr;
17+
DrawAction draw_action = nullptr;
18+
std::function<void()> request_edits_off = nullptr;
19+
1520
// Special commands
1621
bool focus_requested = false;
1722
trn::Transition<sf::Vector2f> focus;
@@ -20,8 +25,6 @@ struct ControlState
2025
bool show_brush_preview = false;
2126
float brush_radius = 0.0f;
2227

23-
std::function<void()> request_edits_off = nullptr;
24-
2528
ControlState() = default;
2629

2730
void executeViewAction(sf::Vector2f mouse_world_position)
@@ -45,6 +48,13 @@ struct ControlState
4548
}
4649
}
4750

51+
void executeDrawAction(sf::RenderTarget& target, const ViewportHandler& vp_handler)
52+
{
53+
if (draw_action) {
54+
draw_action(target, vp_handler);
55+
}
56+
}
57+
4858
void requestFocus(sf::Vector2f position, float zoom_level = 1.0f)
4959
{
5060
focus_requested = true;
@@ -58,4 +68,11 @@ struct ControlState
5868
request_edits_off();
5969
}
6070
}
71+
72+
void resetCallbacks()
73+
{
74+
view_action = nullptr;
75+
draw_action = nullptr;
76+
view_action_end = nullptr;
77+
}
6178
};

include/editor/tool_selector.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct ToolSelector : public GUI::NamedContainer
139139
void setCallback()
140140
{
141141
if (edit_mode) {
142+
// Edit callbacks
142143
switch (current_tool) {
143144
case Tool::BrushWall:
144145
control_state.view_action = [this](sf::Vector2f mouse_position) {
@@ -169,6 +170,18 @@ struct ToolSelector : public GUI::NamedContainer
169170
};
170171
break;
171172
}
173+
// Preview callbacks
174+
control_state.draw_action = [this](sf::RenderTarget& target, const ViewportHandler& vp_handler) {
175+
const int32_t cell_size = simulation.world.map.cell_size;
176+
const float side_size = (2.0f * control_state.brush_radius + 1) * to<float>(cell_size);
177+
sf::RectangleShape brush_preview({side_size, side_size});
178+
brush_preview.setFillColor(sf::Color(100, 100, 100, 100));
179+
brush_preview.setOrigin(side_size * 0.5f, side_size * 0.5f);
180+
const sf::Vector2f current_position = vp_handler.getMouseWorldPosition();
181+
brush_preview.setPosition(to<float>(int(current_position.x / to<float>(cell_size)) * cell_size) + 2.0f,
182+
to<float>(int(current_position.y / to<float>(cell_size)) * cell_size) + 2.0f);
183+
target.draw(brush_preview, vp_handler.getRenderState());
184+
};
172185
}
173186
}
174187

@@ -186,6 +199,7 @@ struct ToolSelector : public GUI::NamedContainer
186199
{
187200
control_state.view_action = nullptr;
188201
control_state.view_action_end = nullptr;
202+
control_state.draw_action = nullptr;
189203
}
190204
};
191205

include/editor/world_view.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct WorldView : GUI::Item
4848
} else if (button == sf::Mouse::Right) {
4949
action_button_click = true;
5050
control_state.executeViewAction(simulation.renderer.vp_handler.getMouseWorldPosition());
51+
} else if (button == sf::Mouse::Middle) {
52+
control_state.resetCallbacks();
5153
}
5254
}
5355

@@ -78,17 +80,7 @@ struct WorldView : GUI::Item
7880
simulation.renderer.vp_handler.setZoom(control_state.zoom);
7981
}
8082
simulation.render(target);
81-
if (control_state.show_brush_preview) {
82-
const int32_t cell_size = simulation.world.map.cell_size;
83-
const float side_size = (2.0f * control_state.brush_radius + 1) * cell_size;
84-
sf::RectangleShape brush_preview({side_size, side_size});
85-
brush_preview.setFillColor(sf::Color(100, 100, 100, 100));
86-
brush_preview.setOrigin(side_size * 0.5f, side_size * 0.5f);
87-
const sf::Vector2f current_position = simulation.renderer.vp_handler.getMouseWorldPosition();
88-
brush_preview.setPosition(to<float>(int(current_position.x / to<float>(cell_size)) * cell_size) + 2.0f,
89-
to<float>(int(current_position.y / to<float>(cell_size)) * cell_size) + 2.0f);
90-
target.draw(brush_preview, simulation.renderer.vp_handler.getRenderState());
91-
}
83+
control_state.executeDrawAction(target, simulation.renderer.vp_handler);
9284
}
9385

9486
void update() override

include/simulation/world/world.hpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ struct World
5353
void addWall(const sf::Vector2i& position)
5454
{
5555
if (map.checkCoords(position)) {
56-
map.get(position).food = 0;
57-
map.get(position).wall = 1;
56+
WorldCell& cell = map.get(position);
57+
cell.food = 0;
58+
cell.wall = 1;
59+
for (auto& markers : cell.markers) {
60+
clearMarkersOfCell(markers);
61+
}
5862
}
5963
}
6064

@@ -89,13 +93,17 @@ struct World
8993
void clearMarkers(uint8_t colony_id)
9094
{
9195
for (auto& cell : map.cells) {
92-
auto& markers = cell.markers[colony_id];
93-
markers.intensity[0] = 0.0f;
94-
markers.intensity[1] = 0.0f;
95-
markers.intensity[2] = 0.0f;
96-
markers.repellent = 0.0f;
97-
markers.permanent = false;
98-
cell.density = 0.0f;
96+
clearMarkersOfCell(cell.markers[colony_id]);
97+
cell.density = 0.0f;
9998
}
10099
}
100+
101+
static void clearMarkersOfCell(ColonyCell& cell)
102+
{
103+
cell.intensity[0] = 0.0f;
104+
cell.intensity[1] = 0.0f;
105+
cell.intensity[2] = 0.0f;
106+
cell.repellent = 0.0f;
107+
cell.permanent = false;
108+
}
101109
};

0 commit comments

Comments
 (0)