[dali_1.2.61] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / image-view-pixel-area / image-view-pixel-area-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
21 #include "shared/view.h"
22
23 using namespace Dali;
24
25 namespace
26 {
27 const char* BIG_TEST_IMAGE( DEMO_IMAGE_DIR "book-landscape-cover.jpg" );
28 const char* SMALL_TEST_IMAGE( DEMO_IMAGE_DIR "gallery-large-1.jpg" );
29
30 const char * const APPLICATION_TITLE( "Pixel Area & Wrap Mode" );
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 Vector4 ORIGINAL_PIXEL_AREA( -0.5f, -0.5f, 2.f, 2.f );
36 }
37
38 class ImageViewPixelAreaApp : public ConnectionTracker
39 {
40 public:
41   ImageViewPixelAreaApp( Application& application )
42   : mApplication( application ),
43     mIndex(0u)
44   {
45     // Connect to the Application's Init signal
46     mApplication.InitSignal().Connect( this, &ImageViewPixelAreaApp::Create );
47   }
48
49   ~ImageViewPixelAreaApp()
50   {
51     // Nothing to do here;
52   }
53
54 private:
55   // The Init signal is received once (only) during the Application lifetime
56   void Create( Application& application )
57   {
58     // Get a handle to the stage
59     Stage stage = Stage::GetCurrent();
60     stage.KeyEventSignal().Connect(this, &ImageViewPixelAreaApp::OnKeyEvent);
61
62     Toolkit::ToolBar toolBar;
63     Toolkit::Control background;
64     // Creates a default view with a default tool bar.
65     mContent = DemoHelper::CreateView( application,
66                                             background,
67                                             toolBar,
68                                             "",
69                                             TOOLBAR_IMAGE,
70                                             APPLICATION_TITLE );
71
72     // Add a button to switch the scene. (right of toolbar)
73     Toolkit::PushButton switchButton = Toolkit::PushButton::New();
74     switchButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_ICON );
75     switchButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, BUTTON_ICON_SELECTED );
76     switchButton.ClickedSignal().Connect( this, &ImageViewPixelAreaApp::OnButtonClicked );
77     toolBar.AddControl( switchButton,
78                         DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
79                         Toolkit::Alignment::HorizontalRight,
80                         DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
81
82
83     // for testing image WITH automatic atlasing
84     visualPropertyMap[0][ Toolkit::ImageVisual::Property::URL ] =  SMALL_TEST_IMAGE;
85     visualPropertyMap[0][ Toolkit::ImageVisual::Property::DESIRED_WIDTH ] = 500;
86     visualPropertyMap[0][ Toolkit::ImageVisual::Property::DESIRED_HEIGHT ] = 500;
87     visualPropertyMap[0][ Toolkit::ImageVisual::Property::WRAP_MODE_U ] = WrapMode::CLAMP_TO_EDGE;
88     visualPropertyMap[0][ Toolkit::ImageVisual::Property::WRAP_MODE_V ] = WrapMode::MIRRORED_REPEAT;
89     visualPropertyMap[0][ Toolkit::ImageVisual::Property::PIXEL_AREA ] = ORIGINAL_PIXEL_AREA;
90
91     // for testing image WITHOUT automatic atlasing
92     visualPropertyMap[1][ Toolkit::ImageVisual::Property::URL ] =  BIG_TEST_IMAGE;
93     visualPropertyMap[1][ Toolkit::ImageVisual::Property::DESIRED_WIDTH ] = 640;
94     visualPropertyMap[1][ Toolkit::ImageVisual::Property::DESIRED_HEIGHT ] = 720;
95     visualPropertyMap[1][ Toolkit::ImageVisual::Property::WRAP_MODE_U ] = WrapMode::MIRRORED_REPEAT;
96     visualPropertyMap[1][ Toolkit::ImageVisual::Property::WRAP_MODE_V ] = WrapMode::REPEAT;
97     visualPropertyMap[1][ Toolkit::ImageVisual::Property::PIXEL_AREA ] = ORIGINAL_PIXEL_AREA;
98
99     CreateScene(  visualPropertyMap[0] );
100
101     mWrapLabel = Toolkit::TextLabel::New(" Automatic atlasing\n WrapMode: CLAMP_TO_EDGE, MIRRORED_REPEAT");
102     mWrapLabel.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
103     mWrapLabel.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER );
104     mWrapLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
105     mWrapLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
106     mWrapLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Color::WHITE );
107     mContent.Add( mWrapLabel );
108
109     mPixelAreaLabel = Toolkit::TextLabel::New( " Use ImageVisual::Property::PIXEL_AREA\n " );
110     mPixelAreaLabel.SetParentOrigin( ParentOrigin::TOP_CENTER );
111     mPixelAreaLabel.SetAnchorPoint(AnchorPoint::BOTTOM_CENTER );
112     mPixelAreaLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
113     mPixelAreaLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
114     mPixelAreaLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Color::WHITE );
115     mWrapLabel.Add( mPixelAreaLabel );
116   }
117
118   void CreateScene( const Property::Value& propertyMap )
119   {
120     for( int i=0; i<3;i++ )
121       for( int j=0; j<3; j++ )
122       {
123         mImageView[i][j] = Toolkit::ImageView::New();
124         mImageView[i][j].SetProperty( Toolkit::ImageView::Property::IMAGE, propertyMap );
125         mImageView[i][j].SetPosition( 50.f*(i-1), 50.f*(j-1) );
126       }
127
128     mImageView[1][1].SetParentOrigin( ParentOrigin::CENTER );
129     mImageView[1][1].SetAnchorPoint(AnchorPoint::CENTER );
130     mImageView[1][1].SetScale( 1.f/3.f );
131     mContent.Add( mImageView[1][1] );
132
133     mImageView[0][0].SetParentOrigin( ParentOrigin::TOP_LEFT );
134     mImageView[0][0].SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT );
135     mImageView[0][0].SetPosition( -50.f, -50.f );
136     mImageView[1][1].Add( mImageView[0][0] );
137
138     mImageView[1][0].SetParentOrigin( ParentOrigin::TOP_CENTER );
139     mImageView[1][0].SetAnchorPoint(AnchorPoint::BOTTOM_CENTER );
140     mImageView[1][1].Add( mImageView[1][0] );
141
142     mImageView[2][0].SetParentOrigin( ParentOrigin::TOP_RIGHT );
143     mImageView[2][0].SetAnchorPoint(AnchorPoint::BOTTOM_LEFT );
144     mImageView[1][1].Add( mImageView[2][0] );
145
146     mImageView[0][1].SetParentOrigin( ParentOrigin::CENTER_LEFT );
147     mImageView[0][1].SetAnchorPoint(AnchorPoint::CENTER_RIGHT );
148     mImageView[1][1].Add( mImageView[0][1] );
149
150     mImageView[2][1].SetParentOrigin( ParentOrigin::CENTER_RIGHT );
151     mImageView[2][1].SetAnchorPoint(AnchorPoint::CENTER_LEFT );
152     mImageView[1][1].Add( mImageView[2][1] );
153
154     mImageView[0][2].SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
155     mImageView[0][2].SetAnchorPoint(AnchorPoint::TOP_RIGHT );
156     mImageView[1][1].Add( mImageView[0][2] );
157
158     mImageView[1][2].SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
159     mImageView[1][2].SetAnchorPoint(AnchorPoint::TOP_CENTER );
160     mImageView[1][1].Add( mImageView[1][2] );
161
162     mImageView[2][2].SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
163     mImageView[2][2].SetAnchorPoint(AnchorPoint::TOP_LEFT );
164     mImageView[1][1].Add( mImageView[2][2] );
165
166   }
167
168   bool OnButtonClicked( Toolkit::Button button )
169     {
170       if( mAnimation )
171       {
172         mAnimation.Stop();
173         mAnimation.Clear();
174       }
175
176       mIndex = ( mIndex+1 ) % 4;
177       if( mIndex%2 == 0 )
178       {
179         // switch to the other image
180         // set the pixel area to image visual, the pixel area property is registered on the renderer
181         mContent.Remove( mImageView[1][1] );
182         CreateScene(  visualPropertyMap[mIndex/2] );
183         if( mIndex == 0 )
184         {
185           mWrapLabel.SetProperty( Toolkit::TextLabel::Property::TEXT," Automatic atlasing\n WrapMode: CLAMP_TO_EDGE, MIRRORED_REPEAT");
186         }
187         else
188         {
189           mWrapLabel.SetProperty( Toolkit::TextLabel::Property::TEXT," No atlasing\n WrapMode: MIRRORED_REPEAT, REPEAT");
190         }
191         mPixelAreaLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, " Use ImageVisual::Property::PIXEL_AREA\n " );
192       }
193       else
194       {
195         // animate the pixel area property on image view,
196         // the animatable pixel area property is registered on the actor, which overwrites the property on the renderer
197         mAnimation = Animation::New( 10.f );
198         float relativeSubSize = 0.33;
199         for( int i=0; i<3;i++ )
200           for( int j=0; j<3; j++ )
201           {
202             mImageView[i][j].SetProperty( Toolkit::ImageView::Property::PIXEL_AREA, ORIGINAL_PIXEL_AREA );
203             mAnimation.AnimateTo( Property(mImageView[i][j], Toolkit::ImageView::Property::PIXEL_AREA),
204                 Vector4( relativeSubSize*i, relativeSubSize*j, relativeSubSize, relativeSubSize ),
205                 AlphaFunction::BOUNCE );
206           }
207         mAnimation.SetLooping( true );
208         mAnimation.Play();
209
210         mPixelAreaLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, " Animate ImageView::Property::PIXEL_AREA \n     (Overwrite the ImageVisual property) " );
211       }
212       return true;
213     }
214
215   void OnKeyEvent(const KeyEvent& event)
216   {
217     if(event.state == KeyEvent::Down)
218     {
219       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
220       {
221         mApplication.Quit();
222       }
223     }
224   }
225
226 private:
227   Application&  mApplication;
228   Layer mContent;
229   Toolkit::ImageView mImageView[3][3];
230   Property::Map visualPropertyMap[2];
231   Toolkit::TextLabel mWrapLabel;
232   Toolkit::TextLabel mPixelAreaLabel;
233   Animation mAnimation;
234   unsigned int mIndex;
235 };
236
237 int DALI_EXPORT_API main( int argc, char **argv )
238 {
239   Application application = Application::New( &argc, &argv );
240   ImageViewPixelAreaApp test( application );
241   application.MainLoop();
242   return 0;
243 }