From b7083b0ebd3e8d48e12e076f593aa47558d8ebad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Fri, 8 Apr 2016 15:54:19 +0200 Subject: [PATCH] =?UTF-8?q?Ajoute=20le=20s=C3=A9lecteur=20dans=20l'=C3=A9d?= =?UTF-8?q?iteur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/editor.hpp | 3 + include/util/widget_selector.hpp | 61 ++++++++++++++++++ src/editor.cpp | 28 +++++--- src/util/widget_selector.cpp | 106 +++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 include/util/widget_selector.hpp create mode 100644 src/util/widget_selector.cpp diff --git a/include/editor.hpp b/include/editor.hpp index 83a9fbe..1e584e8 100644 --- a/include/editor.hpp +++ b/include/editor.hpp @@ -4,6 +4,7 @@ #include #include "level.hpp" #include "util/widget_timer.hpp" +#include "util/widget_selector.hpp" enum class DragMode {NONE, PLACE, SELECT_RECT, SELECT_BULK, REMOVE}; enum class SelectionMode {REPLACE, FLIP, ADD}; @@ -18,7 +19,9 @@ private: sf::Vector2i drag_start; sf::Vector2i drag_end; DragMode drag_mode; + WidgetTimer widget_timer; + WidgetSelector widget_selector; /** * Renvoie l'objet pointé à la position donnée diff --git a/include/util/widget_selector.hpp b/include/util/widget_selector.hpp new file mode 100644 index 0000000..5de17f4 --- /dev/null +++ b/include/util/widget_selector.hpp @@ -0,0 +1,61 @@ +#ifndef __PTF_UTIL_WIDGET_SELECTOR_HPP__ +#define __PTF_UTIL_WIDGET_SELECTOR_HPP__ + +#include +#include +#include "manager.hpp" + +/** + * Un choix possible dans une catégorie + */ +struct SelectorItem { + sf::String name; + + sf::Sprite sprite; + sf::Sprite select_sprite; +}; + +/** + * Représente une catégorie de choix possibles + */ +struct SelectorCategory { + sf::String name; + std::vector> items; + + /** + * Ajoute un nouvel item dans cette catégorie + */ + void addItem(sf::String name, sf::Texture& texture, sf::Texture& select_texture); +}; + +/** + * Affiche une liste de sélection + */ +class WidgetSelector { +private: + Manager& manager; + + sf::RectangleShape background; + std::vector> categories; + SelectorItem* selected; + +public: + WidgetSelector(Manager& manager); + + /** + * Ajoute une nouvelle catégorie de choix + */ + std::shared_ptr addCategory(sf::String name); + + /** + * Traite l'événement et renvoie true si l'on s'en est servi + */ + bool processEvent(const sf::Event& event); + + /** + * Dessine le widget à la position donnée et avec la taille donnée + */ + void draw(sf::Vector2f position, sf::Vector2f size); +}; + +#endif diff --git a/src/editor.cpp b/src/editor.cpp index 2d89172..963e71b 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -8,11 +8,18 @@ const sf::Color SELECTION_COLOR = sf::Color(33, 33, 33, 40); const sf::Color SELECTION_BORDER_COLOR = sf::Color(33, 33, 33, 127); -const int SCROLL_SPEED = 2; -const int SCROLL_PADDING = 8; +const int SCROLL_SPEED = 5; +const int SCROLL_PADDING = 5; Editor::Editor(Manager& manager) : Level(manager), drag_mode(DragMode::NONE), - widget_timer(manager, true, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)) {} + widget_timer(manager, true, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)), + widget_selector(manager) { + + ResourceManager& resources = manager.getResourceManager(); + std::shared_ptr basic = widget_selector.addCategory("BASE"); + basic->addItem("Bloc", resources.getTexture("block.png"), resources.getTexture("block_select.png")); + basic->addItem("Ball", resources.getTexture("ball.png"), resources.getTexture("block_select.png")); +} Editor::~Editor() {} @@ -38,6 +45,11 @@ void Editor::processEvent(const sf::Event& event) { return; } + // traitement des événements du widget selector + if (widget_selector.processEvent(event)) { + return; + } + // lorsque l'on clique dans l'éditeur if (event.type == sf::Event::MouseButtonPressed) { sf::Vector2i mouse_position(event.mouseButton.x, event.mouseButton.y); @@ -191,11 +203,11 @@ void Editor::draw() { widget_timer.setTimeLeft(getTotalTime()); widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0)); - // menu - sf::RectangleShape menu(sf::Vector2f(window_size.x, 64)); - menu.setPosition(sf::Vector2f(0, window_size.y - 64)); - - window.draw(menu); + // sélectionneur d'objet + widget_selector.draw( + sf::Vector2f(window_size.x - 64, 0), + sf::Vector2f(64, window_size.y) + ); } ObjectPtr Editor::getObject(sf::Vector2f position) { diff --git a/src/util/widget_selector.cpp b/src/util/widget_selector.cpp new file mode 100644 index 0000000..1cd2b00 --- /dev/null +++ b/src/util/widget_selector.cpp @@ -0,0 +1,106 @@ +#include +#include +#include "util/widget_selector.hpp" + +const int PADDING = 8; + +WidgetSelector::WidgetSelector(Manager& manager) : manager(manager), selected(NULL) { + // +} + +std::shared_ptr WidgetSelector::addCategory(sf::String name) { + auto cat = std::shared_ptr(new SelectorCategory); + cat->name = name; + + categories.push_back(cat); + return cat; +} + +void SelectorCategory::addItem(sf::String name, sf::Texture& texture, sf::Texture& select_texture) { + auto item = std::shared_ptr(new SelectorItem); + item->sprite.setTexture(texture, true); + item->select_sprite.setTexture(select_texture, true); + + float width = item->sprite.getLocalBounds().width; + + // mise à l'échelle si trop grand ou trop petit + if (width < 20 || width > 28) { + item->sprite.scale(24 / width, 24 / width); + item->select_sprite.scale(24 / width, 24 / width); + width = 24; + } + + item->name = name; + items.push_back(item); +} + +bool WidgetSelector::processEvent(const sf::Event& event) { + if (event.type == sf::Event::MouseButtonPressed) { + sf::Vector2f position(event.mouseButton.x, event.mouseButton.y); + + // clic gauche sur un item : on le sélectionne + if (event.mouseButton.button == sf::Mouse::Left) { + for (unsigned int i = 0; i < categories.size(); i++) { + for (unsigned int j = 0; j < categories[i]->items.size(); j++) { + if (categories[i]->items[j]->sprite.getGlobalBounds().contains(position)) { + selected = categories[i]->items[j].get(); + return true; + } + } + } + + // ne pas laisser traverser les clics sur le sélecteur + if (background.getGlobalBounds().contains(position)) { + return true; + } + } + } + + return false; +} + +void WidgetSelector::draw(sf::Vector2f position, sf::Vector2f size) { + sf::RenderWindow& window = manager.getWindow(); + + background.setSize(size); + background.setPosition(position); + window.draw(background); + + for (unsigned int i = 0; i < categories.size(); i++) { + std::shared_ptr category = categories[i]; + + // affichage du label de la catégorie + sf::Text category_label( + category->name, + manager.getResourceManager().getFont("raleway.ttf"), 12 + ); + + category_label.setColor(sf::Color::Black); + category_label.setPosition(position + sf::Vector2f( + floor(size.x / 2 - category_label.getGlobalBounds().width / 2), + PADDING + )); + + window.draw(category_label); + + for (unsigned int j = 0; j < category->items.size(); j++) { + std::shared_ptr item = category->items[j]; + sf::Vector2f sprite_position = position + sf::Vector2f( + size.x / 2 - item->sprite.getGlobalBounds().width / 2, + PADDING + 24 + 38 * j + ); + + // affichage des sprites des items + le sprite + // de sélection si sélectionné + item->sprite.setPosition(sprite_position); + item->select_sprite.setPosition(sprite_position); + + window.draw(item->sprite); + + if (selected == item.get()) { + window.draw(item->select_sprite); + } + } + } + +}