-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathFrame.cs
348 lines (280 loc) · 8.69 KB
/
Frame.cs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Collections;
namespace SpiffCode
{
/// <summary>
/// Summary description for Frame.
/// </summary>
[Serializable]
public class Frame : ISerializable, ICloneable
{
private int m_cHold = 0;
private Point m_ptSpecial;
private BitmapPlacerList m_plcl;
public Frame() {
m_plcl = new BitmapPlacerList();
}
public int HoldCount {
set {
m_cHold = value;
}
get {
return m_cHold;
}
}
public Point SpecialPoint {
set {
m_ptSpecial = value;
}
get {
return m_ptSpecial;
}
}
public BitmapPlacerList BitmapPlacers {
get {
return m_plcl;
}
}
// Deep copy
public object Clone() {
Frame fr = new Frame();
fr.m_cHold = m_cHold;
fr.m_ptSpecial = new Point(m_ptSpecial.X, m_ptSpecial.Y);
fr.m_plcl = (BitmapPlacerList)m_plcl.Clone();
return fr;
}
public struct DrawArgs { // drwa
public int nScale;
public bool fShowGrid;
public bool fShowOrigin;
public bool fShowSpecialPoint;
public bool fMapSideColors;
public bool fDrawBackground;
public Color clrBackground;
public int cxGrid;
public int cyGrid;
}
public void Draw(Graphics g, Rectangle rcClient, DrawArgs drwa, Point ptOffset) {
int xCenter = rcClient.Width / 2;
int yCenter = rcClient.Height / 2;
int cxT = ((rcClient.Width + drwa.nScale - 1) / drwa.nScale) + 2;
int cyT = ((rcClient.Height + drwa.nScale - 1) / drwa.nScale) + 2;
int xCenterT = cxT / 2;
int yCenterT = cyT / 2;
// NOTE: these 'using' statements (a 'shortcut' for calling .Dispose()) are
// absolutely necessary or we chew up all virtual memory while animating
// Create a temporary bitmap for compositing the grid, frames, origin indicator, etc into
using (Bitmap bmT = new Bitmap(cxT, cyT)) {
// Draw the frame and its indicators (grid, center point, special point, etc)
DrawUnscaled(bmT, cxT, cyT, drwa, ptOffset);
// Force a nice simple fast old-school stretchblt
InterpolationMode imOld = g.InterpolationMode;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
// NOTE: _without_ this the first row and column are only scaled by half!
PixelOffsetMode pomOld = g.PixelOffsetMode;
g.PixelOffsetMode = PixelOffsetMode.Half;
// StretchBlt the temporary composite to the passed-in Graphic
g.DrawImage(bmT, rcClient.Left - ((xCenterT * drwa.nScale) - xCenter),
rcClient.Top - ((yCenterT * drwa.nScale) - yCenter),
cxT * drwa.nScale, cyT * drwa.nScale);
g.PixelOffsetMode = pomOld;
g.InterpolationMode = imOld;
}
}
public void DrawUnscaled(Bitmap bmDst, int cx, int cy, DrawArgs drwa, Point ptOffset) {
Graphics gDst = Graphics.FromImage(bmDst);
int xCenter = cx / 2;
int yCenter = cy / 2;
// Draw background (if enabled)
if (drwa.fDrawBackground)
gDst.Clear(drwa.clrBackground);
#if false
// Draw background bitmap, if any
if (m_bmBackground != null)
gT.DrawImage(m_bmBackground, xCenter - (m_bmBackground.Width / 2) + m_ptBackgroundOffset.X,
yCenter - (m_bmBackground.Height / 2) + m_ptBackgroundOffset.Y,
m_bmBackground.Width, m_bmBackground.Height);
#endif
// Draw grid (if enabled)
// UNDONE: use alpha to draw grid (e.g., brighten or darken)
if (drwa.fShowGrid) {
int cxGrid = drwa.cxGrid;
int cyGrid = drwa.cyGrid;
// Brush br = new SolidBrush(Color.FromKnownColor(KnownColor.LightGray));
Brush br = new SolidBrush(Color.FromArgb(256 / 3, 255, 255, 255));
for (int x = (xCenter + ptOffset.X) % cxGrid; x < cx; x += cxGrid)
gDst.FillRectangle(br, x, 0, 1, cy);
for (int y = (yCenter + ptOffset.Y) % cyGrid; y < cy; y += cyGrid)
gDst.FillRectangle(br, 0, y, cx, 1);
}
BitmapData bmdDst = bmDst.LockBits(
new Rectangle(0, 0, bmDst.Width, bmDst.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// Draw bitmaps from bottom up
for (int i = BitmapPlacers.Count - 1; i >= 0; i--) {
BitmapPlacer plc = BitmapPlacers[i];
XBitmap xbm = plc.XBitmap;
xbm.SuperBlt(0, 0, bmdDst,
xCenter - plc.X + ptOffset.X,
yCenter - plc.Y + ptOffset.Y,
xbm.Bitmap.Width, xbm.Bitmap.Height,
drwa.fMapSideColors);
}
bmDst.UnlockBits(bmdDst);
// Draw origin point (if enabled)
if (drwa.fShowOrigin) {
// This is really weird but if we don't do our own clipping then SetPixel will
// raise an exception!
int x = xCenter + ptOffset.X;
int y = yCenter + ptOffset.Y;
if (x >= 0 && y >= 0 && x < bmDst.Width && y < bmDst.Height)
bmDst.SetPixel(x, y, Color.FromKnownColor(KnownColor.Orange));
}
// Draw special point (if enabled)
if (drwa.fShowSpecialPoint) {
// This is really weird but if we don't do our own clipping then SetPixel will
// raise an exception!
int x = xCenter + ptOffset.X + m_ptSpecial.X;
int y = yCenter + ptOffset.Y + m_ptSpecial.Y;
if (x >= 0 && y >= 0 && x < bmDst.Width && y < bmDst.Height)
bmDst.SetPixel(x, y, Color.FromArgb(0, 255, 0));
}
gDst.Dispose();
}
// ISerializable interface implementation
private Frame(SerializationInfo seri, StreamingContext stmc) : this() {
try {
m_plcl = (BitmapPlacerList)seri.GetValue("BitmapPlacers", typeof(BitmapPlacerList));
} catch {
m_plcl = new BitmapPlacerList();
m_plcl.Add(new BitmapPlacer());
m_plcl[0].X = seri.GetInt32("BitmapX");
m_plcl[0].Y = seri.GetInt32("BitmapY");
m_plcl[0].XBitmap = (XBitmap)seri.GetValue("XBitmap", typeof(XBitmap));
}
try {
m_cHold = seri.GetInt32("HoldCount");
} catch {}
try {
m_ptSpecial = (Point)seri.GetValue("SpecialPoint", typeof(Point));
} catch {}
}
void ISerializable.GetObjectData(SerializationInfo seri, StreamingContext stmc) {
seri.AddValue("BitmapPlacers", m_plcl);
seri.AddValue("HoldCount", m_cHold);
seri.AddValue("SpecialPoint", m_ptSpecial);
}
}
[Serializable]
public class BitmapPlacerList : CollectionBase, ISerializable, ICloneable {
public BitmapPlacerList() {
}
// Automatically expand the collection to include as many
// elements as the caller is expecting.
public BitmapPlacer this[int i] {
get {
// while (i >= InnerList.Count)
// InnerList.Add(new BitmapPlacer());
return (BitmapPlacer)InnerList[i];
}
set {
// while (i >= InnerList.Count)
// InnerList.Add(new BitmapPlacer());
InnerList[i] = value;
}
}
public int Add(BitmapPlacer plc) {
return ((IList)this).Add(plc);
}
public void Insert(int iplc, BitmapPlacer plc) {
InnerList.Insert(iplc, plc);
}
public int Index(BitmapPlacer plc) {
for (int i = 0; i < InnerList.Count; i++) {
if (plc == (BitmapPlacer)InnerList[i]) {
return i;
}
}
return -1;
}
// Deep copy
public object Clone() {
BitmapPlacerList plcl = new BitmapPlacerList();
for (int i = 0; i < InnerList.Count; i++) {
plcl.Add((BitmapPlacer)((BitmapPlacer)InnerList[i]).Clone());
}
return plcl;
}
// ISerializable interface implementation
private BitmapPlacerList(SerializationInfo seri, StreamingContext stmc) : this() {
for (int i = 0; i < seri.MemberCount; i++) {
BitmapPlacer plc = (BitmapPlacer)seri.GetValue(i.ToString(), typeof(BitmapPlacer));
InnerList.Add(plc);
}
}
void ISerializable.GetObjectData(SerializationInfo seri, StreamingContext stmc) {
int i = 0;
foreach (BitmapPlacer plc in InnerList) {
seri.AddValue(i.ToString(), plc);
i++;
}
}
}
[Serializable]
public class BitmapPlacer : ISerializable, ICloneable {
private Point m_pt;
private XBitmap m_xbm;
public BitmapPlacer() {
}
public Point Point {
get {
return m_pt;
}
set {
m_pt = value;
}
}
public int X {
get {
return m_pt.X;
}
set {
m_pt.X = value;
}
}
public int Y {
get {
return m_pt.Y;
}
set {
m_pt.Y = value;
}
}
public XBitmap XBitmap {
get {
return m_xbm;
}
set {
m_xbm = value;
}
}
public object Clone() {
BitmapPlacer plc = new BitmapPlacer();
plc.m_pt = new Point(m_pt.X, m_pt.Y);
plc.m_xbm = m_xbm.Clone();
return plc;
}
private BitmapPlacer(SerializationInfo seri, StreamingContext stmc) : this() {
m_pt = (Point)seri.GetValue("Point", typeof(Point));
m_xbm = (XBitmap)seri.GetValue("XBitmap", typeof(XBitmap));
}
void ISerializable.GetObjectData(SerializationInfo seri, StreamingContext stmc) {
seri.AddValue("Point", m_pt);
seri.AddValue("XBitmap", XBitmap);
}
}
}