2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/devel-api/images/texture-set-image.h>
19 #include <dali/public-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/controls/bubble-effect/bubble-emitter.h>
22 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
28 #include "shared/view.h"
29 #include "shared/utility.h"
32 using namespace Dali::Toolkit;
34 class NewWindowController;
38 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-2.jpg" );
39 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
40 const char * const LOSE_CONTEXT_IMAGE( DEMO_IMAGE_DIR "icon-cluster-wobble.png" );
41 const char * const LOSE_CONTEXT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-cluster-wobble-selected.png" );
42 const char * const BASE_IMAGE( DEMO_IMAGE_DIR "gallery-large-14.jpg" );
43 const char * const EFFECT_IMAGE( DEMO_IMAGE_DIR "gallery-large-18.jpg" );
44 const char * const LOGO_IMAGE(DEMO_IMAGE_DIR "dali-logo.png");
46 const float EXPLOSION_DURATION(1.2f);
47 const unsigned int EMIT_INTERVAL_IN_MS(40);
48 const float TRACK_DURATION_IN_MS(970);
50 Application gApplication;
51 NewWindowController* gNewWindowController(NULL);
53 #define MAKE_SHADER(A)#A
55 const char* VERTEX_COLOR_MESH = MAKE_SHADER(
56 attribute mediump vec3 aPosition;\n
57 attribute lowp vec3 aColor;\n
58 uniform mediump mat4 uMvpMatrix;\n
59 uniform mediump vec3 uSize;\n
60 varying lowp vec3 vColor;\n
64 gl_Position = uMvpMatrix * vec4( aPosition*uSize, 1.0 );\n
69 const char* FRAGMENT_COLOR_MESH = MAKE_SHADER(
70 uniform lowp vec4 uColor;\n
71 varying lowp vec3 vColor;\n
75 gl_FragColor = vec4(vColor,1.0)*uColor;
79 const char* VERTEX_TEXTURE_MESH = MAKE_SHADER(
80 attribute mediump vec3 aPosition;\n
81 attribute highp vec2 aTexCoord;\n
82 uniform mediump mat4 uMvpMatrix;\n
83 uniform mediump vec3 uSize;\n
84 varying mediump vec2 vTexCoord;\n
88 gl_Position = uMvpMatrix * vec4( aPosition*uSize, 1.0 );\n
89 vTexCoord = aTexCoord;\n
93 const char* FRAGMENT_TEXTURE_MESH = MAKE_SHADER(
94 varying mediump vec2 vTexCoord;\n
95 uniform lowp vec4 uColor;\n
96 uniform sampler2D sTexture;\n
100 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;
104 const char* FRAGMENT_BLEND_SHADER = MAKE_SHADER(
105 varying mediump vec2 vTexCoord;\n
106 uniform sampler2D sTexture;\n
107 uniform sampler2D sEffect;\n
108 uniform mediump float alpha;\n
112 mediump vec4 fragColor = texture2D(sTexture, vTexCoord);\n
113 mediump vec4 fxColor = texture2D(sEffect, vTexCoord);\n
114 gl_FragColor = mix(fragColor,fxColor, alpha);\n
118 }; // anonymous namespace
121 class NewWindowController : public ConnectionTracker
124 NewWindowController( Application& app );
125 void Create( Application& app );
126 void Destroy( Application& app );
128 void AddBubbles( Actor& parentActor, const Vector2& stageSize);
129 void AddMeshActor( Actor& parentActor );
130 void AddBlendingImageActor( Actor& parentActor );
131 void AddTextLabel( Actor& parentActor );
133 ImageView CreateBlurredMirrorImage(const char* imageName);
134 FrameBufferImage CreateFrameBufferForImage( const char* imageName, Property::Map& shaderEffect, const Vector3& rgbDelta );
135 void SetUpBubbleEmission( const Vector2& emitPosition, const Vector2& direction );
136 Geometry CreateMeshGeometry();
137 Dali::Property::Map CreateColorModifierer();
139 static void NewWindow(void);
141 bool OnTrackTimerTick();
142 void OnKeyEvent(const KeyEvent& event);
143 bool OnLoseContextButtonClicked( Toolkit::Button button );
144 void OnContextLost();
145 void OnContextRegained();
148 Application mApplication;
149 TextLabel mTextActor;
151 Toolkit::Control mView; ///< The View instance.
152 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
153 TextLabel mTitleActor; ///< The Toolbar's Title.
154 Layer mContentLayer; ///< Content layer (scrolling cluster content)
155 Toolkit::PushButton mLoseContextButton;
157 Toolkit::BubbleEmitter mEmitter;
158 Timer mEmitTrackTimer;
159 bool mNeedNewAnimation;
160 unsigned int mAnimateComponentCount;
161 Animation mEmitAnimation;
165 NewWindowController::NewWindowController( Application& application )
166 : mApplication(application),
167 mNeedNewAnimation(true)
169 mApplication.InitSignal().Connect(this, &NewWindowController::Create);
170 mApplication.TerminateSignal().Connect(this, &NewWindowController::Destroy);
173 void NewWindowController::Create( Application& app )
175 Stage stage = Stage::GetCurrent();
176 stage.SetBackgroundColor(Color::YELLOW);
178 stage.KeyEventSignal().Connect(this, &NewWindowController::OnKeyEvent);
180 // The Init signal is received once (only) during the Application lifetime
182 // Hide the indicator bar
183 mApplication.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
185 mContentLayer = DemoHelper::CreateView( app,
190 "Context recovery" );
192 Size stageSize = stage.GetSize();
193 ImageView backgroundActor = ImageView::New( BACKGROUND_IMAGE, Dali::ImageDimensions( stageSize.x, stageSize.y ) );
194 backgroundActor.SetParentOrigin( ParentOrigin::CENTER );
195 mContentLayer.Add(backgroundActor);
197 // Point the default render task at the view
198 RenderTaskList taskList = stage.GetRenderTaskList();
199 RenderTask defaultTask = taskList.GetTask( 0u );
202 defaultTask.SetSourceActor( mView );
205 mLoseContextButton = Toolkit::PushButton::New();
206 mLoseContextButton.SetUnselectedImage( LOSE_CONTEXT_IMAGE );
207 mLoseContextButton.SetSelectedImage( LOSE_CONTEXT_IMAGE_SELECTED );
208 mLoseContextButton.ClickedSignal().Connect( this, &NewWindowController::OnLoseContextButtonClicked );
209 mToolBar.AddControl( mLoseContextButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
211 Actor logoLayoutActor = Actor::New();
212 logoLayoutActor.SetParentOrigin(ParentOrigin::CENTER);
213 logoLayoutActor.SetPosition(0.0f, -200.0f, 0.0f);
214 logoLayoutActor.SetScale(0.5f);
215 backgroundActor.Add(logoLayoutActor);
217 ImageView imageView = ImageView::New( LOGO_IMAGE );
218 imageView.SetName("daliLogo");
219 imageView.SetParentOrigin(ParentOrigin::CENTER);
220 imageView.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER);
221 logoLayoutActor.Add(imageView);
223 ImageView mirrorImageView = CreateBlurredMirrorImage(LOGO_IMAGE);
224 mirrorImageView.SetParentOrigin(ParentOrigin::TOP_CENTER);
225 mirrorImageView.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER);
226 logoLayoutActor.Add(mirrorImageView);
228 AddBubbles( backgroundActor, stage.GetSize());
229 AddMeshActor( backgroundActor );
230 AddBlendingImageActor( backgroundActor );
231 AddTextLabel( backgroundActor );
233 stage.ContextLostSignal().Connect(this, &NewWindowController::OnContextLost);
234 stage.ContextRegainedSignal().Connect(this, &NewWindowController::OnContextRegained);
237 void NewWindowController::Destroy( Application& app )
239 UnparentAndReset(mTextActor);
242 void NewWindowController::AddBubbles( Actor& parentActor, const Vector2& stageSize)
244 mEmitter = Toolkit::BubbleEmitter::New( stageSize,
245 DemoHelper::LoadImage( DEMO_IMAGE_DIR "bubble-ball.png" ),
246 200, Vector2( 5.0f, 5.0f ) );
248 Image background = DemoHelper::LoadImage(BACKGROUND_IMAGE);
249 mEmitter.SetBackground( background, Vector3(0.5f, 0.f,0.5f) );
250 mEmitter.SetBubbleDensity( 9.f );
251 Actor bubbleRoot = mEmitter.GetRootActor();
252 parentActor.Add( bubbleRoot );
253 bubbleRoot.SetParentOrigin(ParentOrigin::CENTER);
254 bubbleRoot.SetZ(0.1f);
256 mEmitTrackTimer = Timer::New( EMIT_INTERVAL_IN_MS );
257 mEmitTrackTimer.TickSignal().Connect(this, &NewWindowController::OnTrackTimerTick);
258 mEmitTrackTimer.Start();
261 void NewWindowController::AddMeshActor( Actor& parentActor )
263 Geometry meshGeometry = CreateMeshGeometry();
265 // Create a coloured mesh
266 Shader shaderColorMesh = Shader::New( VERTEX_COLOR_MESH, FRAGMENT_COLOR_MESH );
267 Renderer colorMeshRenderer = Renderer::New( meshGeometry, shaderColorMesh );
269 Actor colorMeshActor = Actor::New();
270 colorMeshActor.AddRenderer( colorMeshRenderer );
271 colorMeshActor.SetSize( 175.f,175.f, 175.f );
272 colorMeshActor.SetParentOrigin( ParentOrigin::CENTER );
273 colorMeshActor.SetAnchorPoint(AnchorPoint::TOP_CENTER);
274 colorMeshActor.SetPosition(Vector3(0.0f, 50.0f, 0.0f));
275 colorMeshActor.SetOrientation( Degree(75.f), Vector3::XAXIS );
276 colorMeshActor.SetName("ColorMeshActor");
278 // Create a textured mesh
279 Texture effectTexture = DemoHelper::LoadTexture(EFFECT_IMAGE);
280 Shader shaderTextureMesh = Shader::New( VERTEX_TEXTURE_MESH, FRAGMENT_TEXTURE_MESH );
281 TextureSet textureSet = TextureSet::New();
282 textureSet.SetTexture( 0u, effectTexture );
283 Renderer textureMeshRenderer = Renderer::New( meshGeometry, shaderTextureMesh );
284 textureMeshRenderer.SetTextures( textureSet );
286 Actor textureMeshActor = Actor::New();
287 textureMeshActor.AddRenderer( textureMeshRenderer );
288 textureMeshActor.SetSize( 175.f,175.f, 175.f );
289 textureMeshActor.SetParentOrigin( ParentOrigin::CENTER );
290 textureMeshActor.SetAnchorPoint(AnchorPoint::TOP_CENTER);
291 textureMeshActor.SetPosition(Vector3(0.0f, 200.0f, 0.0f));
292 textureMeshActor.SetOrientation( Degree(75.f), Vector3::XAXIS );
293 textureMeshActor.SetName("TextureMeshActor");
295 Layer layer3d = Layer::New();
296 layer3d.SetParentOrigin( ParentOrigin::CENTER );
297 layer3d.SetAnchorPoint( AnchorPoint::CENTER );
298 layer3d.SetBehavior(Layer::LAYER_3D);
300 layer3d.Add( colorMeshActor );
301 layer3d.Add( textureMeshActor );
302 parentActor.Add(layer3d);
305 void NewWindowController::AddBlendingImageActor( Actor& parentActor )
307 Property::Map colorModifier = CreateColorModifierer();
309 FrameBufferImage fb2 = CreateFrameBufferForImage( EFFECT_IMAGE, colorModifier, Vector3( 0.5f, 0.5f, 0.5f ) );
311 ImageView tmpActor = ImageView::New(fb2);
312 parentActor.Add(tmpActor);
313 tmpActor.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
314 tmpActor.SetAnchorPoint(AnchorPoint::TOP_RIGHT);
315 tmpActor.SetPosition(Vector3(0.0f, 150.0f, 0.0f));
316 tmpActor.SetScale(0.25f);
318 // create blending shader effect
319 Property::Map customShader;
320 customShader[ "fragmentShader" ] = FRAGMENT_BLEND_SHADER;
322 map[ "shader" ] = customShader;
324 ImageView blendActor = ImageView::New( BASE_IMAGE );
325 blendActor.SetProperty( ImageView::Property::IMAGE, map );
326 blendActor.RegisterProperty( "alpha", 0.5f );
328 blendActor.SetParentOrigin(ParentOrigin::CENTER_RIGHT);
329 blendActor.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
330 blendActor.SetPosition(Vector3(0.0f, 100.0f, 0.0f));
331 blendActor.SetSize(140, 140);
332 parentActor.Add(blendActor);
334 TextureSet textureSet = blendActor.GetRendererAt(0u).GetTextures();
335 TextureSetImage( textureSet, 1u, fb2 );
338 void NewWindowController::AddTextLabel( Actor& parentActor )
340 mTextActor = TextLabel::New("Some text");
341 mTextActor.SetParentOrigin(ParentOrigin::CENTER);
342 mTextActor.SetColor(Color::RED);
343 mTextActor.SetName("PushMe text");
344 parentActor.Add( mTextActor );
347 ImageView NewWindowController::CreateBlurredMirrorImage(const char* imageName)
349 Image image = DemoHelper::LoadImage(imageName);
351 Vector2 FBOSize = Vector2( image.GetWidth(), image.GetHeight() );
352 FrameBufferImage fbo = FrameBufferImage::New( FBOSize.width, FBOSize.height, Pixel::RGBA8888);
354 GaussianBlurView gbv = GaussianBlurView::New(5, 2.0f, Pixel::RGBA8888, 0.5f, 0.5f, true);
355 gbv.SetBackgroundColor(Color::TRANSPARENT);
356 gbv.SetUserImageAndOutputRenderTarget( image, fbo );
357 gbv.SetSize(FBOSize);
358 Stage::GetCurrent().Add(gbv);
361 ImageView blurredActor = ImageView::New(fbo);
362 blurredActor.SetSize(FBOSize);
363 blurredActor.SetScale(1.0f, -1.0f, 1.0f);
367 FrameBufferImage NewWindowController::CreateFrameBufferForImage(const char* imageName, Property::Map& shaderEffect, const Vector3& rgbDelta )
369 Stage stage = Stage::GetCurrent();
370 Uint16Pair intFboSize = ResourceImage::GetImageSize( imageName );
371 Vector2 FBOSize = Vector2(intFboSize.GetWidth(), intFboSize.GetHeight());
373 FrameBufferImage framebuffer = FrameBufferImage::New(FBOSize.x, FBOSize.y );
375 RenderTask renderTask = stage.GetRenderTaskList().CreateTask();
377 ImageView imageView = ImageView::New( imageName );
378 imageView.SetName("Source image actor");
379 imageView.SetProperty( ImageView::Property::IMAGE, shaderEffect );
380 imageView.RegisterProperty( "uRGBDelta", rgbDelta );
382 imageView.SetParentOrigin(ParentOrigin::CENTER);
383 imageView.SetAnchorPoint(AnchorPoint::CENTER);
384 imageView.SetScale(1.0f, -1.0f, 1.0f);
385 stage.Add(imageView); // Not in default image view
387 CameraActor cameraActor = CameraActor::New(FBOSize);
388 cameraActor.SetParentOrigin(ParentOrigin::CENTER);
389 cameraActor.SetFieldOfView(Math::PI*0.25f);
390 cameraActor.SetNearClippingPlane(1.0f);
391 cameraActor.SetAspectRatio(FBOSize.width / FBOSize.height);
392 cameraActor.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
393 cameraActor.SetPosition(0.0f, 0.0f, ((FBOSize.height * 0.5f) / tanf(Math::PI * 0.125f)));
394 stage.Add(cameraActor);
396 renderTask.SetSourceActor(imageView);
397 renderTask.SetInputEnabled(false);
398 renderTask.SetTargetFrameBuffer(framebuffer);
399 renderTask.SetCameraActor( cameraActor );
400 renderTask.SetClearColor( Color::TRANSPARENT );
401 renderTask.SetClearEnabled( true );
402 renderTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
407 void NewWindowController::SetUpBubbleEmission( const Vector2& emitPosition, const Vector2& direction)
409 if( mNeedNewAnimation )
411 float duration = Random::Range(1.f, 1.5f);
412 mEmitAnimation = Animation::New( duration );
413 mNeedNewAnimation = false;
414 mAnimateComponentCount = 0;
417 mEmitter.EmitBubble( mEmitAnimation, emitPosition, direction, Vector2(10,10) );
419 mAnimateComponentCount++;
421 if( mAnimateComponentCount % 6 ==0 )
423 mEmitAnimation.Play();
424 mNeedNewAnimation = true;
428 Geometry NewWindowController::CreateMeshGeometry()
430 // Create vertices and specify their color
434 Vector2 textureCoordinates;
438 Vertex vertexData[5] = {
439 { Vector3( 0.0f, 0.0f, 0.5f ), Vector2(0.5f, 0.5f), Vector3(1.0f, 1.0f, 1.0f) },
440 { Vector3( -0.5f, -0.5f, 0.0f ), Vector2(0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.0f) },
441 { Vector3( 0.5f, -0.5f, 0.0f ), Vector2(1.0f, 0.0f), Vector3(1.0f, 1.0f, 0.0f) },
442 { Vector3( -0.5f, 0.5f, 0.0f ), Vector2(0.0f, 1.0f), Vector3(0.0f, 1.0f, 0.0f) },
443 { Vector3( 0.5f, 0.5f, 0.0f ), Vector2(1.0f, 1.0f), Vector3(0.0f, 0.0f, 1.0f) } };
445 Property::Map vertexFormat;
446 vertexFormat["aPosition"] = Property::VECTOR3;
447 vertexFormat["aTexCoord"] = Property::VECTOR2;
448 vertexFormat["aColor"] = Property::VECTOR3;
449 PropertyBuffer vertices = PropertyBuffer::New( vertexFormat );
450 vertices.SetData( vertexData, 5 );
452 // Specify all the faces
453 unsigned short indexData[12] = { 0,1,3,0,2,4,0,3,4,0,2,1 };
455 // Create the geometry object
456 Geometry geometry = Geometry::New();
457 geometry.AddVertexBuffer( vertices );
458 geometry.SetIndexBuffer( &indexData[0], 12 );
463 Dali::Property::Map NewWindowController::CreateColorModifierer()
465 const char* fragmentShader ( DALI_COMPOSE_SHADER (
466 precision highp float;\n
467 uniform vec3 uRGBDelta;\n
468 uniform float uIgnoreAlpha;\n
470 varying mediump vec2 vTexCoord;\n
471 uniform sampler2D sTexture;\n
473 float rand(vec2 co) \n
475 return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n}
478 vec4 color = texture2D(sTexture, vTexCoord); \n
479 // modify the hsv Value
480 color.rgb += uRGBDelta * rand(vTexCoord); \n
481 // if the new vale exceeds one, then decrease it
482 color.rgb -= max(color.rgb*2.0 - vec3(2.0), 0.0);\n
483 // if the new vale drops below zero, then increase it
484 color.rgb -= min(color.rgb*2.0, 0.0);\n
485 gl_FragColor = color; \n
490 Property::Map customShader;
491 customShader[ "fragmentShader" ] = fragmentShader;
492 map[ "shader" ] = customShader;
497 void NewWindowController::NewWindow(void)
499 PositionSize posSize(0, 0, 720, 1280);
500 gApplication.ReplaceWindow(posSize, "NewWindow"); // Generates a new window
503 bool NewWindowController::OnLoseContextButtonClicked( Toolkit::Button button )
505 // Add as an idle callback to avoid ProcessEvents being recursively called.
506 mApplication.AddIdle( MakeCallback( NewWindowController::NewWindow ) );
510 bool NewWindowController::OnTrackTimerTick()
513 const float radius(250.0f);
515 time += EMIT_INTERVAL_IN_MS;
516 float modTime = time / TRACK_DURATION_IN_MS;
517 float angle = 2.0f*Math::PI*modTime;
519 Vector2 position(radius*cosf(angle), radius*-sinf(angle));
520 Vector2 aimPos(radius*2*sinf(angle), radius*2*-cosf(angle));
521 Vector2 direction = aimPos-position;
522 Vector2 stageSize = Stage::GetCurrent().GetSize();
524 SetUpBubbleEmission( stageSize*0.5f+position, direction );
525 SetUpBubbleEmission( stageSize*0.5f+position*0.75f, direction );
526 SetUpBubbleEmission( stageSize*0.5f+position*0.7f, direction );
531 void NewWindowController::OnKeyEvent(const KeyEvent& event)
533 if(event.state == KeyEvent::Down)
535 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
542 void NewWindowController::OnContextLost()
544 printf("Stage reporting context loss\n");
547 void NewWindowController::OnContextRegained()
549 printf("Stage reporting context regain\n");
552 void RunTest(Application& app)
554 gNewWindowController = new NewWindowController(app);
555 app.MainLoop(Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS);
558 // Entry point for Linux & Tizen applications
560 int DALI_EXPORT_API main(int argc, char **argv)
562 gApplication = Application::New(&argc, &argv, DEMO_THEME_PATH);
563 RunTest(gApplication);