Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
layermanager.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 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( QObject* pParent ) : BaseManager( pParent )
30 {
31 }
32 
33 LayerManager::~LayerManager()
34 {
35 }
36 
37 bool LayerManager::init()
38 {
39  lastCameraLayer = 0;
40  return true;
41 }
42 
43 Status LayerManager::load( Object* o )
44 {
45  connect( o, &Object::layerChanged, this, &LayerManager::layerUpdated );
46 
47  mCurrentLayerIndex = o->data()->getCurrentLayer();
48  lastCameraLayer = 0;
49 
50  emit layerCountChanged(o->getLayerCount());
51 
52  return Status::OK;
53 }
54 
55 Status LayerManager::save( Object* o )
56 {
57  o->data()->setCurrentLayer( mCurrentLayerIndex );
58  return Status::OK;
59 }
60 
61 int LayerManager::getLastCameraLayer()
62 {
63  return lastCameraLayer;
64 }
65 
66 Layer* LayerManager::currentLayer()
67 {
68  return currentLayer( 0 );
69 }
70 
71 Layer* LayerManager::currentLayer( int incr )
72 {
73  Q_ASSERT( editor()->object() != NULL );
74 
75  return editor()->object()->getLayer( mCurrentLayerIndex + incr );
76 }
77 
78 Layer* LayerManager::getLayer( int index )
79 {
80  Q_ASSERT( editor()->object() != NULL );
81 
82  return editor()->object()->getLayer( index );
83 }
84 
85 int LayerManager::currentLayerIndex()
86 {
87  return mCurrentLayerIndex;
88 }
89 
90 void LayerManager::setCurrentLayer( int layerIndex )
91 {
92  Object* o = editor()->object();
93 
94  if ( layerIndex >= o->getLayerCount() )
95  {
96  Q_ASSERT( false );
97  return;
98  }
99 
100  if ( mCurrentLayerIndex != layerIndex )
101  {
102  mCurrentLayerIndex = layerIndex;
103  Q_EMIT currentLayerChanged( mCurrentLayerIndex );
104  }
105 
106  if ( editor()->object() )
107  {
108  if ( editor()->object()->getLayer( layerIndex )->type() == Layer::CAMERA )
109  {
110  lastCameraLayer = layerIndex;
111  }
112  }
113 }
114 
115 void LayerManager::setCurrentLayer( Layer* layer )
116 {
117  Object* o = editor()->object();
118 
119  for ( int i = 0; i < o->getLayerCount(); ++i )
120  {
121  if ( layer == o->getLayer( i ) )
122  {
123  setCurrentLayer( i );
124  return;
125  }
126  }
127 }
128 
129 void LayerManager::gotoNextLayer()
130 {
131  if ( mCurrentLayerIndex < editor()->object()->getLayerCount() - 1 )
132  {
133  mCurrentLayerIndex += 1;
134  Q_EMIT currentLayerChanged( mCurrentLayerIndex );
135  }
136 }
137 
138 void LayerManager::gotoPreviouslayer()
139 {
140  if ( mCurrentLayerIndex > 0 )
141  {
142  mCurrentLayerIndex -= 1;
143  Q_EMIT currentLayerChanged( mCurrentLayerIndex );
144  }
145 }
146 
147 LayerBitmap* LayerManager::createBitmapLayer( const QString& strLayerName )
148 {
149  LayerBitmap* layer = editor()->object()->addNewBitmapLayer();
150  layer->setName( strLayerName );
151 
152  Q_EMIT layerCountChanged( count() );
153 
154  return layer;
155 }
156 
157 LayerVector* LayerManager::createVectorLayer( const QString& strLayerName )
158 {
159  LayerVector* layer = editor()->object()->addNewVectorLayer();
160  layer->setName( strLayerName );
161 
162  Q_EMIT layerCountChanged( count() );
163 
164  return layer;
165 }
166 
167 LayerCamera* LayerManager::createCameraLayer( const QString& strLayerName )
168 {
169  LayerCamera* layer = editor()->object()->addNewCameraLayer();
170  layer->setName( strLayerName );
171 
172  Q_EMIT layerCountChanged( count() );
173 
174  return layer;
175 }
176 
177 LayerSound* LayerManager::createSoundLayer( const QString& strLayerName )
178 {
179  LayerSound* layer = editor()->object()->addNewSoundLayer();
180  layer->setName( strLayerName );
181 
182  Q_EMIT layerCountChanged( count() );
183 
184  return layer;
185 }
186 
187 int LayerManager::LastFrameAtFrame( int frameIndex )
188 {
189  Object* pObj = editor()->object();
190  for ( int i = frameIndex; i >= 0; i -= 1 )
191  {
192  for ( int layerIndex = 0; layerIndex < pObj->getLayerCount(); ++layerIndex )
193  {
194  auto pLayer = pObj->getLayer( layerIndex );
195  if ( pLayer->keyExists( i ) )
196  {
197  return i;
198  }
199  }
200  }
201  return -1;
202 }
203 
204 int LayerManager::firstKeyFrameIndex()
205 {
206  int minPosition = INT_MAX;
207 
208  Object* pObj = editor()->object();
209  for ( int i = 0; i < pObj->getLayerCount(); ++i )
210  {
211  Layer* pLayer = pObj->getLayer( i );
212 
213  int position = pLayer->firstKeyFramePosition();
214  if ( position < minPosition )
215  {
216  minPosition = position;
217  }
218  }
219  return minPosition;
220 }
221 
222 int LayerManager::lastKeyFrameIndex()
223 {
224  int maxPosition = 0;
225 
226  for ( int i = 0; i < editor()->object()->getLayerCount(); ++i )
227  {
228  Layer* pLayer = editor()->object()->getLayer( i );
229 
230  int position = pLayer->getMaxKeyFramePosition();
231  if ( position > maxPosition )
232  {
233  maxPosition = position;
234  }
235  }
236  return maxPosition;
237 }
238 
239 int LayerManager::count()
240 {
241  return editor()->object()->getLayerCount();
242 }
243 
244 bool LayerManager::deleteCurrentLayer()
245 {
246  // FIXME:
247  if ( currentLayer()->type() == Layer::CAMERA )
248  {
249  return false;
250  }
251 
252  editor()->object()->deleteLayer( currentLayerIndex() );
253 
254  if ( currentLayerIndex() == editor()->object()->getLayerCount() )
255  {
256  setCurrentLayer( currentLayerIndex() - 1 );
257  }
258 
259  Q_EMIT layerCountChanged( count() );
260 
261  return true;
262 }
263 
264 int LayerManager::projectLength()
265 {
266  int maxFrame = -1;
267 
268  Object* pObject = editor()->object();
269  for ( int i = 0; i < pObject->getLayerCount(); i++ )
270  {
271  int frame = pObject->getLayer( i )->getMaxKeyFramePosition();
272  if ( frame > maxFrame )
273  {
274  maxFrame = frame;
275  }
276  }
277  return maxFrame;
278 }
279 
280 void LayerManager::layerUpdated(int layerId)
281 {
282  emit currentLayerChanged(layerId);
283 }
Definition: layer.h:32
Definition: object.h:71