Skip to content

Commit 04c39d2

Browse files
committed
fix null ref on bitmap clone, adding circular undo buffer
1 parent 7ce74f2 commit 04c39d2

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

PixelArtTool/MainWindow.xaml.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ void Start()
157157
w.MouseWheel += new MouseWheelEventHandler(DrawingMouseWheel);
158158
drawingImage.MouseUp += new MouseButtonEventHandler(DrawingMouseUp);
159159

160+
// FIXME init undos
161+
for (int i = 0; i < maxUndoCount; i++)
162+
{
163+
undoBufferBitmap[i] = canvasBitmap.Clone();
164+
}
165+
166+
160167
// build palette
161168
paletteImage = imgPalette;
162169
RenderOptions.SetBitmapScalingMode(paletteImage, BitmapScalingMode.NearestNeighbor);
@@ -432,7 +439,15 @@ void PaletteLeftButtonDown(object sender, MouseButtonEventArgs e)
432439

433440
void DrawingRightButtonDown(object sender, MouseButtonEventArgs e)
434441
{
435-
ErasePixel(e);
442+
int x = (int)(e.GetPosition(drawingImage).X / canvasScaleX);
443+
int y = (int)(e.GetPosition(drawingImage).Y / canvasScaleX);
444+
445+
ErasePixel(x, y);
446+
447+
if (chkMirrorX.IsChecked == true)
448+
{
449+
ErasePixel(canvasResolutionX - x, y);
450+
}
436451
}
437452

438453
void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
@@ -451,9 +466,11 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
451466
void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
452467
{
453468
// undo test
454-
undoBufferBitmap[currentUndoIndex++] = canvasBitmap.Clone();
469+
//undoBufferBitmap[currentUndoIndex++] = canvasBitmap.Clone();
470+
currentUndoIndex = ++currentUndoIndex % maxUndoCount; // wrap
471+
CopyBitmapPixels(canvasBitmap, undoBufferBitmap[currentUndoIndex]);
455472

456-
// FIXME if undobuffer enabled above, sometimes Exception thrown: 'System.IndexOutOfRangeException' in PixelArtTool.exe
473+
// FIXME if undobuffer clone enabled above, sometimes Exception thrown: 'System.IndexOutOfRangeException' in PixelArtTool.exe
457474
// An unhandled exception of type 'System.IndexOutOfRangeException' occurred in PixelArtTool.exe
458475
// Index was outside the bounds of the array.
459476
// Console.WriteLine(drawingImage);
@@ -469,11 +486,15 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
469486
// mirror
470487
if (chkMirrorX.IsChecked == true)
471488
{
472-
DrawPixel(canvasResolutionX - x - 1, y);
489+
DrawPixel(canvasResolutionX - x, y);
473490
}
474491
break;
475492
case ToolMode.Fill:
476493
FloodFill(x, y, (int)currentColor.ColorBGRA);
494+
if (chkMirrorX.IsChecked == true)
495+
{
496+
FloodFill(canvasResolutionX - x, y, (int)currentColor.ColorBGRA);
497+
}
477498
break;
478499
default:
479500
break;
@@ -506,11 +527,15 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
506527
// mirror
507528
if (chkMirrorX.IsChecked == true)
508529
{
509-
DrawPixel(canvasResolutionX - x - 1, y);
530+
DrawPixel(canvasResolutionX - x, y);
510531
}
511532
break;
512533
case ToolMode.Fill:
513534
FloodFill(x, y, (int)currentColor.ColorBGRA);
535+
if (chkMirrorX.IsChecked == true)
536+
{
537+
FloodFill(canvasResolutionX - x, y, (int)currentColor.ColorBGRA);
538+
}
514539
break;
515540
default:
516541
break;
@@ -523,7 +548,7 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
523548
// mirror
524549
if (chkMirrorX.IsChecked == true)
525550
{
526-
ErasePixel(canvasResolutionX - x - 1, y);
551+
ErasePixel(canvasResolutionX - x, y);
527552
}
528553
}
529554
else if (e.MiddleButton == MouseButtonState.Pressed)
@@ -711,10 +736,10 @@ private void OnKeyUp(object sender, KeyEventArgs e)
711736

712737
private void CallUndo()
713738
{
714-
if (currentUndoIndex > 0)
715-
{
716-
CopyBitmapPixels(undoBufferBitmap[--currentUndoIndex], canvasBitmap);
717-
}
739+
currentUndoIndex--;
740+
// TODO: only wrap to current last active index
741+
if (currentUndoIndex < 0) currentUndoIndex += maxUndoCount; // wrap
742+
CopyBitmapPixels(undoBufferBitmap[currentUndoIndex], canvasBitmap);
718743
}
719744

720745
void CopyBitmapPixels(WriteableBitmap source, WriteableBitmap target)

0 commit comments

Comments
 (0)