2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
5 namespace Tizen.NUI.Samples
7 public class CubeTransitionEffectSample : IExample
9 static string DEMO_IMAGE_DIR = CommonResource.GetDaliResourcePath() + "CubeTransitionEffect/";
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";
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",
50 const int NUM_IMAGES = 21;
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;
69 // The duration of the current image staying on screen when slideshow is on
70 const int VIEWINGTIME = 2000; // 2 seconds
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;
81 private Texture mCurrentTexture;
82 private Texture mNextTexture;
83 private uint mIndex = 0;
84 private bool mIsImageLoading = false;
86 private PanGestureDetector mPanGestureDetector;
88 private CubeTransitionEffect mCubeWaveEffect;
89 private CubeTransitionEffect mCubeCrossEffect;
90 private CubeTransitionEffect mCubeFoldEffect;
91 private CubeTransitionEffect mCurrentEffect;
93 private Timer mViewTimer;
94 private bool mSlideshow = false;
96 private Vector2 mPanPosition;
97 private Vector2 mPanDisplacement;
99 public void Activate()
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;
109 NUIApplication.GetDefaultWindow().GetDefaultLayer().Add(tool_bar);
110 NUIApplication.GetDefaultWindow().GetDefaultLayer().RaiseToTop();
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);
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;
133 mSlideshowButton.RaiseToTop();
135 tool_bar.Add(mSlideshowButton);
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);
147 toggle = new RadioButtonGroup();
148 for (int i = 0; i < 3; i++)
150 radios[i] = new RadioButton();
151 radios[i].Size = new Size(37, 34);
152 toggle.Add(radios[i]);
153 radiosParent.Add(radios[i]);
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;
169 mCurrentTexture = LoadStageFillingTexture(IMAGES[mIndex]);
171 // content layer is 3D.
172 content_layer = new Layer();
173 content_layer.Behavior = Layer.LayerBehavior.Layer3D;
174 NUIApplication.GetDefaultWindow().AddLayer(content_layer);
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;
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);
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;
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);
200 mCubeFoldEffect = new CubeTransitionFoldEffect(NUM_ROWS_FOLD, NUM_COLUMNS_FOLD);
201 mCubeFoldEffect.SetTransitionDuration(ANIMATION_DURATION_FOLD);
202 mCubeFoldEffect.TransitionCompleted += OnCubeEffectCompleted;
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);
210 mViewTimer = new Timer(VIEWINGTIME);
211 mViewTimer.Tick += OnTimerTick;
214 mCurrentEffect = mCubeWaveEffect;
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;
222 mContent.Add(mCurrentEffect);
224 content_layer.Add(mContent);
226 mPanGestureDetector = new PanGestureDetector();
227 mPanGestureDetector.Detected += OnPanGesture;
228 mPanGestureDetector.Attach(mContent);
231 public void Deactivate()
235 tool_bar.Remove(mTitle);
240 if (mSlideshowButton)
242 tool_bar.Remove(mSlideshowButton);
243 mSlideshowButton.Clicked -= OnPushButtonClicked;
244 mSlideshowButton.Dispose();
245 mSlideshowButton = null;
250 for (int i = 0; i < 3; i++)
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]);
262 tool_bar.Remove(radiosParent);
263 radiosParent.Dispose();
269 NUIApplication.GetDefaultWindow().GetDefaultLayer().Remove(tool_bar);
276 mCubeWaveEffect.TransitionCompleted -= OnCubeEffectCompleted;
277 mCubeWaveEffect.Dispose();
278 mCubeWaveEffect = null;
281 if (mCubeCrossEffect)
283 mCubeCrossEffect.TransitionCompleted -= OnCubeEffectCompleted;
284 mCubeCrossEffect.Dispose();
285 mCubeCrossEffect = null;
290 mCubeFoldEffect.TransitionCompleted -= OnCubeEffectCompleted;
291 mCubeFoldEffect.Dispose();
292 mCubeFoldEffect = null;
295 if (mPanGestureDetector)
297 mPanGestureDetector.Detected -= OnPanGesture;
298 mPanGestureDetector.Dispose();
299 mPanGestureDetector = null;
302 if (null != mViewTimer)
305 mViewTimer.Tick -= OnTimerTick;
306 mViewTimer.Dispose();
310 if (null != mCurrentEffect)
312 mCurrentEffect.StopTransition();
313 mContent.Remove(mCurrentEffect);
314 mCurrentEffect.Dispose();
315 mCurrentEffect = null;
320 content_layer.Remove(mContent);
327 NUIApplication.GetDefaultWindow().RemoveLayer(content_layer);
328 content_layer.Dispose();
329 content_layer = null;
333 private void OnPanGesture(object sender, PanGestureDetector.DetectedEventArgs gesture)
335 // does not response when the transition has not finished
336 if (mIsImageLoading || mCubeWaveEffect.IsTransitioning() || mCubeCrossEffect.IsTransitioning() || mCubeFoldEffect.IsTransitioning() || mSlideshow)
341 if (gesture.PanGesture.State == Gesture.StateType.Continuing)
343 if (gesture.PanGesture.Displacement.X < 0)
345 mIndex = (mIndex + 1) % NUM_IMAGES;
349 mIndex = (mIndex + NUM_IMAGES - 1) % NUM_IMAGES;
352 mPanPosition = gesture.PanGesture.Position;
353 mPanDisplacement = gesture.PanGesture.Displacement;
358 private void GoToNextImage()
360 mNextTexture = LoadStageFillingTexture(IMAGES[mIndex]);
362 mCurrentEffect.SetTargetTexture(mNextTexture);
363 mIsImageLoading = false;
365 mCurrentEffect.StartTransition(mPanPosition, mPanDisplacement);
367 mCurrentTexture = mNextTexture;
371 private bool OnTimerTick(object sender, Timer.TickEventArgs args)
375 mIndex = (mIndex + 1) % NUM_IMAGES;
382 private void OnCubeEffectCompleted(object sender, CubeTransitionEffect.TransitionCompletedEventArgs args)
390 private Texture LoadStageFillingTexture(string filepath)
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);
396 Texture texture = new Texture(TextureType.TEXTURE_2D, pd.GetPixelFormat(), pd.GetWidth(), pd.GetHeight());
402 private bool OnToggleButtonClicked(object sender, global::System.EventArgs args)
404 mContent.Remove(mCurrentEffect);
405 if (mCurrentEffect == mCubeWaveEffect)
407 mCurrentEffect = mCubeCrossEffect;
408 mTitle.Text = APPLICATION_TITLE_CROSS;
410 else if (mCurrentEffect == mCubeCrossEffect)
412 mCurrentEffect = mCubeFoldEffect;
413 mTitle.Text = APPLICATION_TITLE_FOLD;
417 mCurrentEffect = mCubeWaveEffect;
418 mTitle.Text = APPLICATION_TITLE_WAVE;
420 mContent.Add(mCurrentEffect);
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);
428 private void OnWaveClicked(object sender, global::System.EventArgs args)
430 mContent.Remove(mCurrentEffect);
431 mCurrentEffect = mCubeWaveEffect;
432 mTitle.Text = APPLICATION_TITLE_WAVE;
433 mContent.Add(mCurrentEffect);
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);
440 private void OnCrossClicked(object sender, global::System.EventArgs args)
442 mContent.Remove(mCurrentEffect);
443 mCurrentEffect = mCubeCrossEffect;
444 mTitle.Text = APPLICATION_TITLE_CROSS;
445 mContent.Add(mCurrentEffect);
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);
452 private void OnFoldClicked(object sender, global::System.EventArgs args)
454 mContent.Remove(mCurrentEffect);
455 mCurrentEffect = mCubeFoldEffect;
456 mTitle.Text = APPLICATION_TITLE_FOLD;
457 mContent.Add(mCurrentEffect);
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);
464 private void OnPushButtonClicked(object sender, global::System.EventArgs args)
466 mSlideshow = !mSlideshow;
469 mPanGestureDetector.Detach(mContent);
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);
475 mPanPosition = new Vector2(NUIApplication.GetDefaultWindow().WindowSize.Width, NUIApplication.GetDefaultWindow().WindowSize.Width * 0.5f);
476 mPanDisplacement = new Vector2(-10.0f, 0.0f);
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);