From 231aee482eef1992614c6410b80ae131df2e3f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Thu, 28 Apr 2016 14:27:48 +0200 Subject: [PATCH] =?UTF-8?q?R=C3=A9impl=C3=A9mentation=20du=20compte=20?= =?UTF-8?q?=C3=A0=20rebours=20pour=20=C3=A9diteur=20&=20jeu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/gui/action_toolbar.hpp | 31 ++++++++++------ res/gui.theme | 5 +++ src/gui/action_toolbar.cpp | 67 ++++++++++++++++++++++++++-------- src/gui/object_toolbar.cpp | 8 ++-- src/states/editor.cpp | 14 +++---- src/states/game.cpp | 6 ++- src/states/level.cpp | 14 ++----- 7 files changed, 96 insertions(+), 49 deletions(-) diff --git a/include/gui/action_toolbar.hpp b/include/gui/action_toolbar.hpp index 118d483..4f755b0 100644 --- a/include/gui/action_toolbar.hpp +++ b/include/gui/action_toolbar.hpp @@ -4,32 +4,31 @@ #include #include #include "../objects/object.hpp" +#include "../utility.hpp" /** - * Barre d'outils pour les actions en jeu + * Barre d'outils pour les boutons d'actions en jeu */ class ActionToolbar { private: // widgets de la barre sfg::Window::Ptr toolbar_window; sfg::Box::Ptr toolbar_box; + sfg::Label::Ptr timer_label; + + int left_buttons_count; + int time; public: ActionToolbar(); /** - * Ajoute un nouveau bouton d'action à la barre d'outils + * Ajoute un nouveau bouton d'action à gauche ou + * à droite de la barre d'action d'outils */ sfg::Button::Ptr addButton( - sf::Image image, - std::function callback = std::function() - ); - - /** - * Ajoute un nouvel espaceur - */ - sfg::Alignment::Ptr addSpacer( - float width, bool expand, bool fill + std::string name, Utility::Direction direction, + int position, std::function callback = std::function() ); /** @@ -41,6 +40,16 @@ public: * Récupère la taille désirée par la barre d'outils */ float getHeight(); + + /** + * Récupère le temps restant + */ + int getTime(); + + /** + * Modifie le temps restant + */ + void setTime(int set_time); }; #endif diff --git a/res/gui.theme b/res/gui.theme index efed20e..3abae8d 100644 --- a/res/gui.theme +++ b/res/gui.theme @@ -11,6 +11,11 @@ Color: #141414ff; } +.timer { + FontName: monoid; + FontSize: 18; +} + Window { BackgroundColor: #FFFFFFff; BorderWidth: 0; diff --git a/src/gui/action_toolbar.cpp b/src/gui/action_toolbar.cpp index c03b28a..9408b46 100644 --- a/src/gui/action_toolbar.cpp +++ b/src/gui/action_toolbar.cpp @@ -1,6 +1,7 @@ +#include "resource_manager.hpp" #include "gui/action_toolbar.hpp" -ActionToolbar::ActionToolbar() { +ActionToolbar::ActionToolbar() : left_buttons_count(0) { // création de la boîte de la barre d'outils toolbar_box = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 5.f); @@ -8,39 +9,52 @@ ActionToolbar::ActionToolbar() { toolbar_window = sfg::Window::Create(sfg::Window::Style::BACKGROUND); toolbar_window->Add(toolbar_box); + // ajout du timer et des espaceurs + sfg::Alignment::Ptr left_spacer = sfg::Alignment::Create(); + sfg::Alignment::Ptr right_spacer = sfg::Alignment::Create(); + timer_label = sfg::Label::Create("00:00"); + + left_spacer->SetRequisition(sf::Vector2f(5.f, 1.f)); + timer_label->SetClass("timer"); + right_spacer->SetRequisition(sf::Vector2f(5.f, 1.f)); + + toolbar_box->PackEnd(left_spacer, true, false); + toolbar_box->PackEnd(timer_label); + toolbar_box->PackEnd(right_spacer, true, false); + // pour les styles toolbar_window->SetId("action_toolbar"); } sfg::Button::Ptr ActionToolbar::addButton( - sf::Image image, std::function callback + std::string name, Utility::Direction direction, + int position, std::function callback ) { // création d'un bouton avec pour image l'image passée sfg::Button::Ptr button = sfg::Button::Create(""); - button->SetImage(sfg::Image::Create(image)); button->SetClass("notext"); + button->SetImage(sfg::Image::Create( + *ResourceManager::get().getImage("toolbar/icon_" + name + ".tga") + )); // liaison du bouton avec la callback, s'il y en a une if (callback) { button->GetSignal(sfg::Widget::OnLeftClick).Connect(callback); } - // ajout du bouton à la barre + // ajout du bouton à la barre selon la position donnée + if (direction == Utility::Direction::WEST) { + position += left_buttons_count + 3; + } else { + left_buttons_count++; + } + toolbar_box->PackEnd(button, false, false); + toolbar_box->ReorderChild(button, position); + return button; } -sfg::Alignment::Ptr ActionToolbar::addSpacer( - float width, bool expand, bool fill -) { - sfg::Alignment::Ptr spacer = sfg::Alignment::Create(); - spacer->SetRequisition(sf::Vector2f(width, 1.f)); - - // ajout de l'espaceur à la barre - toolbar_box->PackEnd(spacer, expand, fill); - return spacer; -} - sfg::Window::Ptr ActionToolbar::getWindow() { return toolbar_window; } @@ -48,3 +62,26 @@ sfg::Window::Ptr ActionToolbar::getWindow() { float ActionToolbar::getHeight() { return toolbar_window->GetRequisition().y; } + +int ActionToolbar::getTime() { + return time; +} + +void ActionToolbar::setTime(int set_time) { + time = set_time; + + // mise à jour du temps affiché sur le label + sf::String minutes = std::to_string(time / 60); + sf::String seconds = std::to_string(time % 60); + + // ajout d'un zéro devant les secondes si nécessaire + if (minutes.getSize() == 1) { + minutes = "0" + minutes; + } + + if (seconds.getSize() == 1) { + seconds = "0" + seconds; + } + + timer_label->SetText(minutes + ":" + seconds); +} diff --git a/src/gui/object_toolbar.cpp b/src/gui/object_toolbar.cpp index f4d2a27..e290cf5 100644 --- a/src/gui/object_toolbar.cpp +++ b/src/gui/object_toolbar.cpp @@ -36,14 +36,14 @@ ObjectToolbar::ObjectToolbar() { &ObjectToolbar::createGravityBlock, this, Utility::Direction::EAST )); - addCreator("gravity_block_south", std::bind( - &ObjectToolbar::createGravityBlock, this, Utility::Direction::SOUTH - )); - addCreator("gravity_block_west", std::bind( &ObjectToolbar::createGravityBlock, this, Utility::Direction::WEST )); + addCreator("gravity_block_south", std::bind( + &ObjectToolbar::createGravityBlock, this, Utility::Direction::SOUTH + )); + // attachement de la liste des créateurs à l'interface toolbar_box->PackEnd(creators_table); diff --git a/src/states/editor.cpp b/src/states/editor.cpp index 2540a2c..d56be78 100644 --- a/src/states/editor.cpp +++ b/src/states/editor.cpp @@ -36,21 +36,18 @@ Editor::Editor(Manager& manager) : Level(manager), drag_control_point(nullptr), drag_mode(Editor::DragMode::NONE) { // ajout des boutons d'action de la barre d'action + action_toolbar.addButton("gear", Utility::Direction::WEST, 0); action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_gear.tga") - ); - - action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_save.tga"), + "save", Utility::Direction::WEST, 1, std::bind(&Editor::save, this) ); action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_test.tga"), + "test", Utility::Direction::WEST, 2, std::bind(&Editor::test, this) ); - // ajout de la barre d'objets + // ajout de la barre d'objets à l'écran getManager().addWidget(object_toolbar.getWindow()); } @@ -271,6 +268,9 @@ void Editor::frame() { // dessin de la frame draw(); + // màj du temps du timer + action_toolbar.setTime(total_time); + // scroll de la caméra lorsque la souris se situe sur les bords if (window.hasFocus()) { sf::Vector2f cur_center = getCenterGoal(); diff --git a/src/states/game.cpp b/src/states/game.cpp index 4bdc23c..4ccc9ee 100644 --- a/src/states/game.cpp +++ b/src/states/game.cpp @@ -25,12 +25,12 @@ Game::Game(Manager& manager, bool test) : Level(manager), // ajout des boutons d'action de la barre d'action if (!isTest()) { action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_restart.tga"), + "restart", Utility::Direction::WEST, 0, std::bind(&Game::restart, this) ); action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_pause.tga"), + "pause", Utility::Direction::WEST, 1, std::bind(&Game::switchPause, this) ); } @@ -197,6 +197,8 @@ void Game::update() { time_left -= Manager::FRAME_TIME.asSeconds(); } + action_toolbar.setTime(std::ceil(time_left)); + // on tue les objets qui étaient déjà planifiés pour mourir for (auto it = pending_kill.begin(); it != pending_kill.end(); it++) { removeObject(*it); diff --git a/src/states/level.cpp b/src/states/level.cpp index de80c51..c63b8e2 100644 --- a/src/states/level.cpp +++ b/src/states/level.cpp @@ -70,27 +70,21 @@ Level::Level(Manager& manager) : State(manager), // ajout des boutons d'action de la barre d'action action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_back.tga"), + "back", Utility::Direction::EAST, 0, std::bind(&Manager::popState, &getManager()) ); mute_button = action_toolbar.addButton( - *ResourceManager::get().getImage("toolbar/icon_music.tga"), + "music", Utility::Direction::EAST, 1, []() { - // on inverse le drapeau de muet + // on inverse l'état muet ou non 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); + // ajout de la barre d'objets à l'écran getManager().addWidget(action_toolbar.getWindow()); }