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 
29 LayerManager::LayerManager(Editor* editor) : BaseManager(editor)
30 {
31 }
32 
33 LayerManager::~LayerManager()
34 {
35 }
36 
37 bool LayerManager::init()
38 {
39  return true;
40 }
41 
42 Status LayerManager::load(Object*)
43 {
44  mLastCameraLayerIdx = 0;
45  // Do not emit layerCountChanged here because the editor has not updated to this object yet
46  // Leave that to the caller of this function
47  return Status::OK;
48 }
49 
50 Status LayerManager::save(Object* o)
51 {
52  o->data()->setCurrentLayer(editor()->currentLayerIndex());
53  return Status::OK;
54 }
55 
56 Layer* LayerManager::getLastCameraLayer()
57 {
58  Layer* layer = object()->getLayer(mLastCameraLayerIdx);
59  if (layer->type() == Layer::CAMERA)
60  {
61  return layer;
62  }
63 
64  // it's not a camera layer
65  std::vector<LayerCamera*> camLayers = object()->getLayersByType<LayerCamera>();
66  if (camLayers.size() > 0)
67  {
68  return camLayers[0];
69  }
70  return nullptr;
71 }
72 
73 Layer* LayerManager::currentLayer()
74 {
75  Layer* layer = currentLayer(0);
76  Q_ASSERT(layer != nullptr);
77  return layer;
78 }
79 
80 Layer* LayerManager::currentLayer(int incr)
81 {
82  Q_ASSERT(object() != NULL);
83  return object()->getLayer(editor()->currentLayerIndex() + incr);
84 }
85 
86 Layer* LayerManager::getLayer(int index)
87 {
88  Q_ASSERT(object() != NULL);
89  return object()->getLayer(index);
90 }
91 
92 Layer* LayerManager::findLayerByName(QString sName, Layer::LAYER_TYPE type)
93 {
94  return object()->findLayerByName(sName, type);
95 }
96 
97 int LayerManager::currentLayerIndex()
98 {
99  return editor()->currentLayerIndex();
100 }
101 
102 void LayerManager::setCurrentLayer(int layerIndex)
103 {
104  Q_ASSERT(layerIndex >= 0);
105 
106  Object* o = object();
107  if (layerIndex >= o->getLayerCount())
108  {
109  Q_ASSERT(false);
110  return;
111  }
112 
113  // Do not check if layer index has changed
114  // because the current layer may have changed either way
115  editor()->setCurrentLayerIndex(layerIndex);
116  emit currentLayerChanged(layerIndex);
117 
118  if (object())
119  {
120  if (object()->getLayer(layerIndex)->type() == Layer::CAMERA)
121  {
122  mLastCameraLayerIdx = layerIndex;
123  }
124  }
125 }
126 
127 void LayerManager::setCurrentLayer(Layer* layer)
128 {
129  setCurrentLayer(getIndex(layer));
130 }
131 
132 void LayerManager::gotoNextLayer()
133 {
134  if (editor()->currentLayerIndex() < object()->getLayerCount() - 1)
135  {
136  editor()->setCurrentLayerIndex(editor()->currentLayerIndex() + 1);
137  emit currentLayerChanged(editor()->currentLayerIndex());
138  }
139 }
140 
141 void LayerManager::gotoPreviouslayer()
142 {
143  if (editor()->currentLayerIndex() > 0)
144  {
145  editor()->setCurrentLayerIndex(editor()->currentLayerIndex() - 1);
146  emit currentLayerChanged(editor()->currentLayerIndex());
147  }
148 }
149 
150 QString LayerManager::nameSuggestLayer(const QString& name)
151 {
152  // if no layers: return name
153  if (count() == 0)
154  {
155  return name;
156  }
157  QVector<QString> sLayers;
158  // fill Vector with layer names
159  for (int i = 0; i < count(); i++)
160  {
161  sLayers.append(getLayer(i)->name());
162  }
163  // if name is not in list, return name
164  if (!sLayers.contains(name))
165  {
166  return name;
167  }
168  int newIndex = 2;
169  QString newName = name;
170  do {
171  newName = QStringLiteral("%1 %2")
172  .arg(name).arg(QString::number(newIndex++));
173  } while (sLayers.contains(newName));
174  return newName;
175 }
176 
177 LayerBitmap* LayerManager::createBitmapLayer(const QString& strLayerName)
178 {
179  LayerBitmap* layer = object()->addNewBitmapLayer();
180  layer->setName(strLayerName);
181 
182  emit layerCountChanged(count());
183  setCurrentLayer(getLastLayerIndex());
184 
185  return layer;
186 }
187 
188 LayerVector* LayerManager::createVectorLayer(const QString& strLayerName)
189 {
190  LayerVector* layer = object()->addNewVectorLayer();
191  layer->setName(strLayerName);
192 
193  emit layerCountChanged(count());
194  setCurrentLayer(getLastLayerIndex());
195 
196  return layer;
197 }
198 
199 LayerCamera* LayerManager::createCameraLayer(const QString& strLayerName)
200 {
201  LayerCamera* layer = object()->addNewCameraLayer();
202  layer->setName(strLayerName);
203 
204  emit layerCountChanged(count());
205  setCurrentLayer(getLastLayerIndex());
206 
207  return layer;
208 }
209 
210 LayerSound* LayerManager::createSoundLayer(const QString& strLayerName)
211 {
212  LayerSound* layer = object()->addNewSoundLayer();
213  layer->setName(strLayerName);
214 
215  emit layerCountChanged(count());
216  setCurrentLayer(getLastLayerIndex());
217 
218  return layer;
219 }
220 
221 int LayerManager::lastFrameAtFrame(int frameIndex)
222 {
223  Object* o = object();
224  for (int i = frameIndex; i >= 0; i -= 1)
225  {
226  for (int layerIndex = 0; layerIndex < o->getLayerCount(); ++layerIndex)
227  {
228  auto pLayer = o->getLayer(layerIndex);
229  if (pLayer->keyExists(i))
230  {
231  return i;
232  }
233  }
234  }
235  return -1;
236 }
237 
238 int LayerManager::firstKeyFrameIndex()
239 {
240  int minPosition = INT_MAX;
241 
242  Object* o = object();
243  for (int i = 0; i < o->getLayerCount(); ++i)
244  {
245  Layer* pLayer = o->getLayer(i);
246 
247  int position = pLayer->firstKeyFramePosition();
248  if (position < minPosition)
249  {
250  minPosition = position;
251  }
252  }
253  return minPosition;
254 }
255 
256 int LayerManager::lastKeyFrameIndex()
257 {
258  int maxPosition = 0;
259 
260  for (int i = 0; i < object()->getLayerCount(); ++i)
261  {
262  Layer* pLayer = object()->getLayer(i);
263 
264  int position = pLayer->getMaxKeyFramePosition();
265  if (position > maxPosition)
266  {
267  maxPosition = position;
268  }
269  }
270  return maxPosition;
271 }
272 
273 int LayerManager::count()
274 {
275  return object()->getLayerCount();
276 }
277 
278 Status LayerManager::deleteLayer(int index)
279 {
280  Layer* layer = object()->getLayer(index);
281  if (layer->type() == Layer::CAMERA)
282  {
283  std::vector<LayerCamera*> camLayers = object()->getLayersByType<LayerCamera>();
284  if (camLayers.size() == 1)
285  return Status::ERROR_NEED_AT_LEAST_ONE_CAMERA_LAYER;
286  }
287 
288  object()->deleteLayer(layer);
289 
290  // current layer is the last layer && we are deleting it
291  if (index == object()->getLayerCount() &&
292  index == currentLayerIndex())
293  {
294  setCurrentLayer(currentLayerIndex() - 1);
295  }
296  if (index >= currentLayerIndex())
297  {
298  // current layer has changed, so trigger updates
299  setCurrentLayer(currentLayerIndex());
300  }
301 
302  emit layerDeleted(index);
303  emit layerCountChanged(count());
304 
305  return Status::OK;
306 }
307 
308 Status LayerManager::renameLayer(Layer* layer, const QString& newName)
309 {
310  if (newName.isEmpty()) return Status::FAIL;
311 
312  layer->setName(newName);
313  currentLayerChanged(getIndex(layer));
314  return Status::OK;
315 }
316 
317 void LayerManager::notifyLayerChanged(Layer* layer)
318 {
319  emit currentLayerChanged(getIndex(layer));
320 }
321 
326 int LayerManager::animationLength(bool includeSounds)
327 {
328  int maxFrame = -1;
329 
330  Object* o = object();
331  for (int i = 0; i < o->getLayerCount(); i++)
332  {
333  if (o->getLayer(i)->type() == Layer::SOUND)
334  {
335  if (!includeSounds)
336  continue;
337 
338  Layer* soundLayer = o->getLayer(i);
339  soundLayer->foreachKeyFrame([&maxFrame](KeyFrame* keyFrame)
340  {
341  int endPosition = keyFrame->pos() + (keyFrame->length() - 1);
342  if (endPosition > maxFrame)
343  {
344  maxFrame = endPosition;
345  }
346  });
347  }
348  else
349  {
350  int lastFramePos = o->getLayer(i)->getMaxKeyFramePosition();
351  if (lastFramePos > maxFrame)
352  {
353  maxFrame = lastFramePos;
354  }
355  }
356  }
357  return maxFrame;
358 }
359 
360 void LayerManager::notifyAnimationLengthChanged()
361 {
362  emit animationLengthChanged(animationLength(true));
363 }
364 
365 int LayerManager::getIndex(Layer* layer) const
366 {
367  const Object* o = object();
368  for (int i = 0; i < o->getLayerCount(); ++i)
369  {
370  if (layer == o->getLayer(i))
371  return i;
372  }
373  return -1;
374 }
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:39
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Definition: object.h:54
Definition: editor.h:51
int animationLength(bool includeSounds=true)
Get the length of current project.