Browse Source

Added VisualExtensions.PointToClient.

In addition moved PointToScreen to VisualExtensions, and renamed
IRenderRoot.TranslatePointToScreen to IRenderRoot.PointToScreen.
Steven Kirk 9 years ago
parent
commit
3fde7f979c

+ 8 - 0
src/Gtk/Perspex.Gtk/WindowImpl.cs

@@ -172,6 +172,14 @@ namespace Perspex.Gtk
                     new Rectangle((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height), true);
         }
 
+        public Point PointToClient(Point point)
+        {
+            int x, y;
+            GdkWindow.GetDeskrelativeOrigin(out x, out y);
+
+            return new Point(point.X - x, point.Y - y);
+        }
+
         public Point PointToScreen(Point point)
         {
             int x, y;

+ 7 - 0
src/Perspex.Controls/Platform/ITopLevelImpl.cs

@@ -72,6 +72,13 @@ namespace Perspex.Platform
         /// </summary>
         void SetInputRoot(IInputRoot inputRoot);
 
+        /// <summary>
+        /// Converts a point from screen to client coordinates.
+        /// </summary>
+        /// <param name="point">The point in screen coordinates.</param>
+        /// <returns>The point in client coordinates.</returns>
+        Point PointToClient(Point point);
+
         /// <summary>
         /// Converts a point from client to screen coordinates.
         /// </summary>

+ 6 - 2
src/Perspex.Controls/Platform/PlatformManager.cs

@@ -111,11 +111,15 @@ namespace Perspex.Controls.Platform
                 Input?.Invoke(obj);
             }
 
-            public Point PointToScreen(Point point)
+            public Point PointToClient(Point point)
             {
-                return _tl.PointToScreen(point*ScalingFactor)/ScalingFactor;
+                return _tl.PointToClient(point / ScalingFactor) * ScalingFactor;
             }
 
+            public Point PointToScreen(Point point)
+            {
+                return _tl.PointToScreen(point * ScalingFactor) / ScalingFactor;
+            }
 
             public void Invalidate(Rect rc)
             {

+ 8 - 6
src/Perspex.Controls/TopLevel.cs

@@ -212,12 +212,14 @@ namespace Perspex.Controls
             private set;
         }
 
-        /// <summary>
-        /// Translates a point from window coordinates into screen coordinates.
-        /// </summary>
-        /// <param name="p">The point.</param>
-        /// <returns>The point in screen coordinates.</returns>
-        Point IRenderRoot.TranslatePointToScreen(Point p)
+        /// <inheritdoc/>
+        Point IRenderRoot.PointToClient(Point p)
+        {
+            return PlatformImpl.PointToClient(p);
+        }
+
+        /// <inheritdoc/>
+        Point IRenderRoot.PointToScreen(Point p)
         {
             return PlatformImpl.PointToScreen(p);
         }

+ 1 - 0
src/Perspex.SceneGraph/Perspex.SceneGraph.csproj

@@ -127,6 +127,7 @@
     <Compile Include="Size.cs" />
     <Compile Include="Thickness.cs" />
     <Compile Include="Vector.cs" />
+    <Compile Include="VisualExtensions.cs" />
     <Compile Include="Visual.cs" />
     <Compile Include="VisualTreeAttachmentEventArgs.cs" />
     <Compile Include="VisualTree\IHostedVisualTreeRoot.cs" />

+ 11 - 4
src/Perspex.SceneGraph/Rendering/IRenderRoot.cs

@@ -16,10 +16,17 @@ namespace Perspex.Rendering
         IRenderQueueManager RenderQueueManager { get; }
 
         /// <summary>
-        /// Translates a point to screen co-ordinates.
+        /// Converts a point from screen to client coordinates.
         /// </summary>
-        /// <param name="p">The point.</param>
-        /// <returns>The point in screen co-ordinates.</returns>
-        Point TranslatePointToScreen(Point p);
+        /// <param name="point">The point in screen coordinates.</param>
+        /// <returns>The point in client coordinates.</returns>
+        Point PointToClient(Point point);
+
+        /// <summary>
+        /// Converts a point from client to screen coordinates.
+        /// </summary>
+        /// <param name="point">The point in client coordinates.</param>
+        /// <returns>The point in screen coordinates.</returns>
+        Point PointToScreen(Point point);
     }
 }

