Skip to content

Commit 8f09fdd

Browse files
committed
Cleanup - AnimatedPictureBox.
1 parent cc5b90d commit 8f09fdd

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

UoFiddler.Controls/UserControls/AnimatedPictureBox.cs

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,27 @@
1313
using System.Collections.Generic;
1414
using System.ComponentModel;
1515
using System.Drawing;
16+
using System.Linq;
1617
using System.Windows.Forms;
1718
using UoFiddler.Controls.Classes;
1819

1920
namespace UoFiddler.Controls.UserControls
2021
{
2122
[Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
22-
public partial class AnimatedPictureBox : PictureBox
23+
public class AnimatedPictureBox : PictureBox
2324
{
2425
private List<AnimatedFrame> _frames;
2526
private int _frameIndex;
26-
private Timer _timer;
27+
private readonly Timer _timer;
2728
private bool _animate;
2829
private bool _showFrameBounds;
2930
private Size _animationSize;
3031
private Point _drawCenter;
3132
private Point _draggedOffset = new(0, 0);
3233
private Point _mouseDownLocation;
33-
private bool _isDragging = false;
34+
private bool _isDragging;
35+
36+
public event EventHandler FrameChanged;
3437

3538
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
3639
public List<AnimatedFrame> Frames
@@ -59,23 +62,25 @@ public AnimatedFrame FirstFrame
5962
get => _frames?.FirstOrDefault();
6063
}
6164

62-
public event EventHandler FrameChanged;
6365
public AnimatedFrame CurrentFrame
6466
{
6567
get => _frames?[_frameIndex];
6668
}
69+
6770
public int FrameIndex
6871
{
6972
get => _frameIndex;
7073
set
7174
{
7275
var newValue = value % Math.Max(_frames?.Count ?? 1, 1);
73-
if (_frameIndex != newValue)
76+
if (_frameIndex == newValue)
7477
{
75-
_frameIndex = newValue;
76-
FrameChanged?.Invoke(this, EventArgs.Empty);
77-
Invalidate();
78+
return;
7879
}
80+
81+
_frameIndex = newValue;
82+
FrameChanged?.Invoke(this, EventArgs.Empty);
83+
Invalidate();
7984
}
8085
}
8186

@@ -150,64 +155,76 @@ public AnimatedPictureBox()
150155

151156
private void OnMouseDown(object sender, MouseEventArgs e)
152157
{
153-
if (e.Button == MouseButtons.Left)
158+
if (e.Button != MouseButtons.Left)
154159
{
155-
_mouseDownLocation = e.Location;
156-
_isDragging = true;
157-
Cursor = Cursors.SizeAll; // Change cursor to dragging cursor
160+
return;
158161
}
162+
163+
_mouseDownLocation = e.Location;
164+
_isDragging = true;
165+
Cursor = Cursors.SizeAll; // Change cursor to dragging cursor
159166
}
160167

161168
private void OnMouseMove(object sender, MouseEventArgs e)
162169
{
163-
if (_isDragging)
170+
if (!_isDragging)
164171
{
165-
// Calculate the new center point based on the mouse movement
166-
_draggedOffset.X += e.X - _mouseDownLocation.X;
167-
_draggedOffset.Y += e.Y - _mouseDownLocation.Y;
168-
// Update the mouse down location to the new location
169-
_mouseDownLocation = e.Location;
170-
// Refresh the PictureBox to trigger the Paint event
171-
Invalidate();
172+
return;
172173
}
174+
175+
// Calculate the new center point based on the mouse movement
176+
_draggedOffset.X += e.X - _mouseDownLocation.X;
177+
_draggedOffset.Y += e.Y - _mouseDownLocation.Y;
178+
179+
// Update the mouse down location to the new location
180+
_mouseDownLocation = e.Location;
181+
182+
// Refresh the PictureBox to trigger the Paint event
183+
Invalidate();
173184
}
174185

175186
private void OnMouseUp(object sender, MouseEventArgs e)
176187
{
177-
if (_isDragging)
188+
if (!_isDragging)
178189
{
179-
_isDragging = false;
180-
Cursor = Cursors.Default; // Change cursor back to default
190+
return;
181191
}
192+
193+
_isDragging = false;
194+
Cursor = Cursors.Default; // Change cursor back to default
182195
}
183196

184197
private void Timer_Tick(object sender, EventArgs e)
185198
{
186-
if (_frames.Count > 0)
199+
if (_frames.Count == 0)
187200
{
188-
_frameIndex = (_frameIndex + 1) % _frames.Count;
189-
FrameChanged?.Invoke(this, EventArgs.Empty);
190-
Invalidate(); // Force a repaint
201+
return;
191202
}
203+
204+
_frameIndex = (_frameIndex + 1) % _frames.Count;
205+
FrameChanged?.Invoke(this, EventArgs.Empty);
206+
Invalidate(); // Force a repaint
192207
}
193208

194209
protected override void OnPaint(PaintEventArgs e)
195210
{
196211
base.OnPaint(e);
197212
AnimatedFrame frame = _frameIndex < _frames?.Count ? _frames[_frameIndex] : null;
198-
if (frame != null)
213+
if (frame == null)
199214
{
200-
var location = new Point(
201-
_drawCenter.X - frame.Center.X + (Width - _animationSize.Width) / 2 + _draggedOffset.X,
202-
_drawCenter.Y - frame.Center.Y - frame.Bitmap.Height + (Height - _animationSize.Height) / 2 + _draggedOffset.Y
203-
);
215+
return;
216+
}
204217

205-
e.Graphics.DrawImage(frame.Bitmap, location);
218+
var location = new Point(
219+
_drawCenter.X - frame.Center.X + (Width - _animationSize.Width) / 2 + _draggedOffset.X,
220+
_drawCenter.Y - frame.Center.Y - frame.Bitmap.Height + (Height - _animationSize.Height) / 2 + _draggedOffset.Y
221+
);
206222

207-
if (_showFrameBounds)
208-
{
209-
e.Graphics.DrawRectangle(new Pen(Color.Red), new Rectangle(location, frame.Bitmap.Size));
210-
}
223+
e.Graphics.DrawImage(frame.Bitmap, location);
224+
225+
if (_showFrameBounds)
226+
{
227+
e.Graphics.DrawRectangle(new Pen(Color.Red), new Rectangle(location, frame.Bitmap.Size));
211228
}
212229
}
213230

@@ -216,6 +233,7 @@ protected override void Dispose(bool disposing)
216233
if (disposing)
217234
{
218235
_timer.Dispose();
236+
219237
if (_frames != null)
220238
{
221239
foreach (var frame in _frames)
@@ -224,23 +242,27 @@ protected override void Dispose(bool disposing)
224242
}
225243
}
226244
}
245+
227246
base.Dispose(disposing);
228247
}
229248

230249
public void Reset()
231250
{
232251
_frameIndex = 0;
252+
233253
if (_frames != null)
234254
{
235255
foreach (var frame in _frames)
236256
{
237257
frame.Bitmap?.Dispose();
238258
}
239259
}
260+
240261
_frames = [];
241262
_timer.Stop();
242263
_animate = false;
243264
_showFrameBounds = false;
265+
244266
Invalidate();
245267
}
246268
}

0 commit comments

Comments
 (0)