mattercontrol/MatterControlLib/PartPreviewWindow/RoundedToggleSwitch.cs

253 lines
6.5 KiB
C#
Raw Normal View History

2018-04-04 10:11:57 -07:00
/*
Copyright (c) 2018, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Linq;
2018-04-04 10:11:57 -07:00
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.VectorMath;
2019-05-27 16:38:56 -07:00
using static MatterHackers.VectorMath.Easing;
2018-04-04 10:11:57 -07:00
namespace MatterHackers.MatterControl.CustomWidgets
{
2018-04-12 09:54:31 -07:00
internal class ToggleSwitchAnimation : Animation
{
internal double animationRatio = 0;
internal double finalRadius = 22 * GuiWidget.DeviceScale;
public override bool OnUpdate(UpdateEvent updateEvent)
2018-04-12 09:54:31 -07:00
{
if (IsRunning)
{
animationRatio += 1.0 / 7.0;
}
2019-05-22 16:20:22 -07:00
2018-04-12 09:54:31 -07:00
if (animationRatio >= 1)
{
Stop();
}
return base.OnUpdate(updateEvent);
2018-04-12 09:54:31 -07:00
}
}
2018-04-04 10:11:57 -07:00
public class RoundedToggleSwitch : GuiWidget, ICheckbox
{
private ThemeConfig theme;
private Color inactiveBarColor;
private Color activeBarColor;
private bool mouseIsDown;
private bool mouseInBounds = false;
private double centerY;
private double left;
private double right;
private RoundedRect backgroundBar;
2018-04-05 18:24:24 -07:00
private double minWidth = 45 * DeviceScale;
private double barHeight = 12.6 * DeviceScale;
private double toggleRadius = 9 * DeviceScale;
private double toggleRadiusPlusPadding = 10 * DeviceScale;
2018-04-04 10:11:57 -07:00
2018-04-12 09:54:31 -07:00
private ToggleSwitchAnimation animation;
private bool _checked;
2019-05-27 16:38:56 -07:00
public bool Checked
{
get => _checked;
set
{
if (_checked != value)
{
_checked = value;
this.Invalidate();
}
}
}
2018-04-04 10:11:57 -07:00
public event EventHandler CheckedStateChanged;
public RoundedToggleSwitch(ThemeConfig theme)
{
this.theme = theme;
2019-01-08 11:56:49 -08:00
// this.DoubleBuffer = true;
inactiveBarColor = theme.IsDarkTheme ? theme.Shade : theme.SlightShade;
2019-05-27 16:38:56 -07:00
activeBarColor = new Color(theme.PrimaryAccentColor, theme.IsDarkTheme ? 100 : 70);
2018-04-04 10:11:57 -07:00
this.MinimumSize = new Vector2(minWidth, theme.ButtonHeight);
2018-04-04 10:11:57 -07:00
}
public override void OnMouseDown(MouseEventArgs mouseEvent)
{
mouseIsDown = true;
base.OnMouseDown(mouseEvent);
// animation up
2018-04-12 09:54:31 -07:00
animation = new ToggleSwitchAnimation()
{
2018-04-12 09:54:31 -07:00
DrawTarget = this,
FramesPerSecond = 30
};
animation.Start();
this.Parents<SystemWindow>().First().AfterDraw += RoundedToggleSwitch_AfterDraw;
2018-04-04 10:11:57 -07:00
this.Invalidate();
}
private void RoundedToggleSwitch_AfterDraw(object sender, DrawEventArgs e)
{
2019-05-27 16:38:56 -07:00
var position = new Vector2(this.Checked ? LocalBounds.Right - toggleRadiusPlusPadding : toggleRadiusPlusPadding, centerY);
position = this.TransformToScreenSpace(position);
2019-05-27 16:38:56 -07:00
Color toggleColor = this.Checked ? theme.PrimaryAccentColor : Color.Gray;
e.Graphics2D.Circle(position,
2018-04-12 09:54:31 -07:00
animation.finalRadius * Quadratic.Out(animation.animationRatio),
2018-04-12 09:08:00 -07:00
new Color(toggleColor, 50));
2018-04-12 09:54:31 -07:00
if (animation.IsRunning == false && animation.animationRatio == 0)
2018-04-12 09:08:00 -07:00
{
((GuiWidget)sender).AfterDraw -= RoundedToggleSwitch_AfterDraw;
}
}
2018-04-04 10:11:57 -07:00
public override void OnMouseUp(MouseEventArgs mouseEvent)
{
this.Parents<SystemWindow>().First().AfterDraw -= RoundedToggleSwitch_AfterDraw;
2018-04-12 09:54:31 -07:00
animation.Stop();
animation.animationRatio = 0;
2018-04-04 10:11:57 -07:00
mouseIsDown = false;
base.OnMouseUp(mouseEvent);
this.Invalidate();
}
public override void OnMouseMove(MouseEventArgs mouseEvent)
{
var inBounds = this.PositionWithinLocalBounds(mouseEvent.X, mouseEvent.Y);
if (inBounds != mouseInBounds)
{
mouseInBounds = inBounds;
this.Invalidate();
}
base.OnMouseMove(mouseEvent);
}
2018-04-04 10:11:57 -07:00
public override void OnClick(MouseEventArgs mouseEvent)
{
base.OnClick(mouseEvent);
bool newValue = !this.Checked;
2019-05-27 16:38:56 -07:00
bool checkStateChanged = newValue != this.Checked;
2018-04-04 10:11:57 -07:00
this.Checked = newValue;
// After setting CheckedState, fire event if different
if (checkStateChanged)
{
OnCheckStateChanged();
this.Invalidate();
}
}
public virtual void OnCheckStateChanged()
{
CheckedStateChanged?.Invoke(this, null);
}
public override void OnDraw(Graphics2D graphics2D)
{
base.OnDraw(graphics2D);
2019-05-27 16:38:56 -07:00
var position = this.Checked ? LocalBounds.Right - toggleRadiusPlusPadding : toggleRadiusPlusPadding;
2018-04-05 17:56:56 -07:00
2019-05-27 16:38:56 -07:00
Color barColor = this.Checked ? activeBarColor : inactiveBarColor;
Color toggleColor = this.Checked ? theme.PrimaryAccentColor : Color.Gray;
2018-04-04 10:11:57 -07:00
if (mouseIsDown && mouseInBounds)
2018-04-04 10:11:57 -07:00
{
2019-05-27 16:38:56 -07:00
barColor = this.Checked ? inactiveBarColor : activeBarColor;
2018-04-04 10:11:57 -07:00
}
2018-04-05 17:56:56 -07:00
if (!Enabled)
{
graphics2D.Render(new Stroke(backgroundBar), inactiveBarColor);
graphics2D.Circle(
new Vector2(
position,
centerY),
toggleRadius,
inactiveBarColor);
}
else
{
// Draw bar
graphics2D.Render(backgroundBar, barColor);
2018-04-04 10:11:57 -07:00
2018-04-05 17:56:56 -07:00
// Draw toggle circle
graphics2D.Circle(
new Vector2(
position,
centerY),
toggleRadius,
toggleColor);
}
}
public override void OnBoundsChanged(EventArgs e)
{
2018-04-05 17:56:56 -07:00
centerY = this.LocalBounds.YCenter;
var halfBarHeight = barHeight / 2;
2018-04-05 18:24:24 -07:00
var diff = toggleRadiusPlusPadding - halfBarHeight;
right = LocalBounds.Right - diff;
left = diff;
backgroundBar = new RoundedRect(
new RectangleDouble(
left,
2018-04-05 17:56:56 -07:00
centerY - halfBarHeight,
right,
2018-04-05 17:56:56 -07:00
centerY + halfBarHeight),
halfBarHeight);
base.OnBoundsChanged(e);
2018-04-04 10:11:57 -07:00
}
}
}