Police monospace pour le timer, centrage timer, affichage timer sur jeu

This commit is contained in:
Mattéo Delabre 2016-04-06 15:21:46 +02:00
parent 914297225b
commit 2e2ba7150f
7 changed files with 81 additions and 34 deletions

View File

@ -10,6 +10,7 @@
*/
class Game : public Level {
private:
WidgetTimer widget_timer;
sf::Time next_frame_time;
bool test_mode;
std::shared_ptr<View> 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();

View File

@ -13,17 +13,20 @@
class WidgetTimer {
private:
Manager& manager;
bool can_change;
std::function<void(int)> 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<void(int)> time_left_cb);
WidgetTimer(Manager& manager, bool can_change, std::function<void(int)> time_left_cb = std::function<void(int)>());
/**
* Process l'événement et renvoie true si

BIN
res/monoid.ttf Normal file

Binary file not shown.

View File

@ -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() {}

View File

@ -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<CollisionData> colliding;

View File

@ -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<void(void)> 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);

View File

@ -1,33 +1,33 @@
#include <cmath>
#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<void(int)> 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<void(int)> 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
// 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) {