|
| 1 | +#region License |
| 2 | +// Copyright (c) 2009 Sander van Rossen |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | +#endregion |
| 22 | + |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.Linq; |
| 26 | +using System.Text; |
| 27 | +using System.Windows.Forms; |
| 28 | +using System.Drawing; |
| 29 | +using System.Drawing.Drawing2D; |
| 30 | + |
| 31 | +namespace Graph.Items |
| 32 | +{ |
| 33 | + public sealed class NodeNumericSliderItem : NodeItem |
| 34 | + { |
| 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 | + |
| 152 | + internal override void Render(Graphics graphics, SizeF minimumSize, PointF location) |
| 153 | + { |
| 154 | + var size = Measure(graphics); |
| 155 | + size.Width = Math.Max(minimumSize.Width, size.Width); |
| 156 | + size.Height = Math.Max(minimumSize.Height, size.Height); |
| 157 | + |
| 158 | + var sliderOffset = Spacing + this.textSize.Width; |
| 159 | + var sliderWidth = size.Width - sliderOffset ; |
| 160 | + |
| 161 | + var textRect = new RectangleF(location, size); |
| 162 | + var sliderBox = new RectangleF(location, size); |
| 163 | + var sliderRect = new RectangleF(location, size); |
| 164 | + |
| 165 | + // Calculate bounds for outer rectangle |
| 166 | + sliderRect.X = sliderRect.Right - sliderWidth; |
| 167 | + sliderRect.Y += ((sliderRect.Bottom - sliderRect.Top) - SliderHeight) / 2.0f; |
| 168 | + sliderRect.Width = sliderWidth; |
| 169 | + sliderRect.Height = SliderHeight; |
| 170 | + |
| 171 | + textRect.Width -= sliderWidth + Spacing; |
| 172 | + |
| 173 | + var valueSize = (this.MaxValue - this.MinValue); |
| 174 | + this.sliderRect = sliderRect; |
| 175 | + this.sliderRect.X += SliderBoxSize / 2.0f; |
| 176 | + |
| 177 | + // Calculate bounds for inner rectangle |
| 178 | + sliderBox.X = sliderRect.X; |
| 179 | + sliderBox.Y = sliderRect.Y; |
| 180 | + sliderBox.Width = (this.Value * this.sliderRect.Width) / valueSize; |
| 181 | + sliderBox.Height = SliderHeight; |
| 182 | + |
| 183 | + // Draw label |
| 184 | + graphics.DrawString(this.Text, SystemFonts.MenuFont, Brushes.Black, textRect, GraphConstants.LeftTextStringFormat); |
| 185 | + |
| 186 | + // Draw inner rectangle |
| 187 | + graphics.FillRectangle(Brushes.LightGray, sliderBox.X, sliderBox.Y, sliderBox.Width, sliderBox.Height); |
| 188 | + |
| 189 | + // Draw outer rectangle |
| 190 | + if ((state & (RenderState.Hover | RenderState.Dragging)) != 0) |
| 191 | + graphics.DrawRectangle(Pens.White, sliderRect.X, sliderRect.Y, sliderRect.Width, sliderRect.Height); |
| 192 | + else |
| 193 | + graphics.DrawRectangle(Pens.Black, sliderRect.X, sliderRect.Y, sliderRect.Width, sliderRect.Height); |
| 194 | + |
| 195 | + // Draw value marker into box |
| 196 | + if ((state & (RenderState.Hover | RenderState.Dragging)) != 0) |
| 197 | + graphics.DrawLine(Pens.White, sliderBox.X + sliderBox.Width, sliderBox.Y, sliderBox.X + sliderBox.Width, sliderBox.Y + sliderBox.Height); |
| 198 | + else |
| 199 | + graphics.DrawLine(Pens.Black, sliderBox.X + sliderBox.Width, sliderBox.Y, sliderBox.X + sliderBox.Width, sliderBox.Y + sliderBox.Height); |
| 200 | + |
| 201 | + // Draw value |
| 202 | + graphics.DrawString(this.Value.ToString(), SystemFonts.MenuFont, Brushes.Black, sliderRect, GraphConstants.LeftTextStringFormat); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments