diff --git a/include/game.hpp b/include/game.hpp index 3e3e0d9..e242a18 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -10,6 +10,7 @@ */ class Game : public Level { private: + WidgetTimer widget_timer; sf::Time next_frame_time; bool test_mode; std::shared_ptr return_view; @@ -20,6 +21,13 @@ private: */ void update(); +protected: + /** + * Dessine tous les objets, le fond et + * l'interface de jeu + */ + virtual void draw(); + public: Game(Manager& manager); virtual ~Game(); diff --git a/include/util/widget_timer.hpp b/include/util/widget_timer.hpp index e602c74..2417c6f 100644 --- a/include/util/widget_timer.hpp +++ b/include/util/widget_timer.hpp @@ -13,17 +13,20 @@ class WidgetTimer { private: Manager& manager; + bool can_change; std::function time_left_cb; int time_left; sf::RectangleShape timer_zone; - sf::Text timer_text; + sf::Text timer_seconds_text; + sf::Text timer_sep_text; + sf::Text timer_minutes_text; WidgetButton timer_up; WidgetButton timer_down; public: - WidgetTimer(Manager& manager, std::function time_left_cb); + WidgetTimer(Manager& manager, bool can_change, std::function time_left_cb = std::function()); /** * Process l'événement et renvoie true si diff --git a/res/monoid.ttf b/res/monoid.ttf new file mode 100644 index 0000000..8034ff6 Binary files /dev/null and b/res/monoid.ttf differ diff --git a/src/editor.cpp b/src/editor.cpp index a310964..d7f338b 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -6,7 +6,7 @@ #include "constants.hpp" Editor::Editor(Manager& manager) : Level(manager), - widget_timer(manager, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)) {} + widget_timer(manager, true, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)) {} Editor::~Editor() {} diff --git a/src/game.cpp b/src/game.cpp index fd1f4e9..f4d821d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,6 +2,7 @@ #include "constants.hpp" Game::Game(Manager& manager) : Level(manager), + widget_timer(manager, false), next_frame_time(manager.getCurrentTime()), test_mode(false), return_view(nullptr) {} @@ -48,6 +49,16 @@ void Game::frame() { } } +void Game::draw() { + Level::draw(); + + sf::View window_view = manager.getWindowView(); + + // dessin du widget + widget_timer.setTimeLeft(getTotalTime()); + widget_timer.draw(sf::Vector2f(window_view.getSize().x / 2 - 50, 0)); +} + void Game::update() { std::vector colliding; diff --git a/src/util/widget_button.cpp b/src/util/widget_button.cpp index 8e763e5..6a16d5c 100644 --- a/src/util/widget_button.cpp +++ b/src/util/widget_button.cpp @@ -4,7 +4,8 @@ const unsigned int WidgetButton::ARROW_UP = 0; const unsigned int WidgetButton::ARROW_DOWN = 1; const sf::Color ARROW_COLOR = sf::Color(33, 33, 33); -const sf::Color CLICKED_COLOR = sf::Color(187, 222, 251); +const sf::Color CLICKED_COLOR = sf::Color(190, 190, 190); +const sf::Color REST_COLOR = sf::Color(230, 230, 230); WidgetButton::WidgetButton( Manager& manager, std::function click_cb, @@ -57,7 +58,7 @@ void WidgetButton::draw(sf::Vector2f position) { } // coloration des boutons si enfoncement - button.setFillColor(sf::Color::Transparent); + button.setFillColor(REST_COLOR); if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { sf::Vector2f mouse_position = (sf::Vector2f) sf::Mouse::getPosition(window); diff --git a/src/util/widget_timer.cpp b/src/util/widget_timer.cpp index cd869c8..d4fd0c2 100644 --- a/src/util/widget_timer.cpp +++ b/src/util/widget_timer.cpp @@ -1,33 +1,33 @@ #include #include "util/widget_timer.hpp" -/** - * Formattage du temps en m:ss - */ -std::string formatTime(int time) { - std::string minutes = std::to_string(time / 60); - std::string seconds = std::to_string(time % 60); - - // ajout d'un zéro devant les secondes si nécessaire - if (seconds.size() == 1) { - seconds = "0" + seconds; - } - - return minutes + ":" + seconds; -} - -WidgetTimer::WidgetTimer(Manager& manager, std::function time_left_cb) : - manager(manager), time_left_cb(time_left_cb), timer_zone(sf::Vector2f(100, 32)), +WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function time_left_cb) : + manager(manager), can_change(can_change), time_left_cb(time_left_cb), + timer_zone(sf::Vector2f(100, 32)), timer_up(manager, std::bind(&WidgetTimer::addTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_UP), timer_down(manager, std::bind(&WidgetTimer::subtractTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_DOWN) { // initialisation des formes - timer_text.setFont(manager.getResourceManager().getFont("main_font.ttf")); - timer_text.setCharacterSize(24); - timer_text.setColor(sf::Color::Black); + timer_seconds_text.setFont(manager.getResourceManager().getFont("monoid.ttf")); + timer_seconds_text.setCharacterSize(18); + timer_seconds_text.setColor(sf::Color::Black); + + timer_sep_text.setString(":"); + timer_sep_text.setFont(manager.getResourceManager().getFont("monoid.ttf")); + timer_sep_text.setCharacterSize(18); + timer_sep_text.setColor(sf::Color::Black); + + timer_minutes_text.setFont(manager.getResourceManager().getFont("monoid.ttf")); + timer_minutes_text.setCharacterSize(18); + timer_minutes_text.setColor(sf::Color::Black); } bool WidgetTimer::processEvent(const sf::Event& event) { + // si le timer n'est pas modifiable, pas d'évent à gérer + if (!can_change) { + return false; + } + // gestion des boutons if (timer_up.processEvent(event)) { return true; @@ -51,7 +51,7 @@ bool WidgetTimer::processEvent(const sf::Event& event) { sf::Vector2f position(event.mouseWheelScroll.x, event.mouseWheelScroll.y); if (timer_zone.getGlobalBounds().contains(position)) { - time_left_cb(time_left + round(event.mouseWheelScroll.delta)); + time_left_cb(time_left + round(event.mouseWheelScroll.delta * 10)); return true; } } @@ -67,21 +67,45 @@ void WidgetTimer::draw(sf::Vector2f position) { window.draw(timer_zone); // affichage du temps du niveau - timer_text.setString(formatTime(time_left)); - timer_text.setPosition(position + sf::Vector2f(60 - round(timer_text.getGlobalBounds().width), 0)); - window.draw(timer_text); + sf::String minutes = std::to_string(time_left / 60); + sf::String seconds = std::to_string(time_left % 60); - // affichage des boutons - timer_up.draw(position + sf::Vector2f(70, 0)); - timer_down.draw(position + sf::Vector2f(70, 16)); + // 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_minutes_text.setString(minutes); + timer_seconds_text.setString(seconds); + + float base_x = can_change ? 30 : 45; + timer_sep_text.setPosition(position + sf::Vector2f(base_x, 6)); + timer_seconds_text.setPosition(position + sf::Vector2f(base_x + 8, 6)); + timer_minutes_text.setPosition(position + sf::Vector2f( + base_x - 3 - floor(timer_minutes_text.getGlobalBounds().width), 6 + )); + + window.draw(timer_sep_text); + window.draw(timer_seconds_text); + window.draw(timer_minutes_text); + + // interface de modification du temps + if (can_change) { + timer_up.draw(position + sf::Vector2f(70, 0)); + timer_down.draw(position + sf::Vector2f(70, 16)); + } } void WidgetTimer::addTime() { - time_left_cb(time_left + 5); + time_left_cb(time_left + 1); } void WidgetTimer::subtractTime() { - time_left_cb(time_left - 5); + time_left_cb(time_left - 1); } void WidgetTimer::setTimeLeft(int set_time_left) {