18#include "layermanager.h"
23#include "layersound.h"
24#include "layerbitmap.h"
25#include "layervector.h"
26#include "layercamera.h"
34LayerManager::~LayerManager()
38bool LayerManager::init()
45 mLastCameraLayerIdx = 0;
53 o->data()->setCurrentLayer(editor()->currentLayerIndex());
57LayerCamera* LayerManager::getCameraLayerBelow(
int layerIndex)
const
59 return static_cast<LayerCamera*
>(object()->getLayerBelow(layerIndex, Layer::CAMERA));
62Layer* LayerManager::getLastCameraLayer()
64 Layer* layer = object()->getLayer(mLastCameraLayerIdx);
65 if (layer->type() == Layer::CAMERA)
71 std::vector<LayerCamera*> camLayers = object()->getLayersByType<
LayerCamera>();
72 if (camLayers.size() > 0)
79Layer* LayerManager::currentLayer()
81 Layer* layer = currentLayer(0);
82 Q_ASSERT(layer !=
nullptr);
86Layer* LayerManager::currentLayer(
int incr)
88 Q_ASSERT(
object() !=
nullptr);
89 return object()->getLayer(editor()->currentLayerIndex() + incr);
92Layer* LayerManager::getLayer(
int index)
94 Q_ASSERT(
object() !=
nullptr);
95 return object()->getLayer(index);
98Layer* LayerManager::findLayerByName(
QString sName, Layer::LAYER_TYPE type)
100 return object()->findLayerByName(sName, type);
103Layer* LayerManager::findLayerById(
int layerId)
105 return object()->findLayerById(layerId);
108int LayerManager::currentLayerIndex()
110 return editor()->currentLayerIndex();
113void LayerManager::setCurrentLayer(
int layerIndex)
115 Q_ASSERT(layerIndex >= 0);
116 Q_ASSERT(layerIndex <
object()->getLayerCount());
119 Layer* previousLayer = currentLayer();
120 previousLayer->deselectAll();
122 emit currentLayerWillChange(layerIndex);
126 editor()->setCurrentLayerIndex(layerIndex);
127 emit currentLayerChanged(layerIndex);
129 if (
object()->getLayer(layerIndex)->type() == Layer::CAMERA)
131 mLastCameraLayerIdx = layerIndex;
135void LayerManager::setCurrentLayer(
Layer* layer)
137 setCurrentLayer(getIndex(layer));
140void LayerManager::gotoNextLayer()
142 if (editor()->currentLayerIndex() <
object()->getLayerCount() - 1)
144 currentLayer()->deselectAll();
145 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() + 1);
146 emit currentLayerChanged(editor()->currentLayerIndex());
150void LayerManager::gotoPreviouslayer()
152 if (editor()->currentLayerIndex() > 0)
154 currentLayer()->deselectAll();
155 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() - 1);
156 emit currentLayerChanged(editor()->currentLayerIndex());
169 for (
int i = 0; i < count(); i++)
171 sLayers.
append(getLayer(i)->name());
181 newName = QStringLiteral(
"%1 %2")
183 }
while (sLayers.
contains(newName));
189 Layer* layer =
nullptr;
192 layer = object()->addNewBitmapLayer();
195 layer = object()->addNewVectorLayer();
198 layer = object()->addNewSoundLayer();
201 layer = object()->addNewCameraLayer();
208 layer->setName(strLayerName);
209 emit layerCountChanged(count());
210 setCurrentLayer(getLastLayerIndex());
217 LayerBitmap* layer = object()->addNewBitmapLayer();
218 layer->setName(strLayerName);
220 emit layerCountChanged(count());
221 setCurrentLayer(getLastLayerIndex());
228 LayerVector* layer = object()->addNewVectorLayer();
229 layer->setName(strLayerName);
231 emit layerCountChanged(count());
232 setCurrentLayer(getLastLayerIndex());
239 LayerCamera* layer = object()->addNewCameraLayer();
240 layer->setName(strLayerName);
242 emit layerCountChanged(count());
243 setCurrentLayer(getLastLayerIndex());
250 LayerSound* layer = object()->addNewSoundLayer();
251 layer->setName(strLayerName);
253 emit layerCountChanged(count());
254 setCurrentLayer(getLastLayerIndex());
259int LayerManager::lastFrameAtFrame(
int frameIndex)
262 for (
int i = frameIndex; i >= 0; i -= 1)
264 for (
int layerIndex = 0; layerIndex < o->getLayerCount(); ++layerIndex)
266 auto pLayer = o->getLayer(layerIndex);
267 if (pLayer->keyExists(i))
276int LayerManager::firstKeyFrameIndex()
278 int minPosition = INT_MAX;
281 for (
int i = 0; i < o->getLayerCount(); ++i)
283 Layer* pLayer = o->getLayer(i);
285 int position = pLayer->firstKeyFramePosition();
286 if (position < minPosition)
288 minPosition = position;
294int LayerManager::lastKeyFrameIndex()
298 for (
int i = 0; i < object()->getLayerCount(); ++i)
300 Layer* pLayer = object()->getLayer(i);
302 int position = pLayer->getMaxKeyFramePosition();
303 if (position > maxPosition)
305 maxPosition = position;
311int LayerManager::count()
313 return object()->getLayerCount();
316bool LayerManager::canDeleteLayer(
int index)
const
321Status LayerManager::deleteLayer(
int index)
323 Layer* layer = object()->getLayer(index);
324 if (layer->type() == Layer::CAMERA)
326 std::vector<LayerCamera*> camLayers = object()->getLayersByType<
LayerCamera>();
327 if (camLayers.size() == 1)
328 return Status::ERROR_NEED_AT_LEAST_ONE_CAMERA_LAYER;
330 Q_ASSERT(
object()->getLayerCount() >= 2);
333 if (index ==
object()->getLayerCount() - 1 &&
334 index == currentLayerIndex())
336 setCurrentLayer(currentLayerIndex() - 1);
338 object()->deleteLayer(layer);
339 if (index >= currentLayerIndex())
342 setCurrentLayer(currentLayerIndex());
345 emit layerDeleted(index);
346 emit layerCountChanged(count());
353 if (newName.
isEmpty())
return Status::FAIL;
355 layer->setName(newName);
356 emit currentLayerChanged(getIndex(layer));
360void LayerManager::notifyLayerChanged(
Layer* layer)
362 emit currentLayerChanged(getIndex(layer));
374 for (
int i = 0; i < o->getLayerCount(); i++)
376 if (o->getLayer(i)->type() == Layer::SOUND)
381 Layer* soundLayer = o->getLayer(i);
382 soundLayer->foreachKeyFrame([&maxFrame](
KeyFrame* keyFrame)
384 int endPosition = keyFrame->pos() + (keyFrame->length() - 1);
385 if (endPosition > maxFrame)
387 maxFrame = endPosition;
393 int lastFramePos = o->getLayer(i)->getMaxKeyFramePosition();
394 if (lastFramePos > maxFrame)
396 maxFrame = lastFramePos;
408int LayerManager::getIndex(
Layer* layer)
const
410 const Object* o = object();
411 for (
int i = 0; i < o->getLayerCount(); ++i)
413 if (layer == o->getLayer(i))
int animationLength(bool includeSounds=true)
Get the length of current project.
Layer * createLayer(Layer::LAYER_TYPE type, const QString &strLayerName)
Returns a new Layer with the given LAYER_TYPE.
void notifyAnimationLengthChanged()
This should be emitted whenever the animation length frames, eg.
bool canDeleteLayer(int index) const
Allows you to check whether the layer at the given index can be deleted.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
QString number(int n, int base)
void append(const T &value)
bool contains(const T &value) const const