2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 const char* const IMAGE_PATH(DEMO_IMAGE_DIR "gallery-large-20.jpg");
29 class ImageViewAlphaBlendApp : public ConnectionTracker
32 ImageViewAlphaBlendApp(Application& application)
33 : mApplication(application),
36 // Connect to the Application's Init signal
37 mApplication.InitSignal().Connect(this, &ImageViewAlphaBlendApp::Create);
40 ~ImageViewAlphaBlendApp()
42 // Nothing to do here;
46 // The Init signal is received once (only) during the Application lifetime
47 void Create(Application& application)
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;
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;
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);
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);
82 imageView2.SetProperty(Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true);
83 imageView3.SetProperty(Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true);
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);
92 void OnKeyEvent(const KeyEvent& event)
94 if(event.GetState() == KeyEvent::DOWN)
96 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
103 std::string CreateTexture(const Vector4& color1, const Vector4& color2)
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;
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);
122 return Toolkit::TextureManager::AddTexture(texture);
125 template<typename TextT>
126 Toolkit::ImageView CreateImageView(TextT&& filename)
128 auto imageView = Toolkit::ImageView::New();
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);
140 Application& mApplication;
144 int DALI_EXPORT_API main(int argc, char** argv)
146 Application application = Application::New(&argc, &argv);
147 ImageViewAlphaBlendApp test(application);
148 application.MainLoop();