Changed DALI_*_DIR to DEMO_*_DIR
[platform/core/uifw/dali-demo.git] / examples / image-view-alpha-blending / image-view-alpha-blending-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 class ImageViewAlphaBlendApp : public ConnectionTracker
23 {
24 public:
25   ImageViewAlphaBlendApp( Application& application )
26   : mApplication( application ),
27     mIndex(0u)
28   {
29     // Connect to the Application's Init signal
30     mApplication.InitSignal().Connect( this, &ImageViewAlphaBlendApp::Create );
31   }
32
33   ~ImageViewAlphaBlendApp()
34   {
35     // Nothing to do here;
36   }
37
38 private:
39   // The Init signal is received once (only) during the Application lifetime
40   void Create( Application& application )
41   {
42     // Get a handle to the stage
43     Stage stage = Stage::GetCurrent();
44     stage.KeyEventSignal().Connect(this, &ImageViewAlphaBlendApp::OnKeyEvent);
45
46     Vector4 green0 = Vector4( 0.f, 1.f, 0.f, 0.25f );
47     Vector4 green1 = Vector4( 0.f, 0.25f, 0.f, 0.25f );
48     BufferImage redGreen0 = CreateBufferImage( Color::RED, green0 );
49     BufferImage redGreen1 = CreateBufferImage( Color::RED, green1 );
50     ResourceImage testImage = ResourceImage::New( DEMO_IMAGE_DIR "gallery-large-20.jpg" );
51     float imageSize = 512.f;
52
53     Toolkit::ImageView imageView0 = Toolkit::ImageView::New( testImage );
54     imageView0.SetSize(imageSize, imageSize);
55     imageView0.SetParentOrigin( ParentOrigin::CENTER );
56     imageView0.SetY( -imageSize*0.5f );
57     stage.Add(imageView0);
58     Toolkit::ImageView imageView1 = Toolkit::ImageView::New( redGreen0 );
59     imageView1.SetParentOrigin( ParentOrigin::CENTER );
60     imageView1.SetSize(imageSize, imageSize);
61     imageView0.Add(imageView1);
62
63     Toolkit::ImageView imageView2 = Toolkit::ImageView::New( testImage);
64     imageView2.SetSize(imageSize, imageSize);
65     imageView2.SetParentOrigin( ParentOrigin::CENTER );
66     imageView2.SetY( imageSize*0.5f );
67     stage.Add(imageView2);
68     Toolkit::ImageView imageView3 = Toolkit::ImageView::New( redGreen1);
69     imageView3.SetParentOrigin( ParentOrigin::CENTER );
70     imageView3.SetSize(imageSize, imageSize);
71     imageView2.Add(imageView3);
72
73     imageView2.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
74     imageView3.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
75
76     Animation animation = Animation::New( 10.f );
77     animation.AnimateTo(Property(imageView0, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod( 2.f, 8.f ));
78     animation.AnimateTo(Property(imageView2, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod( 2.f, 8.f ));
79     animation.SetLooping( true );
80     animation.Play();
81   }
82
83   void OnKeyEvent(const KeyEvent& event)
84   {
85     if(event.state == KeyEvent::Down)
86     {
87       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
88       {
89         mApplication.Quit();
90       }
91     }
92   }
93
94   BufferImage CreateBufferImage( const Vector4& color1, const Vector4& color2 )
95   {
96     BufferImage image = BufferImage::New( 2, 1, Pixel::RGBA8888 );
97     PixelBuffer* pixelBuffer = image.GetBuffer();
98     pixelBuffer[0]=0xFF * color1.x;
99     pixelBuffer[1]=0xFF * color1.y;
100     pixelBuffer[2]=0xFF * color1.z;
101     pixelBuffer[3]=0xFF * color1.w;
102     pixelBuffer[4]=0xFF * color2.x;
103     pixelBuffer[5]=0xFF * color2.y;
104     pixelBuffer[6]=0xFF * color2.z;
105     pixelBuffer[7]=0xFF * color2.w;
106     image.Update();
107     return image;
108   }
109
110
111 private:
112   Application&  mApplication;
113   unsigned int mIndex;
114 };
115
116 void RunTest( Application& application )
117 {
118   ImageViewAlphaBlendApp test( application );
119
120   application.MainLoop();
121 }
122
123 // Entry point for Linux & Tizen applications
124 //
125 int main( int argc, char **argv )
126 {
127   Application application = Application::New( &argc, &argv );
128
129   RunTest( application );
130
131   return 0;
132 }