Repository navigation
Expand file tree
/
Copy pathCatGraphWindow.cs
More file actions
392 lines (336 loc) · 10.4 KB
/
Copy pathCatGraphWindow.cs
File metadata and controls
392 lines (336 loc) · 10.4 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/// Dedicated to the public domain by Christopher Diggins
/// http://creativecommons.org/licenses/publicdomain/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
namespace Cat
{
public partial class GraphWindow : Form
{
#if (!NOGRAPHICS)
List<GraphicCommand> mCmds = new List<GraphicCommand>();
Mutex mMutex = new Mutex();
public GraphWindow()
{
InitializeComponent();
}
public void ClearCmds()
{
mMutex.WaitOne();
mCmds.Clear();
mMutex.ReleaseMutex();
// Tell the parent thread to invalidate
MethodInvoker p = Invalidate;
Invoke(p);
}
public void AddCmd(GraphicCommand c)
{
mMutex.WaitOne();
try
{
mCmds.Add(c);
}
finally
{
mMutex.ReleaseMutex();
}
}
public void SaveToFile(string s)
{
mMutex.WaitOne();
try
{
Bitmap b = new Bitmap(Width, Height, CreateGraphics());
WindowGDI.Initialize(Graphics.FromImage(b));
foreach (GraphicCommand c in mCmds) {
WindowGDI.Draw(c);
}
b.Save(s);
}
finally
{
mMutex.ReleaseMutex();
}
}
public delegate void Proc();
public void SafeClose()
{
if (InvokeRequired)
{
Proc p = SafeClose;
Invoke(p, null);
}
else
{
Close();
}
}
public void SafeShow()
{
if (InvokeRequired)
{
Proc p = SafeShow;
Invoke(p, null);
}
else
{
Show();
}
}
private void GraphWindow_Paint(object sender, PaintEventArgs e)
{
mMutex.WaitOne();
try
{
Bitmap b = new Bitmap(Width, Height, CreateGraphics());
WindowGDI.Initialize(Graphics.FromImage(b));
foreach (GraphicCommand c in mCmds)
WindowGDI.Draw(c);
if (Config.gbShowTurtle)
WindowGDI.DrawTurtle();
// TODO: replace this call with a bit block transfer API call
e.Graphics.DrawImageUnscaled(b, 0, 0);
}
finally
{
mMutex.ReleaseMutex();
}
}
private void GraphWindow_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void GraphWindow_FormClosing(object sender, FormClosingEventArgs e)
{
WindowGDI.NullifyWindow();
}
#endif
}
#if (!NOGRAPHICS)
public class WindowGDI
{
static GraphWindow mWindow;
static EventWaitHandle mWait = new EventWaitHandle(false, EventResetMode.AutoReset);
static Pen mPen = new Pen(Color.Black);
static bool mbPenUp = false;
static Pen turtlePen = new Pen(Color.Blue);
static Brush turtleBrush = new SolidBrush(Color.DarkSeaGreen);
static bool mbShowTurtle = true;
static Graphics mg;
public static void Initialize(Graphics g)
{
mg = g;
//Point mOrigin = new Point(0, 0);
mPen.LineJoin = LineJoin.Bevel;
turtlePen.Width = 1;
mg = g;
mg.TranslateTransform(mWindow.Width / 2, mWindow.Height / 2);
}
private static void PrivateDraw(GraphicCommand c)
{
c.Invoke(null, typeof(WindowGDI));
}
public static void Draw(GraphicCommand c)
{
PrivateDraw(c);
}
public static void Render(GraphicCommand c)
{
if (mWindow == null)
OpenWindow();
mWindow.AddCmd(c);
}
public static void Invalidate()
{
if (mWindow != null)
mWindow.Invalidate();
}
public static void Rotate(double x)
{
mg.RotateTransform((float)x);
}
public static void Line(double x1, double y1, double x2, double y2)
{
if (!mbPenUp)
mg.DrawLine(mPen, (float)x1, (float)y1, (float)x2, (float)y2);
}
public static void Slide(double x, double y)
{
mg.TranslateTransform((float)(x), (float)(y));
}
public static void Scale(double x, double y)
{
mg.ScaleTransform((float)x, (float)y);
}
public static void SetPenUp(bool b)
{
mbPenUp = b;
}
public static bool IsPenUp()
{
return mbPenUp;
}
public static void Ellipse(double x, double y, double w, double h)
{
if (!mbPenUp)
mg.DrawEllipse(mPen, (float)x, (float)y, (float)w, (float)h);
}
public static void DrawTurtleFoot(int x, int y)
{
int footSize = 10;
Rectangle rect = new Rectangle(x - (footSize / 2), y - (footSize / 2), footSize, footSize);
mg.FillEllipse(turtleBrush, rect);
mg.DrawEllipse(turtlePen, rect);
}
public static void SetTurtleVisibility(bool b)
{
mbShowTurtle = b;
}
public static bool GetTurtleVisibility()
{
return mbShowTurtle;
}
public static void DrawTurtle()
{
if (!mbShowTurtle)
return;
int w = 26;
int h = 26;
// draw feet
int x = (h / 2) - 3;
int y = (w / 2) - 3;
DrawTurtleFoot(-x, y);
DrawTurtleFoot(-x, -y);
DrawTurtleFoot(x, y);
DrawTurtleFoot(x, -y);
// draw head
DrawTurtleFoot(w / 2 + 3, 0);
// draw eyes
Rectangle eye1 = new Rectangle(w / 2 + 4, 2, 1, 1);
Rectangle eye2 = new Rectangle(w / 2 + 4, -3, 1, 1);
mg.DrawRectangle(turtlePen, eye1);
mg.DrawRectangle(turtlePen, eye2);
// draw tail
Rectangle rect = new Rectangle(-(w/2) - 6, -2, 12, 4);
mg.FillEllipse(turtleBrush, rect);
mg.DrawEllipse(turtlePen, rect);
// draw body
rect = new Rectangle(-w / 2, -h / 2, w, h);
mg.FillEllipse(turtleBrush, rect);
mg.DrawEllipse(turtlePen, rect);
// clip everything else to the body
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(rect);
mg.SetClip(clipPath);
// stripe the body
turtlePen.Width = 2;
mg.DrawLine(turtlePen, 0, -h/2, 0, h/2);
int curvature = w / 2;
mg.DrawEllipse(turtlePen, 7, -h/2, curvature, h);
mg.DrawEllipse(turtlePen, -(curvature + 7), -h / 2, curvature, h);
mg.ResetClip();
}
public static void SetPenColor(Color x)
{
mPen.Color = x;
}
public static void SetPenWidth(int x)
{
mPen.Width = x;
}
public static void SetSolidFill(Color x)
{
mPen.Brush = new SolidBrush(x);
}
public static void ClearFill()
{
mPen.Brush = null;
}
#region color functions
static public Color Rgb(int r, int g, int b) { return Color.FromArgb(r, g, b); }
#endregion
#region public functions
static public void OpenWindow()
{
if (mWindow != null) return;
Thread t = new Thread(new ThreadStart(LaunchWindow));
t.Start();
mWait.WaitOne();
}
static public void NullifyWindow()
{
mWindow = null;
}
static public void CloseWindow()
{
if (mWindow == null) return;
mWindow.SafeClose();
}
static public void ClearWindow()
{
mWindow.ClearCmds();
}
static public void SaveToFile(string s)
{
mWindow.SaveToFile(s);
}
#endregion
static private void LaunchWindow()
{
mWindow = new GraphWindow();
mWait.Set();
Application.Run(mWindow);
}
}
public class GraphicCommand
{
public GraphicCommand(string sCmd, Object[] args)
{
msCommand = sCmd;
maArgs = args;
maArgTypes = new Type[maArgs.Length];
for (int i = 0; i < maArgs.Length; ++i)
maArgTypes[i] = maArgs[i].GetType();
}
public MethodInfo GetMethod(Type t)
{
MethodInfo ret = t.GetMethod(msCommand);
if (ret == null)
{
ret = t.GetMethod(msCommand, GetArgTypes());
if (ret == null)
throw new Exception("Could not find method " + msCommand + " on type " + t.ToString() + " with matching types");
}
return ret;
}
public Type[] GetArgTypes()
{
return maArgTypes;
}
public string GetMethodName()
{
return msCommand;
}
public Object[] GetArgs()
{
return maArgs;
}
string msCommand;
Object[] maArgs;
Type[] maArgTypes;
public void Invoke(Object o, Type t)
{
MethodInfo mi = GetMethod(t);
mi.Invoke(o, maArgs);
}
}
#endif
}