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 13, 2025
1 parent d00a0aa commit a2ff953
Show file tree
Hide file tree
Showing 13 changed files with 1,087 additions and 34 deletions.
78 changes: 71 additions & 7 deletions src/Tizen.NUI/src/devel/Lite/L.Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,40 @@ namespace Tizen.NUI.L
[EditorBrowsable(EditorBrowsableState.Never)]
public struct Color
{
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);
public static readonly Color Red = new Color(1, 0, 0, 1);
public static readonly Color Green = new Color(0, 1, 0, 1);
public static readonly Color Blue = new Color(0, 0, 1, 1);
/// <summary>
/// The default color. (This is to distinguish from zero corners)
/// </summary>
public static readonly Color Default = new (-1, -1, -1, -1);

/// <summary>
/// The transparent color.
/// </summary>
public static readonly Color Transparent = new (0, 0, 0, 0);

/// <summary>
/// The transparent color.
/// </summary>
public static readonly Color Black = new (0, 0, 0, 1);

/// <summary>
/// The white color.
/// </summary>
public static readonly Color White = new (1, 1, 1, 1);

/// <summary>
/// The red color.
/// </summary>
public static readonly Color Red = new (1, 0, 0, 1);

/// <summary>
/// The green color.
/// </summary>
public static readonly Color Green = new (0, 1, 0, 1);

/// <summary>
/// The blue color.
/// </summary>
public static readonly Color Blue = new (0, 0, 1, 1);

/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
Expand All @@ -46,6 +74,39 @@ 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 value of 0xRRGGBB format.</param>
/// <param name="alpha">The alpha value between 0.0 and 1.0.</param>
/// <example>
/// <code>
/// new L.Color(0xFF0000, 1f); // Solid red
/// new L.Color(0x00FF00, 0.5f) // Half transparent green
/// </code>
/// </example>
public Color(uint value, float alpha)
{
R = ((value >> 16) & 0xff) / 255.0f;
G = ((value >> 8) & 0xff) / 255.0f;
B = (value & 0xff) / 255.0f;
A = alpha;
}

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

/// <summary>
/// Gets a value indicating whether this is default.
/// </summary>
public readonly bool IsDefault => R == -1 && G == -1 && B == -1 && A == -1;

/// <summary>
/// Gets a value indicating whether this is zero.
/// </summary>
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,12 +146,15 @@ 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>
/// <param name="alpha">The new alpha value.</param>
/// <returns>A new color object with the specified alpha value.</returns>
public readonly Color WithAlpha(float alpha) => new Color(R, G, B, alpha);
public readonly Color WithAlpha(float alpha) => new (R, G, B, alpha);

internal readonly NUI.Color ToReferenceType() => new NUI.Color(R, G, B, A);
}
Expand Down
92 changes: 92 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,92 @@
/*
* 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;

namespace Tizen.NUI.L
{
/// <summary>
/// Defines a value type of corner radius.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public struct Corner
{
/// <summary>
/// The default corner. (This is to distinguish from zero corners)
/// </summary>
public static readonly Corner Default = new (-1, -1, -1, -1);

/// <summary>
/// The zero corner.
/// </summary>
public static readonly Corner Zero = new (0.0f, 0.0f, 0.0f, 0.0f);

/// <summary>
/// Initializes a new instance of the <see cref="Corner"/> struct.
/// </summary>
/// <param name="uniform">The uniform corner value.</param>
public Corner(float uniform) : this(uniform, uniform, uniform, uniform)
{
}

/// <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;
}

/// <summary>
/// Gets a value indicating whether this is default.
/// </summary>
public readonly bool IsDefault => TopLeft == -1 && TopRight == -1 && BottomRight == -1 && BottomLeft == -1;

/// <summary>
/// Gets a value indicating whether this is zero.
/// </summary>
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 right corner of the rectangle.
/// </summary>
public float BottomRight { get; }

/// <summary>
/// The radius of the bottom left corner of the rectangle.
/// </summary>
public float BottomLeft { 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. The default is 0.</param>
/// <param name="offsetY">Optional. The y offset value from the top left corner. The default is 0.</param>
/// <param name="extraWidth">Optional. The shadow will extend its size by specified amount of length. The default is 0.</param>
/// <param name="extraHeight">Optional. The shadow will extend its size by specified amount of length. The default is 0.</param>
/// <param name="cutoutPolicy">The policy of the shadow cutout. The default is <see cref="ColorVisualCutoutPolicyType.None"/>.</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. The default is 0.</param>
/// <param name="offsetY">Optional. The y offset value from the top left corner. The default is 0.</param>
/// <param name="extraWidth">Optional. The shadow will extend its size by specified amount of length. The default is 0.</param>
/// <param name="extraHeight">Optional. The shadow will extend its size by specified amount of length. The default is 0.</param>
/// <param name="cutoutPolicy">The policy of the shadow cutout. The default is <see cref="ColorVisualCutoutPolicyType.None"/>.</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;
}
}
}
7 changes: 6 additions & 1 deletion src/Tizen.NUI/src/devel/Lite/L.Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ namespace Tizen.NUI.L
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly struct Vector2
{
public static readonly Vector2 Zero = new Vector2(0.0f, 0.0f);
/// <summary>
/// The zero vector2.
/// </summary>
public static readonly Vector2 Zero = new (0.0f, 0.0f);

/// <summary>
/// Initializes a new instance of the <see cref="Vector2"/> struct.
Expand Down Expand Up @@ -53,6 +56,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 a2ff953

Please sign in to comment.