Add LayerIndex and SpritePosition types.

This commit is contained in:
David Capello 2012-08-23 21:32:55 -03:00
parent 7f2f965b9c
commit 919e892748
10 changed files with 165 additions and 34 deletions

View File

@ -59,14 +59,14 @@ void GotoPreviousLayerCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
Sprite* sprite(document->getSprite());
int i = sprite->layerToIndex(sprite->getCurrentLayer());
SpritePosition pos = sprite->getCurrentPosition();
if (i > 0)
i--;
if (pos.layerIndex() > 0)
pos.layerIndex(pos.layerIndex().previous());
else
i = sprite->countLayers()-1;
pos.layerIndex(LayerIndex(sprite->countLayers()-1));
sprite->setCurrentLayer(sprite->indexToLayer(i));
sprite->setCurrentPosition(pos);
// Flash the current layer
ASSERT(current_editor != NULL && "Current editor cannot be null when we have a current sprite");
@ -108,14 +108,14 @@ void GotoNextLayerCommand::onExecute(Context* context)
{
ActiveDocumentWriter document(context);
Sprite* sprite(document->getSprite());
int i = sprite->layerToIndex(sprite->getCurrentLayer());
SpritePosition pos = sprite->getCurrentPosition();
if (i < sprite->countLayers()-1)
i++;
if (pos.layerIndex() < sprite->countLayers()-1)
pos.layerIndex(pos.layerIndex().next());
else
i = 0;
pos.layerIndex(LayerIndex(0));
sprite->setCurrentLayer(sprite->indexToLayer(i));
sprite->setCurrentPosition(pos);
// Flash the current layer
ASSERT(current_editor != NULL && "Current editor cannot be null when we have a current sprite");

View File

@ -1288,7 +1288,7 @@ void AnimationEditor::regenerateLayers()
m_layers = (Layer**)base_malloc(sizeof(Layer*) * m_nlayers);
for (c=0; c<m_nlayers; c++)
m_layers[c] = (Layer*)m_sprite->indexToLayer(m_nlayers-c-1);
m_layers[c] = (Layer*)m_sprite->indexToLayer(LayerIndex(m_nlayers-c-1));
}
}

View File

@ -440,7 +440,7 @@ Document* Document::duplicate(DuplicateType type) const
// Set as current layer the same layer as the source
{
int index = sourceSprite->layerToIndex(sourceSprite->getCurrentLayer());
LayerIndex index = sourceSprite->layerToIndex(sourceSprite->getCurrentLayer());
spriteCopy->setCurrentLayer(spriteCopy->indexToLayer(index));
}

View File