+ 0 - 34
src/Perspex.SceneGraph/Visual.cs

@@ -291,17 +291,6 @@ namespace Perspex
             Contract.Requires<ArgumentNullException>(context != null);
         }
 
-        /// <summary>
-        /// Converts a point from control coordinates to screen coordinates.
-        /// </summary>
-        /// <param name="point">The point to convert.</param>
-        /// <returns>The point in screen coordinates.</returns>
-        public Point PointToScreen(Point point)
-        {
-            var p = GetOffsetFromRoot(this);
-            return p.Item1.TranslatePointToScreen(point + p.Item2);
-        }
-
         /// <summary>
         /// Returns a transform that transforms the visual's coordinates into the coordinates
         /// of the specified <paramref name="visual"/>.
@@ -412,29 +401,6 @@ namespace Perspex
             return result;
         }
 
-        /// <summary>
-        /// Gets the root of the controls visual tree and the distance from the root.
-        /// </summary>
-        /// <param name="v">The visual.</param>
-        /// <returns>A tuple containing the root and the distance from the root</returns>
-        private static Tuple<IRenderRoot, Vector> GetOffsetFromRoot(IVisual v)
-        {
-            var result = new Vector();
-
-            while (!(v is IRenderRoot))
-            {
-                result = new Vector(result.X + v.Bounds.X, result.Y + v.Bounds.Y);
-                v = v.VisualParent;
-
-                if (v == null)
-                {
-                    throw new InvalidOperationException("Control is not attached to visual tree.");
-                }
-            }
-
-            return Tuple.Create((IRenderRoot)v, result);
-        }
-
         /// <summary>
         /// Called when a visual's <see cref="RenderTransform"/> changes.
         /// </summary>

+ 74 - 0
src/Perspex.SceneGraph/VisualExtensions.cs

@@ -0,0 +1,74 @@
+// Copyright (c) The Perspex Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Linq;
+using System.Reactive.Linq;
+using Perspex.Animation;
+using Perspex.Collections;
+using Perspex.Data;
+using Perspex.Media;
+using Perspex.Platform;
+using Perspex.Rendering;
+using Perspex.VisualTree;
+using Serilog;
+using Serilog.Core.Enrichers;
+
+namespace Perspex
+{
+    /// <summary>
+    /// Extension methods for <see cref="IVisual"/>.
+    /// </summary>
+    public static class VisualExtensions
+    {
+        /// <summary>
+        /// Converts a point from screen to client coordinates.
+        /// </summary>
+        /// <param name="visual">The visual.</param>
+        /// <param name="point">The point in screen coordinates.</param>
+        /// <returns>The point in client coordinates.</returns>
+        public static Point PointToClient(this IVisual visual, Point point)
+        {
+            var p = GetRootAndPosition(visual);
+            return p.Item1.PointToClient(point + p.Item2);
+        }
+
+        /// <summary>
+        /// Converts a point from client to screen coordinates.
+        /// </summary>
+        /// <param name="visual">The visual.</param>
+        /// <param name="point">The point in client coordinates.</param>
+        /// <returns>The point in screen coordinates.</returns>
+        public static Point PointToScreen(this IVisual visual, Point point)
+        {
+            var p = GetRootAndPosition(visual);
+            return p.Item1.PointToScreen(point + p.Item2);
+        }
+
+        /// <summary>
+        /// Gets the root of the control's visual tree and the position of the control 
+        /// in the root's coordinate space.
+        /// </summary>
+        /// <param name="v">The visual.</param>
+        /// <returns>A tuple containing the root and the position of the control.</returns>
+        private static Tuple<IRenderRoot, Vector> GetRootAndPosition(IVisual v)
+        {
+            var result = new Vector();
+
+            while (!(v is IRenderRoot))
+            {
+                result = new Vector(result.X + v.Bounds.X, result.Y + v.Bounds.Y);
+                v = v.VisualParent;
+
+                if (v == null)
+                {
+                    throw new InvalidOperationException("Control is not attached to visual tree.");
+                }
+            }
+
+            return Tuple.Create((IRenderRoot)v, result);
+        }
+    }
+}

