From ef3e444c625c0db5ab10e7987921382b41942ed6 Mon Sep 17 00:00:00 2001 From: "Piotr Czaja/Advanced Frameworks (PLT) /SRPOL/Engineer/Samsung Electronics" Date: Wed, 22 Sep 2021 13:19:50 +0200 Subject: [PATCH] Add dp geometry utils. --- Fitness/Views/DpUtils.cs | 63 +++++++++++++++++++++++++++++++++ Fitness/Views/ExtentsInDpExtension.cs | 38 ++++++++++++++++++++ Fitness/Views/PixelSizeInDpExtension.cs | 35 ++++++++++++++++++ Fitness/Views/PositionInDpExtension.cs | 44 +++++++++++++++++++++++ Fitness/Views/SizeInDpExtension.cs | 41 +++++++++++++++++++++ 5 files changed, 221 insertions(+) create mode 100644 Fitness/Views/DpUtils.cs create mode 100644 Fitness/Views/ExtentsInDpExtension.cs create mode 100644 Fitness/Views/PixelSizeInDpExtension.cs create mode 100644 Fitness/Views/PositionInDpExtension.cs create mode 100644 Fitness/Views/SizeInDpExtension.cs diff --git a/Fitness/Views/DpUtils.cs b/Fitness/Views/DpUtils.cs new file mode 100644 index 0000000..2f6f595 --- /dev/null +++ b/Fitness/Views/DpUtils.cs @@ -0,0 +1,63 @@ +using System; +using Tizen.NUI; + +namespace Fitness.Views +{ + /// + /// Utils for converting dp pixels. + /// + public static class DpUtils + { + private const float DpPixelDivider = 2.75f; + + /// + /// Converts dp pixels into pixels. + /// + /// The number of dp pixels. + /// The number of pixels. + public static int DpToPixels(int pixels) + { + return (int)Math.Round(DpTypeConverter.Instance.ConvertToPixel(pixels / DpPixelDivider), MidpointRounding.AwayFromZero); + } + + /// + /// Converts dp pixels into pixels. + /// + /// The number of dp pixels. + /// The number of pixels. + public static ushort DpToPixels(ushort pixels) + { + return (ushort)Math.Round(DpTypeConverter.Instance.ConvertToPixel(pixels / DpPixelDivider), MidpointRounding.AwayFromZero); + } + + /// + /// Converts dp pixels into pixels. + /// + /// The number of dp pixels. + /// The number of pixels. + public static float DpToPixels(float pixels) + { + return DpTypeConverter.Instance.ConvertToPixel(pixels / DpPixelDivider); + } + + /// + /// Converts dp pixels into pixels. + /// + /// The size to be converted to pixels. + /// The number of pixels. + public static Size2D DpToPixels(Size2D size) + { + return new Size2D(DpToPixels(size.Width), DpToPixels(size.Height)); + } + + /// + /// Converts dp pixels into pixels. + /// + /// The extents collection to be converted to pixels. + /// The number of pixels. + public static Extents DpToPixels(Extents extents) + { + return new Extents(DpToPixels(extents.Start), DpToPixels(extents.End), DpToPixels(extents.Top), DpToPixels(extents.Bottom)); + } + } +} diff --git a/Fitness/Views/ExtentsInDpExtension.cs b/Fitness/Views/ExtentsInDpExtension.cs new file mode 100644 index 0000000..ca26ec7 --- /dev/null +++ b/Fitness/Views/ExtentsInDpExtension.cs @@ -0,0 +1,38 @@ +using System; +using Tizen.NUI; +using Tizen.NUI.Xaml; + +namespace Fitness.Views +{ + /// + /// Converts number of dp pixels to exact number of pixels in the target. + /// + public class ExtentsInDpExtension : IMarkupExtension + { + /// + /// Gets or sets the start offset in dp type. + /// + public ushort Start { get; set; } + + /// + /// Gets or sets the end offset in dp type. + /// + public ushort End { get; set; } + + /// + /// Gets or sets the top offset in dp type. + /// + public ushort Top { get; set; } + + /// + /// Gets or sets the bottom offset in dp type. + /// + public ushort Bottom { get; set; } + + /// + public object ProvideValue(IServiceProvider serviceProvider) + { + return new Extents(DpUtils.DpToPixels(Start), DpUtils.DpToPixels(End), DpUtils.DpToPixels(Top), DpUtils.DpToPixels(Bottom)); + } + } +} diff --git a/Fitness/Views/PixelSizeInDpExtension.cs b/Fitness/Views/PixelSizeInDpExtension.cs new file mode 100644 index 0000000..1448e26 --- /dev/null +++ b/Fitness/Views/PixelSizeInDpExtension.cs @@ -0,0 +1,35 @@ +using System; +using Tizen.NUI; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Binding; +using Tizen.NUI.Xaml; + +namespace Fitness.Views +{ + /// + /// Converts TextLabels's PixelSize to exact number of pixels in the target. + /// + public class PixelSizeInDpExtension : IMarkupExtension + { + /// + /// Initializes a new instance of the class. + /// Creates markups with given size (in number of dp pixels). + /// + /// The number of dp pixels. + public PixelSizeInDpExtension(float dpSize = 0) + { + DpSize = dpSize; + } + + /// + /// Gets or sets the size of font in dp pixels. + /// + public float DpSize { get; set; } + + /// + public object ProvideValue(IServiceProvider serviceProvider) + { + return DpUtils.DpToPixels(DpSize); + } + } +} diff --git a/Fitness/Views/PositionInDpExtension.cs b/Fitness/Views/PositionInDpExtension.cs new file mode 100644 index 0000000..3ef3387 --- /dev/null +++ b/Fitness/Views/PositionInDpExtension.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tizen.NUI; +using Tizen.NUI.Components; +using Tizen.NUI.Xaml; + +namespace Fitness.Views +{ + /// + /// PositionInDpExtension. + /// + public class PositionInDpExtension : IMarkupExtension + { + /// + /// Initializes a new instance of the class. + /// + /// x. + /// y. + public PositionInDpExtension(int x = 0, int y = 0) + { + X = x; + Y = y; + } + + /// + /// Gets or sets x as number of dp pixels. + /// + public int X { get; set; } + + /// + /// Gets or sets y as number of dp pixels. + /// + public int Y { get; set; } + + /// + public object ProvideValue(IServiceProvider serviceProvider) + { + return new Position(DpUtils.DpToPixels(X), DpUtils.DpToPixels(Y)); + } + } +} diff --git a/Fitness/Views/SizeInDpExtension.cs b/Fitness/Views/SizeInDpExtension.cs new file mode 100644 index 0000000..b807964 --- /dev/null +++ b/Fitness/Views/SizeInDpExtension.cs @@ -0,0 +1,41 @@ +using System; +using Tizen.NUI; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Binding; +using Tizen.NUI.Xaml; + +namespace Fitness.Views +{ + /// + /// Creates markups with given width and height (in number of dp pixels). + /// + public class SizeInDpExtension : IMarkupExtension + { + /// + /// Initializes a new instance of the class. + /// + /// The size width. + /// The size height. + public SizeInDpExtension(int width = 0, int height = 0) + { + Width = width; + Height = height; + } + + /// + /// Gets or sets the width. + /// + public int Width { get; set; } + + /// + /// Gets or sets the height. + /// + public int Height { get; set; } + + /// + public object ProvideValue(IServiceProvider serviceProvider) + { + return new Size2D(DpUtils.DpToPixels(Width), DpUtils.DpToPixels(Height)); + } + } +} -- 2.7.4