All Classes Namespaces Functions Variables Enumerations Properties Pages
layermanager.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "layermanager.h"
19 
20 #include "object.h"
21 #include "editor.h"
22 
23 #include "layersound.h"
24 #include "layerbitmap.h"
25 #include "layervector.h"
26 #include "layercamera.h"
27 
28 #include <QDebug>
29 
30 LayerManager::LayerManager(Editor* editor) : BaseManager(editor, __FUNCTION__)
31 {
32 }
33 
34 LayerManager::~LayerManager()
35 {
36 }
37 
38 bool LayerManager::init()
39 {
40  return true;
41 }
42 
43 Status LayerManager::load(Object*)
44 {
45  mLastCameraLayerIdx = 0;
46  // Do not emit layerCountChanged here because the editor has not updated to this object yet
47  // Leave that to the caller of this function
48  return Status::OK;
49 }
50 
51 Status LayerManager::save(Object* o)
52 {
53  o->data()->setCurrentLayer(editor()->currentLayerIndex());
54  return Status::OK;
55 }
56 
57 Layer* LayerManager::getLastCameraLayer()
58 {
59  Layer* layer = object()->getLayer(mLastCameraLayerIdx);
60  if (layer->type() == Layer::CAMERA)
61  {
62  return layer;
63  }
64 
65  // it's not a camera layer
66  std::vector<LayerCamera*> camLayers = object()->getLayersByType<LayerCamera>();
67  if (camLayers.size() > 0)
68  {
69  return camLayers[0];
70  }
71  return nullptr;
72 }
73 
74 Layer* LayerManager::currentLayer()
75 {
76  Layer* layer = currentLayer(0);
77  Q_ASSERT(layer != nullptr);
78  return layer;
79 }
80 
81 Layer* LayerManager::currentLayer(int incr)
82 {
83  Q_ASSERT(object() != nullptr);
84  return object()->getLayer(editor()->currentLayerIndex() + incr);
85 }
86 
87 Layer* LayerManager::getLayer(int index)
88 {
89  Q_ASSERT(object() != nullptr);
90  return object()->getLayer(index);
91 }
92 
93 Layer* LayerManager::findLayerByName(QString sName, Layer::LAYER_TYPE type)
94 {
95  return object()->findLayerByName(sName, type);
96 }
97 
98 int LayerManager::currentLayerIndex()
99 {
100  return editor()->currentLayerIndex();
101 }
102 
103 void LayerManager::setCurrentLayer(int layerIndex)
104 {
105  Q_ASSERT(layerIndex >= 0);
106 
107  Object* o = object();
108  if (layerIndex >= o->getLayerCount())
109  {
110  Q_ASSERT(false);
111  return;
112  }
113 
114  // Deselect frames of previous layer.
115  Layer* previousLayer = object()->getLayer(editor()->currentLayerIndex());
116  if (previousLayer != nullptr) {
117  previousLayer->deselectAll();
118  }
119 
120  // Do not check if layer index has changed
121  // because the current layer may have changed either way
122  editor()->setCurrentLayerIndex(layerIndex);
123  emit currentLayerChanged(layerIndex);
124 
125  if (object())
126  {
127  if (object()->getLayer(layerIndex)->type() == Layer::CAMERA)
128  {
129  mLastCameraLayerIdx = layerIndex;
130  }
131  }
132 }
133 
134 void LayerManager::setCurrentLayer(Layer* layer)
135 {
136  setCurrentLayer(getIndex(layer));
137 }
138 
139 void LayerManager::gotoNextLayer()
140 {
141  if (editor()->currentLayerIndex() < object()->getLayerCount() - 1)
142  {
143  currentLayer()->deselectAll();
144  editor()->setCurrentLayerIndex(editor()->currentLayerIndex() + 1);
145  emit currentLayerChanged(editor()->currentLayerIndex());
146  }
147 }
148 
149 void LayerManager::gotoPreviouslayer()
150 {
151  if (editor()->currentLayerIndex() > 0)
152  {
153  currentLayer()->deselectAll();
154  editor()->setCurrentLayerIndex(editor()->currentLayerIndex() - 1);
155  emit currentLayerChanged(editor()->currentLayerIndex());
156  }
157 }
158 
159 QString LayerManager::nameSuggestLayer(const QString& name)
160 {
161  // if no layers: return name
162  if (count() == 0)
163  {
164  return name;
165  }
166  QVector<QString> sLayers;
167  // fill Vector with layer names
168  for (int i = 0; i < count(); i++)
169  {
170  sLayers.append(getLayer(i)->name());
171  }
172  // if name is not in list, return name
173  if (!sLayers.contains(name))
174  {
175  return name;
176  }
177  int newIndex = 2;
178  QString newName = name;
179  do {
180  newName = QStringLiteral("%1 %2")
181  .arg(name).arg(QString::number(newIndex++));
182  } while (sLayers.contains(newName));
183  return newName;
184 }
185 
186 Layer* LayerManager::createLayer(Layer::LAYER_TYPE type, const QString& strLayerName)
187 {
188  Layer* layer = nullptr;
189  switch (type) {
190  case Layer::BITMAP:
191  layer = object()->addNewBitmapLayer();
192  break;
193  case Layer::VECTOR:
194  layer = object()->addNewVectorLayer();
195  break;
196  case Layer::SOUND:
197  layer = object()->addNewSoundLayer();
198  break;
199  case Layer::CAMERA:
200  layer = object()->addNewCameraLayer();
201  break;
202  default:
203  Q_ASSERT(true);
204  return nullptr;
205  }
206 
207  layer->setName(strLayerName);
208  emit layerCountChanged(count());
209  setCurrentLayer(getLastLayerIndex());
210 
211  return layer;
212 }
213 
214 LayerBitmap* LayerManager::createBitmapLayer(const QString& strLayerName)
215 {
216  LayerBitmap* layer = object()->addNewBitmapLayer();
217  layer->setName(strLayerName);
218 
219  emit layerCountChanged(count());
220  setCurrentLayer(getLastLayerIndex());
221 
222  return layer;
223 }
224 
225 LayerVector* LayerManager::createVectorLayer(const QString& strLayerName)
226 {
227  LayerVector* layer = object()->addNewVectorLayer();
228  layer->setName(strLayerName);
229 
230  emit layerCountChanged(count());
231  setCurrentLayer(getLastLayerIndex());
232 
233  return layer;
234 }
235 
236 LayerCamera* LayerManager::createCameraLayer(const QString& strLayerName)
237 {
238  LayerCamera* layer = object()->addNewCameraLayer();
239  layer->setName(strLayerName);
240 
241  emit layerCountChanged(count());
242  setCurrentLayer(getLastLayerIndex());
243 
244  return layer;
245 }
246 
247 LayerSound* LayerManager::createSoundLayer(const QString& strLayerName)
248 {
249  LayerSound* layer = object()->addNewSoundLayer();
250  layer->setName(strLayerName);
251 
252  emit layerCountChanged(count());
253  setCurrentLayer(getLastLayerIndex());
254 
255  return layer;
256 }
257 
258 int LayerManager::lastFrameAtFrame(int frameIndex)
259 {
260  Object* o = object();
261  for (int i = frameIndex; i >= 0; i -= 1)
262  {
263  for (int layerIndex = 0; layerIndex < o->getLayerCount(); ++layerIndex)
264  {
265  auto pLayer = o->getLayer(layerIndex);
266  if (pLayer->keyExists(i))
267  {
268  return i;
269  }
270  }
271  }
272  return -1;
273 }
274 
275 int LayerManager::firstKeyFrameIndex()
276 {
277  int minPosition = INT_MAX;
278 
279  Object* o = object();
280  for (int i = 0; i < o->getLayerCount(); ++i)
281  {
282  Layer* pLayer = o->getLayer(i);
283 
284  int position = pLayer->firstKeyFramePosition();
285  if (position < minPosition)
286  {
287  minPosition = position;
288  }
289  }
290  return minPosition;
291 }
292 
293 int LayerManager::lastKeyFrameIndex()
294 {
295  int maxPosition = 0;
296 
297  for (int i = 0; i < object()->getLayerCount(); ++i)
298  {
299  Layer* pLayer = object()->getLayer(i);
300 
301  int position = pLayer->getMaxKeyFramePosition();
302  if (position > maxPosition)
303  {
304  maxPosition = position;
305  }
306  }
307  return maxPosition;
308 }
309 
310 int LayerManager::count()
311 {
312  return object()->getLayerCount();
313 }
314 
315 Status LayerManager::deleteLayer(int index)
316 {
317  Layer* layer = object()->getLayer(index);
318  if (layer->type() == Layer::CAMERA)
319  {
320  std::vector<LayerCamera*> camLayers = object()->getLayersByType<LayerCamera>();
321  if (camLayers.size() == 1)
322  return Status::ERROR_NEED_AT_LEAST_ONE_CAMERA_LAYER;
323  }
324 
325  object()->deleteLayer(layer);
326 
327  // current layer is the last layer && we are deleting it
328  if (index == object()->getLayerCount() &&
329  index == currentLayerIndex())
330  {
331  setCurrentLayer(currentLayerIndex() - 1);
332  }
333  if (index >= currentLayerIndex())
334  {
335  // current layer has changed, so trigger updates
336  setCurrentLayer(currentLayerIndex());
337  }
338 
339  emit layerDeleted(index);
340  emit layerCountChanged(count());
341 
342  return Status::OK;
343 }
344 
345 Status LayerManager::renameLayer(Layer* layer, const QString& newName)
346 {
347  if (newName.isEmpty()) return Status::FAIL;
348 
349  layer->setName(newName);
350  currentLayerChanged(getIndex(layer));
351  return Status::OK;
352 }
353 
354 void LayerManager::notifyLayerChanged(Layer* layer)
355 {
356  emit currentLayerChanged(getIndex(layer));
357 }
358 
363 int LayerManager::animationLength(bool includeSounds)
364 {
365  int maxFrame = -1;
366 
367  Object* o = object();
368  for (int i = 0; i < o->getLayerCount(); i++)
369  {
370  if (o->getLayer(i)->type() == Layer::SOUND)
371  {
372  if (!includeSounds)
373  continue;
374 
375  Layer* soundLayer = o->getLayer(i);
376  soundLayer->foreachKeyFrame([&maxFrame](KeyFrame* keyFrame)
377  {
378  int endPosition = keyFrame->pos() + (keyFrame->length() - 1);
379  if (endPosition > maxFrame)
380  {
381  maxFrame = endPosition;
382  }
383  });
384  }
385  else
386  {
387  int lastFramePos = o->getLayer(i)->getMaxKeyFramePosition();
388  if (lastFramePos > maxFrame)
389  {
390  maxFrame = lastFramePos;
391  }
392  }
393  }
394  return maxFrame;
395 }
396 
398 {
399  emit animationLengthChanged(animationLength(true));
400 }
401 
402 int LayerManager::getIndex(Layer* layer) const
403 {
404  const Object* o = object();
405  for (int i = 0; i < o->getLayerCount(); ++i)
406  {
407  if (layer == o->getLayer(i))
408  return i;
409  }
410  return -1;
411 }
void append(const T &value)
QString number(int n, int base)
bool contains(const T &value) const const
bool isEmpty() const const
Definition: layer.h:38
Layer * createLayer(Layer::LAYER_TYPE type, const QString &strLayerName)
Returns a new Layer with the given LAYER_TYPE.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Definition: object.h:41
Definition: editor.h:55
int animationLength(bool includeSounds=true)
Get the length of current project.
void notifyAnimationLengthChanged()
This should be emitted whenever the animation length frames, eg.