Skip to content

Commit

Permalink
Fix things and little refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Dec 9, 2021
1 parent d1c517e commit 328c2e3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
11 changes: 11 additions & 0 deletions include/editor/colony_creator/colony_tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ struct ColonyTool : GUI::Container

auto set_position_button = create<GUI::Button>("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);
Expand Down
29 changes: 23 additions & 6 deletions include/editor/control_state.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#pragma once
#include <functional>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics.hpp>
#include "editor/transition.hpp"


struct ControlState
{
using ViewAction = std::function<void(sf::Vector2f)>;
using Action = std::function<void(void)>;
using DrawAction = std::function<void(sf::RenderTarget&, const ViewportHandler&)>;

// 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<void()> request_edits_off = nullptr;

// Special commands
bool focus_requested = false;
trn::Transition<sf::Vector2f> focus;
Expand All @@ -20,8 +25,6 @@ struct ControlState
bool show_brush_preview = false;
float brush_radius = 0.0f;

std::function<void()> request_edits_off = nullptr;

ControlState() = default;

void executeViewAction(sf::Vector2f mouse_world_position)
Expand All @@ -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;
Expand All @@ -58,4 +68,11 @@ struct ControlState
request_edits_off();
}
}

void resetCallbacks()
{
view_action = nullptr;
draw_action = nullptr;
view_action_end = nullptr;
}
};
14 changes: 14 additions & 0 deletions include/editor/tool_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<float>(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<float>(int(current_position.x / to<float>(cell_size)) * cell_size) + 2.0f,
to<float>(int(current_position.y / to<float>(cell_size)) * cell_size) + 2.0f);
target.draw(brush_preview, vp_handler.getRenderState());
};
}
}

Expand All @@ -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;
}
};

Expand Down
14 changes: 3 additions & 11 deletions include/editor/world_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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<float>(int(current_position.x / to<float>(cell_size)) * cell_size) + 2.0f,
to<float>(int(current_position.y / to<float>(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
Expand Down
26 changes: 17 additions & 9 deletions include/simulation/world/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
}
};

0 comments on commit 328c2e3

Please sign in to comment.