Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

fix(tools): avoid crash when changing frame with the arrows

Ahl7661dc72d7c972
diff --git a/toonz/sources/tnztools/fullcolorbrushtool.cpp b/toonz/sources/tnztools/fullcolorbrushtool.cpp
index b3a2f5888..02fc1d161 100644
--- a/toonz/sources/tnztools/fullcolorbrushtool.cpp
+++ b/toonz/sources/tnztools/fullcolorbrushtool.cpp
@@ -34,6 +34,7 @@
#include "toonz/imagestyles.h"
#include "toonz/preferences.h"
#include "toonz/toonzfolders.h"
+#include "toonz/txshsimplelevel.h"
// TnzCore includes
#include "tgl.h"
@@ -142,7 +143,8 @@ FullColorBrushTool::FullColorBrushTool(std::string name)
, m_notifier(0)
, m_presetsLoaded(false)
, m_firstTime(true)
- , m_started(false) {
+ , m_started(false)
+ , m_strokeFrameId() {
bind(TTool::RasterImage | TTool::EmptyTarget);
m_thickness.setNonLinearSlider();
@@ -272,6 +274,7 @@ void FullColorBrushTool::updateWorkAndBackupRasters(const TRect &rect) {
if (!ri) return;
TRasterP ras = ri->getRaster();
+ if (!ras || !m_workRaster || !m_backUpRas) return;
const int denominator = 8;
TRect enlargedRect = rect + m_lastRect;
@@ -317,6 +320,7 @@ bool FullColorBrushTool::askRead(const TRect &rect) { return askWrite(rect); }
bool FullColorBrushTool::askWrite(const TRect &rect) {
if (rect.isEmpty()) return true;
+ if (!m_tileSaver || !m_workRaster) return false;
m_strokeRect += rect;
m_strokeSegmentRect += rect;
updateWorkAndBackupRasters(rect);
@@ -368,6 +372,13 @@ bool FullColorBrushTool::preLeftButtonDown() {
void FullColorBrushTool::handleMouseEvent(MouseEventType type,
const TPointD &pos,
const TMouseEvent &e) {
+ if (m_skipStrokeUntilDown) {
+ if (type == ME_DOWN)
+ m_skipStrokeUntilDown = false;
+ else if (type != ME_MOVE)
+ return;
+ }
+
TTimerTicks t = TToolTimer::ticks();
bool alt = e.getModifiersMask() & TMouseEvent::ALT_KEY;
bool shift = e.getModifiersMask() & TMouseEvent::SHIFT_KEY;
@@ -396,7 +407,8 @@ void FullColorBrushTool::handleMouseEvent(MouseEventType type,
THoverList hovers(1, pos);
m_inputmanager.hoverEvent(hovers);
} else {
- bool isMyPaint = getApplication()->getCurrentLevelStyle()->getTagId() == 4001;
+ TColorStyle *levelStyle = getApplication()->getCurrentLevelStyle();
+ bool isMyPaint = levelStyle && levelStyle->getTagId() == 4001;
int deviceId = e.isTablet() ? 1 : 0;
double defPressure = isMyPaint ? 0.5 : 1.0;
bool hasPressure = e.isTablet();
@@ -496,44 +508,27 @@ void FullColorBrushTool::inputMouseMove(const TPointD &position,
//-------------------------------------------------------------------------------------------------------------
void FullColorBrushTool::inputSetBusy(bool busy) {
+ if (m_skipStrokeUntilDown && busy) return;
if (m_started == busy) return;
if (busy) {
// begin paint
TRasterImageP ri = (TRasterImageP)getImage(true);
if (!ri) return;// ri = (TRasterImageP)touchImage(); touch in preLeftButtonDown
TRasterP ras = ri->getRaster();
+ if (!ras) return;
if (!(m_workRaster && m_backUpRas)) setWorkAndBackupImages();
+ if (!m_workRaster) return;
m_workRaster->lock();
- m_tileSet = new TTileSetFullColor(ras->getSize());
- m_tileSaver = new TTileSaverFullColor(ras, m_tileSet);
+ m_tileSet = new TTileSetFullColor(ras->getSize());
+ m_tileSaver = new TTileSaverFullColor(ras, m_tileSet);
+ m_strokeFrameId = getCurrentFid();
// update color here since the current style might be switched
// with numpad shortcut keys
updateCurrentStyle();
} else {
- // end paint
- if (TRasterImageP ri = (TRasterImageP)getImage(true)) {
- TRasterP ras = ri->getRaster();
-
- m_lastRect.empty();
- m_workRaster->unlock();
-
- if (m_tileSet->getTileCount() > 0) {
- delete m_tileSaver;
- TTool::Application *app = TTool::getApplication();
- TXshLevel *level = app->getCurrentLevel()->getLevel();
- TXshSimpleLevelP simLevel = level->getSimpleLevel();
- TFrameId frameId = getCurrentFid();
- TRasterP subras = ras->extract(m_strokeRect)->clone();
- TUndoManager::manager()->add(new FullColorBrushUndo(
- m_tileSet, simLevel.getPointer(), frameId, m_isFrameCreated, subras,
- m_strokeRect.getP00()));
- }
-
- notifyImageChanged();
- m_strokeRect.empty();
- }
+ commitStroke();
}
m_started = busy;
}
@@ -551,7 +546,8 @@ void FullColorBrushTool::inputPaintTrackPoint(const TTrackPoint &point,
TRasterImageP ri = (TRasterImageP)getImage(true);
if (!ri) return;
- TRasterP ras = ri->getRaster();
+ TRasterP ras = ri->getRaster();
+ if (!ras) return;
TPointD rasCenter = ras->getCenterD();
// init brush
@@ -566,7 +562,8 @@ void FullColorBrushTool::inputPaintTrackPoint(const TTrackPoint &point,
handler = dynamic_cast<TrackHandler *>(track.handler.getPointer());
if (!handler) return;
- bool isMyPaint = getApplication()->getCurrentLevelStyle()->getTagId() == 4001;
+ TColorStyle *levelStyle = getApplication()->getCurrentLevelStyle();
+ bool isMyPaint = levelStyle && levelStyle->getTagId() == 4001;
double defPressure = isMyPaint ? 0.5 : 1.0;
double pressure = m_enabledPressure ? point.pressure : defPressure;
@@ -607,6 +604,7 @@ void FullColorBrushTool::draw() {
if (!Preferences::instance()->isCursorOutlineEnabled()) return;
TRasterP ras = ri->getRaster();
+ if (!ras) return;
double alpha = 1.0;
double alphaRadius = 3.0;
@@ -660,7 +658,57 @@ TPropertyGroup *FullColorBrushTool::getProperties(int targetType) {
//----------------------------------------------------------------------------------------------------------
-void FullColorBrushTool::onImageChanged() { setWorkAndBackupImages(); }
+void FullColorBrushTool::commitStroke() {
+ if (!m_started) return;
+
+ TFrameId frameId =
+ m_strokeFrameId.isEmptyFrame() ? getCurrentFid() : m_strokeFrameId;
+
+ TRasterImageP ri;
+ TXshSimpleLevel *sl = 0;
+ TTool::Application *app = TTool::getApplication();
+ if (app && app->getCurrentLevel()) {
+ TXshLevel *level = app->getCurrentLevel()->getLevel();
+ sl = level ? level->getSimpleLevel() : 0;
+ if (sl) ri = TRasterImageP(sl->getFrame(frameId, true));
+ }
+ if (!ri) ri = (TRasterImageP)getImage(true);
+ TRasterP ras = ri ? ri->getRaster() : TRasterP();
+
+ m_lastRect.empty();
+ if (m_workRaster) m_workRaster->unlock();
+
+ if (ras && sl && m_tileSet && m_tileSet->getTileCount() > 0) {
+ delete m_tileSaver;
+ m_tileSaver = 0;
+ TRasterP subras = ras->extract(m_strokeRect)->clone();
+ TUndoManager::manager()->add(
+ new FullColorBrushUndo(m_tileSet, sl, frameId, m_isFrameCreated, subras,
+ m_strokeRect.getP00()));
+ m_tileSet = 0;
+ } else {
+ delete m_tileSaver;
+ m_tileSaver = 0;
+ delete m_tileSet;
+ m_tileSet = 0;
+ }
+
+ m_started = false;
+ m_strokeFrameId = TFrameId();
+ m_strokeRect.empty();
+ if (ras) notifyImageChanged(frameId);
+}
+
+//----------------------------------------------------------------------------------------------------------
+
+void FullColorBrushTool::onImageChanged() {
+ m_inputmanager.reset();
+ if (m_started) {
+ commitStroke();
+ m_skipStrokeUntilDown = true;
+ }
+ setWorkAndBackupImages();
+}
//----------------------------------------------------------------------------------------------------------
@@ -668,7 +716,8 @@ void FullColorBrushTool::setWorkAndBackupImages() {
TRasterImageP ri = (TRasterImageP)getImage(false, 1);
if (!ri) return;
- TRasterP ras = ri->getRaster();
+ TRasterP ras = ri->getRaster();
+ if (!ras) return;
TDimension dim = ras->getSize();
if (!m_workRaster || m_workRaster->getLx() != dim.lx ||
diff --git a/toonz/sources/tnztools/fullcolorbrushtool.h b/toonz/sources/tnztools/fullcolorbrushtool.h
index 16ce37f1a..4f52acca9 100644
--- a/toonz/sources/tnztools/fullcolorbrushtool.h
+++ b/toonz/sources/tnztools/fullcolorbrushtool.h
@@ -115,7 +115,8 @@ public:
private:
void updateModifiers();
-
+ void commitStroke();
+
enum MouseEventType { ME_DOWN, ME_DRAG, ME_UP, ME_MOVE };
void handleMouseEvent(MouseEventType type, const TPointD &pos,
const TMouseEvent &e);
@@ -167,8 +168,10 @@ protected:
bool m_firstTime;
bool m_started;
+ TFrameId m_strokeFrameId;
- bool m_propertyUpdating = false;
+ bool m_propertyUpdating = false;
+ bool m_skipStrokeUntilDown = false;
};
//------------------------------------------------------------
diff --git a/toonz/sources/tnztools/toonzrasterbrushtool.cpp b/toonz/sources/tnztools/toonzrasterbrushtool.cpp
index 2e76f380c..f40808fcc 100644
--- a/toonz/sources/tnztools/toonzrasterbrushtool.cpp
+++ b/toonz/sources/tnztools/toonzrasterbrushtool.cpp
@@ -1113,6 +1113,13 @@ bool ToonzRasterBrushTool::preLeftButtonDown() {
void ToonzRasterBrushTool::handleMouseEvent(MouseEventType type,
const TPointD &pos,
const TMouseEvent &e) {
+ if (m_skipStrokeUntilDown) {
+ if (type == ME_DOWN)
+ m_skipStrokeUntilDown = false;
+ else if (type != ME_MOVE)
+ return;
+ }
+
TTimerTicks t = TToolTimer::ticks();
bool alt = e.getModifiersMask() & TMouseEvent::ALT_KEY;
bool shift = e.getModifiersMask() & TMouseEvent::SHIFT_KEY;
@@ -1178,6 +1185,7 @@ void ToonzRasterBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
//--------------------------------------------------------------------------------------------------
void ToonzRasterBrushTool::inputSetBusy(bool busy) {
+ if (m_skipStrokeUntilDown && busy) return;
if (m_painting.active == busy) return;
if (busy) {
@@ -1211,9 +1219,8 @@ void ToonzRasterBrushTool::inputSetBusy(bool busy) {
m_painting.frameId = getFrameId();
- TImageP img = getImage(true);
- TToonzImageP ri(img);
- TRasterCM32P ras = ri->getRaster();
+ TToonzImageP ri(getImage(true));
+ TRasterCM32P ras = ri ? ri->getRaster() : TRasterCM32P();
if (!ras) {
m_painting.active = false;
return;
@@ -1255,51 +1262,7 @@ void ToonzRasterBrushTool::inputSetBusy(bool busy) {
}
} else {
- // finish painting
-
- if (m_painting.myPaint.isActive) {
- // finish myPaint drawing
- m_workRas->unlock();
- }
-
- delete m_painting.tileSaver;
- m_painting.tileSaver = nullptr;
-
- // add undo record
- TFrameId frameId = m_painting.frameId.isEmptyFrame() ? getCurrentFid()
- : m_painting.frameId;
- if (m_painting.tileSet->getTileCount() > 0) {
- TTool::Application *app = TTool::getApplication();
- TXshLevel *level = app->getCurrentLevel()->getLevel();
- TXshSimpleLevelP simLevel = level->getSimpleLevel();
- TRasterCM32P ras = TToonzImageP(getImage(true))->getRaster();
- TRasterCM32P subras = ras->extract(m_painting.affectedRect)->clone();
- TUndoManager::manager()->add(new MyPaintBrushUndo(
- m_painting.tileSet, simLevel.getPointer(), frameId, m_isFrameCreated,
- m_isLevelCreated, subras, m_painting.affectedRect.getP00()));
- // MyPaintBrushUndo will delete tileSet by it self
- } else {
- // delete tileSet here because MyPaintBrushUndo will not do it
- delete m_painting.tileSet;
- }
- m_painting.tileSet = nullptr;
-
- // Restore gap/autoclose Fill Check
- int tc = ToonzCheck::instance()->getChecks();
- if (tc & ToonzCheck::eGap || tc & ToonzCheck::eAutoclose) invalidate();
-
- /*-- 作業中のフレームをリセット --*/
- m_painting.frameId = TFrameId();
-
- m_painting.myPaint.isActive = false;
- m_painting.pencil.isActive = false;
- m_painting.blured.isActive = false;
- m_painting.active = false;
-
- /*-- FIdを指定して、描画中にフレームが動いても、
- 描画開始時のFidのサムネイルが更新されるようにする。--*/
- notifyImageChanged(frameId);
- ToolUtils::updateSaveBox();
+ commitPainting();
}
}
@@ -1310,9 +1273,8 @@ void ToonzRasterBrushTool::inputPaintTrackPoint(const TTrackPoint &point,
bool firstTrack, bool preview) {
if (!m_painting.active || preview) return;
- TImageP img = getImage(true);
- TToonzImageP ri(img);
- TRasterCM32P ras = ri->getRaster();
+ TToonzImageP ri(getImage(true));
+ TRasterCM32P ras = ri ? ri->getRaster() : TRasterCM32P();
if (!ras) return;
TPointD rasCenter = convert(ras->getCenter());
TPointD fixedPosition = getCenteredCursorPos(point.position);
@@ -1562,6 +1524,7 @@ void ToonzRasterBrushTool::draw() {
if (TToonzImageP ti = img) {
TRasterP ras = ti->getRaster();
+ if (!ras) return;
int lx = ras->getLx();
int ly = ras->getLy();
drawEmptyCircle(m_brushPos, tround(m_minThick), lx % 2 == 0, ly % 2 == 0,
@@ -1603,7 +1566,59 @@ TPropertyGroup *ToonzRasterBrushTool::getProperties(int idx) {
//----------------------------------------------------------------------------------------------------------
+void ToonzRasterBrushTool::commitPainting() {
+ if (!m_painting.active) return;
+
+ if (m_painting.myPaint.isActive && m_workRas) m_workRas->unlock();
+
+ delete m_painting.tileSaver;
+ m_painting.tileSaver = nullptr;
+
+ TFrameId frameId =
+ m_painting.frameId.isEmptyFrame() ? getCurrentFid() : m_painting.frameId;
+ TXshSimpleLevel *sl = 0;
+ TTool::Application *app = TTool::getApplication();
+ if (app && app->getCurrentLevel()) {
+ TXshLevel *level = app->getCurrentLevel()->getLevel();
+ sl = level ? level->getSimpleLevel() : 0;
+ }
+
+ TToonzImageP ti = sl ? TToonzImageP(sl->getFrame(frameId, true))
+ : TToonzImageP(getImage(true));
+ TRasterCM32P ras = ti ? ti->getRaster() : TRasterCM32P();
+
+ if (m_painting.tileSet && m_painting.tileSet->getTileCount() > 0 && ras &&
+ sl) {
+ TRasterCM32P subras = ras->extract(m_painting.affectedRect)->clone();
+ TUndoManager::manager()->add(new MyPaintBrushUndo(
+ m_painting.tileSet, sl, frameId, m_isFrameCreated, m_isLevelCreated,
+ subras, m_painting.affectedRect.getP00()));
+ } else {
+ delete m_painting.tileSet;
+ }
+ m_painting.tileSet = nullptr;
+
+ int tc = ToonzCheck::instance()->getChecks();
+ if (tc & ToonzCheck::eGap || tc & ToonzCheck::eAutoclose) invalidate();
+
+ m_painting.frameId = TFrameId();
+ m_painting.myPaint.isActive = false;
+ m_painting.pencil.isActive = false;
+ m_painting.blured.isActive = false;
+ m_painting.active = false;
+
+ notifyImageChanged(frameId);
+ ToolUtils::updateSaveBox();
+}
+
+//----------------------------------------------------------------------------------------------------------
+
void ToonzRasterBrushTool::onImageChanged() {
+ m_inputmanager.reset();
+ if (m_painting.active) {
+ commitPainting();
+ m_skipStrokeUntilDown = true;
+ }
if (!isEnabled()) return;
setWorkAndBackupImages();
}
@@ -1613,7 +1628,8 @@ void ToonzRasterBrushTool::onImageChanged() {
void ToonzRasterBrushTool::setWorkAndBackupImages() {
TToonzImageP ti = (TToonzImageP)getImage(false, 1);
if (!ti) return;
- TRasterP ras = ti->getRaster();
+ TRasterP ras = ti->getRaster();
+ if (!ras) return;
TDimension dim = ras->getSize();
if (!m_workRas || m_workRas->getLx() < dim.lx || m_workRas->getLy() < dim.ly)
diff --git a/toonz/sources/tnztools/toonzrasterbrushtool.h b/toonz/sources/tnztools/toonzrasterbrushtool.h
index 0f4efb0b0..a2df069de 100644
--- a/toonz/sources/tnztools/toonzrasterbrushtool.h
+++ b/toonz/sources/tnztools/toonzrasterbrushtool.h
@@ -196,6 +196,7 @@ public:
private:
void updateModifiers();
+ void commitPainting();
enum MouseEventType { ME_DOWN, ME_DRAG, ME_UP, ME_MOVE };
void handleMouseEvent(MouseEventType type, const TPointD &pos,
@@ -314,7 +315,8 @@ protected:
QElapsedTimer m_brushTimer;
int m_minCursorThick, m_maxCursorThick;
- bool m_propertyUpdating = false;
+ bool m_propertyUpdating = false;
+ bool m_skipStrokeUntilDown = false;
protected:
static void drawLine(const TPointD &point, const TPointD &centre,
diff --git a/toonz/sources/tnztools/toonzvectorbrushtool.cpp b/toonz/sources/tnztools/toonzvectorbrushtool.cpp
index 30b27455d..09a3bad25 100644
--- a/toonz/sources/tnztools/toonzvectorbrushtool.cpp
+++ b/toonz/sources/tnztools/toonzvectorbrushtool.cpp
@@ -729,6 +729,14 @@ void ToonzVectorBrushTool::onActivate() {
//--------------------------------------------------------------------------------------------------
+void ToonzVectorBrushTool::onImageChanged() {
+ m_inputmanager.reset();
+ m_active = false;
+ m_tracks.clear();
+}
+
+//-----------------------------------------------------------------------------
+
void ToonzVectorBrushTool::onDeactivate() {
/*---
* ドラッグ中にツールが切り替わった場合に備え、onDeactivateにもMouseReleaseと同じ処理を行う
@@ -899,6 +907,7 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
TStroke *stroke = m_tracks.front().makeStroke(error);
TVectorImageP vi = getImage(true);
+ if (!vi) return;
if (!isJustCreatedSpline(vi.getPointer())) {
m_currentColor = TPixel32::Green;
@@ -927,6 +936,7 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
// paint regular strokes
TVectorImageP vi = getImage(true);
+ if (!vi) return;
QMutexLocker lock(vi->getMutex());
TTool::Application *app = TTool::getApplication();
diff --git a/toonz/sources/tnztools/toonzvectorbrushtool.h b/toonz/sources/tnztools/toonzvectorbrushtool.h
index 0c8006338..357d892ac 100644
--- a/toonz/sources/tnztools/toonzvectorbrushtool.h
+++ b/toonz/sources/tnztools/toonzvectorbrushtool.h
@@ -135,6 +135,7 @@ public:
void onActivate() override;
void onDeactivate() override;
+ void onImageChanged() override;
bool preLeftButtonDown() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;