[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / image-view-alpha-blending / image-view-alpha-blending-example.cpp
1 /*
2  * Copyright (c) 2022 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     const Vector2 windowSize = window.GetSize();
53     const bool portraitOrientation = windowSize.width < windowSize.height;
54     const Property::Index positionProperty = portraitOrientation ? Actor::Property::POSITION_Y : Actor::Property::POSITION_X;
55
56     auto  green0    = Vector4(0.f, 1.f, 0.f, 0.25f);
57     auto  green1    = Vector4(0.f, 0.25f, 0.f, 0.25f);
58     auto  redGreen0 = CreateTexture(Color::RED, green0);
59     auto  redGreen1 = CreateTexture(Color::RED, green1);
60     float imageSize = 512.f;
61
62     Toolkit::ImageView imageView0 = CreateImageView(IMAGE_PATH);
63     imageView0.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
64     imageView0.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
65     imageView0.SetProperty(positionProperty, -imageSize * 0.5f);
66     window.Add(imageView0);
67     Toolkit::ImageView imageView1 = CreateImageView(redGreen0);
68     imageView1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
69     imageView1.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
70     imageView0.Add(imageView1);
71
72     Toolkit::ImageView imageView2 = CreateImageView(IMAGE_PATH);
73     imageView2.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
74     imageView2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
75     imageView2.SetProperty(positionProperty, imageSize * 0.5f);
76     window.Add(imageView2);
77     Toolkit::ImageView imageView3 = CreateImageView(redGreen1);
78     imageView3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
79     imageView3.SetProperty(Actor::Property::SIZE, Vector2(imageSize, imageSize));
80     imageView2.Add(imageView3);
81
82     imageView2.SetProperty(Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true);
83     imageView3.SetProperty(Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true);
84
85     Animation animation = Animation::New(10.f);
86     animation.AnimateTo(Property(imageView0, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod(2.f, 8.f));
87     animation.AnimateTo(Property(imageView2, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 0.0f), AlphaFunction::BOUNCE, TimePeriod(2.f, 8.f));
88     animation.SetLooping(true);
89     animation.Play();
90   }
91
92   void OnKeyEvent(const KeyEvent& event)
93   {
94     if(event.GetState() == KeyEvent::DOWN)
95     {
96       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
97       {
98         mApplication.Quit();
99       }
100     }
101   }
102
103   std::string CreateTexture(const Vector4& color1, const Vector4& color2)
104   {
105     const auto width       = 2u;
106     const auto height      = 1u;
107     auto       size        = width * height * 4;
108     auto       pixelBuffer = new unsigned char[size];
109     pixelBuffer[0]         = 0xFF * color1.x;
110     pixelBuffer[1]         = 0xFF * color1.y;
111     pixelBuffer[2]         = 0xFF * color1.z;
112     pixelBuffer[3]         = 0xFF * color1.w;
113     pixelBuffer[4]         = 0xFF * color2.x;
114     pixelBuffer[5]         = 0xFF * color2.y;
115     pixelBuffer[6]         = 0xFF * color2.z;
116     pixelBuffer[7]         = 0xFF * color2.w;
117
118     auto pixelData = PixelData::New(pixelBuffer, size, width, height, Pixel::RGBA8888, PixelData::ReleaseFunction::DELETE_ARRAY);
119     auto texture   = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
120     texture.Upload(pixelData);
121
122     return Toolkit::TextureManager::AddTexture(texture);
123   }
124
125   template<typename TextT>
126   Toolkit::ImageView CreateImageView(TextT&& filename)
127   {
128     auto imageView = Toolkit::ImageView::New();
129
130     Property::Map propertyMap;
131     propertyMap.Insert(Toolkit::ImageVisual::Property::URL, std::forward<TextT>(filename));
132     propertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
133     propertyMap.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, Toolkit::DevelVisual::FILL);
134     imageView.SetProperty(Toolkit::ImageView::Property::IMAGE, propertyMap);
135
136     return imageView;
137   }
138
139 private:
140   Application& mApplication;
141   unsigned int mIndex;
142 };
143
144 int DALI_EXPORT_API main(int argc, char** argv)
145 {
146   Application            application = Application::New(&argc, &argv);
147   ImageViewAlphaBlendApp test(application);
148   application.MainLoop();
149   return 0;
150 }