Réimplémentation du compte à rebours pour éditeur & jeu
This commit is contained in:
		
							parent
							
								
									c9b6ce93ea
								
							
						
					
					
						commit
						231aee482e
					
				|  | @ -4,32 +4,31 @@ | |||
| #include <SFGUI/Widgets.hpp> | ||||
| #include <SFML/Graphics.hpp> | ||||
| #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<void()> callback = std::function<void()>() | ||||
|     ); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Ajoute un nouvel espaceur | ||||
|      */ | ||||
|     sfg::Alignment::Ptr addSpacer( | ||||
|         float width, bool expand, bool fill | ||||
|         std::string name, Utility::Direction direction, | ||||
|         int position, std::function<void()> callback = std::function<void()>() | ||||
|     ); | ||||
| 
 | ||||
|     /**
 | ||||
|  | @ -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 | ||||
|  |  | |||
|  | @ -11,6 +11,11 @@ | |||
|     Color: #141414ff; | ||||
| } | ||||
| 
 | ||||
| .timer { | ||||
|     FontName: monoid; | ||||
|     FontSize: 18; | ||||
| } | ||||
| 
 | ||||
| Window { | ||||
|     BackgroundColor: #FFFFFFff; | ||||
|     BorderWidth: 0; | ||||
|  |  | |||
|  | @ -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<void()> callback | ||||
|     std::string name, Utility::Direction direction, | ||||
|     int position, std::function<void()> 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); | ||||
| } | ||||
|  |  | |||
|  | @ -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); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
|  | @ -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()); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue