2016-04-04 18:25:56 +00:00
|
|
|
#include <cmath>
|
2016-04-07 09:22:20 +00:00
|
|
|
#include <algorithm>
|
2016-04-03 20:08:11 +00:00
|
|
|
#include "editor.hpp"
|
2016-04-05 23:13:00 +00:00
|
|
|
#include "game.hpp"
|
2016-04-04 18:25:56 +00:00
|
|
|
#include "block.hpp"
|
2016-04-03 20:08:11 +00:00
|
|
|
#include "constants.hpp"
|
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
const sf::Color SELECTION_COLOR = sf::Color(33, 33, 33, 40);
|
|
|
|
const sf::Color SELECTION_BORDER_COLOR = sf::Color(33, 33, 33, 127);
|
|
|
|
|
|
|
|
Editor::Editor(Manager& manager) : Level(manager), drag_mode(DragMode::NONE),
|
2016-04-06 13:21:46 +00:00
|
|
|
widget_timer(manager, true, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)) {}
|
2016-04-03 20:08:11 +00:00
|
|
|
|
|
|
|
Editor::~Editor() {}
|
|
|
|
|
2016-04-05 18:07:58 +00:00
|
|
|
void Editor::load(std::ifstream& file) {
|
|
|
|
Level::load(file);
|
2016-04-06 11:41:12 +00:00
|
|
|
manager.setTitle(sf::String(L"Édition de ") + getName());
|
2016-04-05 18:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 20:08:11 +00:00
|
|
|
void Editor::frame() {
|
2016-04-04 18:25:56 +00:00
|
|
|
const std::vector<sf::Event>& events = manager.getEvents();
|
|
|
|
|
2016-04-05 22:31:46 +00:00
|
|
|
// traitement des événements
|
2016-04-04 18:25:56 +00:00
|
|
|
for (unsigned int i = 0; i < events.size(); i++) {
|
2016-04-07 20:01:07 +00:00
|
|
|
processEvent(events[i]);
|
|
|
|
}
|
2016-04-04 18:25:56 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// dessin de la frame
|
|
|
|
draw();
|
|
|
|
sf::sleep(sf::seconds(1.f / 30));
|
|
|
|
}
|
2016-04-05 22:31:46 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
bool Editor::processEvent(const sf::Event& event) {
|
|
|
|
// traitement des événements du widget timer
|
|
|
|
if (widget_timer.processEvent(event)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-04 18:25:56 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// lorsque l'on clique dans l'éditeur
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
|
|
|
sf::Vector2f position(event.mouseButton.x, event.mouseButton.y);
|
|
|
|
ObjectPtr pointed_object = getObject(position);
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
if (event.mouseButton.button == sf::Mouse::Left) {
|
|
|
|
// clic + shift : sélection par rectangle de sélection
|
|
|
|
if (manager.isKeyPressed(sf::Keyboard::LShift)) {
|
|
|
|
drag_start = position;
|
|
|
|
drag_end = position;
|
|
|
|
drag_mode = DragMode::SELECT_RECT;
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// clic sur un objet : démarrage de la sélection libre
|
|
|
|
if (pointed_object != nullptr) {
|
|
|
|
if (manager.isKeyPressed(sf::Keyboard::LControl)) {
|
2016-04-07 18:59:08 +00:00
|
|
|
drag_start = position;
|
|
|
|
drag_end = position;
|
2016-04-07 20:01:07 +00:00
|
|
|
drag_mode = DragMode::SELECT_BULK;
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
select(pointed_object, SelectionMode::ADD);
|
|
|
|
} else {
|
|
|
|
select(pointed_object, SelectionMode::FLIP);
|
2016-04-05 15:50:28 +00:00
|
|
|
}
|
2016-04-07 20:01:07 +00:00
|
|
|
|
|
|
|
return true;
|
2016-04-07 18:59:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 20:19:38 +00:00
|
|
|
// clic gauche dans le vide : démarrage du placement en drag&drop
|
2016-04-07 20:01:07 +00:00
|
|
|
drag_start = position;
|
2016-04-07 18:59:08 +00:00
|
|
|
drag_end = position;
|
2016-04-07 20:19:38 +00:00
|
|
|
drag_mode = DragMode::PLACE;
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
select(addObject(position), SelectionMode::REPLACE);
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
if (event.mouseButton.button == sf::Mouse::Right) {
|
2016-04-07 20:19:38 +00:00
|
|
|
// clic droit sur un objet : démarrage de la suppression en drag&drop
|
|
|
|
if (pointed_object != nullptr) {
|
|
|
|
drag_start = position;
|
|
|
|
drag_end = position;
|
|
|
|
drag_mode = DragMode::REMOVE;
|
|
|
|
|
|
|
|
removeObject(pointed_object);
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lorsqu'on déplace la souris
|
|
|
|
if (event.type == sf::Event::MouseMoved) {
|
|
|
|
sf::Vector2f position(event.mouseMove.x, event.mouseMove.y);
|
|
|
|
ObjectPtr pointed_object = getObject(position);
|
|
|
|
drag_end = position;
|
|
|
|
|
|
|
|
// mode placement d'objets
|
2016-04-07 20:19:38 +00:00
|
|
|
if (drag_mode == DragMode::PLACE && pointed_object == nullptr) {
|
2016-04-07 20:01:07 +00:00
|
|
|
select(addObject(position), SelectionMode::ADD);
|
|
|
|
return true;
|
2016-04-04 18:25:56 +00:00
|
|
|
}
|
2016-04-05 23:13:00 +00:00
|
|
|
|
2016-04-07 20:19:38 +00:00
|
|
|
// mode suppression d'objets
|
|
|
|
if (drag_mode == DragMode::REMOVE && pointed_object != nullptr) {
|
|
|
|
removeObject(pointed_object);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// mode sélection libre : on l'objet à la sélection
|
|
|
|
if (drag_mode == DragMode::SELECT_BULK) {
|
|
|
|
select(position, SelectionMode::ADD);
|
|
|
|
return true;
|
2016-04-07 18:59:08 +00:00
|
|
|
}
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// lorsqu'on relâche un clic dans l'éditeur
|
|
|
|
if (event.type == sf::Event::MouseButtonReleased) {
|
|
|
|
sf::Vector2f position(event.mouseButton.x, event.mouseButton.y);
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// mode sélection rectangulaire : on applique la sélection
|
|
|
|
if (drag_mode == DragMode::SELECT_RECT) {
|
|
|
|
select(drag_start, drag_end);
|
|
|
|
}
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
drag_mode = DragMode::NONE;
|
2016-04-07 20:19:38 +00:00
|
|
|
return true;
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// gestion des touches
|
|
|
|
if (event.type == sf::Event::KeyPressed) {
|
|
|
|
// appui sur suppr : suppression des blocs sélectionnés
|
|
|
|
if (event.key.code == sf::Keyboard::Delete) {
|
|
|
|
std::vector<ObjectPtr>& objects = getObjects();
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
for (unsigned int i = 0; i < selection.size(); i++) {
|
|
|
|
objects.erase(std::remove(
|
|
|
|
objects.begin(), objects.end(), selection[i]
|
|
|
|
), objects.end());
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|
2016-04-07 20:01:07 +00:00
|
|
|
|
|
|
|
selection.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// appui sur espace : test du niveau en cours d'édition
|
|
|
|
if (event.key.code == sf::Keyboard::Space) {
|
|
|
|
test();
|
|
|
|
return true;
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|
2016-04-04 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
return false;
|
2016-04-03 20:08:11 +00:00
|
|
|
}
|
2016-04-05 15:50:28 +00:00
|
|
|
|
|
|
|
void Editor::draw() {
|
|
|
|
Level::draw();
|
2016-04-05 22:31:46 +00:00
|
|
|
|
2016-04-05 17:27:37 +00:00
|
|
|
sf::RenderWindow& window = manager.getWindow();
|
|
|
|
sf::View window_view = manager.getWindowView();
|
2016-04-05 15:50:28 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
// dessin du rectangle de sélection
|
|
|
|
if (drag_mode == DragMode::SELECT_RECT) {
|
|
|
|
sf::RectangleShape selection_rect(drag_end - drag_start);
|
|
|
|
selection_rect.setPosition(drag_start);
|
|
|
|
selection_rect.setFillColor(SELECTION_COLOR);
|
|
|
|
selection_rect.setOutlineThickness(2.f);
|
|
|
|
selection_rect.setOutlineColor(SELECTION_BORDER_COLOR);
|
|
|
|
|
|
|
|
window.draw(selection_rect);
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:31:46 +00:00
|
|
|
// dessin du widget timer
|
|
|
|
widget_timer.setTimeLeft(getTotalTime());
|
|
|
|
widget_timer.draw(sf::Vector2f(window_view.getSize().x / 2 - 50, 0));
|
|
|
|
|
2016-04-05 15:50:28 +00:00
|
|
|
// menu
|
2016-04-05 17:27:37 +00:00
|
|
|
sf::RectangleShape menu(sf::Vector2f(window_view.getSize().x, 64));
|
|
|
|
menu.setPosition(sf::Vector2f(0, window_view.getSize().y - 64));
|
|
|
|
|
|
|
|
window.draw(menu);
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
ObjectPtr Editor::getObject(sf::Vector2f position) {
|
2016-04-05 17:27:37 +00:00
|
|
|
std::vector<ObjectPtr>& objects = getObjects();
|
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
|
|
|
if (objects[i]->getAABB()->contains(position)) {
|
|
|
|
return objects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectPtr Editor::addObject(sf::Vector2f position) {
|
|
|
|
std::vector<ObjectPtr>& objects = getObjects();
|
|
|
|
|
|
|
|
// on arrondit à l'unité de grille la plus proche
|
2016-04-05 17:27:37 +00:00
|
|
|
position /= Constants::GRID;
|
|
|
|
position.x = round(position.x);
|
|
|
|
position.y = round(position.y);
|
|
|
|
position *= Constants::GRID;
|
|
|
|
|
|
|
|
// TODO: ajouter un objet du type choisi, pas uniquement de bloc
|
2016-04-06 11:54:03 +00:00
|
|
|
ObjectPtr object = ObjectPtr(new Block);
|
2016-04-05 17:27:37 +00:00
|
|
|
object->setPosition(position);
|
|
|
|
|
|
|
|
// avant d'ajouter l'objet, on vérifie qu'il ne soit
|
|
|
|
// pas superposé à un autre
|
|
|
|
float overlaps = false;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
|
|
|
if (objects[i]->getAABB()->intersects(*object->getAABB())) {
|
|
|
|
overlaps = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!overlaps) {
|
|
|
|
objects.push_back(object);
|
2016-04-07 18:59:08 +00:00
|
|
|
return object;
|
2016-04-05 17:27:37 +00:00
|
|
|
}
|
2016-04-07 18:59:08 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2016-04-05 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
void Editor::removeObject(ObjectPtr object) {
|
|
|
|
if (object == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-05 17:27:37 +00:00
|
|
|
std::vector<ObjectPtr>& objects = getObjects();
|
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
// on supprime l'objet de la sélection
|
|
|
|
selection.erase(std::remove(
|
|
|
|
selection.begin(), selection.end(), object
|
|
|
|
), selection.end());
|
|
|
|
|
|
|
|
// on supprime l'objet de la liste d'objets
|
|
|
|
objects.erase(std::remove(
|
|
|
|
objects.begin(), objects.end(), object
|
2016-04-07 20:19:38 +00:00
|
|
|
), objects.end());
|
2016-04-07 18:59:08 +00:00
|
|
|
}
|
2016-04-05 17:27:37 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
void Editor::removeObject(sf::Vector2f position) {
|
|
|
|
removeObject(getObject(position));
|
|
|
|
}
|
2016-04-07 09:22:20 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
void Editor::select(ObjectPtr object, SelectionMode mode) {
|
|
|
|
if (object == nullptr) {
|
|
|
|
return;
|
2016-04-05 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
bool already_selected = std::count(selection.begin(), selection.end(), object) > 0;
|
2016-04-05 17:27:37 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
// dans les modes REPLACE et FLIP, on remplace l'ancienne sélection
|
|
|
|
// pour REPLACE, on sélectionne forcément l'objet
|
|
|
|
// pour FLIP, on le sélectionne s'il ne l'est pas, on le désélectionne sinon
|
|
|
|
if (mode == SelectionMode::REPLACE || mode == SelectionMode::FLIP) {
|
|
|
|
for (unsigned int i = 0; i < selection.size(); i++) {
|
|
|
|
selection[i]->setSelected(false);
|
|
|
|
}
|
2016-04-07 09:22:20 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
selection.clear();
|
2016-04-05 17:27:37 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
// on resélectionne l'objet ssi. on force la sélection
|
|
|
|
// ou s'il n'était pas déjà sélectionné
|
|
|
|
if (!already_selected || mode == SelectionMode::REPLACE) {
|
|
|
|
object->setSelected(true);
|
|
|
|
selection.push_back(object);
|
2016-04-05 17:27:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-05 15:50:28 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
// dans le mode ADD, on rajoute juste l'objet à la sélection
|
|
|
|
if (mode == SelectionMode::ADD && !already_selected) {
|
|
|
|
object->setSelected(true);
|
|
|
|
selection.push_back(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::select(sf::Vector2f position, SelectionMode mode) {
|
|
|
|
select(getObject(position), mode);
|
2016-04-05 15:50:28 +00:00
|
|
|
}
|
2016-04-05 23:13:00 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
void Editor::select(sf::Vector2f top_left, sf::Vector2f bottom_right) {
|
|
|
|
std::vector<ObjectPtr>& objects = getObjects();
|
|
|
|
sf::FloatRect selection_rect(
|
|
|
|
std::min(top_left.x, bottom_right.x),
|
|
|
|
std::min(top_left.y, bottom_right.y),
|
|
|
|
std::abs(bottom_right.x - top_left.x),
|
|
|
|
std::abs(bottom_right.y - top_left.y)
|
|
|
|
);
|
|
|
|
|
|
|
|
// réinitialisation de la sélectionne
|
|
|
|
for (unsigned int i = 0; i < selection.size(); i++) {
|
|
|
|
selection[i]->setSelected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
selection.clear();
|
|
|
|
|
|
|
|
// sélection des éléments intersectant le rectangle
|
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
|
|
|
if (objects[i]->getAABB()->intersects(selection_rect)) {
|
|
|
|
select(objects[i], SelectionMode::ADD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::test() {
|
2016-04-05 23:13:00 +00:00
|
|
|
std::shared_ptr<Game> game = std::shared_ptr<Game>(new Game(manager));
|
|
|
|
|
|
|
|
// copie des propriétés
|
|
|
|
game->setName(getName());
|
|
|
|
game->setTotalTime(getTotalTime());
|
2016-04-06 12:02:11 +00:00
|
|
|
game->setBackground(getBackground());
|
2016-04-05 23:13:00 +00:00
|
|
|
|
|
|
|
// copie des objets du niveau vers le jeu
|
|
|
|
std::vector<ObjectPtr>& objects = getObjects();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-04-06 11:54:03 +00:00
|
|
|
game->getObjects().push_back(objects[i]->clone());
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copie de la zone de jeu
|
|
|
|
std::vector<std::pair<float, float>>& zone = getZone();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < zone.size(); i++) {
|
|
|
|
game->getZone().push_back(zone[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// mise en mode test
|
|
|
|
game->setTestMode(manager.getView());
|
|
|
|
manager.setView(game);
|
|
|
|
}
|