Minor refactor to doc::Layer flags

This commit is contained in:
David Capello 2014-11-16 23:03:30 -03:00
parent 9bcc552129
commit cc889f2b2f
26 changed files with 118 additions and 102 deletions

View File

@ -115,10 +115,10 @@ Layer Chunk (0x2004)
entire layers layout:
WORD Flags:
LAYER_IS_READABLE = 0x0001
LAYER_IS_WRITABLE = 0x0002
LAYER_IS_LOCKMOVE = 0x0004
LAYER_IS_BACKGROUND = 0x0008
1 = Visible
2 = Editable
4 = Lock movement
8 = Background
WORD Layer type (0=normal (image) layer, 1=layer set)
WORD Layer child level (see NOTE.1)
WORD Default layer width in pixels (ignored)

View File

@ -278,7 +278,7 @@ void App::initialize(int argc, const char* argv[])
// For each layer, hide other ones and save the sprite.
for (Layer* show : layers) {
for (Layer* hide : layers)
hide->setReadable(hide == show);
hide->setVisible(hide == show);
std::string frameStr;
if (doc->sprite()->totalFrames() > FrameNumber(1))
@ -303,7 +303,7 @@ void App::initialize(int argc, const char* argv[])
// Show only one layer
if (!importLayerSaveAs.empty()) {
for (Layer* layer : layers)
layer->setReadable(layer->name() == importLayerSaveAs);
layer->setVisible(layer->name() == importLayerSaveAs);
}
static_cast<SaveFileBaseCommand*>(command)->setFilename(value.value());

View File

@ -54,8 +54,8 @@ bool BackgroundFromLayerCommand::onEnabled(Context* context)
{
return
context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable |
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable |
ContextFlags::HasActiveImage) &&
// Doesn't have a background layer
!context->checkFlags(ContextFlags::HasBackgroundLayer);

View File

@ -81,8 +81,8 @@ void CelPropertiesCommand::onExecute(Context* context)
// Mini look for the opacity slider
setup_mini_look(slider_opacity);
/* if the layer isn't writable */
if (!layer->isWritable()) {
// If the layer isn't writable
if (!layer->isEditable()) {
button_ok->setText("Locked");
button_ok->setEnabled(false);
}

View File

@ -57,8 +57,8 @@ ClearCommand::ClearCommand()
bool ClearCommand::onEnabled(Context* context)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable |
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable |
ContextFlags::ActiveLayerIsImage);
}

View File

@ -54,8 +54,8 @@ ClearCelCommand::ClearCelCommand()
bool ClearCelCommand::onEnabled(Context* context)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable |
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable |
ContextFlags::ActiveLayerIsImage |
ContextFlags::HasActiveCel);
}

View File

@ -51,8 +51,8 @@ CutCommand::CutCommand()
bool CutCommand::onEnabled(Context* context)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable |
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable |
ContextFlags::HasVisibleMask |
ContextFlags::HasActiveImage);
}

View File

@ -53,8 +53,8 @@ bool LayerFromBackgroundCommand::onEnabled(Context* context)
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::HasActiveSprite |
ContextFlags::HasActiveLayer |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable |
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable |
ContextFlags::ActiveLayerIsImage |
ContextFlags::ActiveLayerIsBackground);
}

View File

@ -50,8 +50,8 @@ bool PasteCommand::onEnabled(Context* context)
return
(clipboard::get_current_format() == clipboard::ClipboardImage &&
context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable |
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable |
ContextFlags::ActiveLayerIsImage)) ||
(clipboard::get_current_format() == clipboard::ClipboardDocumentRange &&
context->checkFlags(ContextFlags::ActiveDocumentIsWritable));

View File

