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