Avoid div by zero by preventing entering/returning a grid bounds w/zero width/height (fix #4146, #4597)

This commit is contained in:
Martín Capello 2024-08-19 18:07:40 -03:00 committed by GitHub
parent 4df11ac1e3
commit 712d84e44e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 1 deletions

View File

@ -114,6 +114,16 @@ void GridSettingsCommand::onExecute(Context* context)
window.gridY()->setTextf("%d", bounds.y);
window.gridW()->setTextf("%d", bounds.w);
window.gridH()->setTextf("%d", bounds.h);
window.gridW()->Leave.connect([&window]{
// Prevent entering a width lesser than 1
if (window.gridW()->textInt() <= 0)
window.gridW()->setText("1");
});
window.gridH()->Leave.connect([&window]{
// Prevent entering a height lesser than 1
if (window.gridH()->textInt() <= 0)
window.gridH()->setText("1");
});
window.openWindowInForeground();
if (window.closer() == window.ok()) {

View File

@ -320,6 +320,18 @@ public:
}
cursorColorType()->Change.connect([this]{ onCursorColorType(); });
// Grid
gridW()->Leave.connect([this] {
// Prevent entering a width lesser than 1
if (gridW()->textInt() <= 0)
gridW()->setText("1");
});
gridH()->Leave.connect([this] {
// Prevent entering a height lesser than 1
if (gridH()->textInt() <= 0)
gridH()->setText("1");
});
// Brush preview
brushPreview()->setSelectedItemIndex(
(int)m_pref.cursor.brushPreview());

View File

@ -35,6 +35,8 @@ bool ExprEntry::onProcessMessage(ui::Message* msg)
onFormatExprFocusLeave(buf);
if (text() != buf)
setText(buf);
Leave();
break;
}
}

View File

@ -20,6 +20,9 @@ namespace app {
int decimals() const { return m_decimals; }
void setDecimals(int decimals) { m_decimals = decimals; }
// Signals
obs::signal<void()> Leave;
protected:
bool onProcessMessage(ui::Message* msg) override;
void onChange() override;

View File

@ -49,6 +49,11 @@ gfx::Rect Sprite::DefaultGridBounds()
void Sprite::SetDefaultGridBounds(const gfx::Rect& defGridBounds)
{
g_defaultGridBounds = defGridBounds;
// Prevent setting an empty grid bounds
if (g_defaultGridBounds.w <= 0)
g_defaultGridBounds.w = 1;
if (g_defaultGridBounds.h <= 0)
g_defaultGridBounds.h = 1;
}
// static

View File

@ -127,7 +127,14 @@ namespace doc {
static void SetDefaultRgbMapAlgorithm(const RgbMapAlgorithm mapAlgo);
const gfx::Rect& gridBounds() const { return m_gridBounds; }
void setGridBounds(const gfx::Rect& rc) { m_gridBounds = rc; }
void setGridBounds(const gfx::Rect& rc) {
m_gridBounds = rc;
// Prevent setting an empty grid bounds
if (m_gridBounds.w <= 0)
m_gridBounds.w = 1;
if (m_gridBounds.h <= 0)
m_gridBounds.h = 1;
}
virtual int getMemSize() const override;