diff --git a/include/editor/colony_creator/colony_tool.hpp b/include/editor/colony_creator/colony_tool.hpp index 307143cf..f150b164 100644 --- a/include/editor/colony_creator/colony_tool.hpp +++ b/include/editor/colony_creator/colony_tool.hpp @@ -45,8 +45,19 @@ struct ColonyTool : GUI::Container auto set_position_button = create("Set Position", [this](){ control_state.requestEditModeOff(); + // Preview callbacks + control_state.draw_action = [this](sf::RenderTarget& target, const ViewportHandler& vp_handler) { + sf::CircleShape c(colony->base.radius); + c.setOrigin(c.getRadius(), c.getRadius()); + c.setPosition(vp_handler.getMouseWorldPosition()); + const sf::Color colony_color = colony->ants_color; + c.setFillColor({colony_color.r, colony_color.g, colony_color.b, 100}); + target.draw(c, vp_handler.getRenderState()); + }; control_state.view_action = [this](sf::Vector2f world_position) { colony->setPosition(world_position); + control_state.draw_action = nullptr; + control_state.view_action = nullptr; }; }); set_position_button->setHeight(20.0f); diff --git a/include/editor/control_state.hpp b/include/editor/control_state.hpp index e479387a..862d7a39 100644 --- a/include/editor/control_state.hpp +++ b/include/editor/control_state.hpp @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include "editor/transition.hpp" @@ -8,10 +8,15 @@ struct ControlState { using ViewAction = std::function; using Action = std::function; + using DrawAction = std::function; + // Actions - Action action = nullptr; - ViewAction view_action = nullptr; - Action view_action_end = nullptr; + Action action = nullptr; + ViewAction view_action = nullptr; + Action view_action_end = nullptr; + DrawAction draw_action = nullptr; + std::function request_edits_off = nullptr; + // Special commands bool focus_requested = false; trn::Transition focus; @@ -20,8 +25,6 @@ struct ControlState bool show_brush_preview = false; float brush_radius = 0.0f; - std::function request_edits_off = nullptr; - ControlState() = default; void executeViewAction(sf::Vector2f mouse_world_position) @@ -45,6 +48,13 @@ struct ControlState } } + void executeDrawAction(sf::RenderTarget& target, const ViewportHandler& vp_handler) + { + if (draw_action) { + draw_action(target, vp_handler); + } + } + void requestFocus(sf::Vector2f position, float zoom_level = 1.0f) { focus_requested = true; @@ -58,4 +68,11 @@ struct ControlState request_edits_off(); } } + + void resetCallbacks() + { + view_action = nullptr; + draw_action = nullptr; + view_action_end = nullptr; + } }; diff --git a/include/editor/tool_selector.hpp b/include/editor/tool_selector.hpp index 341d0cda..2a276f98 100644 --- a/include/editor/tool_selector.hpp +++ b/include/editor/tool_selector.hpp @@ -139,6 +139,7 @@ struct ToolSelector : public GUI::NamedContainer void setCallback() { if (edit_mode) { + // Edit callbacks switch (current_tool) { case Tool::BrushWall: control_state.view_action = [this](sf::Vector2f mouse_position) { @@ -169,6 +170,18 @@ struct ToolSelector : public GUI::NamedContainer }; break; } + // Preview callbacks + control_state.draw_action = [this](sf::RenderTarget& target, const ViewportHandler& vp_handler) { + const int32_t cell_size = simulation.world.map.cell_size; + const float side_size = (2.0f * control_state.brush_radius + 1) * to(cell_size); + sf::RectangleShape brush_preview({side_size, side_size}); + brush_preview.setFillColor(sf::Color(100, 100, 100, 100)); + brush_preview.setOrigin(side_size * 0.5f, side_size * 0.5f); + const sf::Vector2f current_position = vp_handler.getMouseWorldPosition(); + brush_preview.setPosition(to(int(current_position.x / to(cell_size)) * cell_size) + 2.0f, + to(int(current_position.y / to(cell_size)) * cell_size) + 2.0f); + target.draw(brush_preview, vp_handler.getRenderState()); + }; } } @@ -186,6 +199,7 @@ struct ToolSelector : public GUI::NamedContainer { control_state.view_action = nullptr; control_state.view_action_end = nullptr; + control_state.draw_action = nullptr; } }; diff --git a/include/editor/world_view.hpp b/include/editor/world_view.hpp index 9dbb23df..d3ad24b2 100644 --- a/include/editor/world_view.hpp +++ b/include/editor/world_view.hpp @@ -48,6 +48,8 @@ struct WorldView : GUI::Item } else if (button == sf::Mouse::Right) { action_button_click = true; control_state.executeViewAction(simulation.renderer.vp_handler.getMouseWorldPosition()); + } else if (button == sf::Mouse::Middle) { + control_state.resetCallbacks(); } } @@ -78,17 +80,7 @@ struct WorldView : GUI::Item simulation.renderer.vp_handler.setZoom(control_state.zoom); } simulation.render(target); - if (control_state.show_brush_preview) { - const int32_t cell_size = simulation.world.map.cell_size; - const float side_size = (2.0f * control_state.brush_radius + 1) * cell_size; - sf::RectangleShape brush_preview({side_size, side_size}); - brush_preview.setFillColor(sf::Color(100, 100, 100, 100)); - brush_preview.setOrigin(side_size * 0.5f, side_size * 0.5f); - const sf::Vector2f current_position = simulation.renderer.vp_handler.getMouseWorldPosition(); - brush_preview.setPosition(to(int(current_position.x / to(cell_size)) * cell_size) + 2.0f, - to(int(current_position.y / to(cell_size)) * cell_size) + 2.0f); - target.draw(brush_preview, simulation.renderer.vp_handler.getRenderState()); - } + control_state.executeDrawAction(target, simulation.renderer.vp_handler); } void update() override diff --git a/include/simulation/world/world.hpp b/include/simulation/world/world.hpp index 2734cb9b..50fa6972 100644 --- a/include/simulation/world/world.hpp +++ b/include/simulation/world/world.hpp @@ -53,8 +53,12 @@ struct World void addWall(const sf::Vector2i& position) { if (map.checkCoords(position)) { - map.get(position).food = 0; - map.get(position).wall = 1; + WorldCell& cell = map.get(position); + cell.food = 0; + cell.wall = 1; + for (auto& markers : cell.markers) { + clearMarkersOfCell(markers); + } } } @@ -89,13 +93,17 @@ struct World void clearMarkers(uint8_t colony_id) { for (auto& cell : map.cells) { - auto& markers = cell.markers[colony_id]; - markers.intensity[0] = 0.0f; - markers.intensity[1] = 0.0f; - markers.intensity[2] = 0.0f; - markers.repellent = 0.0f; - markers.permanent = false; - cell.density = 0.0f; + clearMarkersOfCell(cell.markers[colony_id]); + cell.density = 0.0f; } } + + static void clearMarkersOfCell(ColonyCell& cell) + { + cell.intensity[0] = 0.0f; + cell.intensity[1] = 0.0f; + cell.intensity[2] = 0.0f; + cell.repellent = 0.0f; + cell.permanent = false; + } };