Release 12.0.0.18314
[platform/core/csapi/tizenfx.git] / src / Tizen.AIAvatar / src / Internal / AvatarBlendShapeIndex.cs
1 /*
2  * Copyright(c) 2024 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System.ComponentModel;
19 using Tizen.NUI;
20 using Tizen.NUI.Scene3D;
21
22 namespace Tizen.AIAvatar
23 {
24     /// <summary>
25     /// Specialized <see cref="BlendShapeIndex"/> to control avatar blend shape.
26     /// </summary>
27     /// <example>
28     /// <code>
29     /// AvatarBlendShapeIndex leftEyeBlink = new AvatarBlendShapeIndex(avatar.BlendShapeMapper, BlendShapeType.EyeBlinkLeft);
30     ///
31     /// // We can change the property later.
32     /// AVatarBlendShapeIndex rightEyeBlink = new AvatarJointTransformIndex(avatar.BlendShapeMapper);
33     /// rightEyeBlink.AvatarBlendShapeType = (uint)BlendShapeType.EyeBlinkRight;
34     /// </code>
35     /// </example>
36     [EditorBrowsable(EditorBrowsableState.Never)]
37     internal class AvatarBlendShapeIndex : BlendShapeIndex
38     {
39         internal AvatarPropertyMapper nameMapper = null;
40         internal uint blendShapeType;
41
42         internal AvatarBlendShapeIndex(AvatarPropertyMapper mapper) : base()
43         {
44             nameMapper = mapper;
45         }
46
47         internal AvatarBlendShapeIndex(AvatarPropertyMapper mapper, PropertyKey blendShapeId) : base(new PropertyKey(GetPropertyNameFromMapper(mapper, blendShapeId)))
48         {
49             nameMapper = mapper;
50         }
51
52         internal AvatarBlendShapeIndex(AvatarPropertyMapper nodeMapper, NodeType nodeType, PropertyKey blendShapeId) 
53             : base(new PropertyKey(GetPropertyNameFromMapper(nodeMapper, (uint)nodeType)), blendShapeId)
54         {
55         }
56
57         internal AvatarBlendShapeIndex(AvatarPropertyMapper blendShapeMapper, BlendShapeType blendShapeType) : this(blendShapeMapper, (uint)blendShapeType)
58         {
59         }
60
61         internal AvatarBlendShapeIndex(AvatarPropertyMapper mapper, uint blendShapeType) : base(new PropertyKey(GetPropertyNameFromMapper(mapper, blendShapeType)))
62         {
63             nameMapper = mapper;
64             this.blendShapeType = blendShapeType;
65         }
66
67         internal AvatarBlendShapeIndex(AvatarPropertyMapper nodeMapper, NodeType nodeType, AvatarPropertyMapper blendShapeMapper, BlendShapeType blendShapeType) 
68             : base(new PropertyKey(GetPropertyNameFromMapper(nodeMapper, (uint)nodeType)), new PropertyKey(GetPropertyNameFromMapper(blendShapeMapper, (uint)blendShapeType)))
69         {
70             nameMapper = blendShapeMapper;
71             this.blendShapeType = (uint)blendShapeType;
72         }
73
74         /// <summary>
75         /// Get the name of given index.
76         /// </summary>
77         /// <param name="mapper">Name mapper for given index</param>
78         /// <param name="id">Target ID what we want to get name</param>
79         /// <returns>Name, or null if invalid</returns>
80         private static string GetPropertyNameFromMapper(AvatarPropertyMapper mapper, PropertyKey id)
81         {
82             if (id == null)
83             {
84                 return "";
85             }
86             if (id.Type == PropertyKey.KeyType.String)
87             {
88                 return id.StringKey;
89             }
90
91             var str = mapper?.GetPropertyName((uint)id.IndexKey) ?? "";
92             return str;
93         }
94
95         /// <summary>
96         /// Get the name of given BlendShape.
97         /// </summary>
98         /// <param name="mapper">Name mapper for given index</param>
99         /// <param name="type">Type of joint what we want to get name</param>
100         /// <returns>Name, or null if invalid</returns>
101         private static string GetPropertyNameFromMapper(AvatarPropertyMapper mapper, uint type)
102         {
103             var str = mapper?.GetPropertyName(type) ?? "";
104             return str;
105         }
106
107         /// <summary>
108         /// TODO : Explain me
109         /// </summary>
110         [EditorBrowsable(EditorBrowsableState.Never)]
111         public AvatarPropertyMapper NameMapper
112         {
113             get
114             {
115                 return nameMapper;
116             }
117             set
118             {
119                 nameMapper = value;
120
121                 using PropertyKey blendShapeId = new(GetPropertyNameFromMapper(nameMapper, blendShapeType));
122                 base.BlendShapeId = blendShapeId;
123             }
124         }
125
126         /// <summary>
127         /// TODO : Explain me
128         /// </summary>
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         public uint AvatarBlendShapeType
131         {
132             get
133             {
134                 return blendShapeType;
135             }
136             set
137             {
138                 blendShapeType = value;
139
140                 using PropertyKey blendShapeId = new(GetPropertyNameFromMapper(nameMapper, blendShapeType));
141                 base.BlendShapeId = blendShapeId;
142             }
143         }
144
145         /// <summary>
146         /// Hijack property to control Avatar specified logic.
147         /// </summary>
148         [EditorBrowsable(EditorBrowsableState.Never)]
149         public new PropertyKey BlendShapeId
150         {
151             get
152             {
153                 return base.BlendShapeId;
154             }
155             set
156             {
157                 if (value != null)
158                 {
159                     if (value.Type == PropertyKey.KeyType.Index)
160                     {
161                         blendShapeType = (uint)value.IndexKey;
162                     }
163                     using PropertyKey blendShapeId = new(GetPropertyNameFromMapper(nameMapper, value));
164                     base.BlendShapeId = blendShapeId;
165                 }
166                 else
167                 {
168                     base.BlendShapeId = value;
169                 }
170             }
171         }
172     }
173 }