2016-05-17 15:30:39 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2014, Lars Brubaker
|
|
|
|
|
|
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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2019-05-27 16:38:56 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
2016-05-17 15:30:39 -07:00
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.Agg.UI;
|
2016-05-29 09:19:46 -07:00
|
|
|
|
using MatterHackers.GuiAutomation;
|
2019-05-27 16:38:56 -07:00
|
|
|
|
using static MatterHackers.VectorMath.Easing;
|
2016-05-17 15:30:39 -07:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AttentionGetter
|
|
|
|
|
|
{
|
2019-05-27 16:38:56 -07:00
|
|
|
|
private static readonly HashSet<GuiWidget> RunningAttentions = new HashSet<GuiWidget>();
|
|
|
|
|
|
private readonly double animationDelay = 1 / 20.0;
|
|
|
|
|
|
private readonly int cycles = 1;
|
|
|
|
|
|
private readonly double lightnessChange = 1;
|
|
|
|
|
|
private readonly double pulseTime = 1.38;
|
2017-10-31 11:43:25 -07:00
|
|
|
|
private Color startColor;
|
2016-05-17 15:30:39 -07:00
|
|
|
|
private Stopwatch timeSinceStart = null;
|
|
|
|
|
|
private GuiWidget widgetToHighlight;
|
|
|
|
|
|
|
2016-06-01 07:07:36 -07:00
|
|
|
|
private AttentionGetter(GuiWidget widgetToHighlight)
|
2016-05-17 15:30:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
this.widgetToHighlight = widgetToHighlight;
|
2016-07-24 16:31:27 -07:00
|
|
|
|
widgetToHighlight.AfterDraw += ConnectToWidget;
|
2016-05-17 15:30:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-01 07:07:36 -07:00
|
|
|
|
public static AttentionGetter GetAttention(GuiWidget widgetToHighlight)
|
|
|
|
|
|
{
|
2019-05-27 16:38:56 -07:00
|
|
|
|
if (!RunningAttentions.Contains(widgetToHighlight))
|
2016-06-01 07:07:36 -07:00
|
|
|
|
{
|
2019-05-27 16:38:56 -07:00
|
|
|
|
RunningAttentions.Add(widgetToHighlight);
|
2016-06-01 07:07:36 -07:00
|
|
|
|
return new AttentionGetter(widgetToHighlight);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-16 11:37:33 -07:00
|
|
|
|
public static double GetFadeInOutPulseRatio(double elapsedTime, double pulseTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
double ratio = elapsedTime;
|
|
|
|
|
|
while (ratio > pulseTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
ratio -= pulseTime;
|
|
|
|
|
|
}
|
2019-05-27 16:38:56 -07:00
|
|
|
|
|
2018-05-16 11:37:33 -07:00
|
|
|
|
ratio = ratio * 2 / pulseTime;
|
|
|
|
|
|
if (ratio > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
ratio = 1 - (ratio - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ratio;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-17 15:30:39 -07:00
|
|
|
|
private void ChangeBackgroundColor()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (widgetToHighlight != null)
|
|
|
|
|
|
{
|
2018-05-16 11:37:33 -07:00
|
|
|
|
double time = GetFadeInOutPulseRatio(timeSinceStart.Elapsed.TotalSeconds, pulseTime);
|
2018-04-12 07:16:58 -07:00
|
|
|
|
double lightnessMultiplier = Quadratic.InOut(time);
|
2016-05-17 15:30:39 -07:00
|
|
|
|
|
2019-04-26 12:14:35 -07:00
|
|
|
|
widgetToHighlight.BackgroundColor = startColor.AdjustLightness(1 + lightnessChange * lightnessMultiplier).ToColor();
|
2016-05-17 15:30:39 -07:00
|
|
|
|
if (widgetToHighlight.HasBeenClosed || timeSinceStart.Elapsed.TotalSeconds > cycles * pulseTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
widgetToHighlight.BackgroundColor = startColor;
|
2016-07-24 16:31:27 -07:00
|
|
|
|
widgetToHighlight.AfterDraw -= ConnectToWidget;
|
2019-05-27 16:38:56 -07:00
|
|
|
|
RunningAttentions.Remove(widgetToHighlight);
|
2016-05-17 15:30:39 -07:00
|
|
|
|
widgetToHighlight = null;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 13:06:08 -08:00
|
|
|
|
private void ConnectToWidget(object drawingWidget, DrawEventArgs e)
|
2016-05-17 15:30:39 -07:00
|
|
|
|
{
|
2019-05-27 16:38:56 -07:00
|
|
|
|
var parent = drawingWidget as GuiWidget;
|
2016-05-17 15:30:39 -07:00
|
|
|
|
while (parent.BackgroundColor.Alpha0To255 == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
parent = parent.Parent;
|
|
|
|
|
|
}
|
2019-05-27 16:38:56 -07:00
|
|
|
|
|
2016-05-17 15:30:39 -07:00
|
|
|
|
startColor = parent.BackgroundColor;
|
|
|
|
|
|
timeSinceStart = Stopwatch.StartNew();
|
2016-07-24 16:31:27 -07:00
|
|
|
|
widgetToHighlight.AfterDraw -= ConnectToWidget;
|
2018-04-11 17:35:34 -07:00
|
|
|
|
var runningInterval = UiThread.SetInterval(ChangeBackgroundColor, animationDelay);
|
2018-11-13 16:52:23 -08:00
|
|
|
|
parent.Closed += (s, e2) => UiThread.ClearInterval(runningInterval);
|
2016-05-17 15:30:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|