[dali_1.1.37] 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
20 using namespace Dali;
21
22 namespace
23 {
24 const char* IMAGE_PATH[] = { DEMO_IMAGE_DIR "gallery-large-7.jpg",
25                              DEMO_IMAGE_DIR "gallery-large-12.jpg",
26                              DEMO_IMAGE_DIR "gallery-large-18.jpg" };
27
28 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
29 }
30
31 class ImageViewPixelAreaApp : public ConnectionTracker
32 {
33 public:
34   ImageViewPixelAreaApp( Application& application )
35   : mApplication( application ),
36     mIndex(0u)
37   {
38     // Connect to the Application's Init signal
39     mApplication.InitSignal().Connect( this, &ImageViewPixelAreaApp::Create );
40   }
41
42   ~ImageViewPixelAreaApp()
43   {
44     // Nothing to do here;
45   }
46
47 private:
48   // The Init signal is received once (only) during the Application lifetime
49   void Create( Application& application )
50   {
51     // Get a handle to the stage
52     Stage stage = Stage::GetCurrent();
53     stage.KeyEventSignal().Connect(this, &ImageViewPixelAreaApp::OnKeyEvent);
54
55     ImageDimensions size( 510, 510 );
56     float subSize = 170.f;
57     float relativeSubSize = 0.33;
58     Animation animation = Animation::New( 10.f );
59     for( int i=0; i<3;i++ )
60       for( int j=0; j<3; j++ )
61       {
62         mImageView[i][j] = Toolkit::ImageView::New( IMAGE_PATH[mIndex], size );
63         mImageView[i][j].SetParentOrigin( ParentOrigin::CENTER );
64         mImageView[i][j].SetAnchorPoint(AnchorPoint::CENTER );
65         mImageView[i][j].SetPosition( subSize*(i-1)*1.1f, subSize*(j-1)*1.1f );
66         mImageView[i][j].SetSize( subSize, subSize );
67         stage.Add(mImageView[i][j]);
68
69         animation.AnimateTo( Property(mImageView[i][j], Toolkit::ImageView::Property::PIXEL_AREA),
70                              Vector4( relativeSubSize*i, relativeSubSize*j, relativeSubSize, relativeSubSize ),
71                              AlphaFunction::BOUNCE );
72       }
73     animation.SetLooping( true );
74     animation.Play();
75
76     // Respond to a click anywhere on the stage
77     stage.GetRootLayer().TouchSignal().Connect( this, &ImageViewPixelAreaApp::OnTouch );
78   }
79
80   bool OnTouch( Actor actor, const TouchData& touch )
81   {
82     if( touch.GetState( 0 ) == PointState::DOWN )
83     {
84       mIndex++;
85       for( int i=0; i<3;i++ )
86         for( int j=0; j<3; j++ )
87         {
88           mImageView[i][j].SetImage( IMAGE_PATH[mIndex%NUM_IMAGES] );
89         }
90     }
91     return true;
92   }
93
94   void OnKeyEvent(const KeyEvent& event)
95   {
96     if(event.state == KeyEvent::Down)
97     {
98       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
99       {
100         mApplication.Quit();
101       }
102     }
103   }
104
105 private:
106   Application&  mApplication;
107   Toolkit::ImageView mImageView[3][3];
108   unsigned int mIndex;
109 };
110
111 void RunTest( Application& application )
112 {
113   ImageViewPixelAreaApp test( application );
114
115   application.MainLoop();
116 }
117
118 // Entry point for Linux & Tizen applications
119 //
120 int DALI_EXPORT_API main( int argc, char **argv )
121 {
122   Application application = Application::New( &argc, &argv );
123
124   RunTest( application );
125
126   return 0;
127 }