using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Research.DynamicDataDisplay.Common;
namespace Microsoft.Research.DynamicDataDisplay.ViewportConstraints
{
///
/// Represents a constraint which returns data rectangle, intersected with specified data domain.
///
public class DomainConstraint : ViewportConstraint
{
///
/// Initializes a new instance of the class.
///
public DomainConstraint() { }
///
/// Initializes a new instance of the class with given domain property.
///
/// The domain.
public DomainConstraint(DataRect domain)
{
this.Domain = domain;
}
private DataRect domain = new DataRect(-1, -1, 2, 2);
///
/// Gets or sets the domain.
///
/// The domain.
public DataRect Domain
{
get { return domain; }
set
{
if (domain != value)
{
domain = value;
RaiseChanged();
}
}
}
///
/// Applies the specified old data rect.
///
/// The old data rect.
/// The new data rect.
/// The viewport.
///
public override DataRect Apply(DataRect oldDataRect, DataRect newDataRect, Viewport2D viewport)
{
DataRect res = domain;
if (domain.IsEmpty)
{
res = newDataRect;
}
else if (newDataRect.IntersectsWith(domain))
{
res = newDataRect;
if (newDataRect.Size == oldDataRect.Size)
{
if (res.XMin < domain.XMin) res.XMin = domain.XMin;
if (res.YMin < domain.YMin) res.YMin = domain.YMin;
if (res.XMax > domain.XMax) res.XMin += domain.XMax - res.XMax;
if (res.YMax > domain.YMax) res.YMin += domain.YMax - res.YMax;
}
else
{
res = DataRect.Intersect(newDataRect, domain);
}
}
return res;
}
}
}