Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CubeTransitionEffectSample.cs
1 
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
4
5 namespace Tizen.NUI.Samples
6 {
7     public class CubeTransitionEffectSample : IExample
8     {
9         static string DEMO_IMAGE_DIR = CommonResource.GetDaliResourcePath() + "CubeTransitionEffect/";
10
11         string TOOLBAR_IMAGE = DEMO_IMAGE_DIR + "top-bar.png";
12         string APPLICATION_TITLE_WAVE = "Cube Transition: Wave";
13         string APPLICATION_TITLE_CROSS = "Cube Transition: Cross";
14         string APPLICATION_TITLE_FOLD = "Cube Transition: Fold";
15         string EFFECT_WAVE_IMAGE = DEMO_IMAGE_DIR + "icon-effect-wave.png";
16         string EFFECT_WAVE_IMAGE_SELECTED = DEMO_IMAGE_DIR + "icon-effect-wave-selected.png";
17         string EFFECT_CROSS_IMAGE = DEMO_IMAGE_DIR + "icon-effect-cross.png";
18         string EFFECT_CROSS_IMAGE_SELECTED = DEMO_IMAGE_DIR + "icon-effect-cross-selected.png";
19         string EFFECT_FOLD_IMAGE = DEMO_IMAGE_DIR + "icon-effect-fold.png";
20         string EFFECT_FOLD_IMAGE_SELECTED = DEMO_IMAGE_DIR + "icon-effect-fold-selected.png";
21         string SLIDE_SHOW_START_ICON = DEMO_IMAGE_DIR + "icon-play.png";
22         string SLIDE_SHOW_START_ICON_SELECTED = DEMO_IMAGE_DIR + "icon-play-selected.png";
23         string SLIDE_SHOW_STOP_ICON = DEMO_IMAGE_DIR + "icon-stop.png";
24         string SLIDE_SHOW_STOP_ICON_SELECTED = DEMO_IMAGE_DIR + "icon-stop-selected.png";
25
26         string[] IMAGES = new string[] {
27           DEMO_IMAGE_DIR + "gallery-large-1.jpg",
28           DEMO_IMAGE_DIR + "gallery-large-2.jpg",
29           DEMO_IMAGE_DIR + "gallery-large-3.jpg",
30           DEMO_IMAGE_DIR + "gallery-large-4.jpg",
31           DEMO_IMAGE_DIR + "gallery-large-5.jpg",
32           DEMO_IMAGE_DIR + "gallery-large-6.jpg",
33           DEMO_IMAGE_DIR + "gallery-large-7.jpg",
34           DEMO_IMAGE_DIR + "gallery-large-8.jpg",
35           DEMO_IMAGE_DIR + "gallery-large-9.jpg",
36           DEMO_IMAGE_DIR + "gallery-large-10.jpg",
37           DEMO_IMAGE_DIR + "gallery-large-11.jpg",
38           DEMO_IMAGE_DIR + "gallery-large-12.jpg",
39           DEMO_IMAGE_DIR + "gallery-large-13.jpg",
40           DEMO_IMAGE_DIR + "gallery-large-14.jpg",
41           DEMO_IMAGE_DIR + "gallery-large-15.jpg",
42           DEMO_IMAGE_DIR + "gallery-large-16.jpg",
43           DEMO_IMAGE_DIR + "gallery-large-17.jpg",
44           DEMO_IMAGE_DIR + "gallery-large-18.jpg",
45           DEMO_IMAGE_DIR + "gallery-large-19.jpg",
46           DEMO_IMAGE_DIR + "gallery-large-20.jpg",
47           DEMO_IMAGE_DIR + "gallery-large-21.jpg",
48         };
49
50         const int NUM_IMAGES = 21;
51
52         // the number of cubes: NUM_COLUMNS*NUM_ROWS
53         // better choose the numbers that can divide viewAreaSize.x
54         const int NUM_COLUMNS_WAVE = 16;
55         const int NUM_COLUMNS_CROSS = 8;
56         const int NUM_COLUMNS_FOLD = 8;
57         // better choose the numbers that can divide viewAreaSize.y
58         const int NUM_ROWS_WAVE = 20;
59         const int NUM_ROWS_CROSS = 10;
60         const int NUM_ROWS_FOLD = 10;
61         //transition effect duration
62         const float ANIMATION_DURATION_WAVE = 1.5f;
63         const float ANIMATION_DURATION_CROSS = 1.0f;
64         const float ANIMATION_DURATION_FOLD = 1.0f;
65         //transition effect displacement
66         const float CUBE_DISPLACEMENT_WAVE = 70.0f;
67         const float CUBE_DISPLACEMENT_CROSS = 30.0f;
68
69         // The duration of the current image staying on screen when slideshow is on
70         const int VIEWINGTIME = 2000; // 2 seconds
71
72         private View tool_bar;
73         private TextLabel mTitle;
74         private Layer content_layer;
75         private View mContent;
76         private View radiosParent;
77         private RadioButtonGroup toggle;
78         private RadioButton[] radios = new RadioButton[3];
79         private Button mSlideshowButton;
80
81         private Texture mCurrentTexture;
82         private Texture mNextTexture;
83         private uint mIndex = 0;
84         private bool mIsImageLoading = false;
85
86         private PanGestureDetector mPanGestureDetector;
87
88         private CubeTransitionEffect mCubeWaveEffect;
89         private CubeTransitionEffect mCubeCrossEffect;
90         private CubeTransitionEffect mCubeFoldEffect;
91         private CubeTransitionEffect mCurrentEffect;
92
93         private Timer mViewTimer;
94         private bool mSlideshow = false;
95
96         private Vector2 mPanPosition;
97         private Vector2 mPanDisplacement;
98
99         public void Activate()
100         {
101             // tool bar
102             tool_bar = new View();
103             tool_bar.BackgroundColor = Color.White;
104             tool_bar.Size2D = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, 100);
105             tool_bar.PositionUsesPivotPoint = true;
106             tool_bar.ParentOrigin = ParentOrigin.TopLeft;
107             tool_bar.PivotPoint = PivotPoint.TopLeft;
108
109             NUIApplication.GetDefaultWindow().GetDefaultLayer().Add(tool_bar);
110             NUIApplication.GetDefaultWindow().GetDefaultLayer().RaiseToTop();
111
112             // title of tool bar
113             mTitle = new TextLabel();
114             mTitle.Text = APPLICATION_TITLE_WAVE;
115             mTitle.FontFamily = "SamsungOne 400";
116             mTitle.PointSize = 20;
117             mTitle.Position2D = new Position2D(400, 42);
118             mTitle.ParentOrigin = ParentOrigin.TopLeft;
119             mTitle.PositionUsesPivotPoint = true;
120             mTitle.PivotPoint = PivotPoint.TopLeft;
121             tool_bar.Add(mTitle);
122
123             // push button of tool bar
124             var style = new ButtonStyle();
125             style.Icon.ResourceUrl = new Selector<string>() { Normal = SLIDE_SHOW_START_ICON, Pressed = SLIDE_SHOW_START_ICON_SELECTED };
126             style.Position = new Position(800, 32);
127             style.ParentOrigin = ParentOrigin.TopLeft;
128             style.PivotPoint = PivotPoint.TopLeft;
129             style.Size = new Size(58, 58);
130             mSlideshowButton = new Button(style);
131             mSlideshowButton.Clicked += OnPushButtonClicked;
132
133             mSlideshowButton.RaiseToTop();
134
135             tool_bar.Add(mSlideshowButton);
136
137             // toggle button of tool bar
138             radiosParent = new View();
139             radiosParent.Size = new Size(200, 40);
140             radiosParent.Position = new Position(900, 42);
141             var layout = new LinearLayout();
142             layout.LinearOrientation = LinearLayout.Orientation.Horizontal;
143             layout.CellPadding = new Size(30, 30);
144             radiosParent.Layout = layout;
145             tool_bar.Add(radiosParent);
146
147             toggle = new RadioButtonGroup();
148             for (int i = 0; i < 3; i++)
149             {
150                 radios[i] = new RadioButton();
151                 radios[i].Size = new Size(37, 34);
152                 toggle.Add(radios[i]);
153                 radiosParent.Add(radios[i]);
154             }
155             var radioStyle = radios[0].Style;
156             radioStyle.Icon.BackgroundImage = new Selector<string>() { Normal = EFFECT_WAVE_IMAGE, Selected = EFFECT_WAVE_IMAGE_SELECTED };
157             radios[0].ApplyStyle(radioStyle);
158             radioStyle = radios[1].Style;
159             radioStyle.Icon.BackgroundImage = new Selector<string>() { Normal = EFFECT_CROSS_IMAGE, Selected = EFFECT_CROSS_IMAGE_SELECTED };
160             radios[1].ApplyStyle(radioStyle);
161             radioStyle = radios[2].Style;
162             radioStyle.Icon.BackgroundImage = new Selector<string>() { Normal = EFFECT_FOLD_IMAGE, Selected = EFFECT_FOLD_IMAGE_SELECTED };
163             radios[2].ApplyStyle(radioStyle);
164             radios[0].SelectedChanged += OnWaveClicked;
165             radios[1].SelectedChanged += OnCrossClicked;
166             radios[2].SelectedChanged += OnFoldClicked;
167             radios[0].IsSelected = true;
168             // load image
169             mCurrentTexture = LoadStageFillingTexture(IMAGES[mIndex]);
170
171             // content layer is 3D.
172             content_layer = new Layer();
173             content_layer.Behavior = Layer.LayerBehavior.Layer3D;
174             NUIApplication.GetDefaultWindow().AddLayer(content_layer);
175
176             //use small cubes
177             mCubeWaveEffect = new CubeTransitionWaveEffect(NUM_ROWS_WAVE, NUM_COLUMNS_WAVE);
178             mCubeWaveEffect.SetTransitionDuration(ANIMATION_DURATION_WAVE);
179             mCubeWaveEffect.SetCubeDisplacement(CUBE_DISPLACEMENT_WAVE);
180             mCubeWaveEffect.TransitionCompleted += OnCubeEffectCompleted;
181
182             mCubeWaveEffect.Position2D = new Position2D(0, tool_bar.Size2D.Height);
183             mCubeWaveEffect.Size2D = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Height - tool_bar.Size2D.Height);
184             mCubeWaveEffect.PivotPoint = PivotPoint.TopLeft;
185             mCubeWaveEffect.ParentOrigin = ParentOrigin.TopLeft;
186             mCubeWaveEffect.SetCurrentTexture(mCurrentTexture);
187
188             // use big cubes
189             mCubeCrossEffect = new CubeTransitionCrossEffect(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS);
190             mCubeCrossEffect.SetTransitionDuration(ANIMATION_DURATION_CROSS);
191             mCubeCrossEffect.SetCubeDisplacement(CUBE_DISPLACEMENT_CROSS);
192             mCubeCrossEffect.TransitionCompleted += OnCubeEffectCompleted;
193
194             mCubeCrossEffect.Position2D = new Position2D(0, tool_bar.Size2D.Height);
195             mCubeCrossEffect.Size2D = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Height - tool_bar.Size2D.Height);
196             mCubeCrossEffect.PivotPoint = PivotPoint.TopLeft;
197             mCubeCrossEffect.ParentOrigin = ParentOrigin.TopLeft;
198             mCubeCrossEffect.SetCurrentTexture(mCurrentTexture);
199
200             mCubeFoldEffect = new CubeTransitionFoldEffect(NUM_ROWS_FOLD, NUM_COLUMNS_FOLD);
201             mCubeFoldEffect.SetTransitionDuration(ANIMATION_DURATION_FOLD);
202             mCubeFoldEffect.TransitionCompleted += OnCubeEffectCompleted;
203
204             mCubeFoldEffect.Position2D = new Position2D(0, tool_bar.Size2D.Height);
205             mCubeFoldEffect.Size2D = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Height - tool_bar.Size2D.Height);
206             mCubeFoldEffect.PivotPoint = PivotPoint.TopLeft;
207             mCubeFoldEffect.ParentOrigin = ParentOrigin.TopLeft;
208             mCubeFoldEffect.SetCurrentTexture(mCurrentTexture);
209
210             mViewTimer = new Timer(VIEWINGTIME);
211             mViewTimer.Tick += OnTimerTick;
212
213             // content
214             mCurrentEffect = mCubeWaveEffect;
215
216             mContent = new View();
217             mContent.Size2D = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Height - tool_bar.Size2D.Height);
218             mContent.ParentOrigin = ParentOrigin.TopLeft;
219             mContent.PositionUsesPivotPoint = true;
220             mContent.PivotPoint = PivotPoint.TopLeft;
221
222             mContent.Add(mCurrentEffect);
223
224             content_layer.Add(mContent);
225
226             mPanGestureDetector = new PanGestureDetector();
227             mPanGestureDetector.Detected += OnPanGesture;
228             mPanGestureDetector.Attach(mContent);
229         }
230
231         public void Deactivate()
232         {
233             if (mTitle)
234             {
235                 tool_bar.Remove(mTitle);
236                 mTitle.Dispose();
237                 mTitle = null;
238             }
239
240             if (mSlideshowButton)
241             {
242                 tool_bar.Remove(mSlideshowButton);
243                 mSlideshowButton.Clicked -= OnPushButtonClicked;
244                 mSlideshowButton.Dispose();
245                 mSlideshowButton = null;
246             }
247
248             if (radiosParent)
249             {
250                 for (int i = 0; i < 3; i++)
251                 {
252                     if (radios[i])
253                     {
254                         if ( 0 == i) radios[0].SelectedChanged -= OnWaveClicked;
255                         if ( 1 == i) radios[1].SelectedChanged -= OnCrossClicked;
256                         if ( 2 == i) radios[2].SelectedChanged -= OnFoldClicked;
257                         radiosParent.Remove(radios[i]);
258                         radios[i].Dispose();
259                         radios[i] = null;
260                     }
261                 }
262                 tool_bar.Remove(radiosParent);
263                 radiosParent.Dispose();
264                 radiosParent = null;
265             }
266
267             if (tool_bar)
268             {
269                 NUIApplication.GetDefaultWindow().GetDefaultLayer().Remove(tool_bar);
270                 tool_bar.Dispose();
271                 tool_bar = null;
272             }
273
274             if (mCubeWaveEffect)
275             {
276                 mCubeWaveEffect.TransitionCompleted -= OnCubeEffectCompleted;
277                 mCubeWaveEffect.Dispose();
278                 mCubeWaveEffect = null;
279             }
280
281             if (mCubeCrossEffect)
282             {
283                 mCubeCrossEffect.TransitionCompleted -= OnCubeEffectCompleted;
284                 mCubeCrossEffect.Dispose();
285                 mCubeCrossEffect = null;
286             }
287
288             if (mCubeFoldEffect)
289             {
290                 mCubeFoldEffect.TransitionCompleted -= OnCubeEffectCompleted;
291                 mCubeFoldEffect.Dispose();
292                 mCubeFoldEffect = null;
293             }
294
295             if (mPanGestureDetector)
296             {
297                 mPanGestureDetector.Detected -= OnPanGesture;
298                 mPanGestureDetector.Dispose();
299                 mPanGestureDetector = null;
300             }
301
302             if (null != mViewTimer)
303             {
304                 mViewTimer.Stop();
305                 mViewTimer.Tick -= OnTimerTick;
306                 mViewTimer.Dispose();
307                 mViewTimer = null;
308             }
309
310             if (null != mCurrentEffect)
311             {
312                 mCurrentEffect.StopTransition();
313                 mContent.Remove(mCurrentEffect);
314                 mCurrentEffect.Dispose();
315                 mCurrentEffect = null;
316             }
317
318             if (mContent)
319             {
320                 content_layer.Remove(mContent);
321                 mContent.Dispose();
322                 mContent = null;
323             }
324
325             if (content_layer)
326             {
327                 NUIApplication.GetDefaultWindow().RemoveLayer(content_layer);
328                 content_layer.Dispose();
329                 content_layer = null;
330             }
331         }
332
333         private void OnPanGesture(object sender, PanGestureDetector.DetectedEventArgs gesture)
334         {
335             // does not response when the transition has not finished
336             if (mIsImageLoading || mCubeWaveEffect.IsTransitioning() || mCubeCrossEffect.IsTransitioning() || mCubeFoldEffect.IsTransitioning() || mSlideshow)
337             {
338                 return;
339             }
340
341             if (gesture.PanGesture.State == Gesture.StateType.Continuing)
342             {
343                 if (gesture.PanGesture.Displacement.X < 0)
344                 {
345                     mIndex = (mIndex + 1) % NUM_IMAGES;
346                 }
347                 else
348                 {
349                     mIndex = (mIndex + NUM_IMAGES - 1) % NUM_IMAGES;
350                 }
351
352                 mPanPosition = gesture.PanGesture.Position;
353                 mPanDisplacement = gesture.PanGesture.Displacement;
354                 GoToNextImage();
355             }
356         }
357
358         private void GoToNextImage()
359         {
360             mNextTexture = LoadStageFillingTexture(IMAGES[mIndex]);
361
362             mCurrentEffect.SetTargetTexture(mNextTexture);
363             mIsImageLoading = false;
364
365             mCurrentEffect.StartTransition(mPanPosition, mPanDisplacement);
366
367             mCurrentTexture = mNextTexture;
368         }
369
370
371         private bool OnTimerTick(object sender, Timer.TickEventArgs args)
372         {
373             if (mSlideshow)
374             {
375                 mIndex = (mIndex + 1) % NUM_IMAGES;
376                 GoToNextImage();
377             }
378
379             return false;
380         }
381
382         private void OnCubeEffectCompleted(object sender, CubeTransitionEffect.TransitionCompletedEventArgs args)
383         {
384             if (mSlideshow)
385             {
386                 mViewTimer.Start();
387             }
388         }
389
390         private Texture LoadStageFillingTexture(string filepath)
391         {
392             Size2D dimensions = new Size2D(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Height);
393             PixelBuffer pb = ImageLoading.LoadImageFromFile(filepath, dimensions, FittingModeType.ScaleToFill);
394             PixelData pd = PixelBuffer.Convert(pb);
395
396             Texture texture = new Texture(TextureType.TEXTURE_2D, pd.GetPixelFormat(), pd.GetWidth(), pd.GetHeight());
397             texture.Upload(pd);
398
399             return texture;
400         }
401
402         private bool OnToggleButtonClicked(object sender, global::System.EventArgs args)
403         {
404             mContent.Remove(mCurrentEffect);
405             if (mCurrentEffect == mCubeWaveEffect)
406             {
407                 mCurrentEffect = mCubeCrossEffect;
408                 mTitle.Text = APPLICATION_TITLE_CROSS;
409             }
410             else if (mCurrentEffect == mCubeCrossEffect)
411             {
412                 mCurrentEffect = mCubeFoldEffect;
413                 mTitle.Text = APPLICATION_TITLE_FOLD;
414             }
415             else
416             {
417                 mCurrentEffect = mCubeWaveEffect;
418                 mTitle.Text = APPLICATION_TITLE_WAVE;
419             }
420             mContent.Add(mCurrentEffect);
421
422             // Set the current image to cube transition effect
423             // only need to set at beginning or change from another effect
424             mCurrentEffect.SetCurrentTexture(mCurrentTexture);
425             return true;
426         }
427
428         private void OnWaveClicked(object sender, global::System.EventArgs args)
429         {
430             mContent.Remove(mCurrentEffect);
431             mCurrentEffect = mCubeWaveEffect;
432             mTitle.Text = APPLICATION_TITLE_WAVE;
433             mContent.Add(mCurrentEffect);
434
435             // Set the current image to cube transition effect
436             // only need to set at beginning or change from another effect
437             mCurrentEffect.SetCurrentTexture(mCurrentTexture);
438         }
439
440         private void OnCrossClicked(object sender, global::System.EventArgs args)
441         {
442             mContent.Remove(mCurrentEffect);
443             mCurrentEffect = mCubeCrossEffect;
444             mTitle.Text = APPLICATION_TITLE_CROSS;
445             mContent.Add(mCurrentEffect);
446
447             // Set the current image to cube transition effect
448             // only need to set at beginning or change from another effect
449             mCurrentEffect.SetCurrentTexture(mCurrentTexture);
450         }
451
452         private void OnFoldClicked(object sender, global::System.EventArgs args)
453         {
454             mContent.Remove(mCurrentEffect);
455             mCurrentEffect = mCubeFoldEffect;
456             mTitle.Text = APPLICATION_TITLE_FOLD;
457             mContent.Add(mCurrentEffect);
458
459             // Set the current image to cube transition effect
460             // only need to set at beginning or change from another effect
461             mCurrentEffect.SetCurrentTexture(mCurrentTexture);
462         }
463
464         private void OnPushButtonClicked(object sender, global::System.EventArgs args)
465         {
466             mSlideshow = !mSlideshow;
467             if (mSlideshow)
468             {
469                 mPanGestureDetector.Detach(mContent);
470
471                 var style = mSlideshowButton.Style;
472                 style.Icon.ResourceUrl = new Selector<string>() { Normal = SLIDE_SHOW_STOP_ICON, Pressed = SLIDE_SHOW_STOP_ICON_SELECTED };
473                 mSlideshowButton.ApplyStyle(style);
474
475                 mPanPosition = new Vector2(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Width * 0.5f);
476                 mPanDisplacement = new Vector2(-10.0f, 0.0f);
477
478                 mViewTimer.Start();
479             }
480             else
481             {
482                 mPanGestureDetector.Attach(mContent);
483                 var style = mSlideshowButton.Style;
484                 style.Icon.ResourceUrl = new Selector<string>() { Normal = SLIDE_SHOW_START_ICON, Pressed = SLIDE_SHOW_START_ICON_SELECTED };
485                 mSlideshowButton.ApplyStyle(style);
486                 mViewTimer.Stop();
487             }
488         }
489     }
490 }