ShareX/ShareX.HelpersLib/Forms/GradientPickerForm.cs

248 lines
7.4 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2020-02-05 20:19:48 +13:00
Copyright (c) 2007-2020 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
{
public partial class GradientPickerForm : Form
{
public GradientInfo Gradient { get; set; }
private bool isReady;
public GradientPickerForm(GradientInfo gradient)
{
Gradient = gradient;
2019-06-24 01:59:48 +12:00
InitializeComponent();
2019-06-25 06:36:16 +12:00
ShareXResources.ApplyTheme(this);
2019-06-24 01:59:48 +12:00
cbGradientType.Items.AddRange(Helpers.GetEnumNamesProper<LinearGradientMode>());
cbGradientType.SelectedIndex = (int)Gradient.Type;
2020-06-09 08:55:36 +12:00
UpdateGradientList();
2020-07-28 00:40:37 +12:00
if (lvGradientPoints.Items.Count > 0)
{
lvGradientPoints.SelectedIndex = 0;
}
2020-06-09 08:55:36 +12:00
}
private void UpdateGradientList()
{
Gradient.Sort();
isReady = false;
lvGradientPoints.Items.Clear();
foreach (GradientStop gradientStop in Gradient.Colors)
{
AddGradientStop(gradientStop);
}
2020-06-09 08:55:36 +12:00
isReady = true;
UpdatePreview();
}
private void UpdatePreview()
{
if (isReady)
{
Bitmap bmp = new Bitmap(pbPreview.ClientRectangle.Width, pbPreview.ClientRectangle.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
Gradient.Draw(g, new Rectangle(0, 0, bmp.Width, bmp.Height));
}
if (pbPreview.Image != null)
{
pbPreview.Image.Dispose();
}
pbPreview.Image = bmp;
}
}
private void AddGradientStop(GradientStop gradientStop)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = string.Format(" {0:0.##}%", gradientStop.Location);
UpdateListViewItemColor(lvi, gradientStop.Color);
lvi.Tag = gradientStop;
lvGradientPoints.Items.Add(lvi);
}
private void UpdateListViewItemColor(ListViewItem lvi, Color color)
{
string argb = color.ToArgb().ToString();
if (!ilColors.Images.ContainsKey(argb))
{
ilColors.Images.Add(argb, ImageHelpers.CreateColorPickerIcon(color, new Rectangle(0, 0, 16, 16)));
}
lvi.ImageKey = argb;
}
2020-06-09 08:55:36 +12:00
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)
{
Color color = cbtnCurrentColor.Color;
float offset = (float)nudLocation.Value;
GradientStop gradientStop = new GradientStop(color, offset);
Gradient.Colors.Add(gradientStop);
2020-06-09 08:55:36 +12:00
UpdateGradientList();
SelectGradientStop(gradientStop);
}
private void btnRemove_Click(object sender, EventArgs e)
{
2020-06-09 08:55:36 +12:00
GradientStop gradientStop = GetSelectedGradientStop();
if (gradientStop != null)
{
Gradient.Colors.Remove(gradientStop);
2020-06-09 08:55:36 +12:00
UpdateGradientList();
}
}
private void btnReverse_Click(object sender, EventArgs e)
{
if (Gradient.IsValid)
{
Gradient.Reverse();
UpdateGradientList();
}
}
private void cbtnCurrentColor_ColorChanged(Color color)
{
2020-06-09 08:55:36 +12:00
GradientStop gradientStop = GetSelectedGradientStop(out ListViewItem lvi);
if (gradientStop != null)
{
gradientStop.Color = color;
UpdateListViewItemColor(lvi, gradientStop.Color);
UpdatePreview();
}
}
private void nudLocation_ValueChanged(object sender, EventArgs e)
{
2020-06-09 08:55:36 +12:00
if (isReady)
{
GradientStop gradientStop = GetSelectedGradientStop();
2020-06-09 08:55:36 +12:00
if (gradientStop != null)
{
gradientStop.Location = (float)nudLocation.Value;
UpdateGradientList();
SelectGradientStop(gradientStop);
}
}
}
private void lvGradientPoints_SelectedIndexChanged(object sender, EventArgs e)
{
2020-06-09 08:55:36 +12:00
GradientStop gradientStop = GetSelectedGradientStop();
if (gradientStop != null)
{
2020-06-09 08:55:36 +12:00
isReady = false;
cbtnCurrentColor.Color = gradientStop.Color;
nudLocation.SetValue((decimal)gradientStop.Location);
2020-06-09 08:55:36 +12:00
isReady = true;
}
}
private void lvGradientPoints_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && lvGradientPoints.SelectedItems.Count > 0)
{
cbtnCurrentColor.ShowColorDialog();
}
}
private void btnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}