Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / alpha-blending-cpu / alpha-blending-cpu-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 <cstring>
20
21 using namespace Dali;
22
23 namespace
24 {
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");
31 } // namespace
32
33 class ImageViewAlphaBlendApp : public ConnectionTracker
34 {
35 public:
36   ImageViewAlphaBlendApp(Application& application)
37   : mApplication(application),
38     mImageCombinationIndex(0)
39   {
40     // Connect to the Application's Init signal
41     mApplication.InitSignal().Connect(this, &ImageViewAlphaBlendApp::Create);
42   }
43
44   ~ImageViewAlphaBlendApp()
45   {
46     // Nothing to do here;
47   }
48
49 private:
50   // The Init signal is received once (only) during the Application lifetime
51   void Create(Application& application)
52   {
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.
55
56     // Get a handle to the window
57     Window window = application.GetWindow();
58     window.KeyEventSignal().Connect(this, &ImageViewAlphaBlendApp::OnKeyEvent);
59     window.SetBackgroundColor(Color::WHITE);
60
61     mImageView = Toolkit::ImageView::New();
62
63     mImageView.SetProperty(Actor::Property::SIZE, Vector2(200, 200));
64     mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
65     window.Add(mImageView);
66
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);
73
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);
80
81     LoadImages();
82
83     window.TouchedSignal().Connect(this, &ImageViewAlphaBlendApp::OnTouched);
84   }
85
86   void OnTouched(const TouchEvent& touch)
87   {
88     static bool touched = false;
89     if(touch.GetState(0) == PointState::DOWN)
90     {
91       touched = true;
92     }
93
94     if(touch.GetState(0) == PointState::UP && touched)
95     {
96       mImageCombinationIndex++;
97       touched = false;
98       LoadImages();
99     }
100   }
101
102   void LoadImages()
103   {
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};
106
107     const char* mask  = masks[mImageCombinationIndex % 2];        // Cycle through masks
108     const char* image = images[(mImageCombinationIndex / 2) % 4]; // then images
109
110     Property::Map map;
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);
114
115     if(mImageCombinationIndex % 2 == 0)
116     {
117       map.Add(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, 1.f);
118       map.Add(Toolkit::ImageVisual::Property::CROP_TO_MASK, false);
119     }
120     else
121     {
122       map.Add(Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE, 1.6f);
123       map.Add(Toolkit::ImageVisual::Property::CROP_TO_MASK, true);
124     }
125
126     mImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, map);
127
128     mImageLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, strrchr(image, '/'));
129     mMaskLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, strrchr(mask, '/'));
130   }
131
132   void OnKeyEvent(const KeyEvent& event)
133   {
134     if(event.GetState() == KeyEvent::DOWN)
135     {
136       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
137       {
138         mApplication.Quit();
139       }
140     }
141   }
142
143 private:
144   Application&       mApplication;
145   Toolkit::ImageView mImageView;
146   Toolkit::TextLabel mImageLabel;
147   Toolkit::TextLabel mMaskLabel;
148
149   int mImageCombinationIndex;
150 };
151
152 int DALI_EXPORT_API main(int argc, char** argv)
153 {
154   Application            application = Application::New(&argc, &argv);
155   ImageViewAlphaBlendApp test(application);
156   application.MainLoop();
157   return 0;
158 }