1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

Use QObject signals & slots for Layers

This commit is contained in:
Tomasz Zieliński
2022-09-06 13:20:11 +02:00
parent b826bc00b9
commit dad4520b18
3 changed files with 36 additions and 24 deletions

View File

@ -363,6 +363,8 @@ MapScene::MapScene(int lvl):
isTerrainSelected(false),
isObjectSelected(false)
{
connect(&selectionTerrainView, &SelectionTerrainLayer::selectionMade, this, &MapScene::terrainSelected);
connect(&selectionObjectsView, &SelectionObjectsLayer::selectionMade, this, &MapScene::objectSelected);
}
std::list<AbstractLayer *> MapScene::getAbstractLayers()

View File

@ -101,7 +101,7 @@ void PassabilityLayer::update()
redraw();
}
SelectionTerrainLayer::SelectionTerrainLayer(MapScene * s): AbstractLayer(s)
SelectionTerrainLayer::SelectionTerrainLayer(MapSceneBase * s): AbstractLayer(s)
{
}
@ -113,7 +113,7 @@ void SelectionTerrainLayer::update()
area.clear();
areaAdd.clear();
areaErase.clear();
selectionMade();
onSelection();
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
pixmap->fill(QColor(0, 0, 0, 0));
@ -154,7 +154,7 @@ void SelectionTerrainLayer::select(const int3 & tile)
areaAdd.insert(tile);
areaErase.erase(tile);
}
selectionMade();
onSelection();
}
void SelectionTerrainLayer::erase(const int3 & tile)
@ -168,7 +168,7 @@ void SelectionTerrainLayer::erase(const int3 & tile)
areaErase.insert(tile);
areaAdd.erase(tile);
}
selectionMade();
onSelection();
}
void SelectionTerrainLayer::clear()
@ -176,7 +176,7 @@ void SelectionTerrainLayer::clear()
areaErase = area;
areaAdd.clear();
area.clear();
selectionMade();
onSelection();
}
const std::set<int3> & SelectionTerrainLayer::selection() const
@ -184,9 +184,9 @@ const std::set<int3> & SelectionTerrainLayer::selection() const
return area;
}
void SelectionTerrainLayer::selectionMade()
void SelectionTerrainLayer::onSelection()
{
dynamic_cast<MapScene*>(scene)->objectSelected(!area.empty());
emit selectionMade(!area.empty());
}
@ -332,7 +332,7 @@ void ObjectsLayer::setDirty(const CGObjectInstance * object)
dirty.insert(object);
}
SelectionObjectsLayer::SelectionObjectsLayer(MapScene * s): AbstractLayer(s), newObject(nullptr)
SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
{
}
@ -342,7 +342,7 @@ void SelectionObjectsLayer::update()
return;
selectedObjects.clear();
selectionMade();
onSelection();
shift = QPoint();
if(newObject)
delete newObject;
@ -459,7 +459,7 @@ void SelectionObjectsLayer::selectObjects(int x1, int y1, int x2, int y2)
selectObject(o.obj, false); //do not inform about each object added
}
}
selectionMade();
onSelection();
}
void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /* = true */)
@ -467,7 +467,7 @@ void SelectionObjectsLayer::selectObject(CGObjectInstance * obj, bool inform /*
selectedObjects.insert(obj);
if (inform)
{
selectionMade();
onSelection();
}
}
@ -484,15 +484,14 @@ std::set<CGObjectInstance*> SelectionObjectsLayer::getSelection() const
void SelectionObjectsLayer::clear()
{
selectedObjects.clear();
selectionMade();
onSelection();
shift.setX(0);
shift.setY(0);
}
void SelectionObjectsLayer::selectionMade()
void SelectionObjectsLayer::onSelection()
{
dynamic_cast<MapScene*>(scene)->objectSelected(!selectedObjects.empty());
emit selectionMade(!selectedObjects.empty());
}
MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)

View File

@ -10,8 +10,9 @@ class MapController;
class CMap;
class MapHandler;
class AbstractLayer
class AbstractLayer : public QObject
{
Q_OBJECT
public:
AbstractLayer(MapSceneBase * s);
@ -37,6 +38,7 @@ private:
class GridLayer: public AbstractLayer
{
Q_OBJECT
public:
GridLayer(MapSceneBase * s);
@ -45,18 +47,18 @@ public:
class PassabilityLayer: public AbstractLayer
{
Q_OBJECT
public:
PassabilityLayer(MapSceneBase * s);
void update() override;
};
class SelectionTerrainLayer: public AbstractLayer
{
Q_OBJECT
public:
//FIXME: now it depends on MapScene::selected, this is getting messy
SelectionTerrainLayer(MapScene * s);
SelectionTerrainLayer(MapSceneBase* s);
void update() override;
@ -66,16 +68,20 @@ public:
void clear();
const std::set<int3> & selection() const;
signals:
void selectionMade(bool anythingSlected);
private:
std::set<int3> area, areaAdd, areaErase;
void selectionMade(); //TODO: consider making it a signal
void onSelection();
};
class TerrainLayer: public AbstractLayer
{
Q_OBJECT
public:
TerrainLayer(MapSceneBase * s);
@ -91,6 +97,7 @@ private:
class ObjectsLayer: public AbstractLayer
{
Q_OBJECT
public:
ObjectsLayer(MapSceneBase * s);
@ -108,9 +115,9 @@ private:
class SelectionObjectsLayer: public AbstractLayer
{
Q_OBJECT
public:
//FIXME: now it depends on MapScene::selected, this is getting messy
SelectionObjectsLayer(MapScene * s);
SelectionObjectsLayer(MapSceneBase* s);
void update() override;
@ -126,12 +133,16 @@ public:
QPoint shift;
CGObjectInstance * newObject;
//FIXME: magic number
int selectionMode = 0; //0 - nothing, 1 - selection, 2 - movement
signals:
void selectionMade(bool anythingSlected);
private:
std::set<CGObjectInstance *> selectedObjects;
void selectionMade(); //TODO: consider making it a signal
void onSelection();
};
class MinimapLayer: public AbstractLayer