Skip to content

Commit

Permalink
Good progress but still issues
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Dec 1, 2021
1 parent d032618 commit 294cb48
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 24 deletions.
5 changes: 4 additions & 1 deletion include/editor/GUI/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Button : public DefaultButton
{
float angle_radius = 5.0f;
SPtr<TextLabel> label;
sf::Color background_color = sf::Color::White;

Button(const std::string& text, ButtonCallBack callback)
: DefaultButton(callback)
Expand All @@ -56,7 +57,9 @@ struct Button : public DefaultButton

void render(sf::RenderTarget& target) override
{
GUI::Item::draw(target, RoundedRectangle(size, position, angle_radius));
auto background = RoundedRectangle(size, position, angle_radius);
background.setFillColor(background_color);
GUI::Item::draw(target, background);
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/editor/GUI/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct Container : public Item
void removeItem(SPtr<Item> item)
{
for (auto sub = sub_items.begin(); sub != sub_items.end(); ++sub) {
if (*sub == item) {
if (sub->get() == item.get()) {
sub_items.erase(sub);
updateItems();
return;
Expand Down
6 changes: 6 additions & 0 deletions include/editor/GUI/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ struct Item

virtual void removeItem(const std::string& item_name)
{
for (auto it = sub_items.begin(); it != sub_items.end(); ++it) {
if ((*it)->name == item_name) {
sub_items.erase(it);
return;
}
}
}

template<typename T>
Expand Down
8 changes: 7 additions & 1 deletion include/editor/GUI/named_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ struct NamedContainer : public Container
{
root->addItem(item, name);
}


template<typename T>
void removeItem(SPtr<T> item)
{
root->removeItem(item);
}

void render(sf::RenderTarget& target) override
{
const float angle_radius = 10.0f;
Expand Down
11 changes: 10 additions & 1 deletion include/editor/colony_creator/colony_creator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ struct ColonyCreator : public GUI::NamedContainer
root->size_type.y = GUI::Size::FitContent;
auto add_button = create<GUI::Button>("Add", [this](){
if (this->colonies_count < 8) {
this->addItem(create<ColonyTool>());
std::cout << "Add " << this->colonies_count << std::endl;
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;
std::cout << "Remove " << this->colonies_count << std::endl;
};
}
});
add_button->setWidth(32.0f, GUI::Size::Fixed);
Expand Down
55 changes: 45 additions & 10 deletions include/editor/colony_creator/colony_tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,73 @@
#include "GUI/container.hpp"
#include "GUI/button.hpp"
#include "GUI/rounded_rectangle.hpp"
#include "editor/color_picker/color_picker.hpp"


struct ColonyTool : GUI::Container
{
SPtr<GUI::Container> top_zone;
SPtr<edtr::ColorPicker> color_picker;

ColonyTool()
: GUI::Container(Container::Orientation::Horizontal)
: GUI::Container(Container::Orientation::Vertical)
{


padding = 5.0f;
size_type.y = GUI::Size::FitContent;

auto color_button = create<GUI::Button>("", [](){});
top_zone = create<GUI::Container>(GUI::Container::Orientation::Horizontal);
top_zone->size_type.y = GUI::Size::FitContent;
top_zone->padding = 0.0f;
this->addItem(top_zone);

auto color_button = create<GUI::Button>("", [this](){
this->createColorPicker();
});
color_button->setHeight(30.0f);
color_button->setWidth(30.0f);
addItem(color_button);
top_zone->addItem(color_button, "colony_color_button");

auto to_focus_button = create<GUI::Button>("Focus", [](){});
to_focus_button->setHeight(20.0f);
to_focus_button->setWidth(40.0f);
addItem(to_focus_button);
top_zone->addItem(to_focus_button);

auto remove_button = create<GUI::Button>("Remove", [](){});
remove_button->setHeight(20.0f);
remove_button->setWidth(60.0f);
addItem(remove_button);
top_zone->addItem(remove_button, "remove");

// Create color picker
color_picker = create<edtr::ColorPicker>();
color_picker->setHeight(100.0f);
watch(color_picker, [this](){
setColor(color_picker->getColor());
});
}

void setColor(sf::Color color) const
{
top_zone->getByName<GUI::Button>("colony_color_button")->background_color = color;
}

void createColorPicker()
{
if (getByName<edtr::ColorPicker>("color_picker")) {
return;
}

this->addItem(color_picker, "color_picker");
}

void onMouseOut() override
{
GUI::Container::removeItem(color_picker);
}

void render(sf::RenderTarget& target) override
{
// auto background = GUI::RoundedRectangle(size, position, 3.0f);
// background.setFillColor(sf::Color::Red);
// GUI::Item::draw(target, background);
auto background = GUI::RoundedRectangle(size, position, 5.0f);
background.setFillColor(sf::Color(200, 200, 200));
GUI::Item::draw(target, background);
}
};
15 changes: 7 additions & 8 deletions include/editor/color_picker/color_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ struct ColorVariation : public GUI::Item
: GUI::Item(size_)
, color_va(sf::Quads, 8)
, color(sf::Color::Red)
, selected_color(color)
, current_color(color)
, selection(size.x, 0.0f)
, selection(0.0f, 0.0f)
{
initializeVA();
updateSelectedColor({});
}

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

void onSizeChange() override
{
selection = sf::Vector2f(size.x, 0.0f);
updateSelectedColor({});
initializeVA();
}

Expand Down Expand Up @@ -92,13 +91,13 @@ struct ColorVariation : public GUI::Item
void render(sf::RenderTarget& target) override
{
draw(target, color_va);
const float selection_radius = 16.0f;
const float selection_radius = 8.0f;
sf::CircleShape c(selection_radius);
c.setOrigin(selection_radius, selection_radius);
c.setPosition(position + selection);
c.setFillColor(current_color);
c.setOutlineColor(sf::Color::White);
c.setOutlineThickness(8.0f);
c.setOutlineThickness(2.0f);
draw(target, c);
}

Expand Down Expand Up @@ -195,7 +194,7 @@ struct ColorPicker : public GUI::Container
spacing = 0.0f;
size_type.y = GUI::Size::FitContent;
// Create sub elemets
const float hue_slider_height = 50.0f;
const float hue_slider_height = 20.0f;
color_variation = create<ColorVariation>();
hue_slider = create<HueSlider>();
hue_slider->setHeight(hue_slider_height);
Expand All @@ -215,7 +214,7 @@ struct ColorPicker : public GUI::Container

void onSizeChange() override
{
setHeight(size.x);
//setHeight(size.x);
}

sf::Color getColor() const
Expand Down
2 changes: 0 additions & 2 deletions include/editor/editor_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ struct EditorScene : public GUI::Scene
auto colonies = create<ColonyCreator>();
toolbox->addItem(colonies);

toolbox->addItem(create<ColorPicker>());

addItem(renderer);
addItem(toolbox, "Toolbox");
}
Expand Down

0 comments on commit 294cb48

Please sign in to comment.