18 #include "layermanager.h"
23 #include "layersound.h"
24 #include "layerbitmap.h"
25 #include "layervector.h"
26 #include "layercamera.h"
33 LayerManager::~LayerManager()
37 bool LayerManager::init()
44 mLastCameraLayerIdx = 0;
52 o->data()->setCurrentLayer(editor()->currentLayerIndex());
56 Layer* LayerManager::getLastCameraLayer()
58 Layer* layer = object()->getLayer(mLastCameraLayerIdx);
59 if (layer->type() == Layer::CAMERA)
65 std::vector<LayerCamera*> camLayers = object()->getLayersByType<
LayerCamera>();
66 if (camLayers.size() > 0)
73 Layer* LayerManager::currentLayer()
75 Layer* layer = currentLayer(0);
76 Q_ASSERT(layer !=
nullptr);
80 Layer* LayerManager::currentLayer(
int incr)
82 Q_ASSERT(
object() != NULL);
83 return object()->getLayer(editor()->currentLayerIndex() + incr);
86 Layer* LayerManager::getLayer(
int index)
88 Q_ASSERT(
object() != NULL);
89 return object()->getLayer(index);
92 Layer* LayerManager::findLayerByName(
QString sName, Layer::LAYER_TYPE type)
94 return object()->findLayerByName(sName, type);
97 int LayerManager::currentLayerIndex()
99 return editor()->currentLayerIndex();
102 void LayerManager::setCurrentLayer(
int layerIndex)
104 Q_ASSERT(layerIndex >= 0);
107 if (layerIndex >= o->getLayerCount())
115 editor()->setCurrentLayerIndex(layerIndex);
116 emit currentLayerChanged(layerIndex);
120 if (
object()->getLayer(layerIndex)->type() == Layer::CAMERA)
122 mLastCameraLayerIdx = layerIndex;
127 void LayerManager::setCurrentLayer(
Layer* layer)
129 setCurrentLayer(getIndex(layer));
132 void LayerManager::gotoNextLayer()
134 if (editor()->currentLayerIndex() <
object()->getLayerCount() - 1)
136 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() + 1);
137 emit currentLayerChanged(editor()->currentLayerIndex());
141 void LayerManager::gotoPreviouslayer()
143 if (editor()->currentLayerIndex() > 0)
145 editor()->setCurrentLayerIndex(editor()->currentLayerIndex() - 1);
146 emit currentLayerChanged(editor()->currentLayerIndex());
159 for (
int i = 0; i < count(); i++)
161 sLayers.
append(getLayer(i)->name());
171 newName = QStringLiteral(
"%1 %2")
173 }
while (sLayers.
contains(newName));
179 LayerBitmap* layer = object()->addNewBitmapLayer();
180 layer->setName(strLayerName);
182 emit layerCountChanged(count());
183 setCurrentLayer(getLastLayerIndex());
190 LayerVector* layer = object()->addNewVectorLayer();
191 layer->setName(strLayerName);
193 emit layerCountChanged(count());
194 setCurrentLayer(getLastLayerIndex());
201 LayerCamera* layer = object()->addNewCameraLayer();
202 layer->setName(strLayerName);
204 emit layerCountChanged(count());
205 setCurrentLayer(getLastLayerIndex());
212 LayerSound* layer = object()->addNewSoundLayer();
213 layer->setName(strLayerName);
215 emit layerCountChanged(count());
216 setCurrentLayer(getLastLayerIndex());
221 int LayerManager::lastFrameAtFrame(
int frameIndex)
224 for (
int i = frameIndex; i >= 0; i -= 1)
226 for (
int layerIndex = 0; layerIndex < o->getLayerCount(); ++layerIndex)
228 auto pLayer = o->getLayer(layerIndex);
229 if (pLayer->keyExists(i))
238 int LayerManager::firstKeyFrameIndex()
240 int minPosition = INT_MAX;
243 for (
int i = 0; i < o->getLayerCount(); ++i)
245 Layer* pLayer = o->getLayer(i);
247 int position = pLayer->firstKeyFramePosition();
248 if (position < minPosition)
250 minPosition = position;
256 int LayerManager::lastKeyFrameIndex()
260 for (
int i = 0; i < object()->getLayerCount(); ++i)
262 Layer* pLayer = object()->getLayer(i);
264 int position = pLayer->getMaxKeyFramePosition();
265 if (position > maxPosition)
267 maxPosition = position;
273 int LayerManager::count()
275 return object()->getLayerCount();
278 Status LayerManager::deleteLayer(
int index)
280 Layer* layer = object()->getLayer(index);
281 if (layer->type() == Layer::CAMERA)
283 std::vector<LayerCamera*> camLayers = object()->getLayersByType<
LayerCamera>();
284 if (camLayers.size() == 1)
285 return Status::ERROR_NEED_AT_LEAST_ONE_CAMERA_LAYER;
288 object()->deleteLayer(layer);
291 if (index ==
object()->getLayerCount() &&
292 index == currentLayerIndex())
294 setCurrentLayer(currentLayerIndex() - 1);
296 if (index >= currentLayerIndex())
299 setCurrentLayer(currentLayerIndex());
302 emit layerDeleted(index);
303 emit layerCountChanged(count());
310 if (newName.
isEmpty())
return Status::FAIL;
312 layer->setName(newName);
313 currentLayerChanged(getIndex(layer));
317 void LayerManager::notifyLayerChanged(
Layer* layer)
319 emit currentLayerChanged(getIndex(layer));
331 for (
int i = 0; i < o->getLayerCount(); i++)
333 if (o->getLayer(i)->type() == Layer::SOUND)
338 Layer* soundLayer = o->getLayer(i);
339 soundLayer->foreachKeyFrame([&maxFrame](
KeyFrame* keyFrame)
341 int endPosition = keyFrame->pos() + (keyFrame->length() - 1);
342 if (endPosition > maxFrame)
344 maxFrame = endPosition;
350 int lastFramePos = o->getLayer(i)->getMaxKeyFramePosition();
351 if (lastFramePos > maxFrame)
353 maxFrame = lastFramePos;
360 void LayerManager::notifyAnimationLengthChanged()
365 int LayerManager::getIndex(
Layer* layer)
const
367 const Object* o = object();
368 for (
int i = 0; i < o->getLayerCount(); ++i)
370 if (layer == o->getLayer(i))
void append(const T &value)
QString number(int n, int base)
bool contains(const T &value) const const
bool isEmpty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
int animationLength(bool includeSounds=true)
Get the length of current project.