From 224da7d76de99313b109db6de456ca69889169f4 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Wed, 11 Aug 2021 16:34:58 +0900 Subject: [PATCH] [NUI] CanvasView: Add picture class for drawing images Add Picture class that can load image and rasterize it on CanvasView's canvas. This image is drawn with Vector Primitives. And, since it inherits Drawable, user can use transformation method, and can use masking and clipping methods together with other shapes. --- .../src/internal/Interop/Interop.Picture.cs | 39 ++ .../BaseComponents/VectorGraphics/Picture.cs | 95 ++++ .../Tizen.NUI.Samples/Samples/CanvasViewSamsple.cs | 8 + .../res/images/Dali/DaliDemo/Kid1.svg | 491 +++++++++++++++++++++ 4 files changed, 633 insertions(+) create mode 100755 src/Tizen.NUI/src/internal/Interop/Interop.Picture.cs create mode 100755 src/Tizen.NUI/src/public/BaseComponents/VectorGraphics/Picture.cs create mode 100755 test/Tizen.NUI.Samples/Tizen.NUI.Samples/res/images/Dali/DaliDemo/Kid1.svg diff --git a/src/Tizen.NUI/src/internal/Interop/Interop.Picture.cs b/src/Tizen.NUI/src/internal/Interop/Interop.Picture.cs new file mode 100755 index 0000000..a9c0fab --- /dev/null +++ b/src/Tizen.NUI/src/internal/Interop/Interop.Picture.cs @@ -0,0 +1,39 @@ +/* + * Copyright(c) 2021 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +namespace Tizen.NUI +{ + internal static partial class Interop + { + internal static partial class Picture + { + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Picture_New")] + public static extern global::System.IntPtr New(); + + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Picture_Load")] + [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.U1)] + public static extern bool Load(global::System.Runtime.InteropServices.HandleRef picture, string url); + + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Picture_SetSize")] + [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.U1)] + public static extern bool SetSize(global::System.Runtime.InteropServices.HandleRef picture, global::System.Runtime.InteropServices.HandleRef size); + + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Picture_GetSize")] + public static extern global::System.IntPtr GetSize(global::System.Runtime.InteropServices.HandleRef picture); + } + } +} diff --git a/src/Tizen.NUI/src/public/BaseComponents/VectorGraphics/Picture.cs b/src/Tizen.NUI/src/public/BaseComponents/VectorGraphics/Picture.cs new file mode 100755 index 0000000..f314067 --- /dev/null +++ b/src/Tizen.NUI/src/public/BaseComponents/VectorGraphics/Picture.cs @@ -0,0 +1,95 @@ +/* +* Copyright(c) 2021 Samsung Electronics Co., Ltd. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ + +using System; +using System.ComponentModel; + +namespace Tizen.NUI.BaseComponents.VectorGraphics +{ + /// + /// A class representing an image read in one of the supported formats: raw, svg, png and etc. + /// Besides the methods inherited from the Drawable, it provides methods to load and draw images on the canvas. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public class Picture : Drawable + { + /// + /// Creates an initialized Picture. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public Picture() : this(Interop.Picture.New(), true) + { + if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + } + + internal Picture(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn) + { + } + + /// + /// Loads a picture data directly from a file. + /// + /// A path to the picture file. + /// Thrown when url is null. + /// Thrown when image load fail. + [EditorBrowsable(EditorBrowsableState.Never)] + public void Load(string url) + { + if (url == null) + { + throw new ArgumentNullException(nameof(url)); + } + bool ret = Interop.Picture.Load(View.getCPtr(this), url); + if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + if (!ret) + { + throw new Exception("Image load fail : " + url); + } + } + + /// + /// Resize the picture content with the given size. + /// + /// Resize the picture content while keeping the default size aspect ratio. + /// The scaling factor is established for each of dimensions and the smaller value is applied to both of them. + /// + /// A new size of the image in pixels + /// Thrown when size is null. + [EditorBrowsable(EditorBrowsableState.Never)] + public void SetSize(Size2D size) + { + if (size == null) + { + throw new ArgumentNullException(nameof(size)); + } + Interop.Picture.SetSize(View.getCPtr(this), Size2D.getCPtr(size)); + if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + } + + /// + /// Gets the size of the image. + /// + /// The size of the image in pixels. + [EditorBrowsable(EditorBrowsableState.Never)] + public Size2D GetSize() + { + global::System.IntPtr cPtr = Interop.Picture.GetSize(View.getCPtr(this)); + if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + return Size2D.GetSize2DFromPtr(cPtr); + } + } +} diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CanvasViewSamsple.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CanvasViewSamsple.cs index 0a74246..738bdb6 100644 --- a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CanvasViewSamsple.cs +++ b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/CanvasViewSamsple.cs @@ -184,6 +184,12 @@ namespace Tizen.NUI.Samples group2.AddDrawable(circleShape); canvasView.AddDrawable(group2); + Picture picture = new Picture(); + picture.Load(CommonResource.GetDaliResourcePath() + "DaliDemo/Kid1.svg"); + picture.SetSize(new Size2D(150, 150)); + picture.Translate(300.0f, 550.0f); + canvasView.AddDrawable(picture); + // Test Getter Position2D p1 = new Position2D(9, 9), p2 = new Position2D(8, 8); starFillLinearGradient.GetBounds(ref p1, ref p2); @@ -195,6 +201,8 @@ namespace Tizen.NUI.Samples log.Debug(tag, "Gradient Stops :" + i + " " + stops[i].Offset + " " + stops[i].Color.R + " " + stops[i].Color.G + " " + stops[i].Color.B + " " + stops[i].Color.A + "\n"); } + log.Debug(tag, "picture size : " + picture.GetSize().Width + " " + picture.GetSize().Height + "\n"); + log.Debug(tag, "circleShape Color : " + circleShape.FillColor.R + " " + circleShape.FillColor.G + " " + circleShape.FillColor.B + " " + circleShape.FillColor.A + "\n"); log.Debug(tag, "circleShape StrokeColor : " + circleShape.StrokeColor.R + " " + circleShape.StrokeColor.G + " " + circleShape.StrokeColor.B + " " + circleShape.StrokeColor.A + "\n"); diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/res/images/Dali/DaliDemo/Kid1.svg b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/res/images/Dali/DaliDemo/Kid1.svg new file mode 100755 index 0000000..b7d5476 --- /dev/null +++ b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/res/images/Dali/DaliDemo/Kid1.svg @@ -0,0 +1,491 @@ + + + +image/svg+xml -- 2.7.4