Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[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
50 const Dali::Toolkit::Alignment::Padding DEFAULT_PLAY_PADDING(12.0f, 12.0f, 12.0f, 12.0f);
51 const Dali::Toolkit::Alignment::Padding DEFAULT_MODE_SWITCH_PADDING(8.0f, 8.0f, 8.0f, 8.0f);
52
53 void RequestThemeChange()
54 {
55   // Provide the stylesheet
56   Dali::Toolkit::StyleManager styleManager = Dali::Toolkit::StyleManager::Get();
57   styleManager.RequestThemeChange( DALI_DEMO_THEME_PATH );
58 }
59
60 float ScalePointSize(int pointSize)
61 {
62   Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
63   float meanDpi = (dpi.height + dpi.width) * 0.5f;
64   return pointSize * 220.0f / meanDpi;        // 220 is the default horizontal DPI defined in adaptor Application
65 }
66
67 Dali::Layer CreateToolbar( Dali::Toolkit::ToolBar& toolBar,
68                            const std::string& toolbarImagePath,
69                            const std::string& title,
70                            const ViewStyle& style )
71 {
72   Dali::Stage stage = Dali::Stage::GetCurrent();
73
74   Dali::Layer toolBarLayer = Dali::Layer::New();
75   toolBarLayer.SetName( "TOOLBAR_LAYER" );
76   toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
77   toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
78   toolBarLayer.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::WIDTH );
79   toolBarLayer.SetSize( 0.0f, style.mToolBarHeight );
80
81   // Raise tool bar layer to the top.
82   toolBarLayer.RaiseToTop();
83
84   // Tool bar
85   Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
86   Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
87   toolBarBackground.SetName( "TOOLBAR_BACKGROUND" );
88   toolBarBackground.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
89   toolBar = Dali::Toolkit::ToolBar::New();
90   toolBar.SetName( "TOOLBAR" );
91   toolBar.SetBackground( toolBarBackground );
92   toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
93   toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
94   toolBar.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
95   toolBarBackground.SetSortModifier(1.0f);
96
97   // Add the tool bar to the too bar layer.
98   toolBarLayer.Add( toolBar );
99
100   // Tool bar text.
101   if( !title.empty() )
102   {
103     Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New();
104     label.SetAnchorPoint( Dali::AnchorPoint::TOP_LEFT );
105     label.SetDrawMode( Dali::DrawMode::OVERLAY );
106     label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
107     label.SetProperty( Dali::Toolkit::TextLabel::Property::TEXT, title );
108     label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
109     label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
110     label.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT );
111
112     // Add title to the tool bar.
113     const float padding( style.mToolBarPadding );
114     toolBar.AddControl( label, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter, Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
115   }
116
117   return toolBarLayer;
118 }
119
120 Dali::Layer CreateView( Dali::Application& application,
121                         Dali::Toolkit::Control& 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::Control::New();
132   view.SetAnchorPoint( Dali::AnchorPoint::CENTER );
133   view.SetParentOrigin( Dali::ParentOrigin::CENTER );
134   view.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
135
136   // Add the view to the stage before setting the background.
137   stage.Add( view );
138
139   // Set background image, loading it at screen resolution:
140   if ( !backgroundImagePath.empty() )
141   {
142     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 );
143     view.SetBackgroundImage( backgroundImage );
144   }
145
146   // FIXME
147   // Connects the orientation signal with the View::OrientationChanged method.
148   //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
149
150   // Create default ToolBar
151   Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style );
152
153   // Add tool bar layer to the view.
154   view.Add( toolBarLayer );
155
156   // Create a content layer.
157   Dali::Layer contentLayer = Dali::Layer::New();
158   contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
159   contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
160   contentLayer.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
161   view.Add( contentLayer );
162   contentLayer.LowerBelow( toolBarLayer );
163
164   return contentLayer;
165 }
166
167 Dali::Toolkit::TextLabel CreateToolBarLabel( const std::string& text )
168 {
169   Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New( text );
170   label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
171   label.SetDrawMode( Dali::DrawMode::OVERLAY );
172   label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
173   label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
174   label.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT );
175
176   return label;
177 }
178
179 } // DemoHelper
180
181 #endif // __DALI_DEMO_HELPER_VIEW_H__