/////////////////////////////////////////////////////////////////////////////// // Small helping Methods // /////////////////////////////////////////////////////////////////////////////// #region HELPER /// <summary> /// Compares to markers and returns true, if they are at the same position. /// </summary> /// <param name="a">Marker 1</param> /// <param name="b">Marker 2</param> /// <returns>true if at the same place</returns> private static int CompareMarkers(Marker a, Marker b) { return a.Position.CompareTo(b.Position); }
/////////////////////////////////////////////////////////////////////////////// // Eventhandler for Custom Defined Events // /////////////////////////////////////////////////////////////////////////////// #region CUSTOMEVENTHANDLER /// <summary> /// Raised, when a marker is selected. Updates UI fields with marker values. /// </summary> /// <param name="sender">marker that is selected</param> /// <param name="e">An empty <see cref="EventArgs"/></param> private void gradientBuilder_MarkerSelected(object sender, EventArgs e) { Marker tempMarker = sender as Marker; marker = null; // Freeze all other event handlers if (tempMarker != null) { colorEditor.Value = tempMarker.Color; opacityBox.Value = Convert.ToDecimal(tempMarker.Color.A * (100.0f / 255)); positionBox.Enabled = tempMarker.IsMovable; positionBox.Value = (int)(tempMarker.Position * 100.0f); grpEditor.Enabled = true; } else grpEditor.Enabled = false; marker = tempMarker; }
/// <summary> /// Override. If an location on the marker strip is double clicked where there /// are no existing markers, a new marker is added. /// </summary> /// <param name="e"></param> protected override void OnMouseDoubleClick(MouseEventArgs e) { Point pt = new Point(e.X, e.Y); if (gradBarRect.Contains(pt)) { int i = 0; for (; i < markers.Count; i++) if (markers[i].Bounds.Contains(pt)) return; else if (markers[i].Bounds.X > pt.X) break; float a, b; a = (float)e.X - DEFAULT_PADDING; b = (float)markerStrip.Width; Marker newMarker = new Marker(a / b); newMarker.UpdateBounds(compensatedMarkerStrip); // Select the color for the new marker. It should be the current // color of the gradient at the marker point. if (this.gradientBmp != null) newMarker.Color = gradientBmp.GetPixel( (int)newMarker.Bounds.X, 0); newMarker.MarkerUpdated += new EventHandler(marker_MarkerUpdated); markers.Insert(i, newMarker); // Notify that we have a new gradient OnGradientChanged(false); OnMarkerSelected(newMarker); } }
/// <summary> /// Override. Updates gradient with changes. /// </summary> /// <param name="e">MouseEventArgs</param> protected override void OnMouseUp(MouseEventArgs e) { this.Cursor = Cursors.Default; if (movingMarker != null) // A movement was done. Resort the array { markers.Sort(CompareMarkers); Invalidate(gradBarRect); //selectedMarker = movingMarker; //OnMarkerSelected(movingMarker); if (!markerStrip.Contains(e.X, e.Y)) { this.markers.Remove(movingMarker); this.movingMarker = null; } // Notify that we have a new gradient OnGradientChanged(false); } //else if (markers[0].Bounds.Contains(e.X, e.Y)) // OnMarkerSelected(markers[0]); //else if (markers[markers.Count - 1].Bounds.Contains(e.X, e.Y)) // OnMarkerSelected(markers[markers.Count - 1]); movingMarker = null; }
/// <summary> /// Override. Can a marker (other than the first or the last) receive this /// mouse down event? /// </summary> /// <param name="e">MouseEventArgs</param> protected override void OnMouseDown(MouseEventArgs e) { for (int i = 1; i < markers.Count - 1; i++) if (markers[i].Bounds.Contains(e.X, e.Y)) { movingMarker = markers[i]; break; } if (movingMarker != null) { selectedMarker = movingMarker; OnMarkerSelected(movingMarker); } else { if (markers[0].Bounds.Contains(e.X, e.Y)) OnMarkerSelected(markers[0]); else if (markers[markers.Count - 1].Bounds.Contains(e.X, e.Y)) OnMarkerSelected(markers[markers.Count - 1]); } Invalidate(); }
/// <summary> /// Raises MarkerSelected event. /// </summary> /// <param name="selectedMarker">currently selected Marker</param> protected virtual void OnMarkerSelected(Marker selectedMarker) { this.selectedMarker = selectedMarker; Rectangle invalidRect = markerStrip; invalidRect.Inflate((int)selectedMarker.Bounds.Width, 0); this.Invalidate(invalidRect); if (MarkerSelected != null) MarkerSelected(selectedMarker, EventArgs.Empty); }
/////////////////////////////////////////////////////////////////////////////// // Construction and Initializing methods // /////////////////////////////////////////////////////////////////////////////// #region CONSTRUCTION /// <summary> /// Constructor. Initializes gradient builder control. /// </summary> public GradientBuilder() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.UserPaint, true); this.gradient = new Gradient(); this.gradient.GradientDirection = LinearGradientMode.Horizontal; this.markers = new List<Marker>(2); Marker startMarker, endMarker; startMarker = new Marker(0.0f, Color.Black, false); endMarker = new Marker(1.0f, Color.White, false); markers.Add(startMarker); markers.Add(endMarker); startMarker.MarkerUpdated += new EventHandler(marker_MarkerUpdated); endMarker.MarkerUpdated += new EventHandler(marker_MarkerUpdated); }