Police monospace pour le timer, centrage timer, affichage timer sur jeu
This commit is contained in:
parent
914297225b
commit
2e2ba7150f
|
@ -10,6 +10,7 @@
|
||||||
*/
|
*/
|
||||||
class Game : public Level {
|
class Game : public Level {
|
||||||
private:
|
private:
|
||||||
|
WidgetTimer widget_timer;
|
||||||
sf::Time next_frame_time;
|
sf::Time next_frame_time;
|
||||||
bool test_mode;
|
bool test_mode;
|
||||||
std::shared_ptr<View> return_view;
|
std::shared_ptr<View> return_view;
|
||||||
|
@ -20,6 +21,13 @@ private:
|
||||||
*/
|
*/
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
* Dessine tous les objets, le fond et
|
||||||
|
* l'interface de jeu
|
||||||
|
*/
|
||||||
|
virtual void draw();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Game(Manager& manager);
|
Game(Manager& manager);
|
||||||
virtual ~Game();
|
virtual ~Game();
|
||||||
|
|
|
@ -13,17 +13,20 @@
|
||||||
class WidgetTimer {
|
class WidgetTimer {
|
||||||
private:
|
private:
|
||||||
Manager& manager;
|
Manager& manager;
|
||||||
|
bool can_change;
|
||||||
std::function<void(int)> time_left_cb;
|
std::function<void(int)> time_left_cb;
|
||||||
int time_left;
|
int time_left;
|
||||||
|
|
||||||
sf::RectangleShape timer_zone;
|
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_up;
|
||||||
WidgetButton timer_down;
|
WidgetButton timer_down;
|
||||||
|
|
||||||
public:
|
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
|
* Process l'événement et renvoie true si
|
||||||
|
|
Binary file not shown.
|
@ -6,7 +6,7 @@
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
|
|
||||||
Editor::Editor(Manager& manager) : Level(manager),
|
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() {}
|
Editor::~Editor() {}
|
||||||
|
|
||||||
|
|
11
src/game.cpp
11
src/game.cpp
|
@ -2,6 +2,7 @@
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
|
|
||||||
Game::Game(Manager& manager) : Level(manager),
|
Game::Game(Manager& manager) : Level(manager),
|
||||||
|
widget_timer(manager, false),
|
||||||
next_frame_time(manager.getCurrentTime()),
|
next_frame_time(manager.getCurrentTime()),
|
||||||
test_mode(false), return_view(nullptr) {}
|
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() {
|
void Game::update() {
|
||||||
std::vector<CollisionData> colliding;
|
std::vector<CollisionData> colliding;
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@ const unsigned int WidgetButton::ARROW_UP = 0;
|
||||||
const unsigned int WidgetButton::ARROW_DOWN = 1;
|
const unsigned int WidgetButton::ARROW_DOWN = 1;
|
||||||
|
|
||||||
const sf::Color ARROW_COLOR = sf::Color(33, 33, 33);
|
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(
|
WidgetButton::WidgetButton(
|
||||||
Manager& manager, std::function<void(void)> click_cb,
|
Manager& manager, std::function<void(void)> click_cb,
|
||||||
|
@ -57,7 +58,7 @@ void WidgetButton::draw(sf::Vector2f position) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// coloration des boutons si enfoncement
|
// coloration des boutons si enfoncement
|
||||||
button.setFillColor(sf::Color::Transparent);
|
button.setFillColor(REST_COLOR);
|
||||||
|
|
||||||
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
||||||
sf::Vector2f mouse_position = (sf::Vector2f) sf::Mouse::getPosition(window);
|
sf::Vector2f mouse_position = (sf::Vector2f) sf::Mouse::getPosition(window);
|
||||||
|
|
|
@ -1,33 +1,33 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "util/widget_timer.hpp"
|
#include "util/widget_timer.hpp"
|
||||||
|
|
||||||
/**
|
WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function<void(int)> time_left_cb) :
|
||||||
* Formattage du temps en m:ss
|
manager(manager), can_change(can_change), time_left_cb(time_left_cb),
|
||||||
*/
|
timer_zone(sf::Vector2f(100, 32)),
|
||||||
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)),
|
|
||||||
timer_up(manager, std::bind(&WidgetTimer::addTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_UP),
|
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) {
|
timer_down(manager, std::bind(&WidgetTimer::subtractTime, this), sf::Vector2f(30, 16), WidgetButton::ARROW_DOWN) {
|
||||||
|
|
||||||
// initialisation des formes
|
// initialisation des formes
|
||||||
timer_text.setFont(manager.getResourceManager().getFont("main_font.ttf"));
|
timer_seconds_text.setFont(manager.getResourceManager().getFont("monoid.ttf"));
|
||||||
timer_text.setCharacterSize(24);
|
timer_seconds_text.setCharacterSize(18);
|
||||||
timer_text.setColor(sf::Color::Black);
|
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) {
|
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
|
// gestion des boutons
|
||||||
if (timer_up.processEvent(event)) {
|
if (timer_up.processEvent(event)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -51,7 +51,7 @@ bool WidgetTimer::processEvent(const sf::Event& event) {
|
||||||
sf::Vector2f position(event.mouseWheelScroll.x, event.mouseWheelScroll.y);
|
sf::Vector2f position(event.mouseWheelScroll.x, event.mouseWheelScroll.y);
|
||||||
|
|
||||||
if (timer_zone.getGlobalBounds().contains(position)) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,21 +67,45 @@ void WidgetTimer::draw(sf::Vector2f position) {
|
||||||
window.draw(timer_zone);
|
window.draw(timer_zone);
|
||||||
|
|
||||||
// affichage du temps du niveau
|
// affichage du temps du niveau
|
||||||
timer_text.setString(formatTime(time_left));
|
sf::String minutes = std::to_string(time_left / 60);
|
||||||
timer_text.setPosition(position + sf::Vector2f(60 - round(timer_text.getGlobalBounds().width), 0));
|
sf::String seconds = std::to_string(time_left % 60);
|
||||||
window.draw(timer_text);
|
|
||||||
|
|
||||||
// affichage des boutons
|
// ajout d'un zéro devant les secondes si nécessaire
|
||||||
timer_up.draw(position + sf::Vector2f(70, 0));
|
if (minutes.getSize() == 1) {
|
||||||
timer_down.draw(position + sf::Vector2f(70, 16));
|
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() {
|
void WidgetTimer::addTime() {
|
||||||
time_left_cb(time_left + 5);
|
time_left_cb(time_left + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetTimer::subtractTime() {
|
void WidgetTimer::subtractTime() {
|
||||||
time_left_cb(time_left - 5);
|
time_left_cb(time_left - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetTimer::setTimeLeft(int set_time_left) {
|
void WidgetTimer::setTimeLeft(int set_time_left) {
|
||||||
|
|
Loading…
Reference in New Issue