Ajout du bloc déplaçable dans la liste & fix segfault
This commit is contained in:
parent
42fc43a83a
commit
48adba301e
|
@ -13,7 +13,10 @@
|
|||
struct ToolbarObject {
|
||||
typedef std::shared_ptr<ToolbarObject> Ptr;
|
||||
|
||||
// image représentant cet objet visuellement
|
||||
sf::Sprite sprite;
|
||||
|
||||
// fonction appelée pour instancier un objet de ce type
|
||||
std::function<Object::Ptr(void)> create_object;
|
||||
};
|
||||
|
||||
|
@ -23,13 +26,16 @@ struct ToolbarObject {
|
|||
struct ToolbarCategory {
|
||||
typedef std::shared_ptr<ToolbarCategory> Ptr;
|
||||
|
||||
// nom de la catégorie
|
||||
sf::String name;
|
||||
|
||||
// liste des objets dans la catégorie
|
||||
std::vector<ToolbarObject::Ptr> objects;
|
||||
|
||||
/**
|
||||
* Ajoute un nouvel objet dans cette catégorie
|
||||
*/
|
||||
void addObject(
|
||||
ToolbarObject::Ptr addObject(
|
||||
sf::Texture& texture,
|
||||
std::function<Object::Ptr(void)> create_object
|
||||
);
|
||||
|
@ -44,13 +50,18 @@ private:
|
|||
|
||||
sf::RectangleShape background;
|
||||
std::vector<ToolbarCategory::Ptr> categories;
|
||||
ToolbarObject* selected;
|
||||
ToolbarObject::Ptr selected;
|
||||
|
||||
/**
|
||||
* Crée un objet de type Block
|
||||
*/
|
||||
Object::Ptr createBlock();
|
||||
|
||||
/**
|
||||
* Crée un objet de type Block déplaçable
|
||||
*/
|
||||
Object::Ptr createMovableBlock();
|
||||
|
||||
/**
|
||||
* Crée un objet de type Player
|
||||
*/
|
||||
|
|
|
@ -327,8 +327,12 @@ Object::Ptr Editor::addObject(sf::Vector2f position) {
|
|||
|
||||
// on arrondit à l'unité de grille la plus proche
|
||||
position = roundVectorToGrid(position);
|
||||
|
||||
Object::Ptr object = widget_toolbar.createObject();
|
||||
|
||||
if (object == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
object->setPosition(position);
|
||||
|
||||
// avant d'ajouter l'objet, on vérifie qu'il ne soit
|
||||
|
|
|
@ -8,12 +8,13 @@ const int PADDING = 8;
|
|||
|
||||
ToolbarCategory::Ptr WidgetToolbar::addCategory(sf::String name) {
|
||||
auto cat = ToolbarCategory::Ptr(new ToolbarCategory);
|
||||
|
||||
cat->name = name;
|
||||
categories.push_back(cat);
|
||||
return cat;
|
||||
}
|
||||
|
||||
void ToolbarCategory::addObject(
|
||||
ToolbarObject::Ptr ToolbarCategory::addObject(
|
||||
sf::Texture& texture,
|
||||
std::function<Object::Ptr(void)> create_object
|
||||
) {
|
||||
|
@ -22,12 +23,19 @@ void ToolbarCategory::addObject(
|
|||
object->sprite.setTexture(texture, true);
|
||||
object->create_object = create_object;
|
||||
objects.push_back(object);
|
||||
return object;
|
||||
}
|
||||
|
||||
Object::Ptr WidgetToolbar::createBlock() {
|
||||
return Object::Ptr(new Block);
|
||||
}
|
||||
|
||||
Object::Ptr WidgetToolbar::createMovableBlock() {
|
||||
Object::Ptr movable = Object::Ptr(new Block);
|
||||
movable->setMass(2.f);
|
||||
return movable;
|
||||
}
|
||||
|
||||
Object::Ptr WidgetToolbar::createPlayer() {
|
||||
return Object::Ptr(new Player);
|
||||
}
|
||||
|
@ -39,19 +47,28 @@ Object::Ptr WidgetToolbar::createGravityBlock(GravityDirection direction) {
|
|||
}
|
||||
|
||||
Object::Ptr WidgetToolbar::createObject() {
|
||||
if (selected != nullptr) {
|
||||
return selected->create_object();
|
||||
}
|
||||
|
||||
WidgetToolbar::WidgetToolbar(Manager& manager) : manager(manager), selected(NULL) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
WidgetToolbar::WidgetToolbar(Manager& manager) : manager(manager), selected(nullptr) {
|
||||
// catégorie des objets plaçables de base
|
||||
ResourceManager& resources = manager.getResourceManager();
|
||||
ToolbarCategory::Ptr basic_cat = addCategory("BASE");
|
||||
|
||||
basic_cat->addObject(
|
||||
selected = basic_cat->addObject(
|
||||
resources.getTexture("toolbar_block.tga"),
|
||||
std::bind(&WidgetToolbar::createBlock, this)
|
||||
);
|
||||
|
||||
basic_cat->addObject(
|
||||
resources.getTexture("toolbar_movable_block.tga"),
|
||||
std::bind(&WidgetToolbar::createMovableBlock, this)
|
||||
);
|
||||
|
||||
basic_cat->addObject(
|
||||
resources.getTexture("toolbar_player.tga"),
|
||||
std::bind(&WidgetToolbar::createPlayer, this)
|
||||
|
@ -90,7 +107,7 @@ bool WidgetToolbar::processEvent(const sf::Event& event) {
|
|||
for (unsigned int i = 0; i < categories.size(); i++) {
|
||||
for (unsigned int j = 0; j < categories[i]->objects.size(); j++) {
|
||||
if (categories[i]->objects[j]->sprite.getGlobalBounds().contains(position)) {
|
||||
selected = categories[i]->objects[j].get();
|
||||
selected = categories[i]->objects[j];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -141,7 +158,7 @@ void WidgetToolbar::draw(sf::Vector2f position, sf::Vector2f size) {
|
|||
total_y
|
||||
);
|
||||
|
||||
if (selected == object.get()) {
|
||||
if (selected == object) {
|
||||
sf::RectangleShape selection_rectangle(sf::Vector2f(32, 32));
|
||||
selection_rectangle.setPosition(sprite_position + sf::Vector2f(-4, -4));
|
||||
selection_rectangle.setFillColor(sf::Color(0, 0, 0, 20));
|
||||
|
|
Loading…
Reference in New Issue