[dali_1.1.30] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / native-image-source / native-image-source-example.cpp
1 /*
2  * Copyright (c) 2016 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/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Toolkit;
23
24 namespace
25 {
26   const std::string JPG_FILENAME = DEMO_IMAGE_DIR "gallery-medium-4.jpg";
27 }
28
29 // This example shows how to create and use a NativeImageSource as the target of the render task.
30 //
31 class NativeImageSourceController : public ConnectionTracker
32 {
33 public:
34
35   NativeImageSourceController( Application& application )
36   : mApplication( application )
37   {
38     // Connect to the Application's Init signal
39     mApplication.InitSignal().Connect( this, &NativeImageSourceController::Create );
40   }
41
42   ~NativeImageSourceController()
43   {
44     // Nothing to do here;
45   }
46
47   // The Init signal is received once (only) during the Application lifetime
48   void Create( Application& application )
49   {
50     // Get a handle to the stage
51     Stage stage = Stage::GetCurrent();
52     stage.SetBackgroundColor( Color::WHITE );
53     stage.KeyEventSignal().Connect(this, &NativeImageSourceController::OnKeyEvent);
54
55     mButtonRefreshAlways = PushButton::New();
56     mButtonRefreshAlways.SetTogglableButton( true );
57     mButtonRefreshAlways.SetSelected( true );
58     mButtonRefreshAlways.SetLabelText( "Refresh ALWAYS" );
59     mButtonRefreshAlways.SetParentOrigin( ParentOrigin::TOP_LEFT );
60     mButtonRefreshAlways.SetAnchorPoint( AnchorPoint::TOP_LEFT );
61     mButtonRefreshAlways.StateChangedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
62     stage.Add( mButtonRefreshAlways );
63
64     mButtonRefreshOnce = PushButton::New();
65     mButtonRefreshOnce.SetLabelText( "Refresh ONCE" );
66     mButtonRefreshOnce.SetParentOrigin( ParentOrigin::TOP_RIGHT );
67     mButtonRefreshOnce.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
68     mButtonRefreshOnce.ClickedSignal().Connect( this, &NativeImageSourceController::OnButtonSelected );
69     stage.Add( mButtonRefreshOnce);
70
71     CreateScene();
72   }
73
74   bool CreateScene()
75   {
76     Stage stage = Stage::GetCurrent();
77     Vector2 stageSize = stage.GetSize();
78
79     float buttonHeight = 100.f;
80     mButtonRefreshAlways.SetSize( stageSize.x / 2.f, buttonHeight );
81     mButtonRefreshOnce.SetSize( stageSize.x / 2.f, buttonHeight );
82
83     Vector2 imageSize( stageSize.x, (stageSize.y-buttonHeight)/2.f );
84
85     // Create the native image source
86     NativeImageSourcePtr nativeImageSourcePtr = NativeImageSource::New( imageSize.width, imageSize.height, NativeImageSource::COLOR_DEPTH_DEFAULT );
87
88     // Create a image view as source actor to be renderer to the native image source
89     Actor sourceActor = ImageView::New(JPG_FILENAME);
90     sourceActor.SetParentOrigin( ParentOrigin::CENTER);
91     sourceActor.SetAnchorPoint( AnchorPoint::CENTER );
92     sourceActor.SetY( - (imageSize.height-buttonHeight)/2.f );
93     stage.Add( sourceActor );
94
95     Animation animation = Animation::New(2.f);
96     Degree relativeRotationDegrees(90.0f);
97     Radian relativeRotationRadians(relativeRotationDegrees);
98     animation.AnimateTo( Property( sourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(0.f, 0.5f));
99     animation.AnimateBy( Property( sourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(0.5f, 0.5f));
100     animation.AnimateBy( Property( sourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(1.f, 0.5f));
101     animation.AnimateBy( Property( sourceActor, Actor::Property::ORIENTATION ), Quaternion( relativeRotationRadians, Vector3::ZAXIS ), AlphaFunction::LINEAR, TimePeriod(1.5f, 0.5f));
102     animation.SetLooping(true);
103     animation.Play();
104
105     // create a offscreen renderer task to render content into the native image source
106     FrameBufferImage targetBuffer = FrameBufferImage::New( *nativeImageSourcePtr );
107
108     CameraActor cameraActor = CameraActor::New(imageSize);
109     cameraActor.SetParentOrigin(ParentOrigin::TOP_CENTER);
110     cameraActor.SetParentOrigin( AnchorPoint::TOP_CENTER );
111     cameraActor.SetY( buttonHeight + imageSize.height/2.f );
112     stage.Add(cameraActor);
113
114     RenderTaskList taskList = stage.GetRenderTaskList();
115     mOffscreenRenderTask = taskList.CreateTask();
116     mOffscreenRenderTask.SetSourceActor( sourceActor );
117     mOffscreenRenderTask.SetClearColor( Color::WHITE );
118     mOffscreenRenderTask.SetClearEnabled(true);
119     mOffscreenRenderTask.SetCameraActor(cameraActor);
120     mOffscreenRenderTask.GetCameraActor().SetInvertYAxis(true);
121     mOffscreenRenderTask.SetTargetFrameBuffer( targetBuffer );
122     mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
123
124     // Display the native image on the screen
125     NativeImage nativeImage = NativeImage::New( *nativeImageSourcePtr );
126     ImageView nativeImageView = ImageView::New( nativeImage );
127     nativeImageView.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
128     nativeImageView.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
129     stage.Add( nativeImageView );
130
131     TextLabel textLabel1 = TextLabel::New( "Resource Image" );
132     textLabel1.SetParentOrigin( ParentOrigin::TOP_CENTER );
133     textLabel1.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
134     nativeImageView.Add( textLabel1 );
135
136     TextLabel textLabel2 = TextLabel::New( "Native Image" );
137     textLabel2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
138     textLabel2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
139     nativeImageView.Add( textLabel2 );
140
141     return false;
142   }
143
144   bool OnButtonSelected( Toolkit::Button button )
145   {
146     Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
147     if( pushButton == mButtonRefreshAlways )
148     {
149       if( mButtonRefreshAlways.IsSelected() )
150       {
151         mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
152       }
153       else
154       {
155         mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
156       }
157     }
158     else if( pushButton == mButtonRefreshOnce )
159     {
160       if( mButtonRefreshAlways.IsSelected() )
161       {
162         mButtonRefreshAlways.SetSelected( false );
163       }
164       mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
165     }
166
167     return true;
168   }
169
170   void OnKeyEvent(const KeyEvent& event)
171   {
172     if(event.state == KeyEvent::Down)
173     {
174       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
175       {
176         mApplication.Quit();
177       }
178     }
179   }
180
181 private:
182   Application&  mApplication;
183   RenderTask mOffscreenRenderTask;
184   PushButton mButtonRefreshAlways;
185   PushButton mButtonRefreshOnce;
186
187 };
188
189 void RunTest( Application& application )
190 {
191   NativeImageSourceController test( application );
192
193   application.MainLoop();
194 }
195
196 // Entry point for Linux & Tizen applications
197 //
198 int main( int argc, char **argv )
199 {
200   Application application = Application::New( &argc, &argv );
201
202   RunTest( application );
203
204   return 0;
205 }