2bd35183ce95a67131849b4878b0150baa8bc024
[platform/core/uifw/dali-demo.git] / examples / image-view-url / image-view-url-example.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
20 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
21 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
22
23 #include "shared/view.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29 const char* BIG_TEST_IMAGE( DEMO_IMAGE_DIR "book-landscape-cover.jpg" );
30
31 const char * const APPLICATION_TITLE( "Image View URL" );
32 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
33 const char * const BUTTON_ICON( DEMO_IMAGE_DIR "icon-change.png" );
34 const char * const BUTTON_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
35
36 const char* FILTER_FRAGMENT_SOURCE =
37 {
38  "precision highp float;\n"
39  "varying mediump vec2 vTexCoord;\n"
40  "uniform sampler2D sTexture;\n"
41  "uniform mediump float uDelta;\n"
42  "void main()\n"
43  "{\n"
44  "  vec4 color = vec4(0.0);\n"
45  "  vec2 texCoord = vTexCoord * 2. - 1.;\n"
46  "  mat2 rotation = mat2(cos(uDelta), -sin(uDelta), sin(uDelta), cos(uDelta));"
47  "  texCoord = (rotation * texCoord) * .5 + .5;\n"
48  "  color += texture2D( sTexture, texCoord );\n"
49  "  gl_FragColor = color;\n"
50  "}\n"
51 };
52
53 const char* DELTA_UNIFORM_NAME = "uDelta";
54
55 const Vector2 TARGET_SIZE(800.f, 800.f);
56 }
57
58 class ImageViewUrlApp : public ConnectionTracker
59 {
60 public:
61   ImageViewUrlApp( Application& application )
62   : mApplication( application )
63   {
64     // Connect to the Application's Init signal
65     mApplication.InitSignal().Connect( this, &ImageViewUrlApp::Create );
66   }
67
68   ~ImageViewUrlApp() = default;
69
70 private:
71   // The Init signal is received once (only) during the Application lifetime
72   void Create( Application& application )
73   {
74     // Get a handle to the stage
75     Stage stage = Stage::GetCurrent();
76     stage.KeyEventSignal().Connect(this, &ImageViewUrlApp::OnKeyEvent);
77
78     Toolkit::ToolBar toolBar;
79     Toolkit::Control background;
80     // Creates a default view with a default tool bar.
81     mContent = DemoHelper::CreateView( application,
82                                             background,
83                                             toolBar,
84                                             "",
85                                             TOOLBAR_IMAGE,
86                                             APPLICATION_TITLE );
87
88     // Add a button to switch the scene. (right of toolbar)
89     Toolkit::PushButton switchButton = Toolkit::PushButton::New();
90     switchButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_ICON );
91     switchButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, BUTTON_ICON_SELECTED );
92     switchButton.ClickedSignal().Connect( this, &ImageViewUrlApp::OnButtonClicked );
93     toolBar.AddControl( switchButton,
94                         DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
95                         Toolkit::Alignment::HorizontalRight,
96                         DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
97
98     CreateRenderTask(  );
99     CreateScene( );
100   }
101
102   void CreateRenderTask()
103   {
104     auto rootActor = Stage::GetCurrent().GetRootLayer();
105
106     auto cameraActor = CameraActor::New(TARGET_SIZE);
107     cameraActor.SetParentOrigin(ParentOrigin::CENTER);
108     cameraActor.SetInvertYAxis(true);
109     rootActor.Add(cameraActor);
110
111     {
112       // create actor to render input with applied shader
113       mActorForInput = Toolkit::ImageView::New(BIG_TEST_IMAGE);
114       mActorForInput.SetParentOrigin(ParentOrigin::CENTER);
115       mActorForInput.SetSize(TARGET_SIZE);
116       Property::Map customShader;
117       customShader[Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FILTER_FRAGMENT_SOURCE;
118       Property::Map visualMap;
119       visualMap.Insert(Toolkit::DevelVisual::Property::SHADER, customShader);
120       mActorForInput.SetProperty(Toolkit::ImageView::Property::IMAGE, visualMap);
121
122       mDeltaPropertyIndex = mActorForInput.RegisterProperty(DELTA_UNIFORM_NAME, 0.f);
123
124       rootActor.Add(mActorForInput);
125
126       RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
127
128       // perform a horizontal blur targeting the internal buffer
129       auto renderTask = taskList.CreateTask();
130       renderTask.SetRefreshRate(RenderTask::REFRESH_ALWAYS);
131       renderTask.SetSourceActor(mActorForInput);
132       renderTask.SetExclusive(true);
133       renderTask.SetInputEnabled(false);
134       renderTask.SetClearColor(Vector4(1.,0.,0.,1.));
135       renderTask.SetClearEnabled(true);
136       renderTask.SetCameraActor(cameraActor);
137
138       mOutputTexture = Texture::New(TextureType::TEXTURE_2D,
139                                     Pixel::RGB888,
140                                     unsigned(TARGET_SIZE.width),
141                                     unsigned(TARGET_SIZE.height));
142       auto framebuffer = FrameBuffer::New(TARGET_SIZE.width, TARGET_SIZE.height, Pixel::RGB888);
143       framebuffer.AttachColorTexture(mOutputTexture);
144
145       renderTask.SetFrameBuffer(framebuffer);
146     }
147     {
148       // animate the shader uniform
149       mAnimation = Animation::New(10.f);
150
151       mActorForInput.SetProperty( mDeltaPropertyIndex, 0.f );
152       mAnimation.AnimateTo( Property( mActorForInput, mDeltaPropertyIndex ), Math::PI * 2.f );
153       mAnimation.SetLooping(true);
154       mAnimation.Play();
155     }
156   }
157
158   void CreateScene( )
159   {
160     auto url = Dali::Toolkit::TextureManager::AddTexture(mOutputTexture);
161     mImageView = Toolkit::ImageView::New(url);
162
163     mImageView.SetParentOrigin(ParentOrigin::CENTER);
164     mImageView.SetAnchorPoint(AnchorPoint::CENTER);
165     mContent.Add(mImageView);
166   }
167
168   bool OnButtonClicked(Toolkit::Button button)
169   {
170     if(mAnimation.GetState() == Animation::State::PLAYING)
171     {
172       mAnimation.Pause();
173     }
174     else
175     {
176       mAnimation.Play();
177     }
178     return true;
179   }
180
181   void OnKeyEvent(const KeyEvent& event)
182   {
183     if(event.state == KeyEvent::Down)
184     {
185       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
186       {
187         mApplication.Quit();
188       }
189     }
190   }
191
192 private:
193   Application&  mApplication;
194   Layer mContent;
195   Toolkit::ImageView mImageView;
196   Animation mAnimation;
197   Actor mActorForInput;
198   Property::Index mDeltaPropertyIndex;
199   Texture mOutputTexture;
200 };
201
202 void RunTest( Application& application )
203 {
204   ImageViewUrlApp test( application );
205
206   application.MainLoop();
207 }
208
209 // Entry point for Linux & Tizen applications
210 //
211 int DALI_EXPORT_API main( int argc, char **argv )
212 {
213   Application application = Application::New( &argc, &argv );
214
215   RunTest( application );
216
217   return 0;
218 }