Skip to content

Commit

Permalink
[NUI] Add markup for some view properties using value type
Browse files Browse the repository at this point in the history
This also includes PropertyMap optimizations to prevent value type being conveted to reference type again.

Signed-off-by: Jiyun Yang <[email protected]>
  • Loading branch information
rabbitfor committed Jan 9, 2025
1 parent a25391b commit 747dfc1
Show file tree
Hide file tree
Showing 14 changed files with 772 additions and 28 deletions.
32 changes: 32 additions & 0 deletions src/Tizen.NUI/src/devel/Lite/L.Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*
*/
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Tizen.NUI.L
{
/// <summary>
/// Defines a value type of color.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[StructLayout(LayoutKind.Sequential)]
public struct Color
{
public static readonly Color Default = new Color(-1, -1, -1, -1);
public static readonly Color Transparent = new Color(0, 0, 0, 0);
public static readonly Color Black = new Color(0, 0, 0, 1);
public static readonly Color White = new Color(1, 1, 1, 1);
Expand All @@ -46,6 +49,32 @@ public Color(float r, float g, float b, float a)
A = a;
}

/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="value">The hex value of 0xRRGGBB format.</param>
/// <example>
/// <code>
/// new L.Color(0xFF0000); // Solid red
/// new L.Color(0xFF0000).WithAlpha(0.5f); // Translucent red
/// </code>
/// </example>
public Color(uint value)
{
R = ((value >> 16) & 0xff) / 255.0f;
G = ((value >> 8) & 0xff) / 255.0f;
B = (value & 0xff) / 255.0f;
A = 1f;
}

internal Color(NUI.Vector4 vector4) : this(vector4.X, vector4.Y, vector4.Z, vector4.W)
{
}

public readonly bool IsDefault => R == -1 && G == -1 && B == -1 && A == -1;

public readonly bool IsZero => R == 0 && G == 0 && B == 0 && A == 0;

/// <summary>
/// Gets the red component of the color.
/// </summary>
Expand Down Expand Up @@ -85,6 +114,9 @@ public float A
/// <returns>The new color.</returns>
public readonly Color MultiplyAlpha(float alpha) => new Color(R, G, B, A * alpha);

/// <inheritdoc/>
public override string ToString() => $"R:{R}, G:{G}, B:{B}, A:{A}";

/// <summary>
/// Returns a new color object with the specified alpha value.
/// </summary>
Expand Down
73 changes: 73 additions & 0 deletions src/Tizen.NUI/src/devel/Lite/L.Corner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright(c) 2025 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.ComponentModel;
using System.Runtime.InteropServices;

namespace Tizen.NUI.L
{
/// <summary>
/// Defines a value type of corner radius.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[StructLayout(LayoutKind.Sequential)]
public struct Corner
{
public static readonly Corner Default = new Corner(-1, -1, -1, -1);
public static readonly Corner Zero = new Corner(0.0f, 0.0f, 0.0f, 0.0f);

/// <summary>
/// Initializes a new instance of the <see cref="Corner"/> struct.
/// </summary>
/// <param name="topLeft">The top-left value.</param>
/// <param name="topRight">The top-right value.</param>
/// <param name="bottomRight">The bottom-right value.</param>
/// <param name="bottomLeft">The bottom-left value.</param>
public Corner(float topLeft, float topRight, float bottomRight, float bottomLeft)
{
TopLeft = topLeft;
TopRight = topRight;
BottomRight = bottomRight;
BottomLeft = bottomLeft;
}

public readonly bool IsDefault => TopLeft == -1 && TopRight == -1 && BottomRight == -1 && BottomLeft == -1;

public readonly bool IsZero => TopLeft == 0 && TopRight == 0 && BottomRight == 0 && BottomLeft == 0;

/// <summary>
/// The radius of the top left corner of the rectangle.
/// </summary>
public float TopLeft { get; }

/// <summary>
/// The radius of the top right corner of the rectangle.
/// </summary>
public float TopRight { get; }

/// <summary>
/// The radius of the bottom left corner of the rectangle.
/// </summary>
public float BottomLeft { get; }

/// <summary>
/// The radius of the bottom right corner of the rectangle.
/// </summary>
public float BottomRight { get; }

internal readonly NUI.Vector4 ToReferenceType() => new NUI.Vector4(TopLeft, TopRight, BottomRight, BottomLeft);
}
}
106 changes: 92 additions & 14 deletions src/Tizen.NUI/src/devel/Lite/L.Shadow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/
using System.ComponentModel;
using Tizen.NUI.BaseComponents;

namespace Tizen.NUI.L
{
Expand All @@ -28,7 +29,13 @@ public struct Shadow
/// Create a Shadow.
/// </summary>
/// <param name="blurRadius">The blur radius value for the shadow. Bigger value, much blurry.</param>
public Shadow(float blurRadius) : this(blurRadius, L.Color.Black, Vector2.Zero, Vector2.Zero)
/// <param name="offsetX">Optional. The x offset value from the top left corner.</param>
/// <param name="offsetY">Optional. The y offset value from the top left corner.</param>
/// <param name="extraWidth">Optional. The shadow will extend its size by specified amount of length.</param>
/// <param name="extraHeight">Optional. The shadow will extend its size by specified amount of length.</param>
/// <param name="cutoutPolicy">The policy of the shadow cutout.</param>
public Shadow(float blurRadius, float offsetX = 0, float offsetY = 0, float extraWidth = 0, float extraHeight = 0, ColorVisualCutoutPolicyType cutoutPolicy = ColorVisualCutoutPolicyType.None)
: this(blurRadius, L.Color.Black, offsetX, offsetY, extraWidth, extraHeight, cutoutPolicy)
{
}

Expand All @@ -37,14 +44,20 @@ public Shadow(float blurRadius) : this(blurRadius, L.Color.Black, Vector2.Zero,
/// </summary>
/// <param name="blurRadius">The blur radius value for the shadow. Bigger value, much blurry.</param>
/// <param name="color">The color for the shadow.</param>
/// <param name="offset">Optional. The position offset value (x, y) from the top left corner.</param>
/// <param name="extents">Optional. The shadow will extend its size by specified amount of length.</param>
public Shadow(float blurRadius, L.Color color, Vector2 offset, Vector2 extents)
/// <param name="offsetX">Optional. The x offset value from the top left corner.</param>
/// <param name="offsetY">Optional. The y offset value from the top left corner.</param>
/// <param name="extraWidth">Optional. The shadow will extend its size by specified amount of length.</param>
/// <param name="extraHeight">Optional. The shadow will extend its size by specified amount of length.</param>
/// <param name="cutoutPolicy">The policy of the shadow cutout.</param>
public Shadow(float blurRadius, L.Color color, float offsetX = 0, float offsetY = 0, float extraWidth = 0, float extraHeight = 0, ColorVisualCutoutPolicyType cutoutPolicy = ColorVisualCutoutPolicyType.None)
{
BlurRadius = blurRadius;
Color = color;
Offset = offset;
Extents = extents;
OffsetX = offsetX;
OffsetY = offsetY;
ExtraWidth = extraWidth;
ExtraHeight = extraHeight;
CutoutPolicy = cutoutPolicy;
}

/// <summary>
Expand All @@ -56,7 +69,7 @@ public Shadow(float blurRadius, L.Color color, Vector2 offset, Vector2 extents)
public float BlurRadius
{
get;
set;
init;
}

/// <summary>
Expand All @@ -65,30 +78,95 @@ public float BlurRadius
public L.Color Color
{
get;
set;
init;
}

/// <summary>
/// The position offset value (x, y) from the top left corner.
/// </summary>
public Vector2 Offset
public float OffsetX
{
get;
set;
init;
}

/// <summary>
/// The position offset value (x, y) from the top left corner.
/// </summary>
public float OffsetY
{
get;
init;
}

/// <summary>
/// The shadow will extend its size by specified amount of length.<br />
/// If the value is negative then the shadow will shrink.
/// For example, when View's size is (100, 100) and the Shadow's extra size are 5 and -5 respectively,
/// the output shadow will have size (105, 95).
/// </summary>
public float ExtraWidth
{
get;
init;
}

/// <summary>
/// The shadow will extend its size by specified amount of length.<br />
/// If the value is negative then the shadow will shrink.
/// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
/// For example, when View's size is (100, 100) and the Shadow's extra size are 5 and -5 respectively,
/// the output shadow will have size (105, 95).
/// </summary>
public Vector2 Extents
public float ExtraHeight
{
get;
set;
init;
}

internal readonly NUI.Shadow ToShadow() => new NUI.Shadow(BlurRadius, Color.ToReferenceType(), Offset.ToReferenceType(), Extents.ToReferenceType());
/// <summary>
/// The Cutout policy for this shadow.
/// </summary>
/// <remarks>
/// ColorVisualCutoutPolicyType.None = Fully render the shadow color (Default)<br/>
/// ColorVisualCutoutPolicyType.CutoutView = Do not render inside bounding box of view<br/>
/// ColorVisualCutoutPolicyType.CutoutViewWithCornerRadius = Do not render inside view, consider corner radius value<br/>
/// </remarks>
public ColorVisualCutoutPolicyType CutoutPolicy
{
get;
init;
}

internal readonly NUI.Shadow ToShadow() => new NUI.Shadow(BlurRadius, Color.ToReferenceType(), new (OffsetX, OffsetY), new (ExtraWidth, ExtraHeight));

internal readonly PropertyMap BuildMap(View attachedView)
{
using var transform = new PropertyMap()
.Append((int)VisualTransformPropertyType.Offset, new L.Vector2(OffsetX, OffsetY))
.Append((int)VisualTransformPropertyType.OffsetPolicy, new L.Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute))
.Append((int)VisualTransformPropertyType.ExtraSize, new L.Vector2(ExtraWidth, ExtraHeight))
.Append((int)VisualTransformPropertyType.Origin, (int)Visual.AlignType.Center)
.Append((int)VisualTransformPropertyType.AnchorPoint, (int)Visual.AlignType.Center);

PropertyMap map = new PropertyMap()
.Append(Visual.Property.Type, (int)Visual.Type.Color)
.Append(ColorVisualProperty.MixColor, Color)
.Append(ColorVisualProperty.BlurRadius, BlurRadius < 0 ? 0 : BlurRadius)
.Append(ColorVisualProperty.CutoutPolicy, (int)CutoutPolicy)
.Append(Visual.Property.Transform, transform);

if (attachedView.CornerRadius != null || attachedView.CornerRadius != Vector4.Zero)
{
map.Append(Visual.Property.CornerRadius, attachedView.CornerRadius);
map.Append(Visual.Property.CornerRadiusPolicy, (int)attachedView.CornerRadiusPolicy);
}

if (attachedView.CornerSquareness != null || attachedView.CornerSquareness != Vector4.Zero)
{
map.Append(Visual.Property.CornerSquareness, attachedView.CornerSquareness);
}

return map;
}
}
}
2 changes: 2 additions & 0 deletions src/Tizen.NUI/src/devel/Lite/L.Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public float Y
get;
}

public readonly bool IsZero => X == 0 && Y == 0;

/// <summary>
/// Gets the width component of the vector2.
/// </summary>
Expand Down
Loading

0 comments on commit 747dfc1

Please sign in to comment.