File: scene.cpp

package info (click to toggle)
stopmotion 0.6.2%2Bgit.1.10d2ea43-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,936 kB
  • sloc: cpp: 10,055; ansic: 5,502; makefile: 7
file content (228 lines) | stat: -rw-r--r-- 5,973 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/***************************************************************************
 *   Copyright (C) 2005-2008 by Bjoern Erik Nilsen & Fredrik Berg Kjoelstad*
 *   bjoern.nilsen@bjoernen.com & fredrikbk@hotmail.com                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "scene.h"
#include <sstream>
#include <cstring>
#include <unistd.h>

Scene::Scene()
{
	
}


Scene::~Scene()
{
	unsigned int numElem = frames.size();
	for (unsigned int i = 0; i < numElem; ++i) {
		delete frames[i];
	}
}


vector<Frame*>& Scene::getFrames()
{
	return frames;
}


unsigned int Scene::getSize()
{
	return frames.size();
}


Frame* Scene::getFrame(unsigned int frameNumber)
{
	return frames[frameNumber];
}


const vector<char*> Scene::addFrames(const vector<char*>& frameNames, unsigned int index,
		Frontend *frontend, unsigned int &numberOfCanceledFrames)
{
	Logger::get().logDebug("Adding frames in Animation:");
	
	vector<char*> newImagePaths;
	bool isImportingAborted = false;
	
	unsigned i = 0;
	unsigned numElem = frameNames.size();
	
	if (frameNames.size() == 1) {
		char* file = frameNames[i];
		if (access(file, R_OK) == 0) {
			newImagePaths.push_back( addFrame(file, index) );
		}
		else {
			frontend->reportError("You do not have permission to read that file", 0);
			return newImagePaths;
		}
	}	
	else {
		frontend->showProgress("Importing frames from disk ...", numElem * 2);
		
		unsigned numNotReadable = 0;
		for (; i < numElem; ++i) {
			frontend->updateProgress(i);
			char* file = frameNames[i];
			if (access(file, R_OK) == 0) {
				newImagePaths.push_back( addFrame(frameNames[i], index) );
			}
			else {
				Logger::get().logWarning("Wrong permission:");
				Logger::get().logWarning(file);
				++numNotReadable;	
			}
			
			// Doesn't want to process events for each new frame added.
			if ( (i % 10) == 0) {
				frontend->processEvents();
			}
			if ( frontend->isOperationAborted() ) {
				isImportingAborted = true;
				++i; // does this to make cleaning of frames general
				break;
			}
		}
		frontend->updateProgress(numElem);
		
		if (isImportingAborted) {
			numberOfCanceledFrames = i;
			newImagePaths.clear();
		}
		else {
			numberOfCanceledFrames = numElem;
		}

		if (numNotReadable > 0) {
			stringstream ss;
			ss << "You do not have permission to read " << numNotReadable << " file(s)";
			frontend->reportError(ss.str().c_str(), 0);
		}
	}
	
	// trim to size :)
	vector<char*>(newImagePaths).swap(newImagePaths);
	return newImagePaths;
}


void Scene::cleanFrames(unsigned int fromFrame, unsigned int toFrame)
{
	frames.erase(frames.begin() + fromFrame, frames.begin() + toFrame);
}


char* Scene::addFrame(char* frameName, unsigned int &index)
{
	char *newPath;
	
	Frame *f = new Frame(frameName);
	f->copyToTemp();
	newPath = new char[256];
	strcpy( newPath, f->getImagePath() );
	frames.insert(frames.begin() + index, f);
	++index;
	
	return newPath;
}


void Scene::addSavedFrame(Frame *f)
{
	frames.push_back(f);
}


const vector< char * > Scene::removeFrames( unsigned int fromFrame, const unsigned int toFrame )
{
	vector<char*> newImagePaths;
	char *newPath;
	
	if ( toFrame < frames.size() ) { 
		for (unsigned int i = fromFrame; i <= toFrame; ++i) {
			Frame *f = frames[fromFrame];
			f->moveToTrash();
			newPath = new char[256];
			strcpy(newPath, f->getImagePath());
			newImagePaths.push_back(newPath);
			newPath = NULL;
			delete frames[fromFrame];
			frames.erase(frames.begin() + fromFrame);
		}
	}
	
	// trim to size :)
	vector<char*>(newImagePaths).swap(newImagePaths);
	return newImagePaths;
}


void Scene::moveFrames( unsigned int fromFrame, unsigned int toFrame, 
		unsigned int movePosition )
{
	if (movePosition < fromFrame) {
		for (unsigned int i = fromFrame, j = movePosition; i <= toFrame; ++i, ++j) {
			Frame *f = frames[i];
			frames.erase(frames.begin() + i);
			frames.insert(frames.begin() + j, f);
		}
	}
	else {
		for (unsigned int i = fromFrame; i <= toFrame; ++i) {
			Frame *f = frames[fromFrame];
			frames.erase(frames.begin() + fromFrame);
			frames.insert(frames.begin() + movePosition, f);
		}
	}
}


int Scene::addSound( unsigned int frameNumber, const char *sound )
{
	Logger::get().logDebug("Adding sound in scene");
	return frames[frameNumber]->addSound(sound);
}


void Scene::removeSound( unsigned int frameNumber, unsigned int soundNumber )
{
	frames[frameNumber]->removeSound(soundNumber);
}


void Scene::setSoundName( unsigned int frameNumber, unsigned int soundNumber, 
		char * soundName )
{
	frames[frameNumber]->setSoundName(soundNumber, soundName);
}


vector<char *> Scene::getImagePaths( )
{
	vector<char*> imagePaths;
	unsigned int size = frames.size();
	for (unsigned int i = 0; i < size; ++i) {
		imagePaths.push_back(frames[i]->getImagePath());
	}
	return imagePaths;
}