Skip to content

Commit ff4e804

Browse files
NumericSlider now inherits from Slider
1 parent 6782a57 commit ff4e804

File tree

2 files changed

+28
-128
lines changed

2 files changed

+28
-128
lines changed

Graph/Items/NodeNumericSliderItem.cs

Lines changed: 23 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -21,134 +21,34 @@
2121
#endregion
2222

2323
using System;
24-
using System.Collections.Generic;
25-
using System.Linq;
26-
using System.Text;
27-
using System.Windows.Forms;
2824
using System.Drawing;
29-
using System.Drawing.Drawing2D;
3025

3126
namespace Graph.Items
3227
{
33-
public sealed class NodeNumericSliderItem : NodeItem
28+
/// <summary>
29+
/// An item that contains a slider which displays its value as a text on the slider itself
30+
/// </summary>
31+
public sealed class NodeNumericSliderItem : NodeSliderItem
3432
{
35-
public event EventHandler<NodeItemEventArgs> Clicked;
36-
public event EventHandler<NodeItemEventArgs> ValueChanged;
37-
38-
public NodeNumericSliderItem(string text, float sliderSize, float textSize, float minValue, float maxValue, float defaultValue, bool inputEnabled, bool outputEnabled) :
39-
base(inputEnabled, outputEnabled)
40-
{
41-
this.Text = text;
42-
this.MinimumSliderSize = sliderSize;
43-
this.TextSize = textSize;
44-
this.MinValue = Math.Min(minValue, maxValue);
45-
this.MaxValue = Math.Max(minValue, maxValue);
46-
this.Value = defaultValue;
47-
}
48-
49-
#region Text
50-
string internalText = string.Empty;
51-
public string Text
52-
{
53-
get { return internalText; }
54-
set
55-
{
56-
if (internalText == value)
57-
return;
58-
internalText = value;
59-
itemSize = Size.Empty;
60-
}
61-
}
62-
#endregion
63-
64-
#region Dragging
65-
internal bool Dragging { get; set; }
66-
#endregion
67-
68-
public float MinimumSliderSize { get; set; }
69-
public float TextSize { get; set; }
70-
71-
public float MinValue { get; set; }
72-
public float MaxValue { get; set; }
73-
74-
float internalValue = 0.0f;
75-
public float Value
76-
{
77-
get { return internalValue; }
78-
set
79-
{
80-
var newValue = value;
81-
if (newValue < MinValue) newValue = MinValue;
82-
if (newValue > MaxValue) newValue = MaxValue;
83-
if (internalValue == newValue)
84-
return;
85-
internalValue = newValue;
86-
if (ValueChanged != null)
87-
ValueChanged(this, new NodeItemEventArgs(this));
88-
}
89-
}
90-
91-
92-
public override bool OnClick()
93-
{
94-
base.OnClick();
95-
if (Clicked != null)
96-
Clicked(this, new NodeItemEventArgs(this));
97-
return true;
98-
}
99-
100-
public override bool OnStartDrag(PointF location, out PointF original_location)
101-
{
102-
base.OnStartDrag(location, out original_location);
103-
var size = (MaxValue - MinValue);
104-
original_location.Y = location.Y;
105-
original_location.X = ((Value / size) * sliderRect.Width) + sliderRect.Left;
106-
Value = ((location.X - sliderRect.Left) / sliderRect.Width) * size;
107-
Dragging = true;
108-
return true;
109-
}
110-
111-
public override bool OnDrag(PointF location)
112-
{
113-
base.OnDrag(location);
114-
var size = (MaxValue - MinValue);
115-
Value = ((location.X - sliderRect.Left) / sliderRect.Width) * size;
116-
return true;
117-
}
118-
119-
public override bool OnEndDrag() { base.OnEndDrag(); Dragging = false; return true; }
120-
121-
122-
internal SizeF itemSize;
123-
internal SizeF textSize;
124-
internal RectangleF sliderRect;
125-
126-
127-
const int SliderBoxSize = 4;
128-
const int SliderHeight = 12;
129-
const int Spacing = 2;
130-
131-
internal override SizeF Measure(Graphics graphics)
132-
{
133-
if (!string.IsNullOrWhiteSpace(this.Text))
134-
{
135-
if (this.itemSize.IsEmpty)
136-
{
137-
var size = new Size(GraphConstants.MinimumItemWidth, GraphConstants.MinimumItemHeight);
138-
var sliderWidth = this.MinimumSliderSize + SliderBoxSize;
139-
140-
this.textSize = (SizeF)graphics.MeasureString(this.Text, SystemFonts.MenuFont, size, GraphConstants.LeftMeasureTextStringFormat);
141-
this.textSize.Width = Math.Max(this.TextSize, this.textSize.Width + 4);
142-
this.itemSize.Width = Math.Max(size.Width, this.textSize.Width + sliderWidth + Spacing);
143-
this.itemSize.Height = Math.Max(size.Height, this.textSize.Height);
144-
}
145-
return this.itemSize;
146-
} else
147-
{
148-
return new SizeF(GraphConstants.MinimumItemWidth, GraphConstants.MinimumItemHeight);
149-
}
150-
}
151-
33+
/// <summary>
34+
/// Construct a new NodeNumericSliderItem.
35+
/// </summary>
36+
/// <param name="text">The label for the item.</param>
37+
/// <param name="sliderSize">The minimum size the slider should have inside the parent node.</param>
38+
/// <param name="textSize">The text size.</param>
39+
/// <param name="minValue">The lowest possible value for the slider.</param>
40+
/// <param name="maxValue">The highest possible value for the slider.</param>
41+
/// <param name="defaultValue">The value the slider should start with.</param>
42+
/// <param name="inputEnabled">Does the item accept an input to be connected?</param>
43+
/// <param name="outputEnabled">Does the item accept an output to be connected?</param>
44+
public NodeNumericSliderItem( string text, float sliderSize, float textSize, float minValue, float maxValue, float defaultValue, bool inputEnabled, bool outputEnabled ) : base( text, sliderSize, textSize, minValue, maxValue, defaultValue, inputEnabled, outputEnabled ) {}
45+
46+
/// <summary>
47+
/// Render the slider.
48+
/// </summary>
49+
/// <param name="graphics">The <see cref="Graphics"/> instance that should be used for drawing.</param>
50+
/// <param name="minimumSize">The smallest size the slider has to fit into.</param>
51+
/// <param name="location">Where the slider should be drawn.</param>
15252
internal override void Render(Graphics graphics, SizeF minimumSize, PointF location)
15353
{
15454
var size = Measure(graphics);

Graph/Items/NodeSliderItem.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
namespace Graph.Items
3232
{
33-
public sealed class NodeSliderItem : NodeItem
33+
public class NodeSliderItem : NodeItem
3434
{
3535
public event EventHandler<NodeItemEventArgs> Clicked;
3636
public event EventHandler<NodeItemEventArgs> ValueChanged;
@@ -123,10 +123,10 @@ public override bool OnDrag(PointF location)
123123
internal SizeF textSize;
124124
internal RectangleF sliderRect;
125125

126-
127-
const int SliderBoxSize = 4;
128-
const int SliderHeight = 8;
129-
const int Spacing = 2;
126+
127+
protected const int SliderBoxSize = 4;
128+
protected const int SliderHeight = 8;
129+
protected const int Spacing = 2;
130130

131131
internal override SizeF Measure(Graphics graphics)
132132
{

0 commit comments

Comments
 (0)