1 #ifndef __DALI_TOOLKIT_INTERNAL_CUBE_TRANSITION_EFFECT_H__
2 #define __DALI_TOOLKIT_INTERNAL_CUBE_TRANSITION_EFFECT_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/public-api/images/bitmap-image.h>
24 #include <dali/public-api/images/frame-buffer-image.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/render-tasks/render-task.h>
27 #include <dali/public-api/shader-effects/shader-effect.h>
28 #include <dali/public-api/images/resource-image.h>
31 #include <dali-toolkit/public-api/transition-effects/cube-transition-effect.h>
39 class CubeTransitionEffect;
45 * Create a image with size of viewAreaSize
46 * with the effect image as its center part and (0,0,0,1) at other parts
48 class FullAreaImageCreator : public ShaderEffect
54 * Create an uninitialized FullAreaImageCreator
55 * this can be initialized with FullAreaImageCreator::New()
57 FullAreaImageCreator(){}
62 * This is non-virtual since derived Handle types must not contain data or virtual methods.
64 ~FullAreaImageCreator(){}
67 * Create an initialized FullAreaImageCreator.
68 * @return A handle to a newly allocated Dali resource.
70 static FullAreaImageCreator New()
72 std::string vertexShader(
73 "uniform mediump vec4 uRegion; \n"
76 " gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
77 " vTexCoord.s = (aTexCoord.s - uRegion.s) / uRegion.p;"
78 " vTexCoord.t = ( 1.0 - aTexCoord.t - uRegion.t) / uRegion.q;"
82 std::string fragmentShader(
83 "uniform mediump vec4 uRegion; \n"
86 " if( vTexCoord.s > 0.0 && vTexCoord.s < 1.0 && vTexCoord.t > 0.0 && vTexCoord.t < 1.0) \n"
88 " gl_FragColor = texture2D( sEffect, vTexCoord ) * uColor ; \n"
92 " gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); \n"
97 ShaderEffect shaderEffectCustom = ShaderEffect::New(vertexShader, fragmentShader);
98 FullAreaImageCreator handle( shaderEffectCustom );
104 * Set up the position and size of the effect texture
105 * @param[in] viewArea the size of full-area image to create
106 * @param[in] size the size of effect texture
108 void SetRegionSize( const Vector2& viewArea, const Vector2& size )
110 Vector2 sizeRatio( std::min(1.f, size.x / viewArea.x), std::min(1.f, size.y / viewArea.y) );
111 Vector4 region( (1.f-sizeRatio.x)*0.5f,
112 (1.f-sizeRatio.y)*0.5f,
115 SetUniform( "uRegion", region );
120 FullAreaImageCreator( ShaderEffect handle )
121 : ShaderEffect( handle )
129 * CubeTransitionEffect implementation class
131 class CubeTransitionEffect : public Dali::BaseObject, public ConnectionTracker
139 ~CubeTransitionEffect();
142 * @copydoc Toolkit::CubeTransitionEffect::SetTransitionDuration
144 void SetTransitionDuration( float duration );
147 * @copydoc Toolkit::CubeTransitionEffect::GetTransitionDuration
149 float GetTransitionDuration() const;
152 * @copydoc Toolkit::CubeTransitionEffect::SetCubeDisplacement
154 void SetCubeDisplacement( float displacement );
157 * @copydoc Toolkit::CubeTransitionEffect::GetCubeDisplacement
159 float GetCubeDisplacement() const;
162 * @copydoc Toolkit::CubeTransitionEffect::GetRoot
167 * @copydoc Toolkit::CubeTransitionEffect::IsTransiting
172 * @copydoc Toolkit::CubeTransitionEffect::SetFirstImage
174 void SetCurrentImage(ImageActor imageActor);
177 * @copydoc Toolkit::CubeTransitionEffect::SetTargetImage
179 void SetTargetImage(ImageActor imageActor);
182 * @copydoc Toolkit::CubeTransitionEffect::StartTransition(bool)
184 void StartTransition( bool toNextImage = true );
187 * @copydoc Toolkit::CubeTransitionEffect::StartTransition(Vector2, Vector2)
189 void StartTransition( Vector2 panPosition, Vector2 panDisplacement );
192 * @copydoc Toolkit::CubeTransitionEffect::PauseTransition()
194 void PauseTransition();
197 * @copydoc Toolkit::CubeTransitionEffect::ResumeTransition()
199 void ResumeTransition();
202 * @copydoc Toolkit::CubeTransitionEffect::StopTransition()
204 void StopTransition();
209 * @copydoc Toolkit::CubeTransitionEffect::TransitionCompletedSignal()
211 Toolkit::CubeTransitionEffect::TransitionCompletedSignalType& TransitionCompletedSignal();
216 * Construct a new CubeTransitionEffect object
217 * Called in the constructor of subclasses
218 * @param[in] numRows How many rows of cubes
219 * @param[in] numColumns How many columns of cubes
220 * @param[in] viewAreaSize The size of view area for this transition effect
222 CubeTransitionEffect( unsigned int numRows, unsigned int numColumns, Size viewAreaSize );
225 * Initialization steps: creating a layer, two groups of tiles,
226 * and one group of actors (cubes) serving as parents of every two tiles (one from each image).
233 * Create an image actor to serve as a face of the cube
234 * @param[in] image The image to display.
235 * @param[in] color The color to set to the actor
236 * @return The tile actor created
238 ImageActor CreateTile( Image image, const Vector4& color );
241 * Set Image content to tiles
242 * As only when the image ready, can we get correct image attributes
243 * so inside this function, the process needs to be passed to callBack of image resource loading succeed.
244 * @param[in] imageActor The imageActor whose image content will be set to the tiles
246 void SetImage(ImageActor imageActor);
249 * Callback function of image resource loading succeed
250 * Set image and pixelArea to tiles
251 * @param[in] image The image content of the imageActor for transition
253 void OnImageLoaded(ResourceImage image);
256 * Set sub-image to each tile.
257 * @param[in] image The image content of the imageActor for transition
259 void PrepareTiles( Image image );
262 * Callback function of transition animation finished
263 * Hide transition layer, show current imageActor, and set isAnimating flag to false
264 * @param[in] source The cube transition animation
266 void OnTransitionFinished(Animation& source);
269 * This method is called after the CubeTransitionEffect has been initialized. Derived classes should do
270 * any second phase initialization by overriding this method.
272 virtual void OnInitialize() { }
275 * This method is called after the a new transition is activated.
276 * Derived classes should do any specialized transition process by overriding this method.
277 * @param[in] panPosition The press down position of panGesture
278 * @param[in] panDisplacement The displacement vector of panGesture
280 virtual void OnStartTransition( Vector2 panPosition, Vector2 panDisplacement ) {}
283 * This method is called when the transition is forced stop in the middle of animation.
284 * Derived classed should set the rotation status of the cubes to the same as the final state when the animation is finished completely.
285 * So that the next transition would be started correctly.
287 virtual void OnStopTransition() {}
292 unsigned int mNumRows;
293 unsigned int mNumColumns;
295 ActorContainer mBoxes;
296 std::vector< ImageActor > mTiles[2];
301 ImageActor mCurrentImage;
302 unsigned int mContainerIndex; //have the value 0 or 1, refer to mTiles[0] or mTiles[1]
304 bool mChangeTurningDirection;
305 bool mIsToNextImage; //if true, cubes rotate counter-clockwise; else clockwise
306 bool mIsImageLoading;
308 float mAnimationDuration;
309 Animation mAnimation;
313 float mCubeDisplacement;
315 bool mFirstTransition;
317 RenderTask mOffScreenTask;
318 FrameBufferImage mOffScreenBuffer[2];
319 ImageActor mEmptyImage;
320 FullAreaImageCreator mFullImageCreator;
321 unsigned int mBufferIndex;
323 static const Vector4 FULL_BRIGHTNESS;
324 static const Vector4 HALF_BRIGHTNESS;
328 Toolkit::CubeTransitionEffect::TransitionCompletedSignalType mTransitionCompletedSignal;
332 } // namespace Internal
334 // Helpers for public-api forwarding methods
336 inline Internal::CubeTransitionEffect& GetImpl(Dali::Toolkit::CubeTransitionEffect& obj)
338 DALI_ASSERT_ALWAYS(obj);
340 Dali::BaseObject& handle = obj.GetBaseObject();
342 return static_cast<Internal::CubeTransitionEffect&>(handle);
345 inline const Internal::CubeTransitionEffect& GetImpl(const Dali::Toolkit::CubeTransitionEffect& obj)
347 DALI_ASSERT_ALWAYS(obj);
349 const Dali::BaseObject& handle = obj.GetBaseObject();
351 return static_cast<const Internal::CubeTransitionEffect&>(handle);
354 } // namespace Toolkit
358 #endif /* __DALI_TOOLKIT_INTERNAL_CUBE_TRANSITION_EFFECT_H__ */