2016-04-12 16:11:36 +00:00
|
|
|
|
#include "state.hpp"
|
2016-03-28 15:23:47 +00:00
|
|
|
|
#include "manager.hpp"
|
2016-04-20 15:31:13 +00:00
|
|
|
|
#include "resource_manager.hpp"
|
2016-04-19 17:09:46 +00:00
|
|
|
|
#include <algorithm>
|
2016-03-28 15:23:47 +00:00
|
|
|
|
|
2016-04-10 02:47:02 +00:00
|
|
|
|
const unsigned int Manager::FPS = 60;
|
|
|
|
|
const sf::Time Manager::FRAME_TIME = sf::seconds(1.f / Manager::FPS);
|
2016-04-12 16:11:36 +00:00
|
|
|
|
const float Manager::GRID = 32;
|
2016-04-10 02:47:02 +00:00
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
Manager::Manager() : title(sf::String(L"")), previous_time(sf::seconds(0)),
|
2016-04-18 10:05:45 +00:00
|
|
|
|
previous_state(nullptr) {
|
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
// ajout des polices dans le gestionnaire de ressources
|
|
|
|
|
// de la librairie pour l'interface
|
|
|
|
|
desktop.GetEngine().GetResourceManager().AddFont(
|
|
|
|
|
"overpass", ResourceManager::get().getFont("overpass.ttf")
|
|
|
|
|
);
|
2016-04-10 10:27:06 +00:00
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
desktop.GetEngine().GetResourceManager().AddFont(
|
|
|
|
|
"overpass-bold", ResourceManager::get().getFont("overpass-bold.ttf")
|
|
|
|
|
);
|
2016-04-19 23:09:48 +00:00
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
desktop.GetEngine().GetResourceManager().AddFont(
|
|
|
|
|
"monoid", ResourceManager::get().getFont("monoid.ttf")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// chargement du thème de l'interface
|
2016-04-19 22:29:13 +00:00
|
|
|
|
desktop.LoadThemeFromFile("res/gui.theme");
|
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
// création de la fenêtre du jeu
|
|
|
|
|
window.create(sf::VideoMode(704, 480), "Skizzle", sf::Style::Default);
|
2016-04-10 18:34:29 +00:00
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
// FIXME: après avoir supprimé ::useGUIView(), supprimer ceci
|
2016-04-10 18:34:29 +00:00
|
|
|
|
// récupération de la vue par défaut comme vue du gui
|
|
|
|
|
gui_view = window.getDefaultView();
|
2016-04-10 10:27:06 +00:00
|
|
|
|
}
|
2016-03-28 15:23:47 +00:00
|
|
|
|
|
2016-04-19 17:09:46 +00:00
|
|
|
|
Manager::~Manager() {
|
|
|
|
|
widgets.clear();
|
|
|
|
|
desktop.RemoveAll();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
|
void Manager::start() {
|
2016-04-11 19:11:34 +00:00
|
|
|
|
while (!states.empty()) {
|
2016-03-28 15:23:47 +00:00
|
|
|
|
sf::Event event;
|
|
|
|
|
|
|
|
|
|
// traitement des évènements reçus
|
|
|
|
|
while (window.pollEvent(event)) {
|
2016-04-11 19:11:34 +00:00
|
|
|
|
// fermeture de la fenêtre : on dépile tous les états
|
2016-03-28 15:23:47 +00:00
|
|
|
|
if (event.type == sf::Event::Closed) {
|
2016-04-11 19:11:34 +00:00
|
|
|
|
while (!states.empty()) {
|
|
|
|
|
states.pop();
|
|
|
|
|
}
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
// FIXME: après avoir supprimé ::useGUIView(), supprimer ceci
|
2016-04-09 00:32:11 +00:00
|
|
|
|
// redimensionnement de la vue par défaut
|
2016-04-08 00:21:19 +00:00
|
|
|
|
if (event.type == sf::Event::Resized) {
|
2016-04-10 18:34:29 +00:00
|
|
|
|
gui_view = sf::View(sf::FloatRect(
|
2016-04-08 00:21:19 +00:00
|
|
|
|
0, 0, event.size.width, event.size.height
|
|
|
|
|
));
|
|
|
|
|
}
|
2016-04-20 15:31:13 +00:00
|
|
|
|
///////////////////////////
|
2016-04-08 00:21:19 +00:00
|
|
|
|
|
2016-04-18 10:05:45 +00:00
|
|
|
|
// événements de l'interface
|
|
|
|
|
desktop.HandleEvent(event);
|
|
|
|
|
|
2016-04-11 19:11:34 +00:00
|
|
|
|
// s'il n'y a plus d'état, on quitte
|
|
|
|
|
if (states.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 00:04:15 +00:00
|
|
|
|
// seulement si l'événement ne s'est pas passé dans l'interface
|
|
|
|
|
// on le fait passer aux gestionnaires "normaux"
|
|
|
|
|
if (!isInsideGUI(event)) {
|
|
|
|
|
states.top()->processEvent(event);
|
|
|
|
|
}
|
2016-04-11 19:11:34 +00:00
|
|
|
|
}
|
2016-04-12 16:11:36 +00:00
|
|
|
|
|
2016-04-11 19:11:34 +00:00
|
|
|
|
// s'il n'y a plus d'état, on quitte
|
|
|
|
|
if (states.empty()) {
|
|
|
|
|
return;
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 10:05:45 +00:00
|
|
|
|
// si l'état que l'on va utiliser n'est pas le même que précédemment,
|
|
|
|
|
// on l'active. Ceci permet un partage plus facile des ressources
|
|
|
|
|
// globales (vue, musique)
|
2016-04-17 13:25:21 +00:00
|
|
|
|
if (previous_state != states.top().get()) {
|
|
|
|
|
previous_state = states.top().get();
|
2016-04-18 10:05:45 +00:00
|
|
|
|
|
2016-04-19 17:09:46 +00:00
|
|
|
|
// on masque tous les widgets de l'interface
|
|
|
|
|
for (const auto &widget : widgets) {
|
|
|
|
|
widget->Show(false);
|
|
|
|
|
}
|
2016-04-18 10:05:45 +00:00
|
|
|
|
|
|
|
|
|
// on initialise le nouvel état
|
2016-04-17 13:25:21 +00:00
|
|
|
|
previous_state->enable();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 10:05:45 +00:00
|
|
|
|
// demande à l'état actuel de dessiner une frame
|
2016-04-11 19:11:34 +00:00
|
|
|
|
states.top()->frame();
|
2016-04-18 10:05:45 +00:00
|
|
|
|
|
|
|
|
|
// dessin de l'interface
|
|
|
|
|
sf::View normal_view = window.getView();
|
|
|
|
|
|
|
|
|
|
desktop.Update((getCurrentTime() - previous_time).asSeconds());
|
|
|
|
|
previous_time = getCurrentTime();
|
|
|
|
|
sfgui.Display(window);
|
|
|
|
|
|
|
|
|
|
window.setView(normal_view);
|
|
|
|
|
|
|
|
|
|
// envoi à l'écran pour affichage
|
2016-04-05 15:50:28 +00:00
|
|
|
|
window.display();
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 19:11:34 +00:00
|
|
|
|
void Manager::pushState(std::unique_ptr<State> set_state) {
|
|
|
|
|
states.push(std::move(set_state));
|
2016-04-05 23:13:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 19:11:34 +00:00
|
|
|
|
void Manager::popState() {
|
|
|
|
|
states.pop();
|
2016-03-28 17:57:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 00:04:15 +00:00
|
|
|
|
bool Manager::isInsideGUI(const sf::Event& event) {
|
|
|
|
|
sf::Vector2f check_point;
|
|
|
|
|
bool should_check_point = false;
|
|
|
|
|
|
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed ||
|
|
|
|
|
event.type == sf::Event::MouseButtonReleased) {
|
|
|
|
|
check_point.x = event.mouseButton.x;
|
|
|
|
|
check_point.y = event.mouseButton.y;
|
|
|
|
|
should_check_point = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type == sf::Event::MouseMoved) {
|
|
|
|
|
check_point.x = event.mouseMove.x;
|
|
|
|
|
check_point.y = event.mouseMove.y;
|
|
|
|
|
should_check_point = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type == sf::Event::MouseWheelScrolled) {
|
|
|
|
|
check_point.x = event.mouseWheelScroll.x;
|
|
|
|
|
check_point.y = event.mouseWheelScroll.y;
|
|
|
|
|
should_check_point = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// si l'événement est de type souris, on regarde que
|
|
|
|
|
// le point en question ne se trouve pas dans un des widgets
|
|
|
|
|
if (should_check_point) {
|
|
|
|
|
for (auto const &widget : widgets) {
|
|
|
|
|
if (widget->GetAllocation().contains(check_point)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 15:31:13 +00:00
|
|
|
|
sf::Time Manager::getCurrentTime() const {
|
|
|
|
|
return clock.getElapsedTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sf::RenderWindow& Manager::getWindow() {
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: à supprimer après avoir supprimé ::useGUIView()
|
|
|
|
|
void Manager::useGUIView() {
|
|
|
|
|
window.setView(gui_view);
|
|
|
|
|
}
|
|
|
|
|
///////////////////////////
|
|
|
|
|
|
2016-04-19 17:09:46 +00:00
|
|
|
|
void Manager::addWidget(sfg::Widget::Ptr widget) {
|
|
|
|
|
widgets.push_back(widget);
|
|
|
|
|
desktop.Add(widget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Manager::removeWidget(sfg::Widget::Ptr widget) {
|
|
|
|
|
desktop.Remove(widget);
|
|
|
|
|
widgets.erase(std::remove(
|
|
|
|
|
widgets.begin(), widgets.end(), widget
|
|
|
|
|
), widgets.end());
|
2016-04-18 10:05:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 11:41:12 +00:00
|
|
|
|
sf::String Manager::getTitle() {
|
2016-04-05 18:07:58 +00:00
|
|
|
|
return title;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 11:41:12 +00:00
|
|
|
|
void Manager::setTitle(sf::String set_title) {
|
2016-04-12 18:11:51 +00:00
|
|
|
|
if (title != set_title) {
|
|
|
|
|
title = set_title;
|
|
|
|
|
|
|
|
|
|
// màj du titre selon le titre donné par l'état
|
|
|
|
|
if (title.isEmpty()) {
|
|
|
|
|
window.setTitle(sf::String(L"Skizzle"));
|
|
|
|
|
} else {
|
|
|
|
|
window.setTitle(sf::String(L"Skizzle ‒ ") + title);
|
|
|
|
|
}
|
2016-04-05 18:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 15:23:47 +00:00
|
|
|
|
bool Manager::isKeyPressed(sf::Keyboard::Key key) const {
|
2016-04-04 17:53:54 +00:00
|
|
|
|
return sf::Keyboard::isKeyPressed(key) && window.hasFocus();
|
2016-03-28 15:23:47 +00:00
|
|
|
|
}
|
2016-04-09 23:29:42 +00:00
|
|
|
|
|
|
|
|
|
bool Manager::isKeyPressed(Manager::Modifier modifier) const {
|
|
|
|
|
switch (modifier) {
|
|
|
|
|
case Manager::Modifier::CONTROL:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LControl) || isKeyPressed(sf::Keyboard::RControl);
|
|
|
|
|
|
|
|
|
|
case Manager::Modifier::ALT:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LAlt) || isKeyPressed(sf::Keyboard::RAlt);
|
|
|
|
|
|
|
|
|
|
case Manager::Modifier::SYSTEM:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LSystem) || isKeyPressed(sf::Keyboard::RSystem);
|
|
|
|
|
|
|
|
|
|
case Manager::Modifier::SHIFT:
|
|
|
|
|
return isKeyPressed(sf::Keyboard::LShift) || isKeyPressed(sf::Keyboard::RShift);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|