FontEditor: Save discrete undo and redo states for each Command

Makes undo/redo actions compatible with the updated UndoStack sans
finalization. Fixes having to click actions twice.
This commit is contained in:
thankyouverycool 2021-08-26 12:01:52 -04:00 committed by Andreas Kling
parent 92fb2e2a28
commit a621932c11
4 changed files with 13 additions and 16 deletions

View file

@ -345,8 +345,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&&
did_modify_font();
};
m_glyph_editor_widget->on_undo_event = [this](bool) {
// FIXME: UndoStack no longer has finalization concept, so this needs some fixing.
m_glyph_editor_widget->on_undo_event = [this] {
m_undo_stack->push(make<GlyphUndoCommand>(*m_undo_glyph));
did_change_undo_stack();
};

View file

@ -38,13 +38,11 @@ void GlyphEditorWidget::set_glyph(int glyph)
void GlyphEditorWidget::delete_glyph()
{
if (on_undo_event)
on_undo_event(false);
on_undo_event();
auto bitmap = font().glyph(m_glyph).glyph_bitmap();
for (int x = 0; x < bitmap.width(); x++)
for (int y = 0; y < bitmap.height(); y++)
bitmap.set_bit_at(x, y, false);
if (on_undo_event)
on_undo_event(true);
if (on_glyph_altered)
on_glyph_altered(m_glyph);
update();
@ -93,7 +91,7 @@ void GlyphEditorWidget::paste_glyph()
return;
if (on_undo_event)
on_undo_event(false);
on_undo_event();
auto byte_buffer = GUI::Clipboard::the().data();
auto buffer_height = GUI::Clipboard::the().data_and_type().metadata.get("height").value().to_int();
@ -115,8 +113,7 @@ void GlyphEditorWidget::paste_glyph()
bitmap.set_bit_at(x, y, bits[x][y]);
}
}
if (on_undo_event)
on_undo_event(true);
if (on_glyph_altered)
on_glyph_altered(m_glyph);
update();
@ -175,7 +172,7 @@ void GlyphEditorWidget::mousedown_event(GUI::MouseEvent& event)
return;
m_is_clicking_valid_cell = true;
if (on_undo_event)
on_undo_event(false);
on_undo_event();
if (mode() == Paint) {
draw_at_mouse(event);
} else {
@ -195,8 +192,6 @@ void GlyphEditorWidget::mouseup_event(GUI::MouseEvent&)
if (!m_is_clicking_valid_cell)
return;
m_is_clicking_valid_cell = false;
if (on_undo_event)
on_undo_event(true);
}
void GlyphEditorWidget::mousemove_event(GUI::MouseEvent& event)

View file

@ -47,7 +47,7 @@ public:
void set_mode(Mode mode) { m_mode = mode; }
Function<void(int)> on_glyph_altered;
Function<void(bool finalize)> on_undo_event;
Function<void()> on_undo_event;
private:
GlyphEditorWidget() {};

View file

@ -46,20 +46,23 @@ private:
class GlyphUndoCommand : public GUI::Command {
public:
GlyphUndoCommand(UndoGlyph& glyph)
: m_state(glyph.save_state())
: m_undo_state(glyph.save_state())
, m_undo_glyph(glyph)
{
}
virtual void undo() override
{
m_undo_glyph.restore_state(*m_state);
if (!m_redo_state)
m_redo_state = m_undo_state->save_state();
m_undo_glyph.restore_state(*m_undo_state);
}
virtual void redo() override
{
undo();
m_undo_glyph.restore_state(*m_redo_state);
}
private:
RefPtr<UndoGlyph> m_state;
RefPtr<UndoGlyph> m_undo_state;
RefPtr<UndoGlyph> m_redo_state;
UndoGlyph& m_undo_glyph;
};