forked from genstein/trizbort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drawing.cs
246 lines (221 loc) · 9.35 KB
/
Drawing.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
/*
Copyright (c) 2010 by Genstein
This file is (or was originally) part of Trizbort, the Interactive Fiction Mapper.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using PdfSharp.Drawing;
namespace Trizbort
{
internal static class Drawing
{
static Drawing()
{
m_drawLineCursor = LoadCursor(Properties.Resources.DrawLineCursor);
m_drawLineInvertedCursor = LoadCursor(Properties.Resources.DrawLineInvertedCursor);
m_moveLineCursor = LoadCursor(Properties.Resources.MoveLineCursor);
m_moveLineInvertedCursor = LoadCursor(Properties.Resources.MoveLineInvertedCursor);
}
private static Cursor LoadCursor(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
return new Cursor(stream);
}
}
public static Cursor DrawLineCursor
{
get
{
return Drawing.IsDark(Settings.Color[Colors.Canvas]) ? m_drawLineInvertedCursor : m_drawLineCursor;
}
}
public static Cursor MoveLineCursor
{
get
{
return Drawing.IsDark(Settings.Color[Colors.Canvas]) ? m_moveLineInvertedCursor : m_moveLineCursor;
}
}
public static Rectangle ToRectangle(RectangleF rect)
{
return new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
}
public static void AddLine(XGraphicsPath path, LineSegment segment, Random random)
{
if (Settings.HandDrawn)
{
float dx = segment.End.X - segment.Start.X;
float dy = segment.End.Y - segment.Start.Y;
float distance = (float)Math.Sqrt(dx * dx + dy * dy);
int points = random.Next(Math.Max(3, (int)(distance / 15)), Math.Max(6, (int)(distance / 8)));
int lines = points - 1;
Vector last = segment.Start;
for (int line = 0; line < lines; ++line)
{
Vector next;
if (line == 0)
{
next = last;
}
else if (line == lines - 1)
{
next = segment.End;
}
else
{
float fraction = (float)line / (float)(lines - 1);
float x = segment.Start.X + (segment.End.X - segment.Start.X) * fraction;
float y = segment.Start.Y + (segment.End.Y - segment.Start.Y) * fraction;
x += random.Next(-1, 2);
y += random.Next(-1, 2);
next = new Vector(x, y);
}
path.AddLine(last.ToPointF(), next.ToPointF());
last = next;
}
}
else
{
path.AddLine(segment.Start.ToPointF(), segment.End.ToPointF());
}
}
public static void DrawHandle(Canvas canvas, XGraphics graphics, Palette palette, Rect bounds, DrawingContext context, bool alwaysAlpha, bool round)
{
if (bounds.Width <= 0 || bounds.Height <= 0)
{
return;
}
using (var quality = new Smoothing(graphics, XSmoothingMode.Default))
{
XBrush brush;
Pen pen;
var alpha = 180;
if (context.Selected)
{
if (!alwaysAlpha)
{
alpha = 255;
}
brush = palette.Gradient(bounds, Color.FromArgb(alpha, Color.LemonChiffon), Color.FromArgb(alpha, Color.DarkOrange));
pen = palette.Pen(Color.FromArgb(alpha, Color.Chocolate), 0);
}
else
{
brush = palette.Gradient(bounds, Color.FromArgb(alpha, Color.LightCyan), Color.FromArgb(alpha, Color.SteelBlue));
pen = palette.Pen(Color.FromArgb(alpha, Color.Navy), 0);
}
if (round)
{
graphics.DrawEllipse(brush, bounds.ToRectangleF());
graphics.DrawEllipse(pen, bounds.ToRectangleF());
}
else
{
graphics.DrawRectangle(brush, bounds.ToRectangleF());
graphics.DrawRectangle(pen, bounds.ToRectangleF());
}
}
}
public static Color Mix(Color a, Color b, int propA, int propB)
{
return Color.FromArgb(
(byte)(((a.R * propA) + (b.R * propB)) / (propA + propB)),
(byte)(((a.G * propA) + (b.G * propB)) / (propA + propB)),
(byte)(((a.B * propA) + (b.B * propB)) / (propA + propB)));
}
public static bool IsDark(Color color)
{
return Math.Max(color.R, Math.Max(color.G, color.B)) < 128;
}
public static PointF Subtract(PointF a, PointF b)
{
return new PointF(a.X - b.X, a.Y - b.Y);
}
public static PointF Divide(PointF pos, float scalar)
{
return new PointF(pos.X / scalar, pos.Y / scalar);
}
public static void DrawChevron(XGraphics graphics, PointF pos, float angle, float size, Brush fillBrush)
{
if (m_chevronPath == null)
{
var apex = new PointF(0.5f, 0);
var leftCorner = new PointF(-0.5f, 0.5f);
var rightCorner = new PointF(-0.5f, -0.5f);
m_chevronPath = new XGraphicsPath();
m_chevronPath.AddLine(apex, rightCorner);
m_chevronPath.AddLine(rightCorner, leftCorner);
m_chevronPath.AddLine(leftCorner, apex);
}
var state = graphics.Save();
graphics.TranslateTransform(pos.X, pos.Y);
graphics.RotateTransform(angle);
graphics.ScaleTransform(size, size);
graphics.DrawPath(fillBrush, m_chevronPath);
graphics.Restore(state);
}
public static bool SetAlignmentFromCardinalOrOrdinalDirection(XStringFormat format, CompassPoint compassPoint)
{
switch (compassPoint)
{
case CompassPoint.North:
case CompassPoint.NorthEast:
format.LineAlignment = XLineAlignment.Far;
format.Alignment = XStringAlignment.Near;
break;
case CompassPoint.East:
case CompassPoint.SouthEast:
case CompassPoint.South:
format.LineAlignment = XLineAlignment.Near;
format.Alignment = XStringAlignment.Near;
break;
case CompassPoint.West:
case CompassPoint.SouthWest:
format.LineAlignment = XLineAlignment.Near;
format.Alignment = XStringAlignment.Far;
break;
case CompassPoint.NorthWest:
format.LineAlignment = XLineAlignment.Far;
format.Alignment = XStringAlignment.Far;
break;
default:
return false;
}
return true;
}
public static string FontName(Font font)
{
if (!string.IsNullOrEmpty(font.OriginalFontName))
{
return font.OriginalFontName;
}
return font.Name;
}
private static Cursor m_drawLineCursor;
private static Cursor m_drawLineInvertedCursor;
private static Cursor m_moveLineCursor;
private static Cursor m_moveLineInvertedCursor;
private static XGraphicsPath m_chevronPath;
}
}