mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-10 01:13:49 +00:00
Convert Documents from std::list<> to a class.
This commit is contained in:
parent
8d53d10a77
commit
55c611c08b
@ -73,6 +73,7 @@ add_library(aseprite-library
|
||||
check_args.cpp
|
||||
console.cpp
|
||||
context.cpp
|
||||
documents.cpp
|
||||
gfxmode.cpp
|
||||
gui_xml.cpp
|
||||
job.cpp
|
||||
|
@ -18,13 +18,13 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "console.h"
|
||||
#include "context.h"
|
||||
#include "commands/command.h"
|
||||
#include "raster/sprite.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
Context::Context(ISettings* settings)
|
||||
{
|
||||
m_currentSprite = NULL;
|
||||
@ -33,13 +33,6 @@ Context::Context(ISettings* settings)
|
||||
|
||||
Context::~Context()
|
||||
{
|
||||
for (Documents::iterator
|
||||
it = m_documents.begin(), end = m_documents.end(); it != end; ++it) {
|
||||
Sprite* sprite = *it;
|
||||
delete sprite;
|
||||
}
|
||||
m_documents.clear();
|
||||
|
||||
delete m_settings;
|
||||
}
|
||||
|
||||
@ -51,7 +44,7 @@ const Documents& Context::getDocuments() const
|
||||
Sprite* Context::getFirstSprite() const
|
||||
{
|
||||
if (!m_documents.empty())
|
||||
return m_documents.front();
|
||||
return m_documents.getByIndex(0);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
@ -74,7 +67,7 @@ void Context::addSprite(Sprite* sprite)
|
||||
{
|
||||
ASSERT(sprite != NULL);
|
||||
|
||||
m_documents.push_front(sprite);
|
||||
m_documents.addDocument(sprite);
|
||||
|
||||
// Generate onAddSprite event
|
||||
onAddSprite(sprite);
|
||||
@ -84,11 +77,8 @@ void Context::removeSprite(Sprite* sprite)
|
||||
{
|
||||
ASSERT(sprite != NULL);
|
||||
|
||||
Documents::iterator it = std::find(m_documents.begin(), m_documents.end(), sprite);
|
||||
ASSERT(it != m_documents.end());
|
||||
|
||||
// remove the item from the sprites list
|
||||
m_documents.erase(it);
|
||||
// Remove the item from the sprites list.
|
||||
m_documents.removeDocument(sprite);
|
||||
|
||||
// generate on_remove_sprite event
|
||||
onRemoveSprite(sprite);
|
||||
@ -100,16 +90,9 @@ void Context::removeSprite(Sprite* sprite)
|
||||
|
||||
void Context::sendSpriteToTop(Sprite* sprite)
|
||||
{
|
||||
ASSERT(sprite);
|
||||
ASSERT(sprite != NULL);
|
||||
|
||||
Documents::iterator it = std::find(m_documents.begin(), m_documents.end(), sprite);
|
||||
ASSERT(it != m_documents.end());
|
||||
|
||||
// remove the item from the sprites list
|
||||
m_documents.erase(it);
|
||||
|
||||
// add it again
|
||||
m_documents.push_front(sprite);
|
||||
m_documents.moveDocument(sprite, 0);
|
||||
}
|
||||
|
||||
Sprite* Context::getCurrentSprite() const
|
||||
|
@ -19,9 +19,9 @@
|
||||
#ifndef CONTEXT_H_INCLUDED
|
||||
#define CONTEXT_H_INCLUDED
|
||||
|
||||
#include <list>
|
||||
#include "base/disable_copying.h"
|
||||
#include "base/exception.h"
|
||||
#include "documents.h"
|
||||
#include "settings/settings.h"
|
||||
|
||||
class Sprite;
|
||||
@ -29,8 +29,6 @@ class SpriteReader;
|
||||
class Command;
|
||||
class Params;
|
||||
|
||||
typedef std::list<Sprite*> Documents;
|
||||
|
||||
class CommandPreconditionException : public base::Exception
|
||||
{
|
||||
public:
|
||||
|
65
src/documents.cpp
Normal file
65
src/documents.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
/* ASE - Allegro Sprite Editor
|
||||
* Copyright (C) 2001-2011 David Capello
|
||||
*
|
||||
* 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 "config.h"
|
||||
|
||||
#include "documents.h"
|
||||
#include "raster/sprite.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
Documents::Documents()
|
||||
{
|
||||
}
|
||||
|
||||
Documents::~Documents()
|
||||
{
|
||||
deleteAll();
|
||||
}
|
||||
|
||||
void Documents::addDocument(Sprite* sprite)
|
||||
{
|
||||
ASSERT(sprite != NULL);
|
||||
|
||||
m_documents.insert(begin(), sprite);
|
||||
}
|
||||
|
||||
void Documents::removeDocument(Sprite* sprite)
|
||||
{
|
||||
iterator it = std::find(begin(), end(), sprite);
|
||||
ASSERT(it != end());
|
||||
|
||||
if (it != end())
|
||||
m_documents.erase(it);
|
||||
}
|
||||
|
||||
void Documents::moveDocument(Sprite* sprite, int index)
|
||||
{
|
||||
removeDocument(sprite);
|
||||
|
||||
m_documents.insert(begin()+index, sprite);
|
||||
}
|
||||
|
||||
void Documents::deleteAll()
|
||||
{
|
||||
for (iterator it = begin(), end = this->end(); it != end; ++it) {
|
||||
Sprite* sprite = *it;
|
||||
delete sprite;
|
||||
}
|
||||
m_documents.clear();
|
||||
}
|
69
src/documents.h
Normal file
69
src/documents.h
Normal file
@ -0,0 +1,69 @@
|
||||
/* ASE - Allegro Sprite Editor
|
||||
* Copyright (C) 2001-2011 David Capello
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef DOCUMENTS_H_INCLUDED
|
||||
#define DOCUMENTS_H_INCLUDED
|
||||
|
||||
#include "base/disable_copying.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Sprite;
|
||||
|
||||
class Documents
|
||||
{
|
||||
public:
|
||||
typedef std::vector<Sprite*>::iterator iterator;
|
||||
typedef std::vector<Sprite*>::const_iterator const_iterator;
|
||||
|
||||
Documents();
|
||||
~Documents();
|
||||
|
||||
iterator begin() { return m_documents.begin(); }
|
||||
iterator end() { return m_documents.end(); }
|
||||
const_iterator begin() const { return m_documents.begin(); }
|
||||
const_iterator end() const { return m_documents.end(); }
|
||||
|
||||
int size() const { return m_documents.size(); }
|
||||
bool empty() const { return m_documents.empty(); }
|
||||
|
||||
// Adds a new document to the list. If the destructor is called the
|
||||
// document is deleted automatically.
|
||||
void addDocument(Sprite* sprite);
|
||||
|
||||
// Removes a document from the list without deleting it. You must to
|
||||
// delete the document after removing it.
|
||||
void removeDocument(Sprite* sprite);
|
||||
|
||||
// Moves the document to the given location in the same
|
||||
// list. E.g. It is used to reorder documents when they are selected
|
||||
// as active.
|
||||
void moveDocument(Sprite* sprite, int index);
|
||||
|
||||
// Deletes all documents in the list (calling "delete" operation).
|
||||
void deleteAll();
|
||||
|
||||
Sprite* getByIndex(int index) const { return m_documents[index]; }
|
||||
|
||||
private:
|
||||
std::vector<Sprite*> m_documents;
|
||||
|
||||
DISABLE_COPYING(Documents)
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user