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);
103int LayerManager::currentLayerIndex()
105 return editor()->currentLayerIndex();
108void LayerManager::setCurrentLayer(
int layerIndex)
110 Q_ASSERT(layerIndex >= 0);
111 Q_ASSERT(layerIndex <
object()->getLayerCount());
114 Layer* previousLayer = currentLayer();
115 previousLayer->deselectAll();
117 emit currentLayerWillChange(layerIndex);
121 editor()->setCurrentLayerIndex(layerIndex);
122 emit currentLayerChanged(layerIndex);
124 if (
object()->getLayer(layerIndex)->type() == Layer::CAMERA)
126 mLastCameraLayerIdx = layerIndex;
130void LayerManager::setCurrentLayer(
Layer* layer)
132 setCurrentLayer(getIndex(layer));
135void LayerManager::gotoNextLayer()
137 if (editor()->currentLayerIndex() <
object()->getLayerCount() - 1)
139 currentLayer()->deselectAll();
140 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() + 1);
141 emit currentLayerChanged(editor()->currentLayerIndex());
145void LayerManager::gotoPreviouslayer()
147 if (editor()->currentLayerIndex() > 0)
149 currentLayer()->deselectAll();
150 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() - 1);
151 emit currentLayerChanged(editor()->currentLayerIndex());
164 for (
int i = 0; i < count(); i++)
166 sLayers.
append(getLayer(i)->name());
176 newName = QStringLiteral(
"%1 %2")
178 }
while (sLayers.
contains(newName));
184 Layer* layer =
nullptr;
187 layer = object()->addNewBitmapLayer();
190 layer = object()->addNewVectorLayer();
193 layer = object()->addNewSoundLayer();
196 layer = object()->addNewCameraLayer();
203 layer->setName(strLayerName);
204 emit layerCountChanged(count());
205 setCurrentLayer(getLastLayerIndex());
212 LayerBitmap* layer = object()->addNewBitmapLayer();
213 layer->setName(strLayerName);
215 emit layerCountChanged(count());
216 setCurrentLayer(getLastLayerIndex());
223 LayerVector* layer = object()->addNewVectorLayer();
224 layer->setName(strLayerName);
226 emit layerCountChanged(count());
227 setCurrentLayer(getLastLayerIndex());
234 LayerCamera* layer = object()->addNewCameraLayer();
235 layer->setName(strLayerName);
237 emit layerCountChanged(count());
238 setCurrentLayer(getLastLayerIndex());
245 LayerSound* layer = object()->addNewSoundLayer();
246 layer->setName(strLayerName);
248 emit layerCountChanged(count());
249 setCurrentLayer(getLastLayerIndex());
254int LayerManager::lastFrameAtFrame(
int frameIndex)
257 for (
int i = frameIndex; i >= 0; i -= 1)
259 for (
int layerIndex = 0; layerIndex < o->getLayerCount(); ++layerIndex)
261 auto pLayer = o->getLayer(layerIndex);
262 if (pLayer->keyExists(i))
271int LayerManager::firstKeyFrameIndex()
273 int minPosition = INT_MAX;
276 for (
int i = 0; i < o->getLayerCount(); ++i)
278 Layer* pLayer = o->getLayer(i);
280 int position = pLayer->firstKeyFramePosition();
281 if (position < minPosition)
283 minPosition = position;
289int LayerManager::lastKeyFrameIndex()
293 for (
int i = 0; i < object()->getLayerCount(); ++i)
295 Layer* pLayer = object()->getLayer(i);
297 int position = pLayer->getMaxKeyFramePosition();
298 if (position > maxPosition)
300 maxPosition = position;
306int LayerManager::count()
308 return object()->getLayerCount();
311bool LayerManager::canDeleteLayer(
int index)
const
316Status LayerManager::deleteLayer(
int index)
318 Layer* layer = object()->getLayer(index);
319 if (layer->type() == Layer::CAMERA)
321 std::vector<LayerCamera*> camLayers = object()->getLayersByType<
LayerCamera>();
322 if (camLayers.size() == 1)
323 return Status::ERROR_NEED_AT_LEAST_ONE_CAMERA_LAYER;
325 Q_ASSERT(
object()->getLayerCount() >= 2);
328 if (index ==
object()->getLayerCount() - 1 &&
329 index == currentLayerIndex())
331 setCurrentLayer(currentLayerIndex() - 1);
333 object()->deleteLayer(layer);
334 if (index >= currentLayerIndex())
337 setCurrentLayer(currentLayerIndex());
340 emit layerDeleted(index);
341 emit layerCountChanged(count());
348 if (newName.
isEmpty())
return Status::FAIL;
350 layer->setName(newName);
351 currentLayerChanged(getIndex(layer));
355void LayerManager::notifyLayerChanged(
Layer* layer)
357 emit currentLayerChanged(getIndex(layer));
369 for (
int i = 0; i < o->getLayerCount(); i++)
371 if (o->getLayer(i)->type() == Layer::SOUND)
376 Layer* soundLayer = o->getLayer(i);
377 soundLayer->foreachKeyFrame([&maxFrame](
KeyFrame* keyFrame)
379 int endPosition = keyFrame->pos() + (keyFrame->length() - 1);
380 if (endPosition > maxFrame)
382 maxFrame = endPosition;
388 int lastFramePos = o->getLayer(i)->getMaxKeyFramePosition();
389 if (lastFramePos > maxFrame)
391 maxFrame = lastFramePos;
403int LayerManager::getIndex(
Layer* layer)
const
405 const Object* o = object();
406 for (
int i = 0; i < o->getLayerCount(); ++i)
408 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