Nouveau type de sélection
This commit is contained in:
parent
22c2ea9d8b
commit
5ff5ba493a
|
@ -7,6 +7,7 @@
|
|||
class Block : public Object {
|
||||
private:
|
||||
mutable sf::Sprite sprite;
|
||||
mutable sf::Sprite select_sprite;
|
||||
|
||||
public:
|
||||
/**
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
*/
|
||||
class Editor : public Level {
|
||||
private:
|
||||
std::unordered_map<ObjectPtr, bool> selection;
|
||||
std::vector<ObjectPtr> selection;
|
||||
WidgetTimer widget_timer;
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,6 +16,7 @@ private:
|
|||
sf::Vector2f velocity;
|
||||
sf::Vector2f position;
|
||||
|
||||
bool selected;
|
||||
mutable float inv_mass;
|
||||
|
||||
float mass;
|
||||
|
@ -59,7 +60,7 @@ public:
|
|||
/**
|
||||
* Dessine l'objet dans la fenêtre donnée
|
||||
*/
|
||||
virtual void draw(Manager& manager);
|
||||
virtual void draw(Manager& manager) = 0;
|
||||
|
||||
/**
|
||||
* Met à jour la vitesse de l'objet selon les
|
||||
|
@ -133,6 +134,16 @@ public:
|
|||
*/
|
||||
void setPosition(sf::Vector2f set_position);
|
||||
|
||||
/**
|
||||
* Récupère si l'objet est sélectionné
|
||||
*/
|
||||
bool isSelected() const;
|
||||
|
||||
/**
|
||||
* Modifie si l'objet est sélectionné
|
||||
*/
|
||||
void setSelected(bool set_selected);
|
||||
|
||||
/**
|
||||
* Récupère la masse de l'objet
|
||||
*/
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 764 B |
|
@ -12,6 +12,7 @@ Block::Block() : Object() {
|
|||
|
||||
// déplacement de l'origine au centre du bloc
|
||||
sprite.setOrigin(sf::Vector2f(23, 23));
|
||||
select_sprite.setOrigin(sf::Vector2f(23, 23));
|
||||
}
|
||||
|
||||
Block::~Block() {}
|
||||
|
@ -31,11 +32,12 @@ ObjectPtr Block::load(std::ifstream& file) {
|
|||
}
|
||||
|
||||
void Block::draw(Manager& manager) {
|
||||
|
||||
// utilisation de la texture
|
||||
sprite.setTexture(
|
||||
manager.getResourceManager().getTexture("block.png")
|
||||
);
|
||||
ResourceManager& resources = manager.getResourceManager();
|
||||
sf::RenderWindow& window = manager.getWindow();
|
||||
|
||||
sprite.setTexture(resources.getTexture("block.png"));
|
||||
select_sprite.setTexture(resources.getTexture("block_select.png"));
|
||||
|
||||
// coloration du bloc en fonction de sa charge
|
||||
if (getCharge() > 0) {
|
||||
|
@ -47,8 +49,13 @@ void Block::draw(Manager& manager) {
|
|||
}
|
||||
|
||||
sprite.setPosition(getPosition());
|
||||
manager.getWindow().draw(sprite);
|
||||
Object::draw(manager);
|
||||
select_sprite.setPosition(getPosition());
|
||||
|
||||
window.draw(sprite);
|
||||
|
||||
if (isSelected()) {
|
||||
window.draw(select_sprite);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<sf::FloatRect> Block::getAABB() const {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include "editor.hpp"
|
||||
#include "game.hpp"
|
||||
#include "block.hpp"
|
||||
|
@ -56,7 +56,7 @@ void Editor::frame() {
|
|||
|
||||
// dessin de la frame
|
||||
draw();
|
||||
sf::sleep(sf::seconds(1.f / 60));
|
||||
sf::sleep(sf::seconds(1.f / 30));
|
||||
}
|
||||
|
||||
void Editor::draw() {
|
||||
|
@ -64,26 +64,6 @@ void Editor::draw() {
|
|||
|
||||
sf::RenderWindow& window = manager.getWindow();
|
||||
sf::View window_view = manager.getWindowView();
|
||||
sf::Color selection_color(255, 50, 41);
|
||||
|
||||
// dessin de la sélection autour des objets sélectionnés
|
||||
for (auto iter = selection.begin(); iter != selection.end(); iter++) {
|
||||
sf::VertexArray selection(sf::LinesStrip, 5);
|
||||
std::unique_ptr<sf::FloatRect> aabb = iter->first->getAABB();
|
||||
|
||||
selection[0].position = sf::Vector2f(aabb->left - .5f, aabb->top - .5f);
|
||||
selection[0].color = selection_color;
|
||||
selection[1].position = sf::Vector2f(aabb->left + aabb->width + .5f, aabb->top - .5f);
|
||||
selection[1].color = selection_color;
|
||||
selection[2].position = sf::Vector2f(aabb->left + aabb->width + .5f, aabb->top + aabb->height + .5f);
|
||||
selection[2].color = selection_color;
|
||||
selection[3].position = sf::Vector2f(aabb->left - .5f, aabb->top + aabb->height + .5f);
|
||||
selection[3].color = selection_color;
|
||||
selection[4].position = sf::Vector2f(aabb->left - .5f, aabb->top - .5f);
|
||||
selection[4].color = selection_color;
|
||||
|
||||
window.draw(selection);
|
||||
}
|
||||
|
||||
// dessin du widget timer
|
||||
widget_timer.setTimeLeft(getTotalTime());
|
||||
|
@ -136,7 +116,10 @@ void Editor::removeObject(sf::Vector2f position) {
|
|||
}
|
||||
|
||||
if (remove_object_index >= 0) {
|
||||
selection.erase(objects[remove_object_index]);
|
||||
selection.erase(std::remove(
|
||||
selection.begin(), selection.end(), objects[remove_object_index]
|
||||
), selection.end());
|
||||
|
||||
objects.erase(objects.begin() + remove_object_index);
|
||||
}
|
||||
}
|
||||
|
@ -152,16 +135,24 @@ bool Editor::updateSelection(sf::Vector2f position) {
|
|||
|
||||
// si l'objet n'est pas sélectionné, on le sélectionne
|
||||
// sinon on le désélectionne
|
||||
if (selection.count(objects[i])) {
|
||||
selection.erase(objects[i]);
|
||||
if (std::count(selection.begin(), selection.end(), objects[i]) > 0) {
|
||||
objects[i]->setSelected(false);
|
||||
selection.erase(std::remove(
|
||||
selection.begin(), selection.end(), objects[i]
|
||||
), selection.end());
|
||||
} else {
|
||||
// avant de sélectionner le nouvel objet, on
|
||||
// vide la sélection si on n'est pas en mode multi
|
||||
if (!multi) {
|
||||
for (unsigned int i = 0; i < selection.size(); i++) {
|
||||
selection[i]->setSelected(false);
|
||||
}
|
||||
|
||||
selection.clear();
|
||||
}
|
||||
|
||||
selection[objects[i]] = true;
|
||||
selection.push_back(objects[i]);
|
||||
objects[i]->setSelected(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ const unsigned int Object::PROP_LAYER = 6;
|
|||
|
||||
Object::Object() :
|
||||
acceleration(0, 0), velocity(0, 0), position(0, 0),
|
||||
inv_mass(-1.f),
|
||||
selected(false), inv_mass(-1.f),
|
||||
|
||||
// valeurs par défaut pour les propriétés
|
||||
// de tous les objets du jeu
|
||||
|
@ -128,8 +128,6 @@ sf::Vector2f Object::getForces(
|
|||
return forces;
|
||||
}
|
||||
|
||||
void Object::draw(Manager& manager) {}
|
||||
|
||||
void Object::updateVelocity(
|
||||
const Manager& manager, const std::vector<ObjectPtr>& objects, float delta
|
||||
) {
|
||||
|
@ -252,6 +250,14 @@ void Object::setPosition(sf::Vector2f set_position) {
|
|||
position = set_position;
|
||||
}
|
||||
|
||||
bool Object::isSelected() const {
|
||||
return selected;
|
||||
}
|
||||
|
||||
void Object::setSelected(bool set_selected) {
|
||||
selected = set_selected;
|
||||
}
|
||||
|
||||
float Object::getMass() const {
|
||||
return mass;
|
||||
}
|
||||
|
|
|
@ -65,17 +65,21 @@ void Player::draw(Manager& manager) {
|
|||
manager.getResourceManager().getTexture("ball.png")
|
||||
);
|
||||
|
||||
// coloration du jooueur en fonction de son numéro
|
||||
if (getPlayerNumber() == 0) {
|
||||
sprite.setColor(sf::Color(239, 83, 80));
|
||||
} else if (getPlayerNumber() == 1) {
|
||||
sprite.setColor(sf::Color(92, 107, 192));
|
||||
// si le joueur est sélectionné, on le colore en rouge vif
|
||||
if (isSelected()) {
|
||||
sprite.setColor(sf::Color(255, 0, 0));
|
||||
} else {
|
||||
// coloration du joueur en fonction de son numéro
|
||||
if (getPlayerNumber() == 0) {
|
||||
sprite.setColor(sf::Color(239, 83, 80));
|
||||
} else if (getPlayerNumber() == 1) {
|
||||
sprite.setColor(sf::Color(92, 107, 192));
|
||||
}
|
||||
}
|
||||
|
||||
// déplacement du sprite à la position de la balle
|
||||
sprite.setPosition(getPosition());
|
||||
manager.getWindow().draw(sprite);
|
||||
Object::draw(manager);
|
||||
}
|
||||
|
||||
void Player::updatePosition(float delta){
|
||||
|
|
Loading…
Reference in New Issue