[dali_1.0.37] Merge branch 'tizen'
[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.SetSize( 0.0f, style.mToolBarHeight );
80   toolBarLayer.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::WIDTH );
81
82   // Raise tool bar layer to the top.
83   toolBarLayer.RaiseToTop();
84
85   // Tool bar
86   Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
87   Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
88   toolBarBackground.SetName( "TOOLBAR_BACKGROUND" );
89   toolBarBackground.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
90   toolBar = Dali::Toolkit::ToolBar::New();
91   toolBar.SetName( "TOOLBAR" );
92   toolBar.SetBackground( toolBarBackground );
93   toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
94   toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
95   toolBar.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
96   toolBarBackground.SetSortModifier(1.0f);
97
98   // Add the tool bar to the too bar layer.
99   toolBarLayer.Add( toolBar );
100
101   // Tool bar text.
102   if( !title.empty() )
103   {
104     Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New();
105     label.SetAnchorPoint( Dali::AnchorPoint::TOP_LEFT );
106     label.SetDrawMode( Dali::DrawMode::OVERLAY );
107     label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
108     label.SetProperty( Dali::Toolkit::TextLabel::Property::TEXT, title );
109     label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
110     label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
111     label.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::HEIGHT );
112     label.SetColor( DEFAULT_TEXT_STYLE_COLOR );
113
114     // Add title to the tool bar.
115     const float padding( style.mToolBarPadding );
116     toolBar.AddControl( label, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter, Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
117   }
118
119   return toolBarLayer;
120 }
121
122 Dali::Layer CreateView( Dali::Application& application,
123                         Dali::Toolkit::View& view,
124                         Dali::Toolkit::ToolBar& toolBar,
125                         const std::string& backgroundImagePath,
126                         const std::string& toolbarImagePath,
127                         const std::string& title,
128                         const ViewStyle& style = DEFAULT_VIEW_STYLE )
129 {
130   Dali::Stage stage = Dali::Stage::GetCurrent();
131
132   // Create default View.
133   view = Dali::Toolkit::View::New();
134   view.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::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::ImageAttributes backgroundAttributes;
143     backgroundAttributes.SetSize( stage.GetSize() );
144     backgroundAttributes.SetFilterMode( Dali::ImageAttributes::BoxThenLinear );
145     backgroundAttributes.SetScalingMode( Dali::ImageAttributes::ScaleToFill );
146     Dali::Image backgroundImage = Dali::ResourceImage::New( backgroundImagePath, backgroundAttributes );
147     Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( backgroundImage );
148     view.SetBackground( backgroundImageActor );
149   }
150
151   // FIXME
152   // Connects the orientation signal with the View::OrientationChanged method.
153   //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
154
155   // Create default ToolBar
156   Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style );
157
158   // Add tool bar layer to the view.
159   view.AddContentLayer( toolBarLayer );
160
161   // Create a content layer.
162   Dali::Layer contentLayer = Dali::Layer::New();
163   contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
164   contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
165   contentLayer.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::ALL_DIMENSIONS );
166   view.AddContentLayer( contentLayer );
167   contentLayer.LowerBelow( toolBarLayer );
168
169   return contentLayer;
170 }
171
172 Dali::Toolkit::TextLabel CreateToolBarLabel( const std::string& text )
173 {
174   Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New( text );
175   label.SetProperty( Dali::Toolkit::Control::Property::STYLE_NAME, "toolbarlabel" );
176   label.SetDrawMode( Dali::DrawMode::OVERLAY );
177   label.SetProperty( Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
178   label.SetProperty( Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
179   label.SetResizePolicy( Dali::FILL_TO_PARENT, Dali::HEIGHT );
180   label.SetColor( DEFAULT_TEXT_STYLE_COLOR );
181
182   return label;
183 }
184
185 } // DemoHelper
186
187 #endif // __DALI_DEMO_HELPER_VIEW_H__