[dali_1.1.32] Merge branch 'devel/master'
[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 #include <dali-toolkit/devel-api/controls/tool-bar/tool-bar.h>
23
24
25 namespace DemoHelper
26 {
27
28 /**
29  * Provide a style for the view and its tool bar.
30  */
31 struct ViewStyle
32 {
33   ViewStyle( float toolBarButtonPercentage, float toolBarTitlePercentage, float toolBarHeight, float toolBarPadding )
34   : mToolBarButtonPercentage( toolBarButtonPercentage ),
35     mToolBarTitlePercentage( toolBarTitlePercentage ),
36     mToolBarHeight( toolBarHeight ),
37     mToolBarPadding( toolBarPadding )
38   {}
39
40   float mToolBarButtonPercentage; ///< The tool bar button width is a percentage of the tool bar width.
41   float mToolBarTitlePercentage;  ///< The tool bar title width is a percentage of the tool bar width.
42   float mToolBarHeight;           ///< The tool bar height (in pixels).
43   float mToolBarPadding;          ///< The tool bar padding (in pixels)..
44 };
45
46 const ViewStyle DEFAULT_VIEW_STYLE( 0.1f, 0.7f, 80.f, 4.f );
47
48 const char*                   DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
49 const char*                   DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
50 const float                   DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
51
52 const Dali::Toolkit::Alignment::Padding DEFAULT_PLAY_PADDING(12.0f, 12.0f, 12.0f, 12.0f);
53 const Dali::Toolkit::Alignment::Padding DEFAULT_MODE_SWITCH_PADDING(8.0f, 8.0f, 8.0f, 8.0f);
54
55 float ScalePointSize(int pointSize)
56 {
57   Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
58   float meanDpi = (dpi.height + dpi.width) * 0.5f;
59   return pointSize * 220.0f / meanDpi;        // 220 is the default horizontal DPI defined in adaptor Application
60 }
61
62 Dali::Layer CreateToolbar( Dali::Toolkit::ToolBar& toolBar,
63                            const std::string& toolbarImagePath,
64                            const std::string& title,
65                            const ViewStyle& style )
66 {
67   Dali::Stage stage = Dali::Stage::GetCurrent();
68
69   Dali::Layer toolBarLayer = Dali::Layer::New();
70   toolBarLayer.SetName( "TOOLBAR_LAYER" );
71   toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
72   toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
73   toolBarLayer.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::WIDTH );
74   toolBarLayer.SetSize( 0.0f, style.mToolBarHeight );
75
76   // Raise tool bar layer to the top.
77   toolBarLayer.RaiseToTop();
78
79   // Tool bar
80   Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
81   toolBar = Dali::Toolkit::ToolBar::New();
82   toolBar.SetName( "TOOLBAR" );
83   toolBar.SetBackgroundImage( image );
84   toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
85   toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
86   toolBar.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
87
88   // Add the tool bar to the too bar layer.
89   toolBarLayer.Add( toolBar );
90
91   // Tool bar text.
92   if( !title.empty() )
93   {
94     Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New();
95     label.SetAnchorPoint( Dali::AnchorPoint::TOP_LEFT );
96     label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
97     label.SetProperty( Dali::Toolkit::TextLabel::Property::TEXT, title );
98     label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
99     label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
100     label.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT );
101
102     // Add title to the tool bar.
103     const float padding( style.mToolBarPadding );
104     toolBar.AddControl( label, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter, Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
105   }
106
107   return toolBarLayer;
108 }
109
110 Dali::Layer CreateView( Dali::Application& application,
111                         Dali::Toolkit::Control& view,
112                         Dali::Toolkit::ToolBar& toolBar,
113                         const std::string& backgroundImagePath,
114                         const std::string& toolbarImagePath,
115                         const std::string& title,
116                         const ViewStyle& style = DEFAULT_VIEW_STYLE )
117 {
118   Dali::Stage stage = Dali::Stage::GetCurrent();
119
120   // Create default View.
121   view = Dali::Toolkit::Control::New();
122   view.SetAnchorPoint( Dali::AnchorPoint::CENTER );
123   view.SetParentOrigin( Dali::ParentOrigin::CENTER );
124   view.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
125
126   // Add the view to the stage before setting the background.
127   stage.Add( view );
128
129   // Set background image, loading it at screen resolution:
130   if ( !backgroundImagePath.empty() )
131   {
132     Dali::Image backgroundImage = Dali::ResourceImage::New( backgroundImagePath, Dali::ImageDimensions( stage.GetSize().x, stage.GetSize().y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
133     view.SetBackgroundImage( backgroundImage );
134   }
135
136   // FIXME
137   // Connects the orientation signal with the View::OrientationChanged method.
138   //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
139
140   // Create default ToolBar
141   Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style );
142
143   // Add tool bar layer to the view.
144   view.Add( toolBarLayer );
145
146   // Create a content layer.
147   Dali::Layer contentLayer = Dali::Layer::New();
148   contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
149   contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
150   contentLayer.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
151   view.Add( contentLayer );
152   contentLayer.LowerBelow( toolBarLayer );
153
154   return contentLayer;
155 }
156
157 Dali::Toolkit::TextLabel CreateToolBarLabel( const std::string& text )
158 {
159   Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New( text );
160   label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
161   label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
162   label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
163   label.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT );
164
165   return label;
166 }
167
168 } // DemoHelper
169
170 #endif // __DALI_DEMO_HELPER_VIEW_H__