Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layer merge #1206

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions app/ui/mainwindow2.ui
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,11 @@
<string>Flip Rolling</string>
</property>
</action>
<action name="actionMerge_Layer">
<property name="text">
<string>Merge Layers</string>
</property>
</action>
<action name="actionPegbarAlignment">
<property name="text">
<string>Peg bar Alignment</string>
Expand Down
92 changes: 91 additions & 1 deletion core_lib/src/interface/timelinecells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ GNU General Public License for more details.
#include <QResizeEvent>
#include <QMouseEvent>
#include <QInputDialog>
#include <QMessageBox>
#include <QMenu>

#include "object.h"
#include "editor.h"
Expand Down Expand Up @@ -164,11 +166,95 @@ void TimeLineCells::updateContent()
update();
}


bool TimeLineCells::didDetatchLayer() {
return abs(getMouseMoveY()) > mLayerDetatchThreshold;
}

void TimeLineCells::showContextMenu(QPoint pos)
{
pos = this->mapToGlobal(pos);
int index = mEditor->layers()->currentLayerIndex();
bool mergeInMenu = false;
QMenu* menu = new QMenu();
if (mEditor->layers()->currentLayerIndex() > 0 &&
mEditor->layers()->currentLayer()->type() == Layer::BITMAP &&
mEditor->layers()->getLayer(index)->type() == mEditor->layers()->getLayer(index-1)->type())
{
menu->addAction(tr("Merge onto Layer: %1").arg(mEditor->layers()->getLayer(index-1)->name()), this, &TimeLineCells::mergeLayerDown);
mergeInMenu = true;
}

if (mEditor->layers()->currentLayer()->type() == Layer::BITMAP)
{
menu->addAction(tr("Merge visible bitmap layers"), this, &TimeLineCells::mergeVisibleLayers);
mergeInMenu = true;
}

if (mergeInMenu)
{
menu->addSeparator();
}

if (mEditor->layers()->currentLayer()->type() == Layer::BITMAP)
{
menu->addAction(tr("Duplicate Layer: %1").arg(mEditor->layers()->currentLayer()->name()), this, &TimeLineCells::duplicateLayer);
}
menu->addAction(tr("Delete Layer: %1").arg(mEditor->layers()->currentLayer()->name()), this, &TimeLineCells::deleteLayer);

menu->exec(pos);
}

void TimeLineCells::deleteLayer()
{
mEditor->layers()->deleteLayer(mEditor->layers()->currentLayerIndex());
}

void TimeLineCells::duplicateLayer()
{
QString newLayerName = mEditor->layers()->currentLayer()->name() + "_copy";
mEditor->layers()->createBitmapLayer(newLayerName);
mEditor->layers()->duplicateLayer(mEditor->layers()->currentLayer(), mEditor->layers()->findLayerByName(newLayerName));
}

void TimeLineCells::mergeLayerDown()
{
int index = mEditor->layers()->currentLayerIndex();
if (mEditor->layers()->getLayer(index)->type() == mEditor->layers()->getLayer(index-1)->type())
{
mEditor->layers()->mergeLayers(mEditor->layers()->currentLayer(), mEditor->layers()->getLayer(index-1));
}
mEditor->layers()->deleteLayer(index);
}

void TimeLineCells::mergeVisibleLayers()
{
QList<int> indexes;
for (int i = 0; i < mEditor->layers()->count(); i++)
{
if (mEditor->layers()->getLayer(i)->type() == Layer::BITMAP &&
mEditor->layers()->getLayer(i)->visible())
indexes.append(i);
}

if (indexes.size() < 2) { return; }

int count = indexes.size();
while (count > 1)
{
mEditor->layers()->mergeLayers(mEditor->layers()->getLayer(indexes.at(count - 1)),
mEditor->layers()->getLayer(indexes.at(count - 2)));
count--;
}

indexes.removeFirst();

while (!indexes.empty())
{
mEditor->layers()->deleteLayer(indexes.last());
indexes.removeLast();
}
}

void TimeLineCells::drawContent()
{
if (mCache == nullptr)
Expand Down Expand Up @@ -504,6 +590,10 @@ void TimeLineCells::mousePressEvent(QMouseEvent* event)
else
{
mEditor->layers()->setCurrentLayer(layerNumber);
if (event->button() == Qt::RightButton)
{
showContextMenu(event->pos());
}
}
}
if (layerNumber == -1)
Expand Down
6 changes: 6 additions & 0 deletions core_lib/src/interface/timelinecells.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class TimeLineCells : public QWidget
void paintLayerGutter(QPainter& painter);
bool didDetatchLayer();

void showContextMenu(QPoint pos);
void deleteLayer();
void duplicateLayer();
void mergeLayerDown();
void mergeVisibleLayers();

Q_SIGNALS:
void mouseMovedY(int);
void lengthChanged(int);
Expand Down
38 changes: 38 additions & 0 deletions core_lib/src/managers/layermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,44 @@ Status LayerManager::deleteLayer(int index)
return Status::OK;
}

void LayerManager::duplicateLayer(Layer *fromLayer, Layer *toLayer)
{
if (fromLayer == nullptr || toLayer == nullptr) { return; }

for (int i = fromLayer->firstKeyFramePosition(); i <= fromLayer->getMaxKeyFramePosition(); i++)
{
editor()->scrubTo(i);
if (fromLayer->keyExists(i))
{
editor()->layers()->setCurrentLayer(fromLayer);
editor()->copy();
editor()->layers()->setCurrentLayer(toLayer);
if (!toLayer->keyExists(i))
toLayer->addNewKeyFrameAt(i);
editor()->paste();
}
}
}

void LayerManager::mergeLayers(Layer *fromLayer, Layer *toLayer)
{
if (fromLayer == nullptr || toLayer == nullptr) { return; }

for (int i = fromLayer->firstKeyFramePosition(); i <= fromLayer->getMaxKeyFramePosition(); i++)
{
editor()->scrubTo(i);
if (fromLayer->keyExists(i))
{
editor()->layers()->setCurrentLayer(fromLayer);
editor()->copy();
editor()->layers()->setCurrentLayer(toLayer);
if (!toLayer->keyExists(i))
toLayer->addNewKeyFrameAt(i);
editor()->paste();
}
}
}

Status LayerManager::renameLayer(Layer* layer, const QString& newName)
{
if (newName.isEmpty()) return Status::FAIL;
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/managers/layermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class LayerManager : public BaseManager
int count();

Status deleteLayer(int index);
void duplicateLayer(Layer* fromLayer, Layer* toLayer);
void mergeLayers(Layer* fromLayer, Layer* toLayer);
Status renameLayer(Layer*, const QString& newName);
void notifyLayerChanged(Layer*);

Expand Down