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() {
|
|
|
|
loadMainMenu();
|
2016-04-06 09:38:13 +00:00
|
|
|
|
2016-04-09 20:36:07 +00:00
|
|
|
getResourceManager().playMusic("menu.ogg");
|
2016-04-10 02:47:02 +00:00
|
|
|
getWindow().setFramerateLimit(Manager::FPS);
|
2016-04-06 09:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 21:57:23 +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) {
|
|
|
|
if (selection == choices.size() - 1) {
|
|
|
|
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 21:57:23 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Menu::frame() {
|
2016-04-09 20:36:07 +00:00
|
|
|
// titre de la fenêtre
|
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 21:33:05 +00:00
|
|
|
// on s'assure d'être dans la vue par défaut (pas de zoom, 0x0 en haut gauche)
|
2016-04-10 18:34:29 +00:00
|
|
|
getManager().useGUIView();
|
2016-04-09 21:33:05 +00:00
|
|
|
|
|
|
|
// dessin du fond
|
|
|
|
window.clear(sf::Color::White);
|
|
|
|
|
|
|
|
float bg_scale = size.x / background.getLocalBounds().width;
|
|
|
|
background.setScale(bg_scale, bg_scale);
|
|
|
|
background.setPosition(sf::Vector2f(
|
|
|
|
size.x / 2 - background.getGlobalBounds().width / 2,
|
|
|
|
size.y - background.getGlobalBounds().height
|
|
|
|
));
|
|
|
|
|
|
|
|
window.draw(background);
|
2016-04-09 00:32:11 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-04-09 21:33:05 +00:00
|
|
|
// d'abord un fond pour éviter de mélanger texte et image
|
|
|
|
sf::Vector2f mask_position(size.x - (font_size * 10), 0);
|
|
|
|
sf::RectangleShape mask(sf::Vector2f(font_size * 10, size.y));
|
|
|
|
mask.setPosition(mask_position);
|
|
|
|
mask.setFillColor(sf::Color(0, 0, 0, 127));
|
|
|
|
|
|
|
|
window.draw(mask);
|
2016-04-09 01:11:14 +00:00
|
|
|
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::loadMainMenu() {
|
|
|
|
choices.clear();
|
|
|
|
actions.clear();
|
|
|
|
selection = 0;
|
|
|
|
|
2016-04-09 21:33:05 +00:00
|
|
|
sf::Texture& texture = getResourceManager().getTexture("bg_menu.tga");
|
|
|
|
texture.setSmooth(true);
|
|
|
|
background.setTexture(texture);
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
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-11 01:50:08 +00:00
|
|
|
std::vector<std::string> path_list = getResourceManager().getLevelList();
|
|
|
|
std::vector<std::string> name_list;
|
2016-04-07 22:19:01 +00:00
|
|
|
|
2016-04-11 01:50:08 +00:00
|
|
|
for (auto it = path_list.begin(); it != path_list.end(); it++) {
|
|
|
|
choices.push_back(Level::getLevelName(*it));
|
|
|
|
actions.push_back(std::bind(&Menu::launchGame, this, *it));
|
|
|
|
}
|
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()));
|
2016-04-10 18:34:29 +00:00
|
|
|
|
2016-04-11 01:50:08 +00:00
|
|
|
// TODO: charger dynamiquement le niveau dans l'éditeur
|
|
|
|
editor->load(getResourceManager().getLevelPath("editor_result.dat"));
|
2016-04-09 13:53:12 +00:00
|
|
|
getManager().setState(editor);
|
2016-04-09 00:32:11 +00:00
|
|
|
}
|
2016-04-08 00:35:17 +00:00
|
|
|
|
2016-04-11 01:50:08 +00:00
|
|
|
void Menu::launchGame(std::string path) {
|
2016-04-09 02:36:30 +00:00
|
|
|
std::shared_ptr<Game> game = std::shared_ptr<Game>(new Game(getManager()));
|
2016-04-11 01:50:08 +00:00
|
|
|
game->load(path);
|
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
|
|
|
}
|