Ajoute le sélecteur dans l'éditeur
This commit is contained in:
parent
9e03068357
commit
b7083b0ebd
|
@ -4,6 +4,7 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include "level.hpp"
|
#include "level.hpp"
|
||||||
#include "util/widget_timer.hpp"
|
#include "util/widget_timer.hpp"
|
||||||
|
#include "util/widget_selector.hpp"
|
||||||
|
|
||||||
enum class DragMode {NONE, PLACE, SELECT_RECT, SELECT_BULK, REMOVE};
|
enum class DragMode {NONE, PLACE, SELECT_RECT, SELECT_BULK, REMOVE};
|
||||||
enum class SelectionMode {REPLACE, FLIP, ADD};
|
enum class SelectionMode {REPLACE, FLIP, ADD};
|
||||||
|
@ -18,7 +19,9 @@ private:
|
||||||
sf::Vector2i drag_start;
|
sf::Vector2i drag_start;
|
||||||
sf::Vector2i drag_end;
|
sf::Vector2i drag_end;
|
||||||
DragMode drag_mode;
|
DragMode drag_mode;
|
||||||
|
|
||||||
WidgetTimer widget_timer;
|
WidgetTimer widget_timer;
|
||||||
|
WidgetSelector widget_selector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renvoie l'objet pointé à la position donnée
|
* Renvoie l'objet pointé à la position donnée
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
#ifndef __PTF_UTIL_WIDGET_SELECTOR_HPP__
|
||||||
|
#define __PTF_UTIL_WIDGET_SELECTOR_HPP__
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#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<std::shared_ptr<SelectorItem>> 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<std::shared_ptr<SelectorCategory>> categories;
|
||||||
|
SelectorItem* selected;
|
||||||
|
|
||||||
|
public:
|
||||||
|
WidgetSelector(Manager& manager);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ajoute une nouvelle catégorie de choix
|
||||||
|
*/
|
||||||
|
std::shared_ptr<SelectorCategory> 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
|
|
@ -8,11 +8,18 @@
|
||||||
|
|
||||||
const sf::Color SELECTION_COLOR = sf::Color(33, 33, 33, 40);
|
const sf::Color SELECTION_COLOR = sf::Color(33, 33, 33, 40);
|
||||||
const sf::Color SELECTION_BORDER_COLOR = sf::Color(33, 33, 33, 127);
|
const sf::Color SELECTION_BORDER_COLOR = sf::Color(33, 33, 33, 127);
|
||||||
const int SCROLL_SPEED = 2;
|
const int SCROLL_SPEED = 5;
|
||||||
const int SCROLL_PADDING = 8;
|
const int SCROLL_PADDING = 5;
|
||||||
|
|
||||||
Editor::Editor(Manager& manager) : Level(manager), drag_mode(DragMode::NONE),
|
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<SelectorCategory> 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() {}
|
Editor::~Editor() {}
|
||||||
|
|
||||||
|
@ -38,6 +45,11 @@ void Editor::processEvent(const sf::Event& event) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// traitement des événements du widget selector
|
||||||
|
if (widget_selector.processEvent(event)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// lorsque l'on clique dans l'éditeur
|
// lorsque l'on clique dans l'éditeur
|
||||||
if (event.type == sf::Event::MouseButtonPressed) {
|
if (event.type == sf::Event::MouseButtonPressed) {
|
||||||
sf::Vector2i mouse_position(event.mouseButton.x, event.mouseButton.y);
|
sf::Vector2i mouse_position(event.mouseButton.x, event.mouseButton.y);
|
||||||
|
@ -191,11 +203,11 @@ void Editor::draw() {
|
||||||
widget_timer.setTimeLeft(getTotalTime());
|
widget_timer.setTimeLeft(getTotalTime());
|
||||||
widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0));
|
widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0));
|
||||||
|
|
||||||
// menu
|
// sélectionneur d'objet
|
||||||
sf::RectangleShape menu(sf::Vector2f(window_size.x, 64));
|
widget_selector.draw(
|
||||||
menu.setPosition(sf::Vector2f(0, window_size.y - 64));
|
sf::Vector2f(window_size.x - 64, 0),
|
||||||
|
sf::Vector2f(64, window_size.y)
|
||||||
window.draw(menu);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectPtr Editor::getObject(sf::Vector2f position) {
|
ObjectPtr Editor::getObject(sf::Vector2f position) {
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
#include "util/widget_selector.hpp"
|
||||||
|
|
||||||
|
const int PADDING = 8;
|
||||||
|
|
||||||
|
WidgetSelector::WidgetSelector(Manager& manager) : manager(manager), selected(NULL) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<SelectorCategory> WidgetSelector::addCategory(sf::String name) {
|
||||||
|
auto cat = std::shared_ptr<SelectorCategory>(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<SelectorItem>(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<SelectorCategory> 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<SelectorItem> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue