Improve application launching speed
[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().TouchedSignal().Connect( this, &ImageViewPixelAreaApp::OnTouch );
78   }
79
80   bool OnTouch( Actor actor, const TouchEvent& touch )
81   {
82     const TouchPoint &point = touch.GetPoint(0);
83     if(point.state == TouchPoint::Down)
84     {
85       mIndex++;
86       for( int i=0; i<3;i++ )
87         for( int j=0; j<3; j++ )
88         {
89           mImageView[i][j].SetImage( IMAGE_PATH[mIndex%NUM_IMAGES] );
90         }
91     }
92     return true;
93   }
94
95   void OnKeyEvent(const KeyEvent& event)
96   {
97     if(event.state == KeyEvent::Down)
98     {
99       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
100       {
101         mApplication.Quit();
102       }
103     }
104   }
105
106 private:
107   Application&  mApplication;
108   Toolkit::ImageView mImageView[3][3];
109   unsigned int mIndex;
110 };
111
112 void RunTest( Application& application )
113 {
114   ImageViewPixelAreaApp test( application );
115
116   application.MainLoop();
117 }
118
119 // Entry point for Linux & Tizen applications
120 //
121 int DALI_EXPORT_API main( int argc, char **argv )
122 {
123   Application application = Application::New( &argc, &argv );
124
125   RunTest( application );
126
127   return 0;
128 }