mattercontrol/CustomWidgets/SlidePanelWidget.cs

124 lines
3.5 KiB
C#
Raw Normal View History

2014-01-29 19:09:30 -08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl
{
public class SlidePanel : GuiWidget
{
public List<GuiWidget> panels = new List<GuiWidget>();
2014-01-29 19:09:30 -08:00
int currentPanelIndex = -1;
int desiredPanelIndex = 0;
2014-01-29 19:09:30 -08:00
Stopwatch timeHasBeenChanging = new Stopwatch();
public SlidePanel(int count)
{
this.AnchorAll();
for (int i = 0; i < count; i++)
{
GuiWidget newPanel = new FlowLayoutWidget(FlowDirection.TopToBottom);
panels.Add(newPanel);
AddChild(newPanel);
2014-01-29 19:09:30 -08:00
}
}
public int PanelIndex
2014-01-29 19:09:30 -08:00
{
get
{
return currentPanelIndex;
2014-01-29 19:09:30 -08:00
}
set
{
if (currentPanelIndex != value)
2014-01-29 19:09:30 -08:00
{
desiredPanelIndex = value;
2014-01-29 19:09:30 -08:00
timeHasBeenChanging.Restart();
SetSlidePosition();
}
}
}
public void SetPanelIndexImediate(int index)
{
desiredPanelIndex = index;
SetSlidePosition();
}
public GuiWidget GetPanel(int index)
2014-01-29 19:09:30 -08:00
{
return panels[index];
2014-01-29 19:09:30 -08:00
}
public override void OnBoundsChanged(EventArgs e)
{
for (int i = 0; i < panels.Count; i++)
2014-01-29 19:09:30 -08:00
{
panels[i].LocalBounds = LocalBounds;
2014-01-29 19:09:30 -08:00
}
SetSlidePosition();
base.OnBoundsChanged(e);
}
public override void OnDraw(Graphics2D graphics2D)
{
base.OnDraw(graphics2D);
if (currentPanelIndex != desiredPanelIndex)
2014-01-29 19:09:30 -08:00
{
SetSlidePosition();
Invalidate();
}
}
void SetSlidePosition()
{
if (currentPanelIndex != desiredPanelIndex)
2014-01-29 19:09:30 -08:00
{
// set this based on the time that has elapsed and it should give us a nice result (we can even ease in ease out)
double maxOffsetPerDraw = timeHasBeenChanging.ElapsedMilliseconds;
double desiredOffset = desiredPanelIndex * -Width;
double currentOffset = panels[0].OriginRelativeParent.x;
2014-01-29 19:09:30 -08:00
double delta = desiredOffset - currentOffset;
if (delta < 0)
{
delta = Math.Max(-maxOffsetPerDraw, delta);
}
else
{
delta = Math.Min(maxOffsetPerDraw, delta);
}
double offsetThisDraw = currentOffset + delta;
for (int i = 0; i < panels.Count; i++)
2014-01-29 19:09:30 -08:00
{
panels[i].OriginRelativeParent = new Vector2(offsetThisDraw, 0);
2014-01-29 19:09:30 -08:00
offsetThisDraw += Width;
}
if (currentOffset + delta == desiredOffset)
{
currentPanelIndex = desiredPanelIndex;
2014-01-29 19:09:30 -08:00
}
}
else
{
double desiredOffset = desiredPanelIndex * -Width;
for (int i = 0; i < panels.Count; i++)
2014-01-29 19:09:30 -08:00
{
panels[i].OriginRelativeParent = new Vector2(desiredOffset, 0);
2014-01-29 19:09:30 -08:00
desiredOffset += Width;
}
}
}
}
}