2016-04-06 09:38:13 +00:00
|
|
|
#include "menu.hpp"
|
2016-04-09 00:32:11 +00:00
|
|
|
#include "editor.hpp"
|
|
|
|
#include "game.hpp"
|
|
|
|
#include <cmath>
|
2016-04-06 09:38:13 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
const float MAX_WIDTH_PROPORTION = 1.f / 3.f;
|
2016-04-07 20:03:08 +00:00
|
|
|
|
2016-04-09 13:53:12 +00:00
|
|
|
Menu::Menu(Manager& manager) : State(manager) {}
|
2016-04-09 00:32:11 +00:00
|
|
|
Menu::~Menu() {}
|
2016-04-07 22:19:01 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::begin() {
|
2016-04-09 02:36:30 +00:00
|
|
|
ResourceManager& resources = getResourceManager();
|
2016-04-06 09:38:13 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
loadMainMenu();
|
|
|
|
resources.setMusic("menu.wav");
|
|
|
|
resources.playMusic();
|
2016-04-06 09:38:13 +00:00
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
getWindow().setFramerateLimit(60);
|
2016-04-06 09:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::frame(const std::vector<sf::Event>& events) {
|
|
|
|
// traitement des événements
|
2016-04-09 13:53:12 +00:00
|
|
|
State::frame(events);
|
2016-04-06 09:38:13 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
// titre de la fenêtitre
|
2016-04-09 02:36:30 +00:00
|
|
|
getManager().setTitle("");
|
2016-04-07 20:03:08 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
// affichage du menu
|
2016-04-09 02:36:30 +00:00
|
|
|
sf::RenderWindow& window = getWindow();
|
2016-04-09 00:32:11 +00:00
|
|
|
sf::Vector2f size = (sf::Vector2f) window.getSize();
|
2016-04-09 02:36:30 +00:00
|
|
|
sf::Font font = getResourceManager().getFont("raleway.ttf");
|
2016-04-07 20:03:08 +00:00
|
|
|
|
2016-04-09 02:36:30 +00:00
|
|
|
getManager().resetDefaultView();
|
2016-04-09 00:32:11 +00:00
|
|
|
window.clear(sf::Color(66, 40, 245));
|
|
|
|
// TODO: dessiner l'image du fond
|
|
|
|
|
|
|
|
// on crée les textes pour chaque choix et on les dessine
|
|
|
|
float step = size.y / (choices.size() + 1);
|
2016-04-09 01:11:14 +00:00
|
|
|
int font_size = std::max((int) step / 3, 12);
|
|
|
|
|
|
|
|
labels.clear();
|
2016-04-09 00:32:11 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < choices.size(); i++) {
|
2016-04-09 01:11:14 +00:00
|
|
|
sf::Text label(choices[i], font, font_size);
|
|
|
|
sf::FloatRect text_size = label.getLocalBounds();
|
|
|
|
|
|
|
|
sf::Vector2f base_position(
|
|
|
|
size.x - (font_size * 8),
|
|
|
|
step * (i + 1)
|
2016-04-09 00:32:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
label.setColor(sf::Color::White);
|
2016-04-09 01:11:14 +00:00
|
|
|
label.setPosition(base_position - sf::Vector2f(
|
|
|
|
text_size.left, text_size.top + text_size.height / 2
|
|
|
|
));
|
2016-04-09 00:32:11 +00:00
|
|
|
|
2016-04-09 01:11:14 +00:00
|
|
|
// si c'est le choix sélectionné, on le souligne
|
2016-04-09 00:32:11 +00:00
|
|
|
if (selection == i) {
|
2016-04-09 01:11:14 +00:00
|
|
|
sf::RectangleShape underline(sf::Vector2f(text_size.width, 2.f));
|
2016-04-09 00:32:11 +00:00
|
|
|
underline.setFillColor(sf::Color::White);
|
2016-04-09 01:11:14 +00:00
|
|
|
underline.setPosition(base_position + sf::Vector2f(
|
|
|
|
0, text_size.height / 2 + 8
|
|
|
|
));
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
window.draw(underline);
|
|
|
|
}
|
2016-04-07 20:03:08 +00:00
|
|
|
|
2016-04-09 01:11:14 +00:00
|
|
|
labels.push_back(label);
|
2016-04-09 00:32:11 +00:00
|
|
|
window.draw(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.display();
|
2016-04-07 20:03:08 +00:00
|
|
|
}
|
2016-04-06 09:38:13 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::processEvent(const sf::Event& event) {
|
|
|
|
// gestion des touches
|
|
|
|
if (event.type == sf::Event::KeyPressed) {
|
|
|
|
// touche flèche haut : on passe au choix précédent
|
|
|
|
if (event.key.code == sf::Keyboard::Up) {
|
|
|
|
if (selection == 0) {
|
|
|
|
selection = choices.size() - 1;
|
|
|
|
} else {
|
|
|
|
selection--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// touche flèche bas : on passe au choix suivant
|
|
|
|
if (event.key.code == sf::Keyboard::Down) {
|
2016-04-09 01:11:14 +00:00
|
|
|
if (selection == choices.size() - 1) {
|
2016-04-09 00:32:11 +00:00
|
|
|
selection = 0;
|
|
|
|
} else {
|
|
|
|
selection++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// touche entrée : on exécute le choix sélectionné
|
|
|
|
if (event.key.code == sf::Keyboard::Return) {
|
|
|
|
actions[selection]();
|
|
|
|
}
|
2016-04-06 09:38:13 +00:00
|
|
|
}
|
2016-04-09 01:11:14 +00:00
|
|
|
|
|
|
|
// au clic, on exécute le choix pointé s'il y a lieu
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
|
|
|
sf::Vector2f position(event.mouseButton.x, event.mouseButton.y);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < labels.size(); i++) {
|
|
|
|
if (labels[i].getGlobalBounds().contains(position)) {
|
|
|
|
actions[i]();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// au déplacement de souris, on sélectionne le choix pointé s'il y a lieu
|
|
|
|
if (event.type == sf::Event::MouseMoved) {
|
|
|
|
sf::Vector2f position(event.mouseMove.x, event.mouseMove.y);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < labels.size(); i++) {
|
|
|
|
if (labels[i].getGlobalBounds().contains(position)) {
|
|
|
|
selection = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 09:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::loadMainMenu() {
|
|
|
|
choices.clear();
|
|
|
|
actions.clear();
|
|
|
|
selection = 0;
|
|
|
|
|
|
|
|
choices.push_back(sf::String(L"Jouer"));
|
|
|
|
actions.push_back(std::bind(&Menu::loadLevelMenu, this));
|
|
|
|
|
|
|
|
choices.push_back(sf::String(L"Règles du jeu"));
|
|
|
|
actions.push_back(std::bind(&Menu::loadRules, this));
|
|
|
|
|
|
|
|
choices.push_back(sf::String(L"Éditeur"));
|
|
|
|
actions.push_back(std::bind(&Menu::launchEditor, this));
|
|
|
|
|
|
|
|
choices.push_back(sf::String(L"Quitter"));
|
|
|
|
actions.push_back(std::bind(&Menu::quit, this));
|
2016-04-06 09:38:13 +00:00
|
|
|
}
|
2016-04-06 16:16:39 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::loadLevelMenu() {
|
|
|
|
choices.clear();
|
|
|
|
actions.clear();
|
|
|
|
selection = 0;
|
2016-04-06 16:16:39 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
choices.push_back(sf::String(L"Tutoriel"));
|
|
|
|
actions.push_back(std::bind(&Menu::launchGame, this, "level1.dat"));
|
2016-04-07 22:19:01 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
choices.push_back(sf::String(L"Niveau 1"));
|
|
|
|
actions.push_back(std::bind(&Menu::launchGame, this, "level2.dat"));
|
2016-04-07 22:19:01 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
choices.push_back(sf::String(L"Niveau 2"));
|
|
|
|
actions.push_back(std::bind(&Menu::launchGame, this, "level3.dat"));
|
2016-04-07 20:03:08 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
choices.push_back(sf::String(L"Retour"));
|
|
|
|
actions.push_back(std::bind(&Menu::loadMainMenu, this));
|
|
|
|
}
|
2016-04-07 22:19:01 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::loadRules() {
|
|
|
|
// TODO: coder l'affichage des règles
|
|
|
|
}
|
2016-04-06 16:16:39 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::launchEditor() {
|
2016-04-09 13:53:12 +00:00
|
|
|
std::shared_ptr<Editor> editor = std::shared_ptr<Editor>(new Editor(getManager()));
|
|
|
|
getManager().setState(editor);
|
2016-04-09 00:32:11 +00:00
|
|
|
}
|
2016-04-08 00:35:17 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Menu::launchGame(std::string name) {
|
2016-04-09 02:36:30 +00:00
|
|
|
std::shared_ptr<Game> game = std::shared_ptr<Game>(new Game(getManager()));
|
2016-04-09 00:32:11 +00:00
|
|
|
std::string path = "./levels/" + name;
|
|
|
|
|
|
|
|
std::ifstream file;
|
|
|
|
file.open(path, std::ios::binary | std::ios::in);
|
|
|
|
game->load(file);
|
|
|
|
file.close();
|
|
|
|
|
2016-04-09 13:53:12 +00:00
|
|
|
getManager().setState(game);
|
2016-04-09 00:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Menu::quit() {
|
2016-04-09 02:36:30 +00:00
|
|
|
getManager().quit();
|
2016-04-06 16:16:39 +00:00
|
|
|
}
|