Skip to content

Commit

Permalink
Adding simulation interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Dec 4, 2021
1 parent d38c01e commit 67c7cf2
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 166 deletions.
52 changes: 0 additions & 52 deletions include/editor/ world_view.hpp

This file was deleted.

40 changes: 27 additions & 13 deletions include/editor/colony_creator/colony_creator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,51 @@
#include "editor/GUI/container.hpp"
#include "editor/GUI/button.hpp"
#include "colony_tool.hpp"
#include "simulation/simulation.hpp"


namespace edtr
{

struct ColonyCreator : public GUI::NamedContainer
{
uint32_t colonies_count = 0;
Simulation& simulation;
ControlState& control_state;
uint32_t colonies_count = 0;

ColonyCreator()
explicit
ColonyCreator(Simulation& sim, ControlState& control_state_)
: GUI::NamedContainer("Colonies", Container::Orientation::Vertical)
, simulation(sim)
, control_state(control_state_)
{
padding = 5.0f;
root->size_type.y = GUI::Size::FitContent;
auto add_button = create<GUI::Button>("Add", [this](){
if (this->colonies_count < 8) {
auto colony_tool = create<ColonyTool>();

this->addItem(colony_tool);
++this->colonies_count;

colony_tool->top_zone->getByName<GUI::Button>("remove")->click_callback = [this, colony_tool](){
this->removeItem(colony_tool);
--this->colonies_count;
};
}
this->createColony();
});
add_button->setWidth(32.0f, GUI::Size::Fixed);
add_button->setHeight(20.0f, GUI::Size::Fixed);
header->addItem(add_button);
}

void createColony()
{
if (this->colonies_count < Conf::MAX_COLONIES_COUNT) {
Colony& new_colony = simulation.createColony(50.0f, 50.0f);
auto colony_tool = create<ColonyTool>(new_colony, control_state);

// Add the new item to this
this->addItem(colony_tool);
++this->colonies_count;

// Set the correct callback for the remove button
colony_tool->top_zone->getByName<GUI::Button>("remove")->click_callback = [this, colony_tool](){
this->removeItem(colony_tool);
--this->colonies_count;
};
}
}
};

}
Expand Down
16 changes: 15 additions & 1 deletion include/editor/colony_creator/colony_tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
#include "GUI/button.hpp"
#include "GUI/rounded_rectangle.hpp"
#include "editor/color_picker/color_picker.hpp"
#include "editor/control_state.hpp"


struct ColonyTool : GUI::Container
{
Colony& colony;
ControlState& control_state;
SPtr<GUI::Container> top_zone;
SPtr<edtr::ColorPicker> color_picker;

ColonyTool()
ColonyTool(Colony& colony_, ControlState& control_state_)
: GUI::Container(Container::Orientation::Vertical)
, colony(colony_)
, control_state(control_state_)
{
padding = 5.0f;
size_type.y = GUI::Size::FitContent;
Expand All @@ -33,6 +38,15 @@ struct ColonyTool : GUI::Container
to_focus_button->setWidth(40.0f);
top_zone->addItem(to_focus_button);

auto set_position_button = create<GUI::Button>("Set Position", [this](){
control_state.view_action = [](sf::Vector2f world_position) {
std::cout << "World Pos from set position " << world_position.x << " " << world_position.y << std::endl;
};
});
set_position_button->setHeight(20.0f);
set_position_button->setWidth(100.0f);
top_zone->addItem(set_position_button);

auto remove_button = create<GUI::Button>("Remove", [](){});
remove_button->setHeight(20.0f);
remove_button->setWidth(60.0f);
Expand Down
17 changes: 9 additions & 8 deletions include/editor/color_picker/color_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ColorVariation : public GUI::Item
, selection(0.0f, 0.0f)
{
initializeVA();
updateSelectedColor({});
updateSelectedColor();
}

void initializeVA()
Expand All @@ -47,7 +47,7 @@ struct ColorVariation : public GUI::Item

void onSizeChange() override
{
updateSelectedColor({});
updateSelectedColor();
initializeVA();
}

Expand All @@ -67,25 +67,25 @@ struct ColorVariation : public GUI::Item
(to<float>(color.b) + to<float>(255 - color.b) * ratio_x) * ratio_y);
}