+ 7 - 0
src/Windows/Perspex.Win32/WindowImpl.cs

@@ -242,6 +242,13 @@ namespace Perspex.Win32
             UnmanagedMethods.InvalidateRect(_hwnd, ref r, false);
         }
 
+        public Point PointToClient(Point point)
+        {
+            var p = new UnmanagedMethods.POINT { X = (int)point.X, Y = (int)point.Y };
+            UnmanagedMethods.ScreenToClient(_hwnd, ref p);
+            return new Point(p.X, p.Y);
+        }
+
         public Point PointToScreen(Point point)
         {
             var p = new UnmanagedMethods.POINT { X = (int)point.X, Y = (int)point.Y };

+ 2 - 0
src/iOS/Perspex.iOS/PerspexView.cs

@@ -101,6 +101,8 @@ namespace Perspex.iOS
 
         public void SetInputRoot(IInputRoot inputRoot) => _inputRoot = inputRoot;
 
+        public Point PointToClient(Point point) => point;
+
         public Point PointToScreen(Point point) => point;
 
         public void SetCursor(IPlatformHandle cursor)

+ 6 - 1
tests/Perspex.Controls.UnitTests/ControlTests.cs

@@ -174,7 +174,12 @@ namespace Perspex.Controls.UnitTests
                 get { throw new NotImplementedException(); }
             }
 
-            public Point TranslatePointToScreen(Point p)
+            public Point PointToClient(Point p)
+            {
+                throw new NotImplementedException();
+            }
+
+            public Point PointToScreen(Point p)
             {
                 throw new NotImplementedException();
             }

+ 6 - 1
tests/Perspex.Controls.UnitTests/ControlTests_NameScope.cs

@@ -103,7 +103,12 @@ namespace Perspex.Controls.UnitTests
                 get { throw new NotImplementedException(); }
             }
 
-            public Point TranslatePointToScreen(Point p)
+            public Point PointToClient(Point p)
+            {
+                throw new NotImplementedException();
+            }
+
+            public Point PointToScreen(Point p)
             {
                 throw new NotImplementedException();
             }

+ 6 - 1
tests/Perspex.Controls.UnitTests/TestRoot.cs

@@ -25,7 +25,12 @@ namespace Perspex.Controls.UnitTests
 
         public IRenderQueueManager RenderQueueManager => null;
 
-        public Point TranslatePointToScreen(Point p)
+        public Point PointToClient(Point p)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Point PointToScreen(Point p)
         {
             return new Point();
         }

+ 6 - 1
tests/Perspex.Markup.UnitTests/TestRoot.cs

@@ -39,7 +39,12 @@ namespace Perspex.Markup.UnitTests
             get { throw new NotImplementedException(); }
         }
 
-        public Point TranslatePointToScreen(Point p)
+        public Point PointToClient(Point p)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Point PointToScreen(Point p)
         {
             throw new NotImplementedException();
         }

+ 6 - 1
tests/Perspex.Markup.Xaml.UnitTests/TestRoot.cs

@@ -39,7 +39,12 @@ namespace Perspex.Markup.Xaml.UnitTests
             get { throw new NotImplementedException(); }
         }
 
-        public Point TranslatePointToScreen(Point p)
+        public Point PointToClient(Point p)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Point PointToScreen(Point p)
         {
             throw new NotImplementedException();
         }

+ 6 - 1
tests/Perspex.SceneGraph.UnitTests/TestRoot.cs

@@ -19,7 +19,12 @@ namespace Perspex.SceneGraph.UnitTests
             get { throw new NotImplementedException(); }
         }
 
-        public Point TranslatePointToScreen(Point p)
+        public Point PointToClient(Point p)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Point PointToScreen(Point p)
         {
             throw new NotImplementedException();
         }

+ 6 - 1
tests/Perspex.Styling.UnitTests/TestRoot.cs

@@ -26,7 +26,12 @@ namespace Perspex.Styling.UnitTests
             get { throw new NotImplementedException(); }
         }
 
-        public Point TranslatePointToScreen(Point p)
+        public Point PointToClient(Point p)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Point PointToScreen(Point p)
         {
             return new Point();
         }