unosquare / ffmediaelement

FFME: The Advanced WPF MediaElement (based on FFmpeg)
https://unosquare.github.io/ffmediaelement/
Other
1.17k stars 238 forks source link

Cursor property visual not changed #542

Closed nevaran closed 3 years ago

nevaran commented 3 years ago

Issue

When I change the cursor propery in a normal WPF image control, the cursor changes as expected, but doing that in the FFME one, it does not change at all; but the cursor is changed to the expected one when the user is holding down the mouse button instead when hovering over the said control, and there is another control attached as a parent to the FFME one that contains the MouseLeftButtonDown event that changes the cursor.

If the control is a standalone, the cursor seems to not change at all no matter what the property is set to.

Issue Categories

Version Information

Steps to Reproduce

  1. Create a ZoomBorder.cs file and assign the class to it
  2. Paste xaml code into grid or other and assign the required ZoomBorder xmlns
  3. Start a the application with any type of media loaded and hover over the

Expected Results

Sample Code

XAML

<utils:ZoomBorder
                            x:Name="borderMed"
                            Background="Transparent"
                            ClipToBounds="True"
                            ScrollViewer.VerticalScrollBarVisibility="Disabled"
                            >
                            <ffme:MediaElement
                                x:Name="MediaView"
                                LoadedBehavior="Play"
                                LoopingBehavior="Play"
                                MediaClosed="MediaView_MediaClosed"
                                MediaOpened="MediaView_MediaOpened"
                                ScrollViewer.VerticalScrollBarVisibility="Disabled"
                                ScrubbingEnabled="True"
                                SpeedRatio="1"
                                UnloadedBehavior="Manual"
                                />
                        </utils:ZoomBorder>

C

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

namespace FIVStandard.Utils
{
    public class ZoomBorder : Border
    {
        private UIElement child = null;
        private Point origin;
        private Point start;

        private static TranslateTransform GetTranslateTransform(UIElement element)
        {
            return (TranslateTransform)((TransformGroup)element.RenderTransform)
              .Children.First(tr => tr is TranslateTransform);
        }

        private static ScaleTransform GetScaleTransform(UIElement element)
        {
            return (ScaleTransform)((TransformGroup)element.RenderTransform)
              .Children.First(tr => tr is ScaleTransform);
        }

        public override UIElement Child
        {
            get { return base.Child; }
            set
            {
                if (value != null && value != this.Child)
                    this.Initialize(value);
                base.Child = value;
            }
        }

        public void Initialize(UIElement element)
        {
            this.child = element;
            if (child != null)
            {
                TransformGroup group = new TransformGroup();
                ScaleTransform st = new ScaleTransform();
                group.Children.Add(st);
                TranslateTransform tt = new TranslateTransform();
                group.Children.Add(tt);
                child.RenderTransform = group;
                child.RenderTransformOrigin = new Point(0.0, 0.0);
                this.MouseWheel += Child_MouseWheel;
                this.MouseLeftButtonDown += Child_MouseLeftButtonDown;
                this.MouseLeftButtonUp += Child_MouseLeftButtonUp;
                this.MouseMove += Child_MouseMove;
            }
        }

        public void Reset()
        {
            if (child != null)
            {
                // reset zoom
                var st = GetScaleTransform(child);
                st.ScaleX = 1.0;
                st.ScaleY = 1.0;

                // reset pan
                var tt = GetTranslateTransform(child);
                tt.X = 0.0;
                tt.Y = 0.0;
            }
        }

        private void Child_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (child != null)
            {
                var st = GetScaleTransform(child);
                var tt = GetTranslateTransform(child);

                double zoom = e.Delta > 0 ? Properties.Settings.Default.ZoomSensitivity : -Properties.Settings.Default.ZoomSensitivity;
                if (!(e.Delta > 0) && (st.ScaleX < .4 || st.ScaleY < .4))
                    return;

                Point relative = e.GetPosition(child);
                double abosuluteX;
                double abosuluteY;

                abosuluteX = relative.X * st.ScaleX + tt.X;
                abosuluteY = relative.Y * st.ScaleY + tt.Y;

                st.ScaleX += zoom;
                st.ScaleY += zoom;

                //zoom clamp max
                if (st.ScaleX > 50.0)
                    st.ScaleX = 50.0;
                if (st.ScaleY > 50.0)
                    st.ScaleY = 50.0;

                //zoom clamp min
                if (st.ScaleX < 1.0)
                    st.ScaleX = 1.0;
                if (st.ScaleY < 1.0)
                    st.ScaleY = 1.0;

                //snap panning back to center
                if (st.ScaleX == 1.0 && st.ScaleY == 1.0)
                {
                    tt.X = 0.0;
                    tt.Y = 0.0;
                }
                else
                {
                    tt.X = abosuluteX - relative.X * st.ScaleX;
                    tt.Y = abosuluteY - relative.Y * st.ScaleY;
                }
            }
        }

        private void Child_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (child != null)
            {
                var tt = GetTranslateTransform(child);
                start = e.GetPosition(this);
                origin = new Point(tt.X, tt.Y);
                this.Cursor = Cursors.Hand;
                child.CaptureMouse();
            }
        }

        private void Child_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (child != null)
            {
                child.ReleaseMouseCapture();
                this.Cursor = Cursors.Arrow;
            }
        }

        private void Child_MouseMove(object sender, MouseEventArgs e)
        {
            if (child != null)
            {
                if (child.IsMouseCaptured)
                {
                    var tt = GetTranslateTransform(child);
                    Vector v = start - e.GetPosition(this);

                    var st = GetScaleTransform(child);
                    if(st.ScaleX == 1.0 && st.ScaleY == 1.0)
                    {
                        tt.X = 0.0;
                        tt.Y = 0.0;
                    }
                    else
                    {
                        tt.X = origin.X - v.X;
                        tt.Y = origin.Y - v.Y;
                    }
                }
            }
        }
    }
}
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.