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-08 14:28:56 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
2016-04-03 20:08:11 +00:00
|
|
|
|
2016-04-09 23:02:52 +00:00
|
|
|
const sf::Color SELECT_RECT_COLOR = sf::Color(33, 33, 33, 40);
|
|
|
|
const sf::Color SELECT_RECT_BORDER_COLOR = sf::Color(33, 33, 33, 127);
|
2016-04-10 06:33:09 +00:00
|
|
|
const sf::Color ZONE_POINT_COLOR = sf::Color(140, 15, 15, 255);
|
|
|
|
const sf::Color ZONE_BORDER_COLOR = sf::Color(200, 15, 15, 255);
|
2016-04-09 23:02:52 +00:00
|
|
|
|
2016-04-09 23:34:58 +00:00
|
|
|
const float WHEEL_SCROLL_SPEED = -7.f;
|
|
|
|
const float POINTER_SCROLL_SPEED = 5.f;
|
2016-04-10 00:20:39 +00:00
|
|
|
const int POINTER_SCROLL_PADDING = 10;
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-10 06:33:09 +00:00
|
|
|
/**
|
|
|
|
* Arrondit le vecteur donné à une position
|
|
|
|
* sur la grille
|
|
|
|
*/
|
|
|
|
inline sf::Vector2f roundVectorToGrid(sf::Vector2f input) {
|
|
|
|
input /= Constants::GRID;
|
|
|
|
input.x = round(input.x);
|
|
|
|
input.y = round(input.y);
|
|
|
|
input *= Constants::GRID;
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
Editor::Editor(Manager& manager) : Level(manager),
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_control_point(nullptr), drag_mode(Editor::DragMode::NONE),
|
2016-04-08 13:54:19 +00:00
|
|
|
widget_timer(manager, true, std::bind(&Editor::setTotalTime, this, std::placeholders::_1)),
|
2016-04-10 15:40:04 +00:00
|
|
|
widget_toolbar(manager) {
|
2016-04-08 13:54:19 +00:00
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
setName("Nouveau niveau");
|
|
|
|
setTotalTime(30);
|
2016-04-08 13:54:19 +00:00
|
|
|
}
|
2016-04-03 20:08:11 +00:00
|
|
|
|
|
|
|
Editor::~Editor() {}
|
|
|
|
|
2016-04-09 00:32:11 +00:00
|
|
|
void Editor::begin() {
|
|
|
|
Level::begin();
|
2016-04-09 20:36:07 +00:00
|
|
|
|
|
|
|
getResourceManager().playMusic("editor.ogg");
|
2016-04-10 02:47:02 +00:00
|
|
|
getWindow().setFramerateLimit(Manager::FPS);
|
2016-04-05 18:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 00:35:17 +00:00
|
|
|
void Editor::processEvent(const sf::Event& event) {
|
|
|
|
Level::processEvent(event);
|
2016-04-08 00:21:19 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
// traitement des événements du widget chronomètre
|
2016-04-07 20:01:07 +00:00
|
|
|
if (widget_timer.processEvent(event)) {
|
2016-04-08 00:35:17 +00:00
|
|
|
return;
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
2016-04-04 18:25:56 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
// traitement des événements du widget barre d'outils
|
|
|
|
if (widget_toolbar.processEvent(event)) {
|
2016-04-08 13:54:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// lorsque l'on clique dans l'éditeur
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
2016-04-08 00:21:19 +00:00
|
|
|
sf::Vector2i mouse_position(event.mouseButton.x, event.mouseButton.y);
|
2016-04-09 00:32:11 +00:00
|
|
|
sf::Vector2f position = pixelToCoords(mouse_position);
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::Ptr pointed_object = getObject(position);
|
2016-04-10 11:40:43 +00:00
|
|
|
sf::Vector2f* control_point = getControlPoint(position);
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
if (event.mouseButton.button == sf::Mouse::Left) {
|
2016-04-10 11:40:43 +00:00
|
|
|
// clic sur un point de contrôle : déplacement du point
|
|
|
|
if (control_point != nullptr) {
|
|
|
|
drag_control_point = control_point;
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_mode = Editor::DragMode::CONTROL_POINT;
|
2016-04-10 11:40:43 +00:00
|
|
|
return;
|
2016-04-10 06:33:09 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// clic + shift : sélection par rectangle de sélection
|
2016-04-09 23:29:42 +00:00
|
|
|
if (getManager().isKeyPressed(Manager::Modifier::SHIFT)) {
|
2016-04-08 00:21:19 +00:00
|
|
|
drag_start = mouse_position;
|
|
|
|
drag_end = mouse_position;
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_mode = Editor::DragMode::SELECT_RECT;
|
2016-04-10 06:33:09 +00:00
|
|
|
return;
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
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
|
2016-04-10 06:33:09 +00:00
|
|
|
if (pointed_object != nullptr) {
|
2016-04-09 23:29:42 +00:00
|
|
|
if (getManager().isKeyPressed(Manager::Modifier::CONTROL)) {
|
2016-04-08 00:21:19 +00:00
|
|
|
drag_start = mouse_position;
|
|
|
|
drag_end = mouse_position;
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_mode = Editor::DragMode::SELECT_BULK;
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
select(pointed_object, Editor::SelectionMode::ADD);
|
2016-04-07 20:01:07 +00:00
|
|
|
} else {
|
2016-04-10 15:40:04 +00:00
|
|
|
select(pointed_object, Editor::SelectionMode::FLIP);
|
2016-04-05 15:50:28 +00:00
|
|
|
}
|
2016-04-10 06:33:09 +00:00
|
|
|
|
|
|
|
return;
|
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-10 06:33:09 +00:00
|
|
|
drag_start = mouse_position;
|
|
|
|
drag_end = mouse_position;
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_mode = Editor::DragMode::PLACE;
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
select(addObject(position), Editor::SelectionMode::REPLACE);
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
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-10 11:40:43 +00:00
|
|
|
// clic droit sur un point de contrôle : suppression de ce point
|
|
|
|
if (control_point != nullptr) {
|
|
|
|
removeControlPoint(control_point);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-04-08 00:21:19 +00:00
|
|
|
drag_start = mouse_position;
|
|
|
|
drag_end = mouse_position;
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_mode = Editor::DragMode::REMOVE;
|
2016-04-07 20:19:38 +00:00
|
|
|
|
|
|
|
removeObject(pointed_object);
|
2016-04-10 06:33:09 +00:00
|
|
|
return;
|
2016-04-07 20:19:38 +00:00
|
|
|
}
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lorsqu'on déplace la souris
|
|
|
|
if (event.type == sf::Event::MouseMoved) {
|
2016-04-08 00:21:19 +00:00
|
|
|
sf::Vector2i mouse_position(event.mouseMove.x, event.mouseMove.y);
|
2016-04-09 00:32:11 +00:00
|
|
|
sf::Vector2f position = pixelToCoords(mouse_position);
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::Ptr pointed_object = getObject(position);
|
2016-04-08 00:21:19 +00:00
|
|
|
|
|
|
|
drag_end = mouse_position;
|
2016-04-07 20:01:07 +00:00
|
|
|
|
2016-04-10 06:33:09 +00:00
|
|
|
// mode déplacement de point de contrôle
|
2016-04-10 15:40:04 +00:00
|
|
|
if (drag_mode == Editor::DragMode::CONTROL_POINT) {
|
2016-04-10 11:40:43 +00:00
|
|
|
*drag_control_point = roundVectorToGrid(position);
|
2016-04-10 06:33:09 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// mode placement d'objets
|
2016-04-10 15:40:04 +00:00
|
|
|
if (drag_mode == Editor::DragMode::PLACE && pointed_object == nullptr) {
|
|
|
|
select(addObject(position), Editor::SelectionMode::ADD);
|
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
|
2016-04-10 15:40:04 +00:00
|
|
|
if (drag_mode == Editor::DragMode::REMOVE && pointed_object != nullptr) {
|
2016-04-07 20:19:38 +00:00
|
|
|
removeObject(pointed_object);
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:01:07 +00:00
|
|
|
// mode sélection libre : on l'objet à la sélection
|
2016-04-10 15:40:04 +00:00
|
|
|
if (drag_mode == Editor::DragMode::SELECT_BULK) {
|
|
|
|
select(position, Editor::SelectionMode::ADD);
|
2016-04-07 18:59:08 +00:00
|
|
|
}
|
2016-04-10 06:33:09 +00:00
|
|
|
|
|
|
|
return;
|
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) {
|
|
|
|
// mode sélection rectangulaire : on applique la sélection
|
2016-04-10 15:40:04 +00:00
|
|
|
if (drag_mode == Editor::DragMode::SELECT_RECT) {
|
2016-04-09 00:32:11 +00:00
|
|
|
select(pixelToCoords(drag_start), pixelToCoords(drag_end));
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
2016-04-07 18:59:08 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
drag_mode = Editor::DragMode::NONE;
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 23:02:52 +00:00
|
|
|
// lorsqu'on scrolle on déplace la vue
|
|
|
|
if (event.type == sf::Event::MouseWheelScrolled) {
|
|
|
|
sf::View camera = getCamera();
|
|
|
|
|
2016-04-09 23:34:58 +00:00
|
|
|
// la molette est horizontale ssi. elle l'est vraiment ou
|
|
|
|
// si on utilise la molette verticale et shift
|
|
|
|
bool horizontal = (
|
|
|
|
event.mouseWheelScroll.wheel == sf::Mouse::HorizontalWheel ||
|
|
|
|
(event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel &&
|
|
|
|
getManager().isKeyPressed(Manager::Modifier::SHIFT))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (horizontal) {
|
2016-04-09 23:02:52 +00:00
|
|
|
camera.move(sf::Vector2f(event.mouseWheelScroll.delta, 0) * WHEEL_SCROLL_SPEED);
|
2016-04-09 23:34:58 +00:00
|
|
|
} else {
|
2016-04-09 23:02:52 +00:00
|
|
|
camera.move(sf::Vector2f(0, event.mouseWheelScroll.delta) * WHEEL_SCROLL_SPEED);
|
|
|
|
}
|
|
|
|
|
|
|
|
setCamera(camera);
|
2016-04-10 06:33:09 +00:00
|
|
|
return;
|
2016-04-09 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
for (unsigned int i = 0; i < selection.size(); i++) {
|
2016-04-10 19:18:21 +00:00
|
|
|
removeObject(selection[i]);
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|
2016-04-07 20:01:07 +00:00
|
|
|
|
2016-04-07 22:19:01 +00:00
|
|
|
clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
// appui sur Ctrl + A : sélection de tous les objets
|
|
|
|
if (event.key.code == sf::Keyboard::A && event.key.control) {
|
|
|
|
selectAll();
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// appui sur espace : test du niveau en cours d'édition
|
|
|
|
if (event.key.code == sf::Keyboard::Space) {
|
|
|
|
test();
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|
2016-04-04 18:25:56 +00:00
|
|
|
}
|
2016-04-03 20:08:11 +00:00
|
|
|
}
|
2016-04-05 15:50:28 +00:00
|
|
|
|
2016-04-09 21:57:23 +00:00
|
|
|
void Editor::frame() {
|
|
|
|
// titre de la fenêtre
|
|
|
|
getManager().setTitle(sf::String(L"Édition de ") + getName());
|
|
|
|
|
|
|
|
// dessin de la frame
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
|
2016-04-05 15:50:28 +00:00
|
|
|
void Editor::draw() {
|
2016-04-09 02:36:30 +00:00
|
|
|
sf::RenderWindow& window = getWindow();
|
2016-04-08 00:21:19 +00:00
|
|
|
sf::Vector2i window_size = (sf::Vector2i) window.getSize();
|
|
|
|
|
|
|
|
// scroll de la caméra lorsque la souris se situe sur les bords
|
|
|
|
if (window.hasFocus()) {
|
|
|
|
sf::View camera = getCamera();
|
|
|
|
sf::Vector2i mouse = sf::Mouse::getPosition(window);
|
|
|
|
|
2016-04-09 23:02:52 +00:00
|
|
|
// détection du dépassement sur un des 4 bords
|
2016-04-10 00:20:39 +00:00
|
|
|
if (mouse.x < POINTER_SCROLL_PADDING && mouse.x >= -POINTER_SCROLL_PADDING) {
|
|
|
|
camera.move(sf::Vector2f(-POINTER_SCROLL_SPEED, 0));
|
|
|
|
} else if (mouse.x >= window_size.x - POINTER_SCROLL_PADDING && mouse.x < window_size.x + POINTER_SCROLL_PADDING) {
|
|
|
|
camera.move(sf::Vector2f(POINTER_SCROLL_SPEED, 0));
|
|
|
|
} else if (mouse.y < POINTER_SCROLL_PADDING && mouse.y >= -POINTER_SCROLL_PADDING) {
|
|
|
|
camera.move(sf::Vector2f(0, -POINTER_SCROLL_SPEED));
|
|
|
|
} else if (mouse.y >= window_size.y - POINTER_SCROLL_PADDING && mouse.y < window_size.y + POINTER_SCROLL_PADDING) {
|
|
|
|
camera.move(sf::Vector2f(0, POINTER_SCROLL_SPEED));
|
2016-04-08 00:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setCamera(camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
// dessin des objets du niveau
|
|
|
|
Level::draw();
|
2016-04-05 15:50:28 +00:00
|
|
|
|
2016-04-10 06:33:09 +00:00
|
|
|
// dessin de la zone de jeu
|
|
|
|
const std::vector<sf::Vector2f>& zone = getZone();
|
2016-04-10 11:40:43 +00:00
|
|
|
sf::VertexArray zone_polygon(sf::LinesStrip);
|
|
|
|
control_points_circles.clear();
|
2016-04-10 06:33:09 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < zone.size() + 1; i++) {
|
2016-04-10 11:40:43 +00:00
|
|
|
sf::CircleShape circle(5);
|
2016-04-10 06:33:09 +00:00
|
|
|
sf::Vector2f position = zone[i % zone.size()];
|
|
|
|
|
2016-04-10 11:40:43 +00:00
|
|
|
circle.setOrigin(sf::Vector2f(5, 5));
|
|
|
|
circle.setFillColor(ZONE_POINT_COLOR);
|
|
|
|
circle.setPosition(position);
|
2016-04-10 06:33:09 +00:00
|
|
|
|
2016-04-10 11:40:43 +00:00
|
|
|
zone_polygon.append(sf::Vertex(position, ZONE_BORDER_COLOR));
|
|
|
|
control_points_circles.push_back(circle);
|
2016-04-10 06:33:09 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 11:40:43 +00:00
|
|
|
window.draw(zone_polygon);
|
2016-04-10 06:33:09 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < zone.size(); i++) {
|
2016-04-10 11:40:43 +00:00
|
|
|
window.draw(control_points_circles[i]);
|
2016-04-10 06:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// on passe au dessin d'éléments d'interface.
|
|
|
|
// Changement de vue sur la vue par défaut
|
2016-04-10 18:34:29 +00:00
|
|
|
getManager().useGUIView();
|
2016-04-10 06:33:09 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
// dessin du rectangle de sélection
|
2016-04-10 15:40:04 +00:00
|
|
|
if (drag_mode == Editor::DragMode::SELECT_RECT) {
|
2016-04-08 00:21:19 +00:00
|
|
|
sf::Vector2f size = (sf::Vector2f) (drag_end - drag_start);
|
|
|
|
sf::Vector2f pos = (sf::Vector2f) drag_start;
|
|
|
|
|
|
|
|
sf::RectangleShape selection_rect(size);
|
|
|
|
selection_rect.setPosition(pos);
|
2016-04-09 23:02:52 +00:00
|
|
|
selection_rect.setFillColor(SELECT_RECT_COLOR);
|
2016-04-07 18:59:08 +00:00
|
|
|
selection_rect.setOutlineThickness(2.f);
|
2016-04-09 23:02:52 +00:00
|
|
|
selection_rect.setOutlineColor(SELECT_RECT_BORDER_COLOR);
|
2016-04-07 18:59:08 +00:00
|
|
|
|
|
|
|
window.draw(selection_rect);
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:31:46 +00:00
|
|
|
// dessin du widget timer
|
|
|
|
widget_timer.setTimeLeft(getTotalTime());
|
2016-04-08 00:21:19 +00:00
|
|
|
widget_timer.draw(sf::Vector2f(window_size.x / 2 - 50, 0));
|
2016-04-05 22:31:46 +00:00
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
// barre d'outils
|
|
|
|
widget_toolbar.draw(
|
2016-04-08 13:54:19 +00:00
|
|
|
sf::Vector2f(window_size.x - 64, 0),
|
|
|
|
sf::Vector2f(64, window_size.y)
|
|
|
|
);
|
2016-04-05 17:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::Ptr Editor::getObject(sf::Vector2f position) {
|
2016-04-10 19:18:21 +00:00
|
|
|
const std::vector<Object::Ptr>& objects = getObjects();
|
2016-04-05 17:27:37 +00:00
|
|
|
|
2016-04-07 18:59:08 +00:00
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-04-10 03:10:27 +00:00
|
|
|
if (objects[i]->getAABB().contains(position)) {
|
2016-04-07 18:59:08 +00:00
|
|
|
return objects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-10 11:40:43 +00:00
|
|
|
sf::Vector2f* Editor::getControlPoint(sf::Vector2f position) {
|
|
|
|
std::vector<sf::Vector2f>& zone = getZone();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < zone.size(); i++) {
|
|
|
|
if (control_points_circles[i].getGlobalBounds().contains(position)) {
|
|
|
|
return &zone[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-10 05:37:09 +00:00
|
|
|
Object::Ptr Editor::addObject(sf::Vector2f position) {
|
2016-04-10 19:18:21 +00:00
|
|
|
const std::vector<Object::Ptr>& objects = getObjects();
|
2016-04-07 18:59:08 +00:00
|
|
|
|
|
|
|
// on arrondit à l'unité de grille la plus proche
|
2016-04-10 06:33:09 +00:00
|
|
|
position = roundVectorToGrid(position);
|
2016-04-10 15:40:04 +00:00
|
|
|
Object::Ptr object = widget_toolbar.createObject();
|
2016-04-10 18:49:38 +00:00
|
|
|
|
|
|
|
if (object == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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++) {
|
2016-04-10 03:10:27 +00:00
|
|
|
if (objects[i]->getAABB().intersects(object->getAABB())) {
|
2016-04-05 17:27:37 +00:00
|
|
|
overlaps = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!overlaps) {
|
2016-04-10 19:18:21 +00:00
|
|
|
Level::addObject(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-10 05:37:09 +00:00
|
|
|
void Editor::removeObject(Object::Ptr object) {
|
2016-04-07 18:59:08 +00:00
|
|
|
if (object == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// on supprime l'objet de la sélection
|
|
|
|
selection.erase(std::remove(
|
|
|
|
selection.begin(), selection.end(), object
|
|
|
|
), selection.end());
|
|
|
|
|
2016-04-10 19:18:21 +00:00
|
|
|
Level::removeObject(object);
|
2016-04-07 18:59:08 +00:00
|
|
|
}
|
2016-04-05 17:27:37 +00:00
|
|
|
|
2016-04-10 11:40:43 +00:00
|
|
|
void Editor::removeControlPoint(sf::Vector2f* control_point) {
|
|
|
|
if (control_point == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<sf::Vector2f>& zone = getZone();
|
|
|
|
|
|
|
|
// on supprime le point de la liste
|
|
|
|
zone.erase(std::remove(
|
|
|
|
zone.begin(), zone.end(), *control_point
|
|
|
|
), zone.end());
|
|
|
|
}
|
|
|
|
|
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-10 15:40:04 +00:00
|
|
|
void Editor::select(Object::Ptr object, Editor::SelectionMode mode) {
|
2016-04-07 18:59:08 +00:00
|
|
|
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
|
2016-04-10 15:40:04 +00:00
|
|
|
if (mode == Editor::SelectionMode::REPLACE || mode == Editor::SelectionMode::FLIP) {
|
2016-04-07 22:19:01 +00:00
|
|
|
clearSelection();
|
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é
|
2016-04-10 15:40:04 +00:00
|
|
|
if (!already_selected || mode == Editor::SelectionMode::REPLACE) {
|
2016-04-07 18:59:08 +00:00
|
|
|
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
|
2016-04-10 15:40:04 +00:00
|
|
|
if (mode == Editor::SelectionMode::ADD && !already_selected) {
|
2016-04-07 18:59:08 +00:00
|
|
|
object->setSelected(true);
|
|
|
|
selection.push_back(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 15:40:04 +00:00
|
|
|
void Editor::select(sf::Vector2f position, Editor::SelectionMode mode) {
|
2016-04-07 18:59:08 +00:00
|
|
|
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) {
|
2016-04-10 19:18:21 +00:00
|
|
|
const std::vector<Object::Ptr>& objects = getObjects();
|
2016-04-07 20:01:07 +00:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
2016-04-07 22:19:01 +00:00
|
|
|
clearSelection();
|
|
|
|
|
|
|
|
// sélection des éléments intersectant le rectangle
|
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-04-10 03:10:27 +00:00
|
|
|
if (objects[i]->getAABB().intersects(selection_rect)) {
|
2016-04-10 15:40:04 +00:00
|
|
|
select(objects[i], Editor::SelectionMode::ADD);
|
2016-04-07 22:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::clearSelection() {
|
2016-04-07 20:01:07 +00:00
|
|
|
for (unsigned int i = 0; i < selection.size(); i++) {
|
|
|
|
selection[i]->setSelected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
selection.clear();
|
2016-04-07 22:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::selectAll() {
|
2016-04-10 19:18:21 +00:00
|
|
|
const std::vector<Object::Ptr>& objects = getObjects();
|
2016-04-07 20:01:07 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-04-07 22:19:01 +00:00
|
|
|
objects[i]->setSelected(true);
|
|
|
|
selection.push_back(objects[i]);
|
2016-04-07 20:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::test() {
|
2016-04-09 02:36:30 +00:00
|
|
|
std::shared_ptr<Game> game = std::shared_ptr<Game>(new Game(getManager()));
|
2016-04-07 22:19:01 +00:00
|
|
|
clearSelection();
|
2016-04-05 23:13:00 +00:00
|
|
|
|
|
|
|
// copie des propriétés
|
|
|
|
game->setName(getName());
|
|
|
|
game->setTotalTime(getTotalTime());
|
2016-04-06 12:02:11 +00:00
|
|
|
game->setBackground(getBackground());
|
2016-04-10 04:53:45 +00:00
|
|
|
game->setMusic(getMusic());
|
2016-04-05 23:13:00 +00:00
|
|
|
|
|
|
|
// copie des objets du niveau vers le jeu
|
2016-04-10 05:37:09 +00:00
|
|
|
std::vector<Object::Ptr>& objects = getObjects();
|
2016-04-05 23:13:00 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-04-10 19:18:21 +00:00
|
|
|
game->addObject(objects[i]->clone());
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copie de la zone de jeu
|
2016-04-10 06:33:09 +00:00
|
|
|
std::vector<sf::Vector2f>& zone = getZone();
|
2016-04-05 23:13:00 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < zone.size(); i++) {
|
|
|
|
game->getZone().push_back(zone[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// mise en mode test
|
2016-04-09 13:53:12 +00:00
|
|
|
game->setTestMode(getManager().getState());
|
|
|
|
getManager().setState(game);
|
2016-04-05 23:13:00 +00:00
|
|
|
}
|