2 * Copyright (c) 2020 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>
25 const char* const IMAGE_PATH_1(DEMO_IMAGE_DIR "people-small-7b.jpg"); // 100x100
26 const char* const IMAGE_PATH_2(DEMO_IMAGE_DIR "people-medium-7.jpg");
27 const char* const IMAGE_PATH_3(DEMO_IMAGE_DIR "people-medium-7-rgb565.png"); // is compressed
28 const char* const IMAGE_PATH_4(DEMO_IMAGE_DIR "people-medium-7-masked.png"); // has alpha channel
29 const char* const MASK_IMAGE_PATH_1(DEMO_IMAGE_DIR "store_mask_profile_n.png"); // 300x300
30 const char* const MASK_IMAGE_PATH_2(DEMO_IMAGE_DIR "store_mask_profile_f.png");
33 class ImageViewAlphaBlendApp : public ConnectionTracker
36 ImageViewAlphaBlendApp(Application& application)
37 : mApplication(application),
38 mImageCombinationIndex(0)
40 // Connect to the Application's Init signal
41 mApplication.InitSignal().Connect(this, &ImageViewAlphaBlendApp::Create);
44 ~ImageViewAlphaBlendApp()
46 // Nothing to do here;
50 // The Init signal is received once (only) during the Application lifetime
51 void Create(Application& application)
53 // This creates an image view with one of 3 images, and one of 2 masks.
54 // Clicking the screen will cycle through each combination of mask and image.
56 // Get a handle to the window
57 Window window = application.GetWindow();
58 window.KeyEventSignal().Connect(this, &ImageViewAlphaBlendApp::OnKeyEvent);
59 window.SetBackgroundColor(Color::WHITE);
61 mImageView = Toolkit::ImageView::New();
63 mImageView.SetProperty(Actor::Property::SIZE, Vector2(200, 200));
64 mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
65 window.Add(mImageView);
67 mImageLabel = Toolkit::TextLabel::New();
68 mImageLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
69 mImageLabel.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::BOTTOM_CENTER);
70 mImageLabel.SetProperty(Actor::Property::POSITION, Vector3(0.0f, -50.0f, 0.0f));
71 mImageLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Color::BLACK);
72 window.Add(mImageLabel);
74 mMaskLabel = Toolkit::TextLabel::New();
75 mMaskLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
76 mMaskLabel.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::BOTTOM_CENTER);
77 mMaskLabel.SetProperty(Actor::Property::POSITION, Vector3(0.0f, 0.0f, 0.0f));
78 mMaskLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Color::BLACK);
79 window.Add(mMaskLabel);
83 window.TouchedSignal().Connect(this, &ImageViewAlphaBlendApp::OnTouched);
86 void OnTouched(const TouchEvent& touch)
88 static bool touched = false;
89 if(touch.GetState(0) == PointState::DOWN)
94 if(touch.GetState(0) == PointState::UP && touched)
96 mImageCombinationIndex++;
104 const char* images[4] = {IMAGE_PATH_1, IMAGE_PATH_2, IMAGE_PATH_3, IMAGE_PATH_4};
105 const char* masks[2] = {MASK_IMAGE_PATH_1, MASK_IMAGE_PATH_2};
107 const char* mask = masks[mImageCombinationIndex % 2]; // Cycle through masks
108 const char* image = images[(mImageCombinationIndex / 2) % 4]; // then images
111 map.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::Type::IMAGE);
112 map.Add(Toolkit::ImageVisual::Property::URL, image);
113 map.Add(Toolkit::ImageVisual::Property::ALPHA_MASK_URL, mask);
115 if(mImageCombinationIndex % 2 == 0)
117 map.Add(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, 1.f);
118 map.Add(Toolkit::ImageVisual::Property::CROP_TO_MASK, false);
122 map.Add(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, 1.6f);
123 map.Add(Toolkit::ImageVisual::Property::CROP_TO_MASK, true);
126 mImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, map);
128 mImageLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, strrchr(image, '/'));
129 mMaskLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, strrchr(mask, '/'));
132 void OnKeyEvent(const KeyEvent& event)
134 if(event.GetState() == KeyEvent::DOWN)
136 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
144 Application& mApplication;
145 Toolkit::ImageView mImageView;
146 Toolkit::TextLabel mImageLabel;
147 Toolkit::TextLabel mMaskLabel;
149 int mImageCombinationIndex;
152 int DALI_EXPORT_API main(int argc, char** argv)
154 Application application = Application::New(&argc, &argv);
155 ImageViewAlphaBlendApp test(application);
156 application.MainLoop();