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/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
22 using namespace Toolkit;
26 const std::string JPG_FILENAME = DEMO_IMAGE_DIR "gallery-medium-4.jpg";
29 // This example shows how to create and use a NativeImageSource as the target of the render task.
31 class NativeImageSourceController : public ConnectionTracker
35 NativeImageSourceController( Application& application )
36 : mApplication( application )
38 // Connect to the Application's Init signal
39 mApplication.InitSignal().Connect( this, &NativeImageSourceController::Create );
42 ~NativeImageSourceController()
44 // Nothing to do here;
47 // The Init signal is received once (only) during the Application lifetime
48 void Create( Application& application )
50 // Get a handle to the stage
51 Stage stage = Stage::GetCurrent();
52 stage.SetBackgroundColor( Color::WHITE );
53 stage.KeyEventSignal().Connect(this, &NativeImageSourceController::OnKeyEvent);
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 );
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);
76 Stage stage = Stage::GetCurrent();
77 Vector2 stageSize = stage.GetSize();
79 float buttonHeight = 100.f;
80 mButtonRefreshAlways.SetSize( stageSize.x / 2.f, buttonHeight );
81 mButtonRefreshOnce.SetSize( stageSize.x / 2.f, buttonHeight );
83 Vector2 imageSize( stageSize.x, (stageSize.y-buttonHeight)/2.f );
85 // Create the native image source
86 NativeImageSourcePtr nativeImageSourcePtr = NativeImageSource::New( imageSize.width, imageSize.height, NativeImageSource::COLOR_DEPTH_DEFAULT );
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 );
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);
105 // create a offscreen renderer task to render content into the native image source
106 FrameBufferImage targetBuffer = FrameBufferImage::New( *nativeImageSourcePtr );
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);
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 );
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 );
131 TextLabel textLabel1 = TextLabel::New( "Resource Image" );
132 textLabel1.SetParentOrigin( ParentOrigin::TOP_CENTER );
133 textLabel1.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
134 nativeImageView.Add( textLabel1 );
136 TextLabel textLabel2 = TextLabel::New( "Native Image" );
137 textLabel2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
138 textLabel2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
139 nativeImageView.Add( textLabel2 );
144 bool OnButtonSelected( Toolkit::Button button )
146 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
147 if( pushButton == mButtonRefreshAlways )
149 if( mButtonRefreshAlways.IsSelected() )
151 mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
155 mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
158 else if( pushButton == mButtonRefreshOnce )
160 if( mButtonRefreshAlways.IsSelected() )
162 mButtonRefreshAlways.SetSelected( false );
164 mOffscreenRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
170 void OnKeyEvent(const KeyEvent& event)
172 if(event.state == KeyEvent::Down)
174 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
182 Application& mApplication;
183 RenderTask mOffscreenRenderTask;
184 PushButton mButtonRefreshAlways;
185 PushButton mButtonRefreshOnce;
189 void RunTest( Application& application )
191 NativeImageSourceController test( application );
193 application.MainLoop();
196 // Entry point for Linux & Tizen applications
198 int DALI_EXPORT_API main( int argc, char **argv )
200 Application application = Application::New( &argc, &argv );
202 RunTest( application );