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;
|
2018-04-11 17:35:34 -07:00
|
|
|
|
using System.Linq;
|
2018-04-04 10:11:57 -07:00
|
|
|
|
using MatterHackers.Agg;
|
2018-04-05 15:51:09 -07:00
|
|
|
|
using MatterHackers.Agg.Image;
|
2018-04-04 10:11:57 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
|
|
|
|
|
using MatterHackers.Agg.VertexSource;
|
|
|
|
|
|
using MatterHackers.VectorMath;
|
2018-04-12 07:16:58 -07:00
|
|
|
|
using static MatterHackers.Agg.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;
|
|
|
|
|
|
|
2018-05-04 13:47:16 -07:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-04 13:47:16 -07:00
|
|
|
|
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;
|
2018-04-05 15:51:09 -07:00
|
|
|
|
private bool mouseInBounds = false;
|
|
|
|
|
|
|
|
|
|
|
|
private double centerY;
|
|
|
|
|
|
private double left;
|
|
|
|
|
|
private double right;
|
|
|
|
|
|
private RoundedRect backgroundBar;
|
2018-04-05 18:24:24 -07:00
|
|
|
|
|
2018-04-10 17:01:30 -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;
|
|
|
|
|
|
|
2018-04-06 12:47:42 -07:00
|
|
|
|
private bool _checked;
|
|
|
|
|
|
public bool Checked
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _checked;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2018-04-07 14:26:24 -07:00
|
|
|
|
if (_checked != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
_checked = value;
|
|
|
|
|
|
this.Invalidate();
|
|
|
|
|
|
}
|
2018-04-06 12:47:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
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;
|
2018-10-15 08:02:30 -07:00
|
|
|
|
inactiveBarColor = theme.IsDarkTheme ? theme.Shade : theme.SlightShade;
|
2018-10-15 18:25:53 -07:00
|
|
|
|
activeBarColor = new Color(theme.PrimaryAccentColor, (theme.IsDarkTheme ? 100 : 70));
|
2018-04-04 10:11:57 -07:00
|
|
|
|
|
2018-04-10 17:01:30 -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);
|
|
|
|
|
|
|
2018-04-11 17:35:34 -07:00
|
|
|
|
// animation up
|
2018-04-12 09:54:31 -07:00
|
|
|
|
animation = new ToggleSwitchAnimation()
|
2018-04-11 17:35:34 -07:00
|
|
|
|
{
|
2018-04-12 09:54:31 -07:00
|
|
|
|
DrawTarget = this,
|
|
|
|
|
|
FramesPerSecond = 30
|
|
|
|
|
|
};
|
|
|
|
|
|
animation.Start();
|
2018-04-11 17:35:34 -07:00
|
|
|
|
|
|
|
|
|
|
this.Parents<SystemWindow>().First().AfterDraw += RoundedToggleSwitch_AfterDraw;
|
|
|
|
|
|
|
2018-04-04 10:11:57 -07:00
|
|
|
|
this.Invalidate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-11 17:35:34 -07:00
|
|
|
|
private void RoundedToggleSwitch_AfterDraw(object sender, DrawEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var position = new Vector2((this.Checked) ? LocalBounds.Right - toggleRadiusPlusPadding : toggleRadiusPlusPadding, centerY);
|
|
|
|
|
|
position = this.TransformToScreenSpace(position);
|
2018-10-15 18:25:53 -07:00
|
|
|
|
Color toggleColor = (this.Checked) ? theme.PrimaryAccentColor : Color.Gray;
|
2018-04-11 17:35:34 -07:00
|
|
|
|
|
|
|
|
|
|
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-11 17:35:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-04 10:11:57 -07:00
|
|
|
|
public override void OnMouseUp(MouseEventArgs mouseEvent)
|
|
|
|
|
|
{
|
2018-04-11 17:35:34 -07:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-05 15:51:09 -07:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
bool checkStateChanged = (newValue != this.Checked);
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2018-04-05 18:24:24 -07:00
|
|
|
|
var position = (this.Checked) ? LocalBounds.Right - toggleRadiusPlusPadding: toggleRadiusPlusPadding;
|
2018-04-05 17:56:56 -07:00
|
|
|
|
|
2018-04-04 10:11:57 -07:00
|
|
|
|
Color barColor = (this.Checked) ? activeBarColor : inactiveBarColor;
|
2018-10-15 18:25:53 -07:00
|
|
|
|
Color toggleColor = (this.Checked) ? theme.PrimaryAccentColor : Color.Gray;
|
2018-04-04 10:11:57 -07:00
|
|
|
|
|
2018-04-05 15:51:09 -07:00
|
|
|
|
if (mouseIsDown && mouseInBounds)
|
2018-04-04 10:11:57 -07:00
|
|
|
|
{
|
2018-04-05 15:51:09 -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);
|
|
|
|
|
|
}
|
2018-04-05 15:51:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnBoundsChanged(EventArgs e)
|
|
|
|
|
|
{
|
2018-04-05 17:56:56 -07:00
|
|
|
|
centerY = this.LocalBounds.YCenter;
|
|
|
|
|
|
|
2018-04-10 17:01:30 -07:00
|
|
|
|
var halfBarHeight = barHeight / 2;
|
2018-04-05 15:51:09 -07:00
|
|
|
|
|
2018-04-05 18:24:24 -07:00
|
|
|
|
var diff = toggleRadiusPlusPadding - halfBarHeight;
|
|
|
|
|
|
|
|
|
|
|
|
right = LocalBounds.Right - diff;
|
|
|
|
|
|
left = diff;
|
2018-04-05 15:51:09 -07:00
|
|
|
|
|
|
|
|
|
|
backgroundBar = new RoundedRect(
|
|
|
|
|
|
new RectangleDouble(
|
|
|
|
|
|
left,
|
2018-04-05 17:56:56 -07:00
|
|
|
|
centerY - halfBarHeight,
|
2018-04-05 15:51:09 -07:00
|
|
|
|
right,
|
2018-04-05 17:56:56 -07:00
|
|
|
|
centerY + halfBarHeight),
|
2018-04-05 15:51:09 -07:00
|
|
|
|
halfBarHeight);
|
|
|
|
|
|
|
|
|
|
|
|
base.OnBoundsChanged(e);
|
2018-04-04 10:11:57 -07:00
|
|
|
|
}
|
2018-04-05 18:24:24 -07:00
|
|
|
|
|
2018-04-04 10:11:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|