//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//
// UserWin satellite library | Author: Sergei Y.Kitáev
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;

using CommonHelpers;

namespace UserWin {

//**************************************************************************************************
/// <summary>
/// </summary>
public partial class UXDrawingSurfice: Panel {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public List<Visual> Visuals
{
   get { return _Visuals_; }
}

//==================================================================================================

public UXDrawingSurfice()
{
   _Visuals_ = new List<Visual>();
}

//==================================================================================================

public void AddVisual(Visual visual)
{
   _Visuals_.Add( visual );
   AddVisualChild( visual );
   AddLogicalChild( visual );
}

public void RemoveVisual(Visual visual)
{
   RemoveLogicalChild( visual );
   RemoveVisualChild( visual );
   _Visuals_.Remove( visual );
}

//--------------------------------------------------------------------------------------------------

public DrawingVisual GetVisual(Point point)
{
   HitTestResult hitResult =
      VisualTreeHelper.HitTest( this, point );
   return hitResult!=null ?
      hitResult.VisualHit as DrawingVisual :
      null;
}

public DrawingVisual GetVisual(MouseEventArgs e)
{
   return GetVisual( e.GetPosition(this) );
}

//--------------------------------------------------------------------------------------------------

public List<DrawingVisual> GetVisuals(Geometry region)
{
   return GetVisuals( region, IntersectionDetail.FullyContains );
}

public List<DrawingVisual> GetVisuals(Geometry region,IntersectionDetail intersection)
{
   var hits = new List<DrawingVisual>();
   var parameters = new GeometryHitTestParameters( region );

   HitTestResultCallback callback =
      delegate (HitTestResult hitResult)
      {
         var geometryResult = (GeometryHitTestResult)hitResult;
         var visual = hitResult.VisualHit as DrawingVisual;

         if ( visual != null &&
              geometryResult.IntersectionDetail == intersection )
            hits.Add( visual );

         return HitTestResultBehavior.Continue;
      };

   VisualTreeHelper.HitTest( this, null, callback, parameters );

   return hits;
}

//==================================================================================================

protected override int VisualChildrenCount
{
   get { return _Visuals_.Count; }
}

protected override Visual GetVisualChild(int index)
{
   if (index>=0 && index<_Visuals_.Count)
      return _Visuals_[ index ];
   throw new ArgumentOutOfRangeException();
}

//==================================================================================================

static UXDrawingSurfice()
{
   UserWin_Library.Access();
}

//==================================================================================================

List<Visual> _Visuals_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UXDrawingSurfice
//**************************************************************************************************

} // UserWin