@ -58,8 +58,8 @@ bool RemoveLayerCommand::onEnabled(Context* context)
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::HasActiveSprite |
ContextFlags::HasActiveLayer |
ContextFlags::ActiveLayerIsReadable |
ContextFlags::ActiveLayerIsWritable);
ContextFlags::ActiveLayerIsVisible |
ContextFlags::ActiveLayerIsEditable);
}
void RemoveLayerCommand::onExecute(Context* context)

View File

@ -67,11 +67,11 @@ void ContextFlags::update(Context* context)
if (layer->isBackground())
m_flags |= ActiveLayerIsBackground;
if (layer->isReadable())
m_flags |= ActiveLayerIsReadable;
if (layer->isVisible())
m_flags |= ActiveLayerIsVisible;
if (layer->isWritable())
m_flags |= ActiveLayerIsWritable;
if (layer->isEditable())
m_flags |= ActiveLayerIsEditable;
if (layer->isImage()) {
m_flags |= ActiveLayerIsImage;

View File

@ -38,8 +38,8 @@ namespace app {
ActiveDocumentIsWritable = 1 << 8,
ActiveLayerIsImage = 1 << 9,
ActiveLayerIsBackground = 1 << 10,
ActiveLayerIsReadable = 1 << 11,
ActiveLayerIsWritable = 1 << 12,
ActiveLayerIsVisible = 1 << 11,
ActiveLayerIsEditable = 1 << 12,
};
ContextFlags();

View File

@ -970,8 +970,8 @@ void DocumentApi::backgroundFromLayer(LayerImage* layer)
{
ASSERT(layer);
ASSERT(layer->isImage());
ASSERT(layer->isReadable());
ASSERT(layer->isWritable());
ASSERT(layer->isVisible());
ASSERT(layer->isEditable());
ASSERT(layer->sprite() != NULL);
ASSERT(layer->sprite()->backgroundLayer() == NULL);
@ -1041,8 +1041,8 @@ void DocumentApi::layerFromBackground(Layer* layer)
{
ASSERT(layer != NULL);
ASSERT(layer->isImage());
ASSERT(layer->isReadable());
ASSERT(layer->isWritable());
ASSERT(layer->isVisible());
ASSERT(layer->isEditable());
ASSERT(layer->isBackground());
ASSERT(layer->sprite() != NULL);
ASSERT(layer->sprite()->backgroundLayer() != NULL);
@ -1053,7 +1053,7 @@ void DocumentApi::layerFromBackground(Layer* layer)
}
layer->setBackground(false);
layer->setMoveable(true);
layer->setMovable(true);
layer->setName("Layer 0");
}

View File

@ -676,7 +676,7 @@ static Layer* ase_file_read_layer_chunk(FILE* f, Sprite* sprite, Layer** previou
if (layer) {
// flags
layer->setFlags(flags);
layer->setFlags(static_cast<LayerFlags>(flags));
// name
layer->setName(name.c_str());
@ -701,12 +701,12 @@ static void ase_file_write_layer_chunk(FILE* f, ASE_FrameHeader* frame_header, L
ChunkWriter chunk(f, frame_header, ASE_FILE_CHUNK_LAYER);
// Flags
fputw(layer->getFlags(), f);
fputw(static_cast<int>(layer->flags()), f);
/* layer type */
// Layer type
fputw(layer->isImage() ? 0: (layer->isFolder() ? 1: -1), f);
/* layer child level */
// Layer child level
LayerFolder* parent = layer->parent();
int child_level = -1;
while (parent != NULL) {

View File

@ -72,7 +72,7 @@ LayerImage* create_flatten_layer_copy(Sprite* dstSprite, const Layer* srcLayer,
// in the given "frame".
static bool has_cels(const Layer* layer, FrameNumber frame)
{
if (!layer->isReadable())
if (!layer->isVisible())
return false;
switch (layer->type()) {

View File

@ -1307,8 +1307,8 @@ bool Editor::canDraw()
{
return (m_layer != NULL &&
m_layer->isImage() &&
m_layer->isReadable() &&
m_layer->isWritable());
m_layer->isVisible() &&
m_layer->isEditable());
}
bool Editor::isInsideSelection()

View File

@ -203,7 +203,7 @@ bool StandbyState::onMouseDown(Editor* editor, MouseMessage* msg)
"<<You can't move the `Background' layer."
"||&Close");
}
else if (!layer->isMoveable()) {
else if (!layer->isMovable()) {
Alert::show(PACKAGE "<<The layer movement is locked.||&Close");
}
else {
@ -235,7 +235,7 @@ bool StandbyState::onMouseDown(Editor* editor, MouseMessage* msg)
int x, y, opacity;
Image* image = location.image(&x, &y, &opacity);
if (image) {
if (!layer->isWritable()) {
if (!layer->isEditable()) {
Alert::show(PACKAGE "<<The layer is locked.||&Close");
return true;
}
@ -249,7 +249,7 @@ bool StandbyState::onMouseDown(Editor* editor, MouseMessage* msg)
// Move selected pixels
if (editor->isInsideSelection() && msg->left()) {
if (!layer->isWritable()) {
if (!layer->isEditable()) {
Alert::show(PACKAGE "<<The layer is locked.||&Close");
return true;
}

View File

@ -303,7 +303,7 @@ tools::ToolLoop* create_tool_loop(Editor* editor, Context* context)
}
// If the active layer is not visible.
if (!layer->isReadable()) {
if (!layer->isVisible()) {
Alert::show(PACKAGE
"<<The current layer is hidden,"
"<<make it visible and try again"
@ -311,7 +311,7 @@ tools::ToolLoop* create_tool_loop(Editor* editor, Context* context)
return NULL;
}
// If the active layer is read-only.
else if (!layer->isWritable()) {
else if (!layer->isEditable()) {
Alert::show(PACKAGE
"<<The current layer is locked,"
"<<unlock it and try again"

View File

@ -531,9 +531,9 @@ bool Timeline::onProcessMessage(Message* msg)
break;
case A_PART_HEADER_EYE: {
bool newReadableState = !allLayersVisible();
bool newVisibleState = !allLayersVisible();
for (size_t i=0; i<m_layers.size(); i++)
m_layers[i]->setReadable(newReadableState);
m_layers[i]->setVisible(newVisibleState);
// Redraw all views.
m_document->notifyGeneralUpdate();
@ -541,9 +541,9 @@ bool Timeline::onProcessMessage(Message* msg)
}
case A_PART_HEADER_PADLOCK: {
bool newWritableState = !allLayersUnlocked();
bool newEditableState = !allLayersUnlocked();
for (size_t i=0; i<m_layers.size(); i++)
m_layers[i]->setWritable(newWritableState);
m_layers[i]->setEditable(newEditableState);
break;
}
@ -607,7 +607,7 @@ bool Timeline::onProcessMessage(Message* msg)
if (m_hot_layer == m_clk_layer && validLayer(m_hot_layer)) {
Layer* layer = m_layers[m_clk_layer];
ASSERT(layer != NULL);
layer->setReadable(!layer->isReadable());
layer->setVisible(!layer->isVisible());
// Redraw all views.
m_document->notifyGeneralUpdate();
@ -619,7 +619,7 @@ bool Timeline::onProcessMessage(Message* msg)
if (m_hot_layer == m_clk_layer && validLayer(m_hot_layer)) {
Layer* layer = m_layers[m_clk_layer];
ASSERT(layer != NULL);
layer->setWritable(!layer->isWritable());
layer->setEditable(!layer->isEditable());
}
break;
@ -1158,18 +1158,18 @@ void Timeline::drawLayer(ui::Graphics* g, LayerIndex layerIdx)
if (!clip)
return;
// Draw the eye (readable flag).
// Draw the eye (visible flag).
bounds = getPartBounds(A_PART_LAYER_EYE_ICON, layerIdx);
drawPart(g, bounds, NULL,
layer->isReadable() ? m_timelineOpenEyeStyle: m_timelineClosedEyeStyle,
layer->isVisible() ? m_timelineOpenEyeStyle: m_timelineClosedEyeStyle,
is_active,
(hotlayer && m_hot_part == A_PART_LAYER_EYE_ICON),
(clklayer && m_clk_part == A_PART_LAYER_EYE_ICON));
// Draw the padlock (writable flag).
// Draw the padlock (editable flag).
bounds = getPartBounds(A_PART_LAYER_PADLOCK_ICON, layerIdx);
drawPart(g, bounds, NULL,
layer->isWritable() ? m_timelineOpenPadlockStyle: m_timelineClosedPadlockStyle,
layer->isEditable() ? m_timelineOpenPadlockStyle: m_timelineClosedPadlockStyle,
is_active,
(hotlayer && m_hot_part == A_PART_LAYER_PADLOCK_ICON),
(clklayer && m_clk_part == A_PART_LAYER_PADLOCK_ICON));
@ -1755,8 +1755,8 @@ void Timeline::updateStatusBar(ui::Message* msg)
if (layer != NULL) {
sb->setStatusText(0, "Layer '%s' [%s%s]",
layer->name().c_str(),
layer->isReadable() ? "visible": "hidden",
layer->isWritable() ? "": " locked");
layer->isVisible() ? "visible": "hidden",
layer->isEditable() ? "": " locked");
return;
}
break;
@ -1765,7 +1765,7 @@ void Timeline::updateStatusBar(ui::Message* msg)
if (layer != NULL) {
sb->setStatusText(0, "Layer '%s' is %s",
layer->name().c_str(),
layer->isReadable() ? "visible": "hidden");
layer->isVisible() ? "visible": "hidden");
return;
}
break;
@ -1774,7 +1774,7 @@ void Timeline::updateStatusBar(ui::Message* msg)
if (layer != NULL) {
sb->setStatusText(0, "Layer '%s' is %s",
layer->name().c_str(),
layer->isWritable() ? "unlocked (modifiable)": "locked (read-only)");
layer->isEditable() ? "unlocked (editable)": "locked (read-only)");
return;
}
break;
@ -1878,7 +1878,7 @@ void Timeline::setScroll(int x, int y)
bool Timeline::allLayersVisible()
{
for (size_t i=0; i<m_layers.size(); i++)
if (!m_layers[i]->isReadable())
if (!m_layers[i]->isVisible())
return false;
return true;
@ -1887,7 +1887,7 @@ bool Timeline::allLayersVisible()
bool Timeline::allLayersInvisible()
{
for (size_t i=0; i<m_layers.size(); i++)
if (m_layers[i]->isReadable())
if (m_layers[i]->isVisible())
return false;
return true;
@ -1896,7 +1896,7 @@ bool Timeline::allLayersInvisible()
bool Timeline::allLayersLocked()
{
for (size_t i=0; i<m_layers.size(); i++)
if (m_layers[i]->isWritable())
if (m_layers[i]->isEditable())
return false;
return true;
@ -1905,7 +1905,7 @@ bool Timeline::allLayersLocked()
bool Timeline::allLayersUnlocked()
{
for (size_t i=0; i<m_layers.size(); i++)
if (!m_layers[i]->isWritable())
if (!m_layers[i]->isEditable())
return false;
return true;

View File

@ -33,7 +33,7 @@ using namespace undo;
SetLayerFlags::SetLayerFlags(ObjectsContainer* objects, Layer* layer)
: m_layerId(objects->addObject(layer))
, m_flags(layer->getFlags())
, m_flags(uint32_t(layer->flags()))
{
}
@ -49,7 +49,7 @@ void SetLayerFlags::revert(ObjectsContainer* objects, UndoersCollector* redoers)
// Push another SetLayerFlags as redoer
redoers->pushUndoer(new SetLayerFlags(objects, layer));
layer->setFlags(m_flags);
layer->setFlags(LayerFlags(m_flags));
}
} // namespace undoers

View File

@ -390,7 +390,7 @@ Image* RenderEngine::renderSprite(int source_x, int source_y,
{
void (*zoomed_func)(Image*, const Image*, const Palette*, int, int, int, int, int);
const LayerImage* background = m_sprite->backgroundLayer();
bool need_checked_bg = (background != NULL ? !background->isReadable(): true);
bool need_checked_bg = (background != NULL ? !background->isVisible(): true);
uint32_t bg_color = 0;
Image *image;
@ -573,7 +573,7 @@ void RenderEngine::renderLayer(
int blend_mode)
{
// we can't read from this layer
if (!layer->isReadable())
if (!layer->isVisible())
return;
switch (layer->type()) {

View File

@ -21,9 +21,9 @@ namespace doc {
ImagesCollector::ImagesCollector(Layer* layer,
FrameNumber frame,
bool allFrames,
bool forWrite)
bool forEdit)
: m_allFrames(allFrames)
, m_forWrite(forWrite)
, m_forEdit(forEdit)
{
collectFromLayer(layer, frame);
}
@ -32,10 +32,10 @@ void ImagesCollector::collectFromLayer(Layer* layer, FrameNumber frame)
{
const Sprite* sprite = layer->sprite();
if (!layer->isReadable())
if (!layer->isVisible())
return;
if (m_forWrite && !layer->isWritable())
if (m_forEdit && !layer->isEditable())
return;
switch (layer->type()) {

View File

@ -47,12 +47,12 @@ namespace doc {
// Parameters:
// * allFrames: True if you want to collect images from all frames
// or false if you need images from the given "frame" param.
// * forWrite: True if you will modify the images (it is used to avoid
// returning images from layers which are read-only/write-locked).
// * forEdit: True if you will modify the images (it is used to avoid
// returning images from layers which are read-only/write-locked).
ImagesCollector(Layer* layer,
FrameNumber frame,
bool allFrames,
bool forWrite);
bool forEdit);
ItemsIterator begin() { return m_items.begin(); }
ItemsIterator end() { return m_items.end(); }
@ -66,7 +66,7 @@ namespace doc {
Items m_items;
bool m_allFrames;
bool m_forWrite;
bool m_forEdit;
};
} // namespace doc

View File

@ -24,16 +24,15 @@ namespace doc {
Layer::Layer(ObjectType type, Sprite* sprite)
: Object(type)
, m_sprite(sprite)
, m_parent(NULL)
, m_flags(LayerFlags(
int(LayerFlags::Visible) |
int(LayerFlags::Editable)))
{
ASSERT(type == ObjectType::LayerImage || type == ObjectType::LayerFolder);
setName("Layer");
m_sprite = sprite;
m_parent = NULL;
m_flags =
LAYER_IS_READABLE |
LAYER_IS_WRITABLE;
}
Layer::~Layer()
@ -210,7 +209,7 @@ void LayerImage::configureAsBackground()
ASSERT(sprite() != NULL);
ASSERT(sprite()->backgroundLayer() == NULL);
setMoveable(false);
setMovable(false);
setBackground(true);
setName("Background");
@ -303,7 +302,7 @@ void LayerFolder::stackLayer(Layer* layer, Layer* after)
void layer_render(const Layer* layer, Image* image, int x, int y, FrameNumber frame)
{
if (!layer->isReadable())
if (!layer->isVisible())
return;
switch (layer->type()) {

View File

@ -28,11 +28,11 @@ namespace doc {
//////////////////////////////////////////////////////////////////////
// Layer class
enum {
LAYER_IS_READABLE = 0x0001,
LAYER_IS_WRITABLE = 0x0002,
LAYER_IS_LOCKMOVE = 0x0004,
LAYER_IS_BACKGROUND = 0x0008,
enum class LayerFlags {
Visible = 1, // Can be read
Editable = 2, // Can be written
LockMove = 4, // Cannot be moved
Background = 8, // Stack order cannot be changed
};
class Layer : public Object {
@ -57,18 +57,35 @@ namespace doc {
bool isImage() const { return type() == ObjectType::LayerImage; }
bool isFolder() const { return type() == ObjectType::LayerFolder; }
bool isBackground() const { return (m_flags & LAYER_IS_BACKGROUND) == LAYER_IS_BACKGROUND; }
bool isMoveable() const { return (m_flags & LAYER_IS_LOCKMOVE) == 0; }
bool isReadable() const { return (m_flags & LAYER_IS_READABLE) == LAYER_IS_READABLE; }
bool isWritable() const { return (m_flags & LAYER_IS_WRITABLE) == LAYER_IS_WRITABLE; }
void setBackground(bool b) { if (b) m_flags |= LAYER_IS_BACKGROUND; else m_flags &= ~LAYER_IS_BACKGROUND; }
void setMoveable(bool b) { if (b) m_flags &= ~LAYER_IS_LOCKMOVE; else m_flags |= LAYER_IS_LOCKMOVE; }
void setReadable(bool b) { if (b) m_flags |= LAYER_IS_READABLE; else m_flags &= ~LAYER_IS_READABLE; }
void setWritable(bool b) { if (b) m_flags |= LAYER_IS_WRITABLE; else m_flags &= ~LAYER_IS_WRITABLE; }
bool isBackground() const { return hasFlags(LayerFlags::Background); }
bool isVisible() const { return hasFlags(LayerFlags::Visible); }
bool isEditable() const { return hasFlags(LayerFlags::Editable); }
bool isMovable() const { return !hasFlags(LayerFlags::LockMove); }
uint32_t getFlags() const { return m_flags; }
void setFlags(uint32_t flags) { m_flags = flags; }
void setBackground(bool state) { switchFlags(LayerFlags::Background, state); }
void setVisible (bool state) { switchFlags(LayerFlags::Visible, state); }
void setEditable (bool state) { switchFlags(LayerFlags::Editable, state); }
void setMovable (bool state) { switchFlags(LayerFlags::LockMove, !state); }
LayerFlags flags() const {
return m_flags;
}
bool hasFlags(LayerFlags flags) const {
return (int(m_flags) & int(flags)) == int(flags);
}
void setFlags(LayerFlags flags) {
m_flags = flags;
}
void switchFlags(LayerFlags flags, bool state) {
if (state)
m_flags = LayerFlags(int(m_flags) | int(flags));
else
m_flags = LayerFlags(int(m_flags) & ~int(flags));
}
virtual void getCels(CelList& cels) const = 0;
@ -76,7 +93,7 @@ namespace doc {
std::string m_name; // layer name
Sprite* m_sprite; // owner of the layer
LayerFolder* m_parent; // parent layer
unsigned short m_flags;
LayerFlags m_flags; // stack order cannot be changed
// Disable assigment
Layer& operator=(const Layer& other);

View File

@ -35,8 +35,8 @@ void write_layer(std::ostream& os, LayerSubObjectsSerializer* subObjects, Layer*
if (!name.empty())
os.write(name.c_str(), name.size()); // Name
write32(os, layer->getFlags()); // Flags
write16(os, static_cast<int>(layer->type())); // Type
write32(os, static_cast<int>(layer->flags())); // Flags
write16(os, static_cast<int>(layer->type())); // Type
switch (layer->type()) {
@ -136,7 +136,7 @@ Layer* read_layer(std::istream& is, LayerSubObjectsSerializer* subObjects, Sprit
if (layer != NULL) {
layer->setName(&name[0]);
layer->setFlags(flags);
layer->setFlags(static_cast<LayerFlags>(flags));
}
return layer.release();