@ -984,7 +984,7 @@ static Cel *ase_file_read_cel_chunk(FILE *f, Sprite *sprite, FrameNumber frame,
FileOp *fop, ASE_Header *header, size_t chunk_end)
{
/* read chunk data */
int layer_index = fgetw(f);
LayerIndex layer_index = LayerIndex(fgetw(f));
int x = ((short)fgetw(f));
int y = ((short)fgetw(f));
int opacity = fgetc(f);

57
src/raster/layer_index.h Normal file
View File

@ -0,0 +1,57 @@
/* ASEPRITE
* Copyright (C) 2001-2012 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 RASTER_LAYER_INDEX_H_INCLUDED
#define RASTER_LAYER_INDEX_H_INCLUDED
class LayerIndex {
public:
LayerIndex() : m_value(0) { }
explicit LayerIndex(int value) : m_value(value) { }
LayerIndex next(int i = 1) const { return LayerIndex(m_value+i); };
LayerIndex previous(int i = 1) const { return LayerIndex(m_value-i); };
operator int() { return m_value; }
operator const int() const { return m_value; }
LayerIndex& operator=(const LayerIndex& o) { m_value = o.m_value; return *this; }
LayerIndex& operator++() { ++m_value; return *this; }
LayerIndex& operator--() { --m_value; return *this; }
LayerIndex operator++(int) { LayerIndex old(*this); ++m_value; return old; }
LayerIndex operator--(int) { LayerIndex old(*this); --m_value; return old; }
bool operator<(const LayerIndex& o) const { return m_value < o.m_value; }
bool operator>(const LayerIndex& o) const { return m_value > o.m_value; }
bool operator<=(const LayerIndex& o) const { return m_value <= o.m_value; }
bool operator>=(const LayerIndex& o) const { return m_value >= o.m_value; }
bool operator==(const LayerIndex& o) const { return m_value == o.m_value; }
bool operator!=(const LayerIndex& o) const { return m_value != o.m_value; }
private:
int m_value;
};
inline LayerIndex operator+(const LayerIndex& x, const LayerIndex& y) {
return LayerIndex((int)x + (int)y);
}
inline LayerIndex operator-(const LayerIndex& x, const LayerIndex& y) {
return LayerIndex((int)x - (int)y);
}
#endif

View File

@ -27,8 +27,8 @@
#include <cstring>
#include <vector>
static Layer* index2layer(const Layer* layer, int index, int* index_count);
static int layer2index(const Layer* layer, const Layer* find_layer, int* index_count);
static Layer* index2layer(const Layer* layer, const LayerIndex& index, int* index_count);
static LayerIndex layer2index(const Layer* layer, const Layer* find_layer, int* index_count);
//////////////////////////////////////////////////////////////////////
// Constructors/Destructor
@ -177,18 +177,18 @@ void Sprite::setCurrentLayer(Layer* layer)
m_layer = layer;
}
int Sprite::countLayers() const
LayerIndex Sprite::countLayers() const
{
return getFolder()->get_layers_count();
return LayerIndex(getFolder()->get_layers_count());
}
Layer* Sprite::indexToLayer(int index) const
Layer* Sprite::indexToLayer(LayerIndex index) const
{
int index_count = -1;
return index2layer(getFolder(), index, &index_count);
}
int Sprite::layerToIndex(const Layer* layer) const
LayerIndex Sprite::layerToIndex(const Layer* layer) const
{
int index_count = -1;
return layer2index(getFolder(), layer, &index_count);
@ -337,6 +337,22 @@ void Sprite::setCurrentFrame(FrameNumber frame)
m_frame = frame;
}
SpritePosition Sprite::getCurrentPosition() const
{
return SpritePosition(layerToIndex(getCurrentLayer()),
getCurrentFrame());
}
void Sprite::setCurrentPosition(const SpritePosition& spritePosition)
{
Layer* layer = indexToLayer(spritePosition.layerIndex());
ASSERT(layer);
if (layer)
setCurrentLayer(layer);
setCurrentFrame(spritePosition.frameNumber());
}
//////////////////////////////////////////////////////////////////////
// Images
@ -425,7 +441,7 @@ int Sprite::getPixel(int x, int y) const
//////////////////////////////////////////////////////////////////////
static Layer* index2layer(const Layer* layer, int index, int* index_count)
static Layer* index2layer(const Layer* layer, const LayerIndex& index, int* index_count)
{
if (index == *index_count)
return (Layer*)layer;
@ -448,10 +464,10 @@ static Layer* index2layer(const Layer* layer, int index, int* index_count)
}
}
static int layer2index(const Layer* layer, const Layer* find_layer, int* index_count)
static LayerIndex layer2index(const Layer* layer, const Layer* find_layer, int* index_count)
{
if (layer == find_layer)
return *index_count;
return LayerIndex(*index_count);
else {
(*index_count)++;
@ -463,10 +479,10 @@ static int layer2index(const Layer* layer, const Layer* find_layer, int* index_c
for (; it != end; ++it) {
if ((found = layer2index(*it, find_layer, index_count)) >= 0)
return found;
return LayerIndex(found);
}
}
return -1;
return LayerIndex(-1);
}
}

View File

@ -21,8 +21,10 @@
#include "base/disable_copying.h"
#include "raster/frame_number.h"
#include "raster/layer_index.h"
#include "raster/gfxobj.h"
#include "raster/pixel_format.h"
#include "raster/sprite_position.h"
#include <vector>
@ -78,10 +80,10 @@ public:
Layer* getCurrentLayer() const;
void setCurrentLayer(Layer* layer);
int countLayers() const;
LayerIndex countLayers() const;
Layer* indexToLayer(int index) const;
int layerToIndex(const Layer* layer) const;
Layer* indexToLayer(LayerIndex index) const;
LayerIndex layerToIndex(const Layer* layer) const;
////////////////////////////////////////
// Palettes
@ -119,6 +121,12 @@ public:
FrameNumber getCurrentFrame() const { return m_frame; }
void setCurrentFrame(FrameNumber frame);
////////////////////////////////////////
// Position
SpritePosition getCurrentPosition() const;
void setCurrentPosition(const SpritePosition& spritePosition);
////////////////////////////////////////
// Images

View File

@ -0,0 +1,49 @@
/* ASEPRITE
* Copyright (C) 2001-2012 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 RASTER_SPRITE_POSITION_H_INCLUDED
#define RASTER_SPRITE_POSITION_H_INCLUDED
#include "raster/frame_number.h"
#include "raster/layer_index.h"
class Sprite;
class SpritePosition {
public:
SpritePosition() { }
SpritePosition(LayerIndex layerIndex, FrameNumber frameNumber)
: m_layerIndex(layerIndex)
, m_frameNumber(frameNumber) {
}
const LayerIndex& layerIndex() const { return m_layerIndex; }
const FrameNumber& frameNumber() const { return m_frameNumber; }
void layerIndex(LayerIndex layerIndex) { m_layerIndex = layerIndex; }
void frameNumber(FrameNumber frameNumber) { m_frameNumber = frameNumber; }
bool operator==(const SpritePosition& o) const { return m_layerIndex == o.m_layerIndex && m_frameNumber == o.m_frameNumber; }
bool operator!=(const SpritePosition& o) const { return m_layerIndex != o.m_layerIndex || m_frameNumber != o.m_frameNumber; }
private:
LayerIndex m_layerIndex;
FrameNumber m_frameNumber;
};
#endif

View File

@ -161,7 +161,7 @@ StatusBar::StatusBar()
m_timeout = 0;
m_state = SHOW_TEXT;
m_tipwindow = NULL;
m_hot_layer = -1;
m_hot_layer = LayerIndex(-1);
// The extra pixel in left and right borders are necessary so
// m_commandsBox and m_movePixelsBox do not overlap the upper-left
@ -632,7 +632,7 @@ bool StatusBar::onProcessMessage(Message* msg)
int x1 = rc->x2-width + c*width/count;
int x2 = rc->x2-width + (c+1)*width/count;
bool hot = (*it == sprite->getCurrentLayer())
|| (c == m_hot_layer);
|| (LayerIndex(c) == m_hot_layer);
theme->draw_bounds_nw(doublebuffer,
x1, rc->y1, x2, rc->y2,
@ -726,7 +726,7 @@ bool StatusBar::onProcessMessage(Message* msg)
try {
--rc->y2;
int hot_layer = -1;
LayerIndex hot_layer = LayerIndex(-1);
const ActiveDocumentReader document(UIContext::instance());
const Sprite* sprite(document ? document->getSprite(): NULL);
@ -744,7 +744,7 @@ bool StatusBar::onProcessMessage(Message* msg)
if (Rect(Point(x1, rc->y1),
Point(x2, rc->y2)).contains(Point(msg->mouse.x,
msg->mouse.y))) {
hot_layer = c;
hot_layer = LayerIndex(c);
break;
}
}
@ -757,7 +757,7 @@ bool StatusBar::onProcessMessage(Message* msg)
if (Rect(Point(x1, rc->y1),
Point(x2, rc->y2)).contains(Point(msg->mouse.x,
msg->mouse.y))) {
hot_layer = 0;
hot_layer = LayerIndex(0);
}
}
@ -815,7 +815,7 @@ bool StatusBar::onProcessMessage(Message* msg)
}
if (m_hot_layer >= 0) {
m_hot_layer = -1;
m_hot_layer = LayerIndex(-1);
invalidate();
}
}

View File

@ -25,6 +25,7 @@
#include "app/color.h"
#include "base/compiler_specific.h"
#include "observers.h"
#include "raster/layer_index.h"
#include "ui/base.h"
#include "ui/link_label.h"
#include "ui/widget.h"
@ -152,7 +153,7 @@ private:
class CustomizedTipWindow;
CustomizedTipWindow* m_tipwindow;
int m_hot_layer;
LayerIndex m_hot_layer;
StatusBarObservers m_observers;
};