Fix for TextLabel demo styling
[platform/core/uifw/dali-demo.git] / shared / view.h
1 #ifndef __DALI_DEMO_HELPER_VIEW_H__
2 #define __DALI_DEMO_HELPER_VIEW_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali-toolkit/dali-toolkit.h>
22
23 namespace DemoHelper
24 {
25
26 /**
27  * Provide a style for the view and its tool bar.
28  */
29 struct ViewStyle
30 {
31   ViewStyle( float toolBarButtonPercentage, float toolBarTitlePercentage, float toolBarHeight, float toolBarPadding )
32   : mToolBarButtonPercentage( toolBarButtonPercentage ),
33     mToolBarTitlePercentage( toolBarTitlePercentage ),
34     mToolBarHeight( toolBarHeight ),
35     mToolBarPadding( toolBarPadding )
36   {}
37
38   float mToolBarButtonPercentage; ///< The tool bar button width is a percentage of the tool bar width.
39   float mToolBarTitlePercentage;  ///< The tool bar title width is a percentage of the tool bar width.
40   float mToolBarHeight;           ///< The tool bar height (in pixels).
41   float mToolBarPadding;          ///< The tool bar padding (in pixels)..
42 };
43
44 const ViewStyle DEFAULT_VIEW_STYLE( 0.1f, 0.7f, 80.f, 4.f );
45
46 const char*                   DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
47 const char*                   DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
48 const float                   DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
49 const Dali::Vector4           DEFAULT_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
50
51 const Dali::Toolkit::Alignment::Padding DEFAULT_PLAY_PADDING(12.0f, 12.0f, 12.0f, 12.0f);
52 const Dali::Toolkit::Alignment::Padding DEFAULT_MODE_SWITCH_PADDING(8.0f, 8.0f, 8.0f, 8.0f);
53
54 void RequestThemeChange()
55 {
56   // Provide the stylesheet
57   Dali::Toolkit::StyleManager styleManager = Dali::Toolkit::StyleManager::Get();
58   styleManager.RequestThemeChange( DALI_DEMO_THEME_PATH );
59 }
60
61 float ScalePointSize(int pointSize)
62 {
63   Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
64   float meanDpi = (dpi.height + dpi.width) * 0.5f;
65   return pointSize * 220.0f / meanDpi;        // 220 is the default horizontal DPI defined in adaptor Application
66 }
67
68 Dali::Layer CreateToolbar( Dali::Toolkit::ToolBar& toolBar,
69                            const std::string& toolbarImagePath,
70                            const std::string& title,
71                            const ViewStyle& style )
72 {
73   Dali::Stage stage = Dali::Stage::GetCurrent();
74
75   Dali::Layer toolBarLayer = Dali::Layer::New();
76   toolBarLayer.SetName( "TOOLBAR_LAYER" );
77   toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
78   toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
79   toolBarLayer.SetPreferredSize( Dali::Vector2( 0.0f, style.mToolBarHeight ) );
80   toolBarLayer.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::WIDTH );
81   toolBarLayer.SetResizePolicy( Dali::FIXED, Dali::HEIGHT );
82
83   // Raise tool bar layer to the top.
84   toolBarLayer.RaiseToTop();
85
86   // Tool bar
87   Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
88   Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
89   toolBarBackground.SetName( "TOOLBAR_BACKGROUND" );
90   toolBarBackground.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
91   toolBar = Dali::Toolkit::ToolBar::New();
92   toolBar.SetName( "TOOLBAR" );
93   toolBar.SetBackground( toolBarBackground );
94   toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
95   toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
96   toolBar.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
97   toolBarBackground.SetSortModifier(1.0f);
98
99   // Add the tool bar to the too bar layer.
100   toolBarLayer.Add( toolBar );
101
102   // Tool bar text.
103   if( !title.empty() )
104   {
105     Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New();
106     label.SetAnchorPoint( Dali::AnchorPoint::TOP_LEFT );
107     label.SetDrawMode( Dali::DrawMode::OVERLAY );
108     label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
109     label.SetProperty( Dali::Toolkit::TextLabel::Property::TEXT, title );
110     label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
111     label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
112     label.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::HEIGHT );
113     label.SetColor( DEFAULT_TEXT_STYLE_COLOR );
114     toolBarLayer.Add( label );
115   }
116
117   return toolBarLayer;
118 }
119
120 Dali::Layer CreateView( Dali::Application& application,
121                         Dali::Toolkit::View& view,
122                         Dali::Toolkit::ToolBar& toolBar,
123                         const std::string& backgroundImagePath,
124                         const std::string& toolbarImagePath,
125                         const std::string& title,
126                         const ViewStyle& style = DEFAULT_VIEW_STYLE )
127 {
128   Dali::Stage stage = Dali::Stage::GetCurrent();
129
130   // Create default View.
131   view = Dali::Toolkit::View::New();
132   view.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
133
134   // Add the view to the stage before setting the background.
135   stage.Add( view );
136
137   // Set background image, loading it at screen resolution:
138   if ( !backgroundImagePath.empty() )
139   {
140     Dali::ImageAttributes backgroundAttributes;
141     backgroundAttributes.SetSize( stage.GetSize() );
142     backgroundAttributes.SetFilterMode( Dali::ImageAttributes::BoxThenLinear );
143     backgroundAttributes.SetScalingMode( Dali::ImageAttributes::ScaleToFill );
144     Dali::Image backgroundImage = Dali::ResourceImage::New( backgroundImagePath, backgroundAttributes );
145     Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( backgroundImage );
146     view.SetBackground( backgroundImageActor );
147   }
148
149   // FIXME
150   // Connects the orientation signal with the View::OrientationChanged method.
151   //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
152
153   // Create default ToolBar
154   Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style );
155
156   // Add tool bar layer to the view.
157   view.AddContentLayer( toolBarLayer );
158
159   // Create a content layer.
160   Dali::Layer contentLayer = Dali::Layer::New();
161   contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
162   contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
163   contentLayer.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
164   view.AddContentLayer( contentLayer );
165   contentLayer.LowerBelow( toolBarLayer );
166
167   return contentLayer;
168 }
169
170 Dali::Toolkit::TextLabel CreateToolBarLabel( const std::string& text )
171 {
172   Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New( text );
173   label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
174   label.SetDrawMode( Dali::DrawMode::OVERLAY );
175   label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
176   label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
177   label.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::HEIGHT );
178   label.SetColor( DEFAULT_TEXT_STYLE_COLOR );
179
180   return label;
181 }
182
183 } // DemoHelper
184
185 #endif // __DALI_DEMO_HELPER_VIEW_H__