/
VisualView.cs
executable file
·468 lines (422 loc) · 19.7 KB
/
VisualView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (c) 2019 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.Text;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
namespace Tizen.NUI.BaseComponents
{
/// <summary>
/// A visual view control if a user adds any visual to it.
/// </summary>
/// <example>
/// Example:
/// <code>
/// VisualView _visualView = new VisualView();
/// ImageVisualMap imageVisualMap1 = new ImageVisualMap();
/// imageVisualMap1.URL = "./NUISample/res/images/image-1.jpg";
/// imageVisualMap1.VisualSize = new Vector2( 300.0f, 300.0f );
/// imageVisualMap1.Offset = new Vector2( 50.0f, 50.0f );
/// imageVisualMap1.OffsetSizeMode = new Vector4( 1.0f, 1.0f, 1.0f, 1.0f );
/// imageVisualMap1.Origin = AlignType.TOP_BEGIN;
/// imageVisualMap1.AnchorPoint = AlignType.TOP_BEGIN;
/// _visualView.AddVisual("imageVisual1", imageVisualMap1);
/// </code>
/// </example>
/// <since_tizen> 3 </since_tizen>
public class VisualView : CustomView
{
private Dictionary<int, string> visualNameDictionary = null;
private Dictionary<int, VisualBase> visualDictionary = null;
private Dictionary<int, PropertyMap> tranformDictionary = null;
private PropertyArray animateArray = null;
/// <summary>
/// Constructor.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public VisualView() : this(CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport)
{
}
/// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public VisualView(ViewStyle viewStyle) : this(CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport, viewStyle)
{
}
/// <summary>Create a VisualView with specified behaviour.</summary>
/// <param name="behaviour">CustomView behaviour</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public VisualView(CustomViewBehaviour behaviour) : base(typeof(VisualView).FullName, behaviour)
{
}
/// <summary>Create a VisualView with specified behaviour and a style.</summary>
/// <param name="behaviour">CustomView behaviour</param>
/// <param name="viewStyle">The ViewStyle.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public VisualView(CustomViewBehaviour behaviour, ViewStyle viewStyle) : base(typeof(VisualView).FullName, behaviour, viewStyle)
{
}
// static constructor registers the control type (for user can add kinds of visuals to it)
static VisualView()
{
// ViewRegistry registers control type with DALi type registry
// also uses introspection to find any properties that need to be registered with type registry
CustomViewRegistry.Instance.Register(CreateInstance, typeof(VisualView));
}
/// <summary>
/// Gets the total number of visuals which are added by users.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public int NumberOfVisuals
{
get
{
return visualDictionary.Count;
}
}
/// <summary>
/// Overrides the parent method.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public override void OnInitialize()
{
base.OnInitialize();
//Initialize empty
visualNameDictionary = new Dictionary<int, string>();
visualDictionary = new Dictionary<int, VisualBase>();
tranformDictionary = new Dictionary<int, PropertyMap>();
animateArray = new PropertyArray();
}
/// <summary>
/// Adds or updates a visual to visual view.
/// </summary>
/// <param name="visualName">The name of a visual to add. If a name is added to an existing visual name, the visual will be replaced.</param>
/// <param name="visualMap">The property map of a visual to create.</param>
/// <exception cref="ArgumentNullException"> Thrown when visualMap is null. </exception>
/// <since_tizen> 3 </since_tizen>
public void AddVisual(string visualName, VisualMap visualMap)
{
int visualIndex = -1;
/* If the visual had added, then replace it using RegisterVusal. */
//visual.Name = name;
foreach (var item in visualNameDictionary)
{
if (item.Value == visualName)
{
/* Find a existed visual, its key also exited. */
visualIndex = item.Key;
UnregisterVisual(visualIndex);
visualNameDictionary.Remove(visualIndex);
visualDictionary.Remove(visualIndex);
tranformDictionary.Remove(visualIndex);
break;
}
}
if (visualIndex == -1) // The visual is a new one, create index for it. */
{
using (var temp = new PropertyValue(visualName))
{
visualIndex = RegisterProperty(visualName, temp, PropertyAccessMode.ReadWrite);
}
}
if (visualIndex > 0)
{
if (visualMap == null)
{
throw new ArgumentNullException(nameof(visualMap));
}
visualMap.VisualIndex = visualIndex;
visualMap.Name = visualName;
visualMap.Parent = this;
// Register index and name
UpdateVisual(visualIndex, visualName, visualMap);
}
}
/// <summary>
/// Removes a visual by name.
/// </summary>
/// <param name="visualName">The name of a visual to remove.</param>
/// <since_tizen> 3 </since_tizen>
public void RemoveVisual(string visualName)
{
foreach (var item in visualNameDictionary)
{
if (item.Value == visualName)
{
EnableVisual(item.Key, false);
UnregisterVisual(item.Key);
tranformDictionary.Remove(item.Key);
visualDictionary.Remove(item.Key);
visualNameDictionary.Remove(item.Key);
RelayoutRequest();
break;
}
}
}
/// <summary>
/// Removes all visuals of the visual view.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public void RemoveAll()
{
foreach (var item in visualNameDictionary)
{
EnableVisual(item.Key, false);
UnregisterVisual(item.Key);
}
visualDictionary.Clear();
tranformDictionary.Clear();
visualNameDictionary.Clear();
RelayoutRequest();
}
/// <summary>
/// Overrides the method of OnRelayout() for CustomView class.<br />
/// Called after the size negotiation has been finished for this control.<br />
/// The control is expected to assign this given size to itself or its children.<br />
/// Should be overridden by derived classes if they need to layout actors differently after certain operations like add or remove actors, resize, or after changing specific properties.<br />
/// </summary>
/// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored).</remarks>
/// <param name="size">The allocated size.</param>
/// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
/// <since_tizen> 3 </since_tizen>
public override void OnRelayout(Vector2 size, RelayoutContainer container)
{
foreach (var item in visualDictionary)
{
if(item.Value != null)
{
item.Value.SetTransformAndSize(tranformDictionary[item.Key], size);
EnableVisual(item.Key, true);
}
}
}
/// <summary>
/// Creates a visual animation (transition) with the input parameters.
/// </summary>
/// <param name="target">The visual map to animation.</param>
/// <param name="property">The property of visual to animation.</param>
/// <param name="destinationValue">The destination value of property after animation.</param>
/// <param name="startTime">The start time of visual animation.</param>
/// <param name="endTime">The end time of visual animation.</param>
/// <param name="alphaFunction">The alpha function of visual animation.</param>
/// <param name="initialValue">The initial property value of visual animation.</param>
/// <returns>Animation instance</returns>
/// <exception cref="ArgumentNullException"> Thrown when target is null. </exception>
/// <since_tizen> 3 </since_tizen>
public Animation AnimateVisual(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
string strAlpha = alphaFunction?.GetDescription();
foreach (var item in visualNameDictionary.ToList())
{
if (item.Value == target.Name)
{
using (PropertyMap animator = new PropertyMap())
using (PropertyMap timePeriod = new PropertyMap())
using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
using (PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue))
using (PropertyMap transition = new PropertyMap())
using (PropertyValue pvTarget = new PropertyValue(target.Name))
{
if (strAlpha != null)
{
using (PropertyValue pvAlpha = new PropertyValue(strAlpha))
{
animator.Add("alphaFunction", pvAlpha);
}
}
timePeriod.Add("duration", pvDuration);
timePeriod.Add("delay", pvDelay);
using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
{
animator.Add("timePeriod", pvTimePeriod);
}
StringBuilder sb = new StringBuilder(property);
sb[0] = (char)(sb[0] | 0x20);
string _str = sb.ToString();
if (_str == "position") { _str = "offset"; }
transition.Add("target", pvTarget);
using (PropertyValue pvStr = new PropertyValue(_str))
{
transition.Add("property", pvStr);
}
if (initialValue != null)
{
using (PropertyValue initVal = PropertyValue.CreateFromObject(initialValue))
using (PropertyValue pvInitialValue = new PropertyValue(initVal))
{
transition.Add("initialValue", pvInitialValue);
}
}
transition.Add("targetValue", destVal);
using (PropertyValue pvAnimator = new PropertyValue(animator))
{
transition.Add("animator", pvAnimator);
}
using (TransitionData transitionData = new TransitionData(transition))
{
return this.CreateTransition(transitionData);
}
}
}
}
return null;
}
/// <summary>
/// Adds a group visual animation (transition) map with the input parameters.
/// </summary>
/// <param name="target">The visual map to animation.</param>
/// <param name="property">The property of visual to animation.</param>
/// <param name="destinationValue">The destination value of property after animation.</param>
/// <param name="startTime">The start time of visual animation.</param>
/// <param name="endTime">The end time of visual animation.</param>
/// <param name="alphaFunction">The alpha function of visual animation.</param>
/// <param name="initialValue">The initial property value of visual animation.</param>
/// <exception cref="ArgumentNullException"> Thrown when target is null. </exception>
/// <since_tizen> 3 </since_tizen>
public void AnimateVisualAdd(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
string strAlpha = alphaFunction?.GetDescription();
foreach (var item in visualNameDictionary.ToList())
{
if (item.Value == target.Name)
{
using (PropertyMap animator = new PropertyMap())
using (PropertyMap timePeriod = new PropertyMap())
using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
using (PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue))
using (PropertyMap transition = new PropertyMap())
using (PropertyValue pvTarget = new PropertyValue(target.Name))
{
if (strAlpha != null)
{
using (PropertyValue pvStrAlpha = new PropertyValue(strAlpha))
{
animator.Add("alphaFunction", pvStrAlpha);
}
}
timePeriod.Add("duration", pvDuration);
timePeriod.Add("delay", pvDelay);
using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
{
animator.Add("timePeriod", pvTimePeriod);
}
StringBuilder sb = new StringBuilder(property);
sb[0] = (char)(sb[0] | 0x20);
string _str = sb.ToString();
if (_str == "position") { _str = "offset"; }
transition.Add("target", pvTarget);
using (PropertyValue pvStr = new PropertyValue(_str))
{
transition.Add("property", pvStr);
}
if (initialValue != null)
{
using (PropertyValue initVal = PropertyValue.CreateFromObject(initialValue))
using (PropertyValue pvInitialValue = new PropertyValue(initVal))
{
transition.Add("initialValue", pvInitialValue);
}
}
transition.Add("targetValue", destVal);
using (PropertyValue pvAnimator = new PropertyValue(animator))
{
transition.Add("animator", pvAnimator);
}
using (PropertyValue pvTransition = new PropertyValue(transition))
{
PropertyArray temp = animateArray.Add(pvTransition);
temp.Dispose();
}
}
}
}
}
/// <summary>
/// Finishes to add a visual animation (transition) map and creates a transition animation.
/// </summary>
/// <returns>Animation instance.</returns>
/// <since_tizen> 3 </since_tizen>
public Animation AnimateVisualAddFinish()
{
if (animateArray == null || animateArray.Empty())
{
Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
return null;
}
TransitionData transitionData = new TransitionData(animateArray);
Animation ret = this.CreateTransition(transitionData);
transitionData.Dispose();
return ret;
}
/// <summary>
/// temporary fix to pass TCT.
/// </summary>
/// <exception cref="ArgumentNullException"> Thrown when visualMap is null. </exception>
/// <since_tizen> 3 </since_tizen>
public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
{
if (visualMap == null)
{
throw new ArgumentNullException(nameof(visualMap));
}
foreach (var item in visualNameDictionary.ToList())
{
if (item.Value == visualMap.Target)
{
using (TransitionData transitionData = new TransitionData(visualMap.OutputVisualMap))
{
return this.CreateTransition(transitionData);
}
}
}
return null;
}
//temporary fix to pass TCT
internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
{
VisualBase visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
visualNameDictionary[visualIndex] = visualName;
visualDictionary[visualIndex] = visual;
tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
if(visual != null)
{
visual.Name = visualName;
visual.DepthIndex = visualMap.DepthIndex;
RegisterVisual(visualIndex, visual);
RelayoutRequest();
NUILog.Debug("UpdateVisual() name=" + visualName);
}
else
{
NUILog.Debug("UpdateVisual() FAIL! visual create failed name=" + visualName);
}
}
static CustomView CreateInstance()
{
return new VisualView();
}
}
}