diff --git a/include/gui/action_toolbar.hpp b/include/gui/action_toolbar.hpp index a4f20e4..118d483 100644 --- a/include/gui/action_toolbar.hpp +++ b/include/gui/action_toolbar.hpp @@ -4,7 +4,6 @@ #include #include #include "../objects/object.hpp" -#include "../states/level.hpp" /** * Barre d'outils pour les actions en jeu diff --git a/include/gui/object_toolbar.hpp b/include/gui/object_toolbar.hpp index 2311ff2..9a86908 100644 --- a/include/gui/object_toolbar.hpp +++ b/include/gui/object_toolbar.hpp @@ -5,8 +5,8 @@ #include #include #include -#include "../states/level.hpp" #include "../objects/object.hpp" +#include "../utility.hpp" #include "object_button.hpp" /** @@ -43,7 +43,7 @@ private: Object::Ptr createSwitchBlock(); Object::Ptr createFinishBlock(); Object::Ptr createKillBlock(); - Object::Ptr createGravityBlock(GravityDirection direction); + Object::Ptr createGravityBlock(Utility::Direction direction); public: ObjectToolbar(); diff --git a/include/objects/gravity_block.hpp b/include/objects/gravity_block.hpp index 63005af..d71a4e9 100644 --- a/include/objects/gravity_block.hpp +++ b/include/objects/gravity_block.hpp @@ -3,10 +3,10 @@ #include #include +#include "../utility.hpp" #include "block.hpp" class Game; -enum class GravityDirection; /** * Le bloc de gravité est un bloc spécial qui change l'orientation @@ -19,7 +19,7 @@ public: typedef std::shared_ptr Ptr; private: - GravityDirection gravity_direction; + Utility::Direction gravity_direction; sf::Sprite icon_sprite; float opacity; bool used; @@ -73,12 +73,12 @@ public: /** * Récupère la direction de gravité du bloc changeur de gravité */ - GravityDirection getGravityDirection() const; + Utility::Direction getGravityDirection() const; /** * Modifie la direction de gravité du bloc */ - void setGravityDirection(GravityDirection set_gravity_direction); + void setGravityDirection(Utility::Direction set_gravity_direction); }; #endif diff --git a/include/states/editor.hpp b/include/states/editor.hpp index e96be06..f080d04 100644 --- a/include/states/editor.hpp +++ b/include/states/editor.hpp @@ -1,7 +1,6 @@ #ifndef __SKIZZLE_EDITOR_HPP__ #define __SKIZZLE_EDITOR_HPP__ -#include "../gui/action_toolbar.hpp" #include "../gui/object_toolbar.hpp" #include "level.hpp" @@ -22,10 +21,7 @@ private: sf::Vector2f *drag_control_point; DragMode drag_mode; - - ActionToolbar action_toolbar; ObjectToolbar object_toolbar; - sfg::Button::Ptr mute_button; /** * Renvoie l'objet pointé à la position donnée @@ -78,11 +74,16 @@ private: void selectAll(); protected: + /** + * Demande le dessin d'une frame + */ + void frame() override; + /** * Dessine tous les objets, le fond et * l'interface de l'éditeur */ - virtual void draw(); + void draw() override; public: Editor(Manager& manager); @@ -98,11 +99,6 @@ public: */ void processEvent(const sf::Event& event) override; - /** - * Demande le dessin d'une frame - */ - void frame() override; - /** * Lance le test du niveau */ diff --git a/include/states/game.hpp b/include/states/game.hpp index 363a3b8..183c73a 100644 --- a/include/states/game.hpp +++ b/include/states/game.hpp @@ -43,11 +43,16 @@ private: bool isInZone(Object::Ptr object); protected: + /** + * Demande le dessin d'une frame + */ + void frame() override; + /** * Dessine tous les objets, le fond et * l'interface de jeu */ - virtual void draw(); + void draw() override; /** * S'assure que la caméra est centrée sur les joueurs @@ -61,17 +66,12 @@ public: /** * Active l'état */ - virtual void enable(); + void enable() override; /** * Traite l'événement donné */ - virtual void processEvent(const sf::Event& event); - - /** - * Demande le dessin d'une frame - */ - virtual void frame(); + void processEvent(const sf::Event& event) override; /** * Tue l'objet donné diff --git a/include/states/level.hpp b/include/states/level.hpp index 6575495..685280c 100644 --- a/include/states/level.hpp +++ b/include/states/level.hpp @@ -3,15 +3,14 @@ #include #include +#include "../gui/action_toolbar.hpp" +#include "../utility.hpp" #include "../objects/object.hpp" #include "../objects/player.hpp" #include "state.hpp" class Manager; -// liste des directions de la gravité -enum class GravityDirection {NORTH, EAST, SOUTH, WEST}; - /** * La classe Level est une abstraction des * classes affichant une collection d'objets, comme @@ -21,7 +20,7 @@ class Level : public State { private: sf::View camera; float camera_angle; - GravityDirection gravity_direction; + Utility::Direction gravity_direction; sf::String name; std::string path; @@ -36,7 +35,16 @@ private: std::vector players; std::vector zone; + sfg::Button::Ptr mute_button; + protected: + ActionToolbar action_toolbar; + + /** + * Demande le dessin d'une frame + */ + virtual void frame(); + /** * Dessine tous les objets et le fond à l'écran */ @@ -146,12 +154,12 @@ public: /** * Récupère la direction de la gravité */ - GravityDirection getGravityDirection(); + Utility::Direction getGravityDirection(); /** * Modifie la direction de la gravité */ - void setGravityDirection(GravityDirection set_gravity_direction); + void setGravityDirection(Utility::Direction set_gravity_direction); /** * Récupère la liste des objets diff --git a/include/utility.hpp b/include/utility.hpp index 01b6ea3..28bccb9 100644 --- a/include/utility.hpp +++ b/include/utility.hpp @@ -10,6 +10,11 @@ namespace Utility { * avec la vitesse donnée */ float animateValue(float current, float speed, float goal); + + /** + * Liste des directions + */ + enum class Direction {NORTH, EAST, SOUTH, WEST}; } #endif diff --git a/res/textures/toolbar/icon_gear.tga b/res/textures/toolbar/icon_gear.tga index d968b61..9588322 100644 Binary files a/res/textures/toolbar/icon_gear.tga and b/res/textures/toolbar/icon_gear.tga differ diff --git a/src/gui/object_toolbar.cpp b/src/gui/object_toolbar.cpp index 4874668..f4d2a27 100644 --- a/src/gui/object_toolbar.cpp +++ b/src/gui/object_toolbar.cpp @@ -29,19 +29,19 @@ ObjectToolbar::ObjectToolbar() { addCreator("kill_block", std::bind(&ObjectToolbar::createKillBlock, this)); addCreator("gravity_block_north", std::bind( - &ObjectToolbar::createGravityBlock, this, GravityDirection::NORTH + &ObjectToolbar::createGravityBlock, this, Utility::Direction::NORTH )); addCreator("gravity_block_east", std::bind( - &ObjectToolbar::createGravityBlock, this, GravityDirection::EAST + &ObjectToolbar::createGravityBlock, this, Utility::Direction::EAST )); addCreator("gravity_block_south", std::bind( - &ObjectToolbar::createGravityBlock, this, GravityDirection::SOUTH + &ObjectToolbar::createGravityBlock, this, Utility::Direction::SOUTH )); addCreator("gravity_block_west", std::bind( - &ObjectToolbar::createGravityBlock, this, GravityDirection::WEST + &ObjectToolbar::createGravityBlock, this, Utility::Direction::WEST )); // attachement de la liste des créateurs à l'interface @@ -117,7 +117,7 @@ Object::Ptr ObjectToolbar::createKillBlock() { return Object::Ptr(new KillBlock); } -Object::Ptr ObjectToolbar::createGravityBlock(GravityDirection direction) { +Object::Ptr ObjectToolbar::createGravityBlock(Utility::Direction direction) { GravityBlock::Ptr gravity_block = GravityBlock::Ptr(new GravityBlock); gravity_block->setGravityDirection(direction); return std::dynamic_pointer_cast(gravity_block); diff --git a/src/objects/gravity_block.cpp b/src/objects/gravity_block.cpp index a9f865d..58a41a7 100644 --- a/src/objects/gravity_block.cpp +++ b/src/objects/gravity_block.cpp @@ -53,7 +53,7 @@ void GravityBlock::init(std::ifstream& file, Object::Ptr object) { // lecture de la direction de la gravité char gravity_direction; file.read(&gravity_direction, 1); - gravity_block->setGravityDirection((GravityDirection) gravity_direction); + gravity_block->setGravityDirection((Utility::Direction) gravity_direction); // lecture des propriétés d'un bloc Block::init(file, object); @@ -74,30 +74,30 @@ void GravityBlock::save(std::ofstream& file) const { Block::save(file); } -GravityDirection GravityBlock::getGravityDirection() const { +Utility::Direction GravityBlock::getGravityDirection() const { return gravity_direction; } -void GravityBlock::setGravityDirection(GravityDirection set_gravity_direction) { +void GravityBlock::setGravityDirection(Utility::Direction set_gravity_direction) { gravity_direction = set_gravity_direction; // sélectionne le sprite d'icône selon la direction std::string texture; switch (gravity_direction) { - case GravityDirection::NORTH: + case Utility::Direction::NORTH: texture = "north"; break; - case GravityDirection::EAST: + case Utility::Direction::EAST: texture = "east"; break; - case GravityDirection::SOUTH: + case Utility::Direction::SOUTH: texture = "south"; break; - case GravityDirection::WEST: + case Utility::Direction::WEST: texture = "west"; break; } diff --git a/src/states/editor.cpp b/src/states/editor.cpp index 21dd2fe..fce5aa3 100644 --- a/src/states/editor.cpp +++ b/src/states/editor.cpp @@ -35,30 +35,11 @@ inline sf::Vector2f roundVectorToGrid(sf::Vector2f input) { Editor::Editor(Manager& manager) : Level(manager), drag_control_point(nullptr), drag_mode(Editor::DragMode::NONE) { - // ajout des boutons dans la barre d'action + // ajout des boutons d'action de la barre d'action action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_back.tga"), - std::bind(&Manager::popState, &getManager()) + *ResourceManager::get().getImage("toolbar/icon_gear.tga") ); - mute_button = action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_music.tga"), - []() { - // on inverse le drapeau de muet - ResourceManager::get().setMuted( - !ResourceManager::get().isMuted() - ); - } - ); - - action_toolbar.addSpacer(20, true, false); - - action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_no_music.tga") - ); - - action_toolbar.addSpacer(20, true, false); - action_toolbar.addButton( *ResourceManager::get().getImage("toolbar/icon_save.tga"), std::bind(&Editor::save, this) @@ -69,17 +50,11 @@ Editor::Editor(Manager& manager) : Level(manager), std::bind(&Editor::test, this) ); - action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_gear.tga") - ); - - // signalement des sous-widgets - getManager().addWidget(action_toolbar.getWindow()); + // ajout de la barre d'objets getManager().addWidget(object_toolbar.getWindow()); } Editor::~Editor() { - getManager().removeWidget(action_toolbar.getWindow()); getManager().removeWidget(object_toolbar.getWindow()); } @@ -93,8 +68,7 @@ void Editor::enable() { // joue la musique de l'éditeur ResourceManager::get().playMusic("editor.ogg"); - // on affiche les barres d'outils - action_toolbar.getWindow()->Show(true); + // on affiche la barre d'objets object_toolbar.getWindow()->Show(true); } @@ -293,6 +267,8 @@ void Editor::frame() { sf::RenderWindow& window = getManager().getWindow(); sf::Vector2i window_size = (sf::Vector2i) window.getSize(); + Level::frame(); + // dessin de la frame draw(); @@ -315,26 +291,10 @@ void Editor::frame() { setCamera(camera); } - // mise à jour de l'icône du mute - sf::Image image; - - if (ResourceManager::get().isMuted()) { - image = *ResourceManager::get().getImage("toolbar/icon_no_music.tga"); - } else { - image = *ResourceManager::get().getImage("toolbar/icon_music.tga"); - } - - mute_button->SetImage(sfg::Image::Create(image)); - // màj du titre de la fenêtre getManager().setTitle(sf::String(L"Édition de ") + getName()); - // positionnement des barres d'outils au bon endroit - action_toolbar.getWindow()->SetAllocation(sf::FloatRect( - 0, 0, window_size.x, - action_toolbar.getHeight() - )); - + // positionnement de la barre d'objets object_toolbar.getWindow()->SetAllocation(sf::FloatRect( window_size.x - object_toolbar.getWidth(), action_toolbar.getHeight(), diff --git a/src/states/game.cpp b/src/states/game.cpp index 82159ea..bf1a3e1 100644 --- a/src/states/game.cpp +++ b/src/states/game.cpp @@ -61,6 +61,7 @@ void Game::processEvent(const sf::Event& event) { } void Game::frame() { + Level::frame(); sf::Time current_time = getManager().getCurrentTime(); if (current_time >= next_frame_time) { diff --git a/src/states/level.cpp b/src/states/level.cpp index 706be87..1bfd7bc 100644 --- a/src/states/level.cpp +++ b/src/states/level.cpp @@ -48,8 +48,8 @@ namespace { }; } -Level::Level(Manager& manager) : State(manager) { - gravity_direction = GravityDirection::SOUTH; +Level::Level(Manager& manager) : State(manager), + gravity_direction(Utility::Direction::SOUTH) { // métadonnées par défaut setName(sf::String("Nouveau niveau")); @@ -67,9 +67,36 @@ Level::Level(Manager& manager) : State(manager) { // ressources par défaut setMusic(""); setBackground(""); + + // ajout des boutons d'action de la barre d'action + action_toolbar.addButton( + *ResourceManager::get().getImage("toolbar/icon_back.tga"), + std::bind(&Manager::popState, &getManager()) + ); + + mute_button = action_toolbar.addButton( + *ResourceManager::get().getImage("toolbar/icon_music.tga"), + []() { + // on inverse le drapeau de muet + ResourceManager::get().setMuted( + !ResourceManager::get().isMuted() + ); + } + ); + + action_toolbar.addSpacer(5, true, false); + + action_toolbar.addButton( + *ResourceManager::get().getImage("toolbar/icon_no_music.tga") + ); + + action_toolbar.addSpacer(5, true, false); + getManager().addWidget(action_toolbar.getWindow()); } -Level::~Level() {} +Level::~Level() { + getManager().removeWidget(action_toolbar.getWindow()); +} void Level::enable() { // positionnement par défaut de la caméra @@ -78,6 +105,9 @@ void Level::enable() { camera.setSize(window_size.x, window_size.y); camera.setCenter(0, 0); camera_angle = 180.f; + + // on affiche la barre d'actions + action_toolbar.getWindow()->Show(true); } void Level::load() { @@ -251,6 +281,27 @@ void Level::processEvent(const sf::Event& event) { } } +void Level::frame() { + sf::Vector2i window_size = (sf::Vector2i) getManager().getWindow().getSize(); + + // mise à jour de l'icône du mute en fonction de l'état + sf::Image image; + + if (ResourceManager::get().isMuted()) { + image = *ResourceManager::get().getImage("toolbar/icon_no_music.tga"); + } else { + image = *ResourceManager::get().getImage("toolbar/icon_music.tga"); + } + + mute_button->SetImage(sfg::Image::Create(image)); + + // positionnement de la barre d'actions + action_toolbar.getWindow()->SetAllocation(sf::FloatRect( + 0, 0, window_size.x, + action_toolbar.getHeight() + )); +} + void Level::draw() { sf::RenderWindow& window = getManager().getWindow(); sf::Vector2u window_size = window.getSize(); @@ -375,16 +426,16 @@ void Level::setBackground(std::string set_background) { sf::Vector2f Level::getGravity() const { switch (gravity_direction) { - case GravityDirection::NORTH: + case Utility::Direction::NORTH: return sf::Vector2f(0, -GRAVITY); - case GravityDirection::EAST: + case Utility::Direction::EAST: return sf::Vector2f(GRAVITY, 0); - case GravityDirection::SOUTH: + case Utility::Direction::SOUTH: return sf::Vector2f(0, GRAVITY); - case GravityDirection::WEST: + case Utility::Direction::WEST: return sf::Vector2f(-GRAVITY, 0); } @@ -393,16 +444,16 @@ sf::Vector2f Level::getGravity() const { sf::Vector2f Level::getLeftDirection() const { switch (gravity_direction) { - case GravityDirection::NORTH: + case Utility::Direction::NORTH: return sf::Vector2f(MOVE, 0); - case GravityDirection::EAST: + case Utility::Direction::EAST: return sf::Vector2f(0, MOVE); - case GravityDirection::SOUTH: + case Utility::Direction::SOUTH: return sf::Vector2f(-MOVE, 0); - case GravityDirection::WEST: + case Utility::Direction::WEST: return sf::Vector2f(0, -MOVE); } @@ -413,11 +464,11 @@ sf::Vector2f Level::getRightDirection() const { return -1.f * getLeftDirection(); } -GravityDirection Level::getGravityDirection() { +Utility::Direction Level::getGravityDirection() { return gravity_direction; } -void Level::setGravityDirection(GravityDirection set_gravity_direction) { +void Level::setGravityDirection(Utility::Direction set_gravity_direction) { gravity_direction = set_gravity_direction; }