void updateSelectedColor(sf::Vector2f mouse_position)
void updateSelectedColor()
{
selection = mouse_position;
selected_color = getColorAt(mouse_position);
selected_color = getColorAt(selection);
current_color = selected_color;
notifyChanged();
}

void onClick(sf::Vector2f mouse_position, sf::Mouse::Button) override
{
updateSelectedColor(mouse_position);
selection = mouse_position;
updateSelectedColor();
}

void setColor(sf::Color new_color)
{
color = new_color;
color_va[1].color = color;
color_va[2].color = color;
current_color = getColorAt(selection);
updateSelectedColor();
}

void render(sf::RenderTarget& target) override
Expand All @@ -104,7 +104,8 @@ struct ColorVariation : public GUI::Item
void onMouseMove(sf::Vector2f mouse_position) override
{
if (clicking) {
updateSelectedColor(mouse_position);
selection = mouse_position;
updateSelectedColor();
}
}
};
Expand Down
19 changes: 19 additions & 0 deletions include/editor/control_state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <functional>
#include <SFML/System/Vector2.hpp>


struct ControlState
{
using ViewAction = std::function<void(sf::Vector2f)>;
using Action = std::function<void(void)>;

ViewAction view_action = nullptr;

void executeViewAction(sf::Vector2f mouse_world_position)
{
if (view_action) {
view_action(mouse_world_position);
}
}
};
25 changes: 12 additions & 13 deletions include/editor/editor_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "simulation/world/world.hpp"
#include "render/renderer.hpp"
#include "simulation/config.hpp"
#include "editor/ world_view.hpp"
#include "editor/world_view.hpp"


namespace edtr
Expand All @@ -21,19 +21,20 @@ struct EditorScene : public GUI::Scene
{
using Ptr = std::shared_ptr<EditorScene>;

World world;
Simulation& simulation;
ControlState control_state;

SPtr<Toolbox> toolbox;
SPtr<WorldView> renderer;
SPtr<ColorPicker> color_picker;
SPtr<ColorSaver> color_saver;
SPtr<Toolbox> toolbox;
SPtr<WorldView> renderer;
SPtr<ColorPicker> color_picker;
SPtr<ColorSaver> color_saver;
SPtr<SetColorButton> set_color_button;
SPtr<ToolSelector> tool_selector;
SPtr<ToolSelector> tool_selector;

explicit
EditorScene(sf::RenderWindow& window)
EditorScene(sf::RenderWindow& window, Simulation& sim)
: GUI::Scene(window)
, world(Conf::WORLD_WIDTH, Conf::WORLD_HEIGHT)
, simulation(sim)
{
Conf::loadTextures();
initialize();
Expand All @@ -47,18 +48,16 @@ struct EditorScene : public GUI::Scene

void initialize()
{
MapLoader::loadMap(world, "res/map.png");

const sf::Vector2u window_size = window.getSize();

renderer = create<WorldView>(toVector2f(window_size), world);
renderer = create<WorldView>(toVector2f(window_size), simulation, control_state);

toolbox = create<Toolbox>(sf::Vector2f(350.0f, to<float>(window_size.y)));
tool_selector = create<ToolSelector>();

toolbox->addItem(tool_selector);

auto colonies = create<ColonyCreator>();
auto colonies = create<ColonyCreator>(simulation, control_state);
toolbox->addItem(colonies);

addItem(renderer);
Expand Down
17 changes: 0 additions & 17 deletions include/editor/game_scene.hpp

This file was deleted.

53 changes: 0 additions & 53 deletions include/editor/game_view.hpp

This file was deleted.

Loading

0 comments on commit 67c7cf2

Please sign in to comment.