Skip to content

Commit f872163

Browse files
committed
Grid now shows two steps of lines + changed colors (suggested by ryan jon, thanks!)
Also made it easier to change these colors from within the winforms editor. (I really should make *all* colors modifiable from the winforms editor)
1 parent 00124c6 commit f872163

File tree

4 files changed

+102
-37
lines changed

4 files changed

+102
-37
lines changed

Graph/GraphControl.cs

Lines changed: 90 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,71 @@ public GraphControl()
5858
public event EventHandler<NodeConnectionEventArgs> ConnectionRemoved;
5959

6060
#region Grid
61-
public bool ShowGrid = true;
62-
public float GridStep = 16.0f;
63-
private Color internalGridColor = Color.LightGray;
64-
private Pen GridPen = new Pen(Color.LightGray);
65-
public Color GridColor
61+
public bool ShowGrid = true;
62+
public float internalSmallGridStep = 16.0f;
63+
[Description("The distance between the smallest grid lines"), Category("Appearance")]
64+
public float SmallGridStep
65+
{
66+
get
67+
{
68+
return internalSmallGridStep;
69+
}
70+
set
71+
{
72+
if (internalSmallGridStep == value)
73+
return;
74+
75+
internalSmallGridStep = value;
76+
this.Invalidate();
77+
}
78+
}
79+
public float internalLargeGridStep = 16.0f * 8.0f;
80+
[Description("The distance between the largest grid lines"), Category("Appearance")]
81+
public float LargeGridStep
82+
{
83+
get
84+
{
85+
return internalLargeGridStep;
86+
}
87+
set
88+
{
89+
if (internalLargeGridStep == value)
90+
return;
91+
92+
internalLargeGridStep = value;
93+
this.Invalidate();
94+
}
95+
}
96+
private Color internalSmallStepGridColor = Color.Gray;
97+
private Pen SmallGridPen = new Pen(Color.Gray);
98+
[Description("The color for the grid lines with the smallest gap between them"), Category("Appearance")]
99+
public Color SmallStepGridColor
66100
{
67-
get { return internalGridColor; }
101+
get { return internalSmallStepGridColor; }
68102
set
69103
{
70-
if (internalGridColor == value)
104+
if (internalSmallStepGridColor == value)
71105
return;
72106

73-
internalGridColor = value;
74-
GridPen = new Pen(internalGridColor);
107+
internalSmallStepGridColor = value;
108+
SmallGridPen = new Pen(internalSmallStepGridColor);
109+
this.Invalidate();
110+
}
111+
}
112+
private Color internalLargeStepGridColor = Color.LightGray;
113+
private Pen LargeGridPen = new Pen(Color.LightGray);
114+
[Description("The color for the grid lines with the largest gap between them"), Category("Appearance")]
115+
public Color LargeStepGridColor
116+
{
117+
get { return internalLargeStepGridColor; }
118+
set
119+
{
120+
if (internalLargeStepGridColor == value)
121+
return;
122+
123+
internalLargeStepGridColor = value;
124+
LargeGridPen = new Pen(internalLargeStepGridColor);
125+
this.Invalidate();
75126
}
76127
}
77128
#endregion
@@ -114,6 +165,7 @@ IElement HoverElement
114165

115166
#region FocusElement
116167
IElement internalFocusElement;
168+
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
117169
public IElement FocusElement
118170
{
119171
get { return internalFocusElement; }
@@ -356,6 +408,7 @@ bool HasFocus(IElement element)
356408

357409
#region ShowLabels
358410
bool internalShowLabels = false;
411+
[Description("Show labels on the lines that connect the graph nodes"), Category("Appearance")]
359412
public bool ShowLabels
360413
{
361414
get
@@ -384,12 +437,14 @@ public bool ShowLabels
384437
/// <summary>
385438
/// The strategy that will be applied to determine if two node item connectors are compatible with each other
386439
/// </summary>
440+
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
387441
public ICompatibilityStrategy CompatibilityStrategy { get; set; }
388442
#endregion
389443

390444

391445
#region Nodes
392446
readonly List<Node> graphNodes = new List<Node>();
447+
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
393448
public IEnumerable<Node> Nodes { get { return graphNodes; } }
394449
#endregion
395450

@@ -938,7 +993,7 @@ protected override void OnPaint(PaintEventArgs e)
938993
#region OnDrawBackground
939994
virtual protected void OnDrawBackground(PaintEventArgs e)
940995
{
941-
e.Graphics.Clear(Color.White);
996+
e.Graphics.Clear(BackColor);
942997

943998
if (!ShowGrid)
944999
return;
@@ -950,22 +1005,35 @@ virtual protected void OnDrawBackground(PaintEventArgs e)
9501005

9511006
inverse_transformation.TransformPoints(points);
9521007

953-
var left = points[0].X;
954-
var right = points[1].X;
955-
var top = points[0].Y;
956-
var bottom = points[1].Y;
957-
var stepScaled = GridStep;
1008+
var left = points[0].X;
1009+
var right = points[1].X;
1010+
var top = points[0].Y;
1011+
var bottom = points[1].Y;
1012+
var smallStepScaled = SmallGridStep;
9581013

959-
var xOffset = ((float)Math.Round(left / stepScaled) * stepScaled);
960-
var yOffset = ((float)Math.Round(top / stepScaled) * stepScaled);
1014+
var smallXOffset = ((float)Math.Round(left / smallStepScaled) * smallStepScaled);
1015+
var smallYOffset = ((float)Math.Round(top / smallStepScaled) * smallStepScaled);
1016+
1017+
if (smallStepScaled > 3)
1018+
{
1019+
for (float x = smallXOffset; x < right; x += smallStepScaled)
1020+
e.Graphics.DrawLine(SmallGridPen, x, top, x, bottom);
1021+
1022+
for (float y = smallYOffset; y < bottom; y += smallStepScaled)
1023+
e.Graphics.DrawLine(SmallGridPen, left, y, right, y);
1024+
}
1025+
1026+
var largeStepScaled = LargeGridStep;
1027+
var largeXOffset = ((float)Math.Round(left / largeStepScaled) * largeStepScaled);
1028+
var largeYOffset = ((float)Math.Round(top / largeStepScaled) * largeStepScaled);
9611029

962-
if (stepScaled > 3)
1030+
if (largeStepScaled > 3)
9631031
{
964-
for (float x = xOffset; x < right; x += stepScaled)
965-
e.Graphics.DrawLine(GridPen, x, top, x, bottom);
1032+
for (float x = largeXOffset; x < right; x += largeStepScaled)
1033+
e.Graphics.DrawLine(LargeGridPen, x, top, x, bottom);
9661034

967-
for (float y = yOffset; y < bottom; y += stepScaled)
968-
e.Graphics.DrawLine(GridPen, left, y, right, y);
1035+
for (float y = largeYOffset; y < bottom; y += largeStepScaled)
1036+
e.Graphics.DrawLine(LargeGridPen, left, y, right, y);
9691037
}
9701038
}
9711039
#endregion

Graph/GraphRenderer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static void RenderItem(Graphics graphics, SizeF minimumSize, NodeItem item, Poin
7878
item.Render(graphics, minimumSize, position);
7979
}
8080

81+
private static Pen BorderPen = new Pen(Color.FromArgb(64, 64, 64));
82+
8183
static void RenderConnector(Graphics graphics, RectangleF bounds, RenderState state)
8284
{
8385
using (var brush = new SolidBrush(GetArrowLineColor(state)))
@@ -273,7 +275,7 @@ static void Render(Graphics graphics, Node node)
273275
{
274276
graphics.FillPath(Brushes.LightGray, path);
275277
}
276-
graphics.DrawPath(Pens.DarkGray, path);
278+
graphics.DrawPath(BorderPen, path);
277279
}
278280
/*
279281
if (!node.Collapsed)

GraphExample/ExampleForm.Designer.cs

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GraphExample/ExampleForm.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,4 @@
120120
<metadata name="nodeMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
121121
<value>17, 17</value>
122122
</metadata>
123-
<metadata name="nodeToolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
124-
<value>128, 17</value>
125-
</metadata>
126123
</root>

0 commit comments

Comments
 (0)