mattercontrol/original/MatterControlLib/CustomWidgets/SlideWidget.cs

85 lines
1.6 KiB
C#
Raw Permalink Normal View History

2015-04-08 15:20:10 -07:00
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
2015-04-08 15:20:10 -07:00
using System;
using System.Diagnostics;
namespace MatterHackers.MatterControl
{
2015-04-08 15:20:10 -07:00
public class SlideWidget : GuiWidget
{
private Stopwatch timeHasBeenChanging = new Stopwatch();
public SlideWidget()
: base(0.0, 0.0)
{
}
2015-04-08 15:20:10 -07:00
public override void OnDraw(Graphics2D graphics2D)
{
base.OnDraw(graphics2D);
if (this.Width != TargetWidth)
{
SetSlidePosition();
Invalidate();
}
}
2015-04-08 15:20:10 -07:00
private double OriginalWidth { get; set; }
2015-04-08 15:20:10 -07:00
private double TargetWidth { get; set; }
2015-04-08 15:20:10 -07:00
public void SlideIn()
{
this.Visible = true;
if (OriginalWidth == 0)
{
this.OriginalWidth = this.Width;
}
this.TargetWidth = this.OriginalWidth;
this.Width = 0.1;
timeHasBeenChanging.Restart();
SetSlidePosition();
Invalidate();
}
2015-04-08 15:20:10 -07:00
public void SlideOut()
{
this.Visible = true;
if (OriginalWidth == 0)
{
this.OriginalWidth = this.Width;
}
this.TargetWidth = 0;
timeHasBeenChanging.Restart();
SetSlidePosition();
Invalidate();
}
2015-04-08 15:20:10 -07:00
private void SetSlidePosition()
{
if (TargetWidth == 0 && this.Width == 0)
{
this.Visible = false;
this.Width = this.OriginalWidth;
}
else if (this.TargetWidth != this.Width)
{
double maxOffsetPerDraw = timeHasBeenChanging.ElapsedMilliseconds * 3;
2015-04-08 15:20:10 -07:00
double currentWidth = this.Width;
double delta = TargetWidth - currentWidth;
if (delta < 0)
{
delta = Math.Max(-maxOffsetPerDraw, delta);
}
else
{
delta = Math.Min(maxOffsetPerDraw, delta);
}
2015-04-08 15:20:10 -07:00
double offsetThisDraw = currentWidth + delta;
this.Width = offsetThisDraw;
}
}
}
}