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