DirectRendering demo
[platform/core/uifw/dali-demo.git] / examples / advanced-blend-mode / advanced-blend-mode-example.cpp
1 /*
2  * Copyright (c) 2021 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/devel-api/actors/actor-devel.h>
20
21 #include <dali/devel-api/common/capabilities.h>
22 #include <dali/integration-api/debug.h>
23
24 using namespace Dali;
25 using Dali::Toolkit::TextLabel;
26
27 // This example shows how to create and display Hello World! using a simple TextActor
28 //
29 class AdvancedBlendModeController : public ConnectionTracker
30 {
31 public:
32   AdvancedBlendModeController(Application& application)
33   : mApplication(application)
34   {
35     // Connect to the Application's Init signal
36     mApplication.InitSignal().Connect(this, &AdvancedBlendModeController::Create);
37   }
38
39   ~AdvancedBlendModeController() = default; // Nothing to do in destructor
40
41   // The Init signal is received once (only) during the Application lifetime
42   void Create(Application& application)
43   {
44     // Get a handle to the stage
45     Window window = application.GetWindow();
46     window.SetBackgroundColor(Color::BLACK);
47
48     Toolkit::ImageView imageView = Toolkit::ImageView::New();
49     Property::Map      imagePropertyMap;
50     imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
51     imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, DEMO_IMAGE_DIR "gallery-large-19.jpg");
52     imageView.SetProperty(Toolkit::ImageView::Property::IMAGE, imagePropertyMap);
53     imageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
54     imageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
55     imageView.SetProperty(Actor::Property::SIZE, Vector2(600, 600));
56     window.Add(imageView);
57
58     Toolkit::Control control_1 = Toolkit::Control::New();
59     Property::Map    colorVisualMap_1;
60     colorVisualMap_1.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
61     colorVisualMap_1.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, Dali::Color::RED);
62     colorVisualMap_1.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, true);
63     control_1.SetProperty(Toolkit::Control::Property::BACKGROUND, colorVisualMap_1);
64     control_1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
65     control_1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
66     control_1.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
67     control_1.SetProperty(Actor::Property::SIZE, Vector2(600, 200));
68     if(Dali::Capabilities::IsBlendEquationSupported(Dali::DevelBlendEquation::SCREEN))
69     {
70       control_1.SetProperty(Dali::DevelActor::Property::BLEND_EQUATION, Dali::DevelBlendEquation::LUMINOSITY);
71     }
72     window.Add(control_1);
73
74     Toolkit::Control control_2 = Toolkit::Control::New();
75     Property::Map    colorVisualMap_2;
76     colorVisualMap_2.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
77     colorVisualMap_2.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, Dali::Color::GREEN);
78     colorVisualMap_2.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, true);
79     control_2.SetProperty(Toolkit::Control::Property::BACKGROUND, colorVisualMap_2);
80     control_2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
81     control_2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
82     control_2.SetProperty(Actor::Property::POSITION, Vector2(0, 200));
83     control_2.SetProperty(Actor::Property::SIZE, Vector2(600, 200));
84     if(Dali::Capabilities::IsBlendEquationSupported(Dali::DevelBlendEquation::SCREEN))
85     {
86       control_2.SetProperty(Dali::DevelActor::Property::BLEND_EQUATION, Dali::DevelBlendEquation::LUMINOSITY);
87     }
88     window.Add(control_2);
89
90     Toolkit::Control control_3 = Toolkit::Control::New();
91     Property::Map    colorVisualMap_3;
92     colorVisualMap_3.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
93     colorVisualMap_3.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, Dali::Color::BLUE);
94     colorVisualMap_3.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, true);
95     control_3.SetProperty(Toolkit::Control::Property::BACKGROUND, colorVisualMap_3);
96     control_3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
97     control_3.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
98     control_3.SetProperty(Actor::Property::POSITION, Vector2(0, 400));
99     control_3.SetProperty(Actor::Property::SIZE, Vector2(600, 200));
100     if(Dali::Capabilities::IsBlendEquationSupported(Dali::DevelBlendEquation::SCREEN))
101     {
102       control_3.SetProperty(Dali::DevelActor::Property::BLEND_EQUATION, Dali::DevelBlendEquation::LUMINOSITY);
103     }
104     window.Add(control_3);
105
106     // Add text to explain what's being seen.
107     auto label = Toolkit::TextLabel::New("If your device supports advanced blending, this shows an image at different levels of luminosity. If not, it instead shows red/green/blue sections.");
108
109     label[Toolkit::TextLabel::Property::MULTI_LINE] = true;
110     label[Toolkit::TextLabel::Property::TEXT_COLOR] = Color::WHITE;
111     label[Toolkit::TextLabel::Property::POINT_SIZE] = 12.0f;
112     label[Actor::Property::PARENT_ORIGIN]           = ParentOrigin::BOTTOM_CENTER;
113     label[Actor::Property::ANCHOR_POINT]            = AnchorPoint::BOTTOM_CENTER;
114     label.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
115     window.Add(label);
116
117     // Respond to a touch anywhere on the window
118     window.GetRootLayer().TouchedSignal().Connect(this, &AdvancedBlendModeController::OnTouch);
119
120     // Respond to key events
121     window.KeyEventSignal().Connect(this, &AdvancedBlendModeController::OnKeyEvent);
122   }
123
124   bool OnTouch(Actor actor, const TouchEvent& touch)
125   {
126     // quit the application
127     mApplication.Quit();
128     return true;
129   }
130
131   void OnKeyEvent(const KeyEvent& event)
132   {
133     if(event.GetState() == KeyEvent::DOWN)
134     {
135       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
136       {
137         mApplication.Quit();
138       }
139     }
140   }
141
142 private:
143   Application& mApplication;
144 };
145
146 int DALI_EXPORT_API main(int argc, char** argv)
147 {
148   Application                 application = Application::New(&argc, &argv);
149   AdvancedBlendModeController test(application);
150   application.MainLoop();
151   return 0;
152 }