Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / image-view-alpha-blending / image-view-alpha-blending-example.cpp
1 /*
2  * Copyright (c) 2020 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 <dali-toolkit/devel-api/image-loader/texture-manager.h>
20 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26 const char* const IMAGE_PATH(DEMO_IMAGE_DIR "gallery-large-20.jpg");
27 }
28
29 class ImageViewAlphaBlendApp : public ConnectionTracker
30 {
31 public:
32   ImageViewAlphaBlendApp(Application& application)
33   : mApplication(application),
34     mIndex(0u)
35   {
36     // Connect to the Application's Init signal
37     mApplication.InitSignal().Connect(this, &ImageViewAlphaBlendApp::Create);
38   }
39
40   ~ImageViewAlphaBlendApp()
41   {
42     // Nothing to do here;
43   }
44
45 private:
46   // The Init signal is received once (only) during the Application lifetime
47   void Create(Application& application)
48   {
49     // Get a handle to the window
50     Window window = application.GetWindow();
51     window.KeyEventSignal().Connect(this, &ImageViewAlphaBlendApp::OnKeyEvent);
52
53     auto  green0    = Vector4(0.f, 1.f, 0.f, 0.25f);
54     auto  green1    = Vector4(0.f, 0.25f, 0.f, 0.25f);
55     auto  redGreen0 = CreateTexture(Color::RED, green0);
56     auto  redGreen1 = CreateTexture(Color::RED, green1);
57     float imageSize = 512.f;
58
59     Toolkit::ImageView imageView0 = CreateImageView(IMAGE_PATH);
60     imageView0.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
61     imageView0.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
62     imageView0.SetProperty(Actor::Property::POSITION_Y, -imageSize * 0.5f);
63     window.Add(imageView0);
64     Toolkit::ImageView imageView1 = CreateImageView(redGreen0);
65     imageView1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
66     imageView1.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
67     imageView0.Add(imageView1);
68
69     Toolkit::ImageView imageView2 = CreateImageView(IMAGE_PATH);
70     imageView2.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
71     imageView2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
72     imageView2.SetProperty(Actor::Property::POSITION_Y, imageSize * 0.5f);
73     window.Add(imageView2);
74     Toolkit::ImageView imageView3 = CreateImageView(redGreen1);
75     imageView3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
76     imageView3.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
77     imageView2.Add(imageView3);
78
79     imageView2.SetProperty(Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true);
80     imageView3.SetProperty(Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true);
81
82     Animation animation = Animation::New(10.f);
83     animation.AnimateTo(Property(imageView0, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod(2.f, 8.f));
84     animation.AnimateTo(Property(imageView2, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod(2.f, 8.f));
85     animation.SetLooping(true);
86     animation.Play();
87   }
88
89   void OnKeyEvent(const KeyEvent& event)
90   {
91     if(event.GetState() == KeyEvent::DOWN)
92     {
93       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
94       {
95         mApplication.Quit();
96       }
97     }
98   }
99
100   std::string CreateTexture(const Vector4& color1, const Vector4& color2)
101   {
102     const auto width       = 2u;
103     const auto height      = 1u;
104     auto       size        = width * height * 4;
105     auto       pixelBuffer = new unsigned char[size];
106     pixelBuffer[0]         = 0xFF * color1.x;
107     pixelBuffer[1]         = 0xFF * color1.y;
108     pixelBuffer[2]         = 0xFF * color1.z;
109     pixelBuffer[3]         = 0xFF * color1.w;
110     pixelBuffer[4]         = 0xFF * color2.x;
111     pixelBuffer[5]         = 0xFF * color2.y;
112     pixelBuffer[6]         = 0xFF * color2.z;
113     pixelBuffer[7]         = 0xFF * color2.w;
114
115     auto pixelData = PixelData::New(pixelBuffer, size, width, height, Pixel::RGBA8888, PixelData::ReleaseFunction::DELETE_ARRAY);
116     auto texture   = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
117     texture.Upload(pixelData);
118
119     return Toolkit::TextureManager::AddTexture(texture);
120   }
121
122   template<typename TextT>
123   Toolkit::ImageView CreateImageView(TextT&& filename)
124   {
125     auto imageView = Toolkit::ImageView::New();
126
127     Property::Map propertyMap;
128     propertyMap.Insert(Toolkit::ImageVisual::Property::URL, std::forward<TextT>(filename));
129     propertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
130     propertyMap.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, Toolkit::DevelVisual::FILL);
131     imageView.SetProperty(Toolkit::ImageView::Property::IMAGE, propertyMap);
132
133     return imageView;
134   }
135
136 private:
137   Application& mApplication;
138   unsigned int mIndex;
139 };
140
141 int DALI_EXPORT_API main(int argc, char** argv)
142 {
143   Application            application = Application::New(&argc, &argv);
144   ImageViewAlphaBlendApp test(application);
145   application.MainLoop();
146   return 0;
147 }