2016-04-12 16:11:36 +00:00
|
|
|
#include "manager.hpp"
|
2016-04-10 12:07:21 +00:00
|
|
|
#include "widget_timer.hpp"
|
2016-04-05 22:31:10 +00:00
|
|
|
|
2016-04-06 13:21:46 +00:00
|
|
|
WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function<void(int)> time_left_cb) :
|
|
|
|
manager(manager), can_change(can_change), time_left_cb(time_left_cb),
|
|
|
|
timer_zone(sf::Vector2f(100, 32)),
|
2016-04-05 22:31:10 +00:00
|
|
|
timer_up(manager, std::bind(&WidgetTimer::addTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_UP),
|
2016-04-05 23:19:08 +00:00
|
|
|
timer_down(manager, std::bind(&WidgetTimer::subtractTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_DOWN) {
|
2016-04-05 22:31:10 +00:00
|
|
|
|
|
|
|
// initialisation des formes
|
2016-04-06 13:21:46 +00:00
|
|
|
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);
|
2016-04-05 22:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WidgetTimer::processEvent(const sf::Event& event) {
|
2016-04-06 13:21:46 +00:00
|
|
|
// si le timer n'est pas modifiable, pas d'évent à gérer
|
|
|
|
if (!can_change) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:31:10 +00:00
|
|
|
// gestion des boutons
|
|
|
|
if (timer_up.processEvent(event)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer_down.processEvent(event)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
|
|
|
sf::Vector2f position(event.mouseButton.x, event.mouseButton.y);
|
|
|
|
|
|
|
|
// clic dans le widget : ne rien faire, mais empêcher le traversement
|
|
|
|
if (timer_zone.getGlobalBounds().contains(position)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type == sf::Event::MouseWheelScrolled && event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) {
|
|
|
|
// scroll sur le timer : modification du temps alloué au niveau
|
|
|
|
sf::Vector2f position(event.mouseWheelScroll.x, event.mouseWheelScroll.y);
|
|
|
|
|
|
|
|
if (timer_zone.getGlobalBounds().contains(position)) {
|
2016-04-06 13:21:46 +00:00
|
|
|
time_left_cb(time_left + round(event.mouseWheelScroll.delta * 10));
|
2016-04-05 22:31:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetTimer::draw(sf::Vector2f position) {
|
|
|
|
sf::RenderWindow& window = manager.getWindow();
|
|
|
|
|
|
|
|
// zone de fond du timer
|
|
|
|
timer_zone.setPosition(position);
|
|
|
|
window.draw(timer_zone);
|
|
|
|
|
|
|
|
// affichage du temps du niveau
|
2016-04-06 13:21:46 +00:00
|
|
|
sf::String minutes = std::to_string(time_left / 60);
|
|
|
|
sf::String seconds = std::to_string(time_left % 60);
|
2016-04-05 22:31:10 +00:00
|
|
|
|
2016-04-06 13:21:46 +00:00
|
|
|
// 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));
|
|
|
|
}
|
2016-04-05 22:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetTimer::addTime() {
|
2016-04-06 13:21:46 +00:00
|
|
|
time_left_cb(time_left + 1);
|
2016-04-05 22:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetTimer::subtractTime() {
|
2016-04-06 13:21:46 +00:00
|
|
|
time_left_cb(time_left - 1);
|
2016-04-05 22:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetTimer::setTimeLeft(int set_time_left) {
|
|
|
|
time_left = set_time_left;
|
|
|
|
}
|