/src/Core/SwatchControl.cs
C# | 286 lines | 233 code | 44 blank | 9 comment | 26 complexity | c3348f3037270bcecf96c208072d71b0 MD5 | raw file
- /////////////////////////////////////////////////////////////////////////////////
- // Paint.NET //
- // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
- // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
- // See src/Resources/Files/License.txt for full licensing and attribution //
- // details. //
- // . //
- /////////////////////////////////////////////////////////////////////////////////
-
- using PaintDotNet.Base;
- using PaintDotNet.SystemLayer;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
-
- namespace PaintDotNet
- {
- public sealed class SwatchControl
- : Control
- {
- private List<ColorBgra> _colors = new List<ColorBgra>();
- private const int DefaultUnscaledSwatchSize = 12;
- private int _unscaledSwatchSize = DefaultUnscaledSwatchSize;
- private bool _mouseDown;
- private int _mouseDownIndex = -1;
- private bool _blinkHighlight;
- private const int BlinkInterval = 500;
- private Timer _blinkHighlightTimer;
-
- [Browsable(false)]
- public bool BlinkHighlight
- {
- get
- {
- return _blinkHighlight;
- }
-
- set
- {
- _blinkHighlight = value;
- _blinkHighlightTimer.Enabled = value;
- Invalidate();
- }
- }
-
- public event EventHandler ColorsChanged;
- private void OnColorsChanged()
- {
- if (ColorsChanged != null)
- {
- ColorsChanged(this, EventArgs.Empty);
- }
- }
-
- [Browsable(false)]
- public ColorBgra[] Colors
- {
- get
- {
- return _colors.ToArray();
- }
-
- set
- {
- _colors = new List<ColorBgra>(value);
- _mouseDown = false;
- Invalidate();
- OnColorsChanged();
- }
- }
-
- [DefaultValue(DefaultUnscaledSwatchSize)]
- [Browsable(true)]
- public int UnscaledSwatchSize
- {
- get
- {
- return _unscaledSwatchSize;
- }
-
- set
- {
- _unscaledSwatchSize = value;
- _mouseDown = false;
- Invalidate();
- }
- }
-
- public event EventHandler<EventArgs<Pair<int, MouseButtons>>> ColorClicked;
- private void OnColorClicked(int index, MouseButtons buttons)
- {
- if (ColorClicked != null)
- {
- ColorClicked(this, new EventArgs<Pair<int, MouseButtons>>(Pair.Create(index, buttons)));
- }
- }
-
- public SwatchControl()
- {
- InitializeComponent();
- }
-
- private void InitializeComponent()
- {
- _blinkHighlightTimer = new Timer();
- _blinkHighlightTimer.Tick += BlinkHighlightTimerTick;
- _blinkHighlightTimer.Enabled = false;
- _blinkHighlightTimer.Interval = BlinkInterval;
- DoubleBuffered = true;
- ResizeRedraw = true;
- }
-
- private void BlinkHighlightTimerTick(object sender, EventArgs e)
- {
- Invalidate();
- }
-
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (_blinkHighlightTimer != null)
- {
- _blinkHighlightTimer.Dispose();
- _blinkHighlightTimer = null;
- }
- }
-
- base.Dispose(disposing);
- }
-
- private int MouseXYToColorIndex(int x, int y)
- {
- if (x < 0 || y < 0 || x >= ClientSize.Width || y >= ClientSize.Height)
- {
- return -1;
- }
-
- int scaledSwatchSize = UI.ScaleWidth(_unscaledSwatchSize);
- int swatchColumns = ClientSize.Width / scaledSwatchSize;
- int row = y / scaledSwatchSize;
- int col = x / scaledSwatchSize;
- int index = col + (row * swatchColumns);
-
- // Make sure they aren't on the last item of a row that actually got clipped off
- if (col == swatchColumns)
- {
- index = -1;
- }
-
- return index;
- }
-
- protected override void OnMouseLeave(EventArgs e)
- {
- _mouseDown = false;
- Invalidate();
- base.OnMouseLeave(e);
- }
-
- protected override void OnMouseDown(MouseEventArgs e)
- {
- _mouseDown = true;
- _mouseDownIndex = MouseXYToColorIndex(e.X, e.Y);
- Invalidate();
- base.OnMouseDown(e);
- }
-
- protected override void OnMouseUp(MouseEventArgs e)
- {
- int colorIndex = MouseXYToColorIndex(e.X, e.Y);
-
- if (colorIndex == _mouseDownIndex &&
- colorIndex >= 0 &&
- colorIndex < _colors.Count)
- {
- OnColorClicked(colorIndex, e.Button);
- }
-
- _mouseDown = false;
- Invalidate();
- base.OnMouseUp(e);
- }
-
- protected override void OnMouseMove(MouseEventArgs e)
- {
- Invalidate();
- base.OnMouseMove(e);
- }
-
- protected override void OnPaint(PaintEventArgs e)
- {
- e.Graphics.CompositingMode = CompositingMode.SourceOver;
- int scaledSwatchSize = UI.ScaleWidth(_unscaledSwatchSize);
- int swatchColumns = ClientSize.Width / scaledSwatchSize;
-
- Point mousePt = MousePosition;
- mousePt = PointToClient(mousePt);
- int activeIndex = MouseXYToColorIndex(mousePt.X, mousePt.Y);
-
- for (int i = 0; i < _colors.Count; ++i)
- {
- ColorBgra c = _colors[i];
-
- int swatchX = i % swatchColumns;
- int swatchY = i / swatchColumns;
-
- var swatchRect = new Rectangle(
- swatchX * scaledSwatchSize,
- swatchY * scaledSwatchSize,
- scaledSwatchSize,
- scaledSwatchSize);
-
- PushButtonState state;
-
- if (_mouseDown)
- {
- state = i == _mouseDownIndex ? PushButtonState.Pressed : PushButtonState.Normal;
- }
- else if (i == activeIndex)
- {
- state = PushButtonState.Hot;
- }
- else
- {
- state = PushButtonState.Normal;
- }
-
- bool drawOutline;
-
- switch (state)
- {
- case PushButtonState.Hot:
- drawOutline = true;
- break;
-
- case PushButtonState.Pressed:
- drawOutline = false;
- break;
-
- case PushButtonState.Default:
- case PushButtonState.Disabled:
- case PushButtonState.Normal:
- drawOutline = false;
- break;
-
- default:
- throw new InvalidEnumArgumentException();
- }
-
- Utility.DrawColorRectangle(e.Graphics, swatchRect, c.ToColor(), drawOutline);
- }
-
- if (_blinkHighlight)
- {
- int period = (Math.Abs(Environment.TickCount) / BlinkInterval) % 2;
- Color color;
-
- switch (period)
- {
- case 0:
- color = SystemColors.Window;
- break;
-
- case 1:
- color = SystemColors.Highlight;
- break;
-
- default:
- throw new InvalidOperationException();
- }
-
- using (var pen = new Pen(color))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1));
- }
- }
-
- base.OnPaint(e);
- }
- }
- }