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 if (previousLayer !=
object()->getLayer(layerIndex)) {
121 previousLayer->deselectAll();
124 emit currentLayerWillChange(layerIndex);
128 editor()->setCurrentLayerIndex(layerIndex);
129 emit currentLayerChanged(layerIndex);
131 if (
object()->getLayer(layerIndex)->type() == Layer::CAMERA)
133 mLastCameraLayerIdx = layerIndex;
137void LayerManager::setCurrentLayer(
Layer* layer)
139 setCurrentLayer(getIndex(layer));
142void LayerManager::gotoNextLayer()
144 if (editor()->currentLayerIndex() <
object()->getLayerCount() - 1)
146 currentLayer()->deselectAll();
147 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() + 1);
148 emit currentLayerChanged(editor()->currentLayerIndex());
152void LayerManager::gotoPreviouslayer()
154 if (editor()->currentLayerIndex() > 0)
156 currentLayer()->deselectAll();
157 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() - 1);
158 emit currentLayerChanged(editor()->currentLayerIndex());
171 for (
int i = 0; i < count(); i++)
173 sLayers.
append(getLayer(i)->name());
183 newName = QStringLiteral(
"%1 %2")
185 }
while (sLayers.
contains(newName));
191 Layer* layer =
nullptr;
194 layer = object()->addNewBitmapLayer();
197 layer = object()->addNewVectorLayer();
200 layer = object()->addNewSoundLayer();
203 layer = object()->addNewCameraLayer();
210 layer->setName(strLayerName);
211 emit layerCountChanged(count());
212 setCurrentLayer(getLastLayerIndex());
219 LayerBitmap* layer = object()->addNewBitmapLayer();
220 layer->setName(strLayerName);
222 emit layerCountChanged(count());
223 setCurrentLayer(getLastLayerIndex());
230 LayerVector* layer = object()->addNewVectorLayer();
231 layer->setName(strLayerName);
233 emit layerCountChanged(count());
234 setCurrentLayer(getLastLayerIndex());
241 LayerCamera* layer = object()->addNewCameraLayer();
242 layer->setName(strLayerName);
244 emit layerCountChanged(count());
245 setCurrentLayer(getLastLayerIndex());
252 LayerSound* layer = object()->addNewSoundLayer();
253 layer->setName(strLayerName);
255 emit layerCountChanged(count());
256 setCurrentLayer(getLastLayerIndex());
261int LayerManager::lastFrameAtFrame(
int frameIndex)
264 for (
int i = frameIndex; i >= 0; i -= 1)
266 for (
int layerIndex = 0; layerIndex < o->getLayerCount(); ++layerIndex)
268 auto pLayer = o->getLayer(layerIndex);
269 if (pLayer->keyExists(i))
278int LayerManager::firstKeyFrameIndex()
280 int minPosition = INT_MAX;
283 for (
int i = 0; i < o->getLayerCount(); ++i)
285 Layer* pLayer = o->getLayer(i);
287 int position = pLayer->firstKeyFramePosition();
288 if (position < minPosition)
290 minPosition = position;
296int LayerManager::lastKeyFrameIndex()
300 for (
int i = 0; i < object()->getLayerCount(); ++i)
302 Layer* pLayer = object()->getLayer(i);
304 int position = pLayer->getMaxKeyFramePosition();
305 if (position > maxPosition)
307 maxPosition = position;
313int LayerManager::count()
315 return object()->getLayerCount();
318bool LayerManager::canDeleteLayer(
int index)
const
323Status LayerManager::deleteLayer(
int index)
325 Layer* layer = object()->getLayer(index);
326 if (layer->type() == Layer::CAMERA)
328 std::vector<LayerCamera*> camLayers = object()->getLayersByType<
LayerCamera>();
329 if (camLayers.size() == 1)
330 return Status::ERROR_NEED_AT_LEAST_ONE_CAMERA_LAYER;
332 Q_ASSERT(
object()->getLayerCount() >= 2);
335 if (index ==
object()->getLayerCount() - 1 &&
336 index == currentLayerIndex())
338 setCurrentLayer(currentLayerIndex() - 1);
340 object()->deleteLayer(layer);
341 if (index >= currentLayerIndex())
344 setCurrentLayer(currentLayerIndex());
347 emit layerDeleted(index);
348 emit layerCountChanged(count());
355 if (newName.
isEmpty())
return Status::FAIL;
357 layer->setName(newName);
358 emit currentLayerChanged(getIndex(layer));
362void LayerManager::notifyLayerChanged(
Layer* layer)
364 emit currentLayerChanged(getIndex(layer));
376 for (
int i = 0; i < o->getLayerCount(); i++)
378 if (o->getLayer(i)->type() == Layer::SOUND)
383 Layer* soundLayer = o->getLayer(i);
384 soundLayer->foreachKeyFrame([&maxFrame](
KeyFrame* keyFrame)
386 int endPosition = keyFrame->pos() + (keyFrame->length() - 1);
387 if (endPosition > maxFrame)
389 maxFrame = endPosition;
395 int lastFramePos = o->getLayer(i)->getMaxKeyFramePosition();
396 if (lastFramePos > maxFrame)
398 maxFrame = lastFramePos;
410int LayerManager::getIndex(
Layer* layer)
const
412 const Object* o = object();
413 for (
int i = 0; i < o->getLayerCount(); ++i)
415 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