Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / tooltip / tooltip-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 <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/controls/tooltip/tooltip-properties.h>
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27 const Vector4     WINDOW_COLOR(211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f); ///< The color of the window
28 const char* const THEME_PATH(DEMO_STYLE_DIR "tooltip-example-theme.json");               ///< The theme used for this example
29 const float       POSITION_INCREMENTER(0.2f);                                            ///< The position difference between the controls along the Y-Axis.
30 } // unnamed namespace
31
32 /**
33  * @brief Creates a controller which demonstrates the tooltip functionality in control.
34  *
35  * The Control base class supports tooltip functionality. However, the Toolkit Tooltip style is only set on Buttons by default.
36  * This example portrays the different ways in which a tooltip can be displayed and customised.
37  */
38 class TooltipController : public ConnectionTracker
39 {
40 public:
41   TooltipController(Application& application)
42   : mApplication(application),
43     previousPosition(0.0f)
44   {
45     // Connect to the Application's Init signal
46     mApplication.InitSignal().Connect(this, &TooltipController::Create);
47   }
48
49 private:
50   // The Init signal is received once (only) during the Application lifetime
51   void Create(Application& application)
52   {
53     // Set the window background color and connect to the window's key signal to allow Back and Escape to exit.
54     Window window = application.GetWindow();
55     window.SetBackgroundColor(WINDOW_COLOR);
56     window.KeyEventSignal().Connect(this, &TooltipController::OnKeyEvent);
57     const Vector2 windowSize = window.GetSize();
58
59     // Add a text label at the top for information purposes
60     Control label = TextLabel::New("Hover over buttons to see tooltip");
61     label.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
62     label.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
63     label.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "Center");
64     window.Add(label);
65
66     // Simple tooltip from stylesheet
67     Control simple = PushButton::New();
68     simple.SetStyleName("TooltipTextOnly");
69     SetLabel(simple, "Simple");
70     Layout(simple, windowSize);
71     window.Add(simple);
72
73     // Tooltip with icon and text, from stylesheet
74     Control iconWithText = PushButton::New();
75     iconWithText.SetStyleName("TooltipArray");
76     SetLabel(iconWithText, "Icon with Text");
77     Layout(iconWithText, windowSize);
78     window.Add(iconWithText);
79
80     // Tooltip with custom style, from stylesheet
81     Control customFromStylesheet = PushButton::New();
82     customFromStylesheet.SetStyleName("TooltipCustom");
83     SetLabel(customFromStylesheet, "Custom From Stylesheet");
84     Layout(customFromStylesheet, windowSize);
85     window.Add(customFromStylesheet);
86
87     // Tooltip with custom style, from code
88     Control customFromCode = PushButton::New();
89     SetLabel(customFromCode, "Custom From Code");
90     Layout(customFromCode, windowSize);
91     customFromCode.SetProperty(DevelControl::Property::TOOLTIP,
92                                {{Tooltip::Property::CONTENT,
93                                  Property::Array{{{{Toolkit::Visual::Property::TYPE, Visual::IMAGE},
94                                                    {ImageVisual::Property::URL, DEMO_IMAGE_DIR "Logo-for-demo.png"}}},
95                                                  {{{Toolkit::Visual::Property::TYPE, Visual::TEXT},
96                                                    {TextVisual::Property::TEXT_COLOR, Color::WHITE},
97                                                    {TextVisual::Property::TEXT, "Custom coded style\nat hover point"},
98                                                    {TextVisual::Property::MULTI_LINE, true},
99                                                    {TextVisual::Property::HORIZONTAL_ALIGNMENT, "CENTER"},
100                                                    {TextVisual::Property::POINT_SIZE, 16}}}}},
101                                 {Tooltip::Property::LAYOUT, Vector2(2, 1)},
102                                 {Tooltip::Property::POSITION, Tooltip::Position::HOVER_POINT},
103                                 {Tooltip::Property::BACKGROUND,
104                                  {{Tooltip::Background::Property::VISUAL, DEMO_IMAGE_DIR "tooltip.9.png"},
105                                   {Tooltip::Background::Property::BORDER, Rect<int>(1, 5, 5, 1)}}}});
106     window.Add(customFromCode);
107   }
108
109   /**
110    * @brief Called when any key event is received
111    *
112    * Will use this to quit the application if Back or the Escape key is received
113    * @param[in]  event  The key event information
114    */
115   void OnKeyEvent(const KeyEvent& event)
116   {
117     if(event.GetState() == KeyEvent::DOWN)
118     {
119       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
120       {
121         mApplication.Quit();
122       }
123     }
124   }
125
126   /**
127    * @brief Sets the label on the control.
128    * @param[in]  label  The label to set.
129    */
130   void SetLabel(Control control, std::string label)
131   {
132     if(control)
133     {
134       control.SetProperty(Button::Property::LABEL,
135                           Property::Map().Add(Toolkit::Visual::Property::TYPE, Visual::TEXT).Add(TextVisual::Property::TEXT, label));
136     }
137   }
138
139   /**
140    * @brief Lays out the control in the appropriate location.
141    * @param[in]  control    The control to layout.
142    * @param[in]  windowSize  The size of the window, passing it in so we don't have to retrieve it every time.
143    */
144   void Layout(Control control, const Vector2& windowSize)
145   {
146     if(control)
147     {
148       previousPosition += POSITION_INCREMENTER;
149       control.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
150       control.SetProperty(Actor::Property::SIZE_MODE_FACTOR, Vector3(0.75, 0.1, 1.0));
151       control.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
152       control.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
153       control.SetProperty(Actor::Property::POSITION_Y, windowSize.height * previousPosition);
154     }
155   }
156
157 private:
158   Application& mApplication;
159   float        previousPosition;
160 };
161
162 int DALI_EXPORT_API main(int argc, char** argv)
163 {
164   Application application = Application::New(&argc, &argv, THEME_PATH);
165
166   TooltipController test(application);
167
168   application.MainLoop();
169
170   return 0;
171 }