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