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