Automatically sort gradient stops

This commit is contained in:
Jaex 2020-06-08 23:55:36 +03:00
parent b2ed185166
commit c7a4b7a010
2 changed files with 85 additions and 25 deletions

View file

@ -46,6 +46,11 @@ public GradientInfo()
Colors = new List<GradientStop>(); Colors = new List<GradientStop>();
} }
public void Sort()
{
Colors.Sort((x, y) => x.Location.CompareTo(y.Location));
}
public void Draw(Graphics g, Rectangle rect) public void Draw(Graphics g, Rectangle rect)
{ {
if (IsValid) if (IsValid)

View file

@ -45,10 +45,20 @@ public GradientPickerForm(GradientInfo gradient)
cbGradientType.Items.AddRange(Helpers.GetEnumNamesProper<LinearGradientMode>()); cbGradientType.Items.AddRange(Helpers.GetEnumNamesProper<LinearGradientMode>());
cbGradientType.SelectedIndex = (int)Gradient.Type; cbGradientType.SelectedIndex = (int)Gradient.Type;
UpdateGradientList();
}
private void UpdateGradientList()
{
Gradient.Sort();
isReady = false;
lvGradientPoints.Items.Clear();
foreach (GradientStop gradientStop in Gradient.Colors) foreach (GradientStop gradientStop in Gradient.Colors)
{ {
AddGradientStop(gradientStop); AddGradientStop(gradientStop);
} }
isReady = true; isReady = true;
UpdatePreview(); UpdatePreview();
} }
@ -73,12 +83,6 @@ private void UpdatePreview()
} }
} }
private void cbGradientType_SelectedIndexChanged(object sender, EventArgs e)
{
Gradient.Type = (LinearGradientMode)cbGradientType.SelectedIndex;
UpdatePreview();
}
private void AddGradientStop(GradientStop gradientStop) private void AddGradientStop(GradientStop gradientStop)
{ {
ListViewItem lvi = new ListViewItem(); ListViewItem lvi = new ListViewItem();
@ -89,34 +93,80 @@ private void AddGradientStop(GradientStop gradientStop)
lvGradientPoints.Items.Add(lvi); lvGradientPoints.Items.Add(lvi);
} }
private GradientStop GetSelectedGradientStop()
{
return GetSelectedGradientStop(out _);
}
private GradientStop GetSelectedGradientStop(out ListViewItem lvi)
{
if (lvGradientPoints.SelectedItems.Count > 0)
{
lvi = lvGradientPoints.SelectedItems[0];
return lvi.Tag as GradientStop;
}
lvi = null;
return null;
}
private ListViewItem FindListViewItem(GradientStop gradientStop)
{
foreach (ListViewItem lvi in lvGradientPoints.Items)
{
GradientStop itemGradientstop = lvi.Tag as GradientStop;
if (itemGradientstop == gradientStop)
{
return lvi;
}
}
return null;
}
private ListViewItem SelectGradientStop(GradientStop gradientStop)
{
ListViewItem lvi = FindListViewItem(gradientStop);
if (lvi != null)
{
lvi.Selected = true;
}
return lvi;
}
private void cbGradientType_SelectedIndexChanged(object sender, EventArgs e)
{
Gradient.Type = (LinearGradientMode)cbGradientType.SelectedIndex;
UpdatePreview();
}
private void btnAdd_Click(object sender, EventArgs e) private void btnAdd_Click(object sender, EventArgs e)
{ {
Color color = cbtnCurrentColor.Color; Color color = cbtnCurrentColor.Color;
float offset = (float)nudLocation.Value; float offset = (float)nudLocation.Value;
GradientStop gradientStop = new GradientStop(color, offset); GradientStop gradientStop = new GradientStop(color, offset);
Gradient.Colors.Add(gradientStop); Gradient.Colors.Add(gradientStop);
AddGradientStop(gradientStop); UpdateGradientList();
UpdatePreview(); SelectGradientStop(gradientStop);
} }
private void btnRemove_Click(object sender, EventArgs e) private void btnRemove_Click(object sender, EventArgs e)
{ {
if (lvGradientPoints.SelectedItems.Count > 0) GradientStop gradientStop = GetSelectedGradientStop();
if (gradientStop != null)
{ {
ListViewItem lvi = lvGradientPoints.SelectedItems[0];
GradientStop gradientStop = (GradientStop)lvi.Tag;
Gradient.Colors.Remove(gradientStop); Gradient.Colors.Remove(gradientStop);
lvGradientPoints.Items.Remove(lvi); UpdateGradientList();
UpdatePreview();
} }
} }
private void cbtnCurrentColor_ColorChanged(Color color) private void cbtnCurrentColor_ColorChanged(Color color)
{ {
if (lvGradientPoints.SelectedItems.Count > 0) GradientStop gradientStop = GetSelectedGradientStop(out ListViewItem lvi);
if (gradientStop != null)
{ {
ListViewItem lvi = lvGradientPoints.SelectedItems[0];
GradientStop gradientStop = (GradientStop)lvi.Tag;
gradientStop.Color = color; gradientStop.Color = color;
lvi.BackColor = gradientStop.Color; lvi.BackColor = gradientStop.Color;
lvi.ForeColor = ColorHelpers.VisibleColor(gradientStop.Color); lvi.ForeColor = ColorHelpers.VisibleColor(gradientStop.Color);
@ -126,24 +176,29 @@ private void cbtnCurrentColor_ColorChanged(Color color)
private void nudLocation_ValueChanged(object sender, EventArgs e) private void nudLocation_ValueChanged(object sender, EventArgs e)
{ {
if (lvGradientPoints.SelectedItems.Count > 0) if (isReady)
{ {
ListViewItem lvi = lvGradientPoints.SelectedItems[0]; GradientStop gradientStop = GetSelectedGradientStop(out ListViewItem lvi);
GradientStop gradientStop = (GradientStop)lvi.Tag;
gradientStop.Location = (float)nudLocation.Value; if (gradientStop != null)
lvi.Text = string.Format(" {0:0.##}%", gradientStop.Location); {
UpdatePreview(); gradientStop.Location = (float)nudLocation.Value;
UpdateGradientList();
SelectGradientStop(gradientStop);
}
} }
} }
private void lvGradientPoints_SelectedIndexChanged(object sender, EventArgs e) private void lvGradientPoints_SelectedIndexChanged(object sender, EventArgs e)
{ {
if (lvGradientPoints.SelectedItems.Count > 0) GradientStop gradientStop = GetSelectedGradientStop();
if (gradientStop != null)
{ {
ListViewItem lvi = lvGradientPoints.SelectedItems[0]; isReady = false;
GradientStop gradientStop = (GradientStop)lvi.Tag;
cbtnCurrentColor.Color = gradientStop.Color; cbtnCurrentColor.Color = gradientStop.Color;
nudLocation.SetValue((decimal)gradientStop.Location); nudLocation.SetValue((decimal)gradientStop.Location);
isReady = true;
} }
} }