-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAnim.Frame.cs
More file actions
167 lines (152 loc) · 6.48 KB
/
Copy pathAnim.Frame.cs
File metadata and controls
167 lines (152 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Idmr.LfdReader.dll, Library file to read and write LFD resource files
* Copyright (C) 2009-2026 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in help/Idmr.LfdReader.chm
* Version: 3.0
*/
/* CHANGE LOG
* v3.0, 260821
* [ADD] IDisposable
* v2.0, 210309
* [FIX] getting the image when RelativePosition is used
* v1.2, 160712
* [ADD] _isModified edits
* v1.1, 141215
* [UPD] changed license to MPL
* v1.0
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Idmr.Common;
namespace Idmr.LfdReader
{
public partial class Anim : Resource
{
/// <summary>Object for the individual images in the resource.</summary>
/// <remarks>Each Frame is practically an individual <see cref="Delt"/> resource with the added consideration that is must lie within the boundaries set by the parent Anim resource.</remarks>
public class Frame : IDisposable
{
internal Anim _parent;
internal Delt _delt = new Delt();
private bool _disposed;
/// <summary>Blank constructor.</summary>
/// <param name="parent">Parent Anim resource.</param>
internal Frame(Anim parent)
{
_parent = parent;
}
/// <summary>Gets or sets the global left position of the frame.</summary>
/// <exception cref="BoundaryException">Value would result in portions of the image being outside the acceptable boundaries.</exception>
/// <remarks>If <see cref="HasFixedDimensions"/> is <see langword="false"/>, the parent dimensions will adjust as necessary.<br/>
/// Otherwise, out-of-bounds content results in an exception.</remarks>
public short Left
{
get => _delt.Left;
set
{
if (_parent.HasFixedDimensions && (value < _parent._left || value + Width > _parent._left + _parent._width))
throw new BoundaryException("Left", _parent._left + "-" + (_parent._left + _parent._width - Width));
_delt.Left = value;
_parent.recalculateDimensions();
_parent._isModified = true;
}
}
/// <summary>Gets or sets the global top position of the frame.</summary>
/// <exception cref="BoundaryException">Value would result in portions of the image being outside the acceptable boundaries.</exception>
/// <remarks>If <see cref="HasFixedDimensions"/> is <see langword="false"/>, the parent dimensions will adjust as necessary.<br/>
/// Otherwise, out-of-bounds content results in an exception.</remarks>
public short Top
{
get => _delt.Top;
set
{
if (_parent.HasFixedDimensions && (value < _parent._top || value + Height > _parent._top + _parent._height))
throw new BoundaryException("Top", _parent._top + "-" + (_parent._top + _parent._height - Height));
_delt.Top = value;
_parent.recalculateDimensions();
_parent._isModified = true;
}
}
/// <summary>Gets the width of the frame.</summary>
public short Width => _delt.Width;
/// <summary>Gets the height of the frame.</summary>
public short Height => _delt.Height;
/// <summary>Gets or sets the frame image.</summary>
/// <remarks><para><see cref="Delt.ErrorImage"/> is returned if there's an error retrieving the image.</para>
/// <para>When setting the image, if <see cref="HasFixedDimensions"/> is <see langword="false"/>, the parent dimensions will adjust as necessary.
/// Otherwise, out-of-bounds content results in an exception.
/// <i>value</i> is converted to <see cref="PixelFormat.Format8bppIndexed"/> using the current <see cref="Anim"/> palette if it exists.
/// If the palette is undefined, loading a <see cref="PixelFormat.Format8bppIndexed"/> image will set the palette for the <see cref="Anim"/>.</para></remarks>
/// <exception cref="InvalidOperationException">The image is not <see cref="PixelFormat.Format8bppIndexed"/> and the parent <see cref="Anim"/> does not have a defined palette.</exception>
/// <exception cref="BoundaryException">The image's <see cref="Size"/> would result in portions of the image being outside the acceptable boundaries.</exception>
public Bitmap Image
{
get
{
try
{
if (!_parent.RelativePosition) return _delt.Image;
else
{
int localLeft = Left - _parent.Left;
int localTop = Top - _parent.Top;
Bitmap tempImage = new Bitmap(_parent.Width, _parent.Height);
Graphics g = Graphics.FromImage(tempImage);
g.Clear(_parent._palette.Entries[0]);
g.DrawImage(_delt.Image, new Point(localLeft, localTop));
g.Dispose();
Bitmap indexedImage = GraphicsFunctions.ConvertTo8bpp(tempImage, _parent._palette);
return indexedImage;
}
}
catch { return Delt.ErrorImage; }
}
set
{
if (!_parent.HasDefinedPalette)
{
if (value.PixelFormat == PixelFormat.Format8bppIndexed) _parent._palette = value.Palette;
else throw new InvalidOperationException("Image is not Format8bppIndexed and parent Anim does not contain a defined Palette");
}
if (_parent.HasFixedDimensions)
{
if (Left + value.Width > _parent.Left + _parent.Width || Top + value.Height > _parent.Top + _parent.Height)
throw new BoundaryException("Image.Size", (_parent.Left + _parent.Width - Left) + "x" + (_parent.Top + _parent.Height - Top) + " max");
}
_delt.Image = GraphicsFunctions.ConvertTo8bpp(value, _parent._palette);
_parent.recalculateDimensions(); // even if HasFixedDimensions, Anim can shrink
}
}
/// <summary>Gets or sets the global position of the frame.</summary>
/// <exception cref="BoundaryException">The value results in portions of the image being outside the acceptable boundaries.</exception>
/// <remarks>If <see cref="HasFixedDimensions"/> is <see langword="false"/>, the parent dimensions will adjust as necessary.
/// Otherwise, out-of-bounds content results in an exception.</remarks>
public Point Position
{
get => new Point(Left, Top);
set { Left = (short)value.X; Top = (short)value.Y; }
}
#region IDisposable members
/// <summary>Clean up any resources being used.</summary>
/// <param name="disposing"><see langword="true"/> if managed resources should be disposed; otherwise, <see langword="false"/>.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing) _delt.Dispose();
_delt = null;
_parent = null;
_disposed = true;
}
/// <summary>Release all resources used.</summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
}