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