1 #ifndef __DALI_DEMO_HELPER_VIEW_H__
2 #define __DALI_DEMO_HELPER_VIEW_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali-toolkit/dali-toolkit.h>
27 * Provide a style for the view and its tool bar.
31 ViewStyle( float toolBarButtonPercentage, float toolBarTitlePercentage, float toolBarHeight, float toolBarPadding )
32 : mToolBarButtonPercentage( toolBarButtonPercentage ),
33 mToolBarTitlePercentage( toolBarTitlePercentage ),
34 mToolBarHeight( toolBarHeight ),
35 mToolBarPadding( toolBarPadding )
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)..
44 const ViewStyle DEFAULT_VIEW_STYLE( 0.1f, 0.7f, 80.f, 4.f );
46 const char* DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
47 const char* DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
48 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
49 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::EXTRALIGHT);
50 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
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);
55 static Dali::TextStyle defaultTextStyle;
56 static bool textStyleSet=false;
58 float ScalePointSize(int pointSize)
60 Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
61 float meanDpi = (dpi.height + dpi.width) * 0.5f;
62 return pointSize * 220.0f / meanDpi; // 220 is the default horizontal DPI defined in adaptor Application
65 Dali::TextStyle& GetDefaultTextStyle()
69 defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
70 defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
71 defaultTextStyle.SetFontPointSize(Dali::PointSize(ScalePointSize(DEFAULT_TEXT_STYLE_POINT_SIZE)));
72 defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
73 defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
77 return defaultTextStyle;
80 Dali::Layer CreateToolbar( Dali::Toolkit::ToolBar& toolBar,
81 const std::string& toolbarImagePath,
82 const std::string& title,
83 const ViewStyle& style,
84 const Dali::TextStyle& textStyle )
86 Dali::Layer toolBarLayer = Dali::Layer::New();
87 toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
88 toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
89 toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::SourceWidthFixedHeight( style.mToolBarHeight ) ) );
90 toolBarLayer.SetSize( 0.0f, style.mToolBarHeight );
92 // Raise tool bar layer to the top.
93 toolBarLayer.RaiseToTop();
96 Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
97 Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
98 toolBar = Dali::Toolkit::ToolBar::New();
99 toolBar.SetBackground( toolBarBackground );
100 toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
101 toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
102 toolBar.SetSize( 0.0f, style.mToolBarHeight );
103 toolBar.SetSizeMode( Dali::SIZE_EQUAL_TO_PARENT );
104 toolBarBackground.SetSortModifier(1.0f);
106 // Add the tool bar to the too bar layer.
107 toolBarLayer.Add( toolBar );
109 Dali::Font font = Dali::Font::New();
114 Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
115 titleActor.SetName( "ToolbarTitle" );
116 titleActor.SetText( title );
117 titleActor.SetSize( font.MeasureText( title ) );
118 titleActor.SetStyleToCurrentText(textStyle);
120 // Add title to the tool bar.
121 const float padding( style.mToolBarPadding );
122 toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter, Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
128 Dali::Layer CreateView( Dali::Application& application,
129 Dali::Toolkit::View& view,
130 Dali::Toolkit::ToolBar& toolBar,
131 const std::string& backgroundImagePath,
132 const std::string& toolbarImagePath,
133 const std::string& title,
134 const ViewStyle& style,
135 const Dali::TextStyle& textStyle )
137 Dali::Stage stage = Dali::Stage::GetCurrent();
139 // Create default View.
140 view = Dali::Toolkit::View::New();
142 // Add the view to the stage before setting the background.
145 // Set background image.
146 if ( ! backgroundImagePath.empty() )
148 Dali::Image backgroundImage = Dali::ResourceImage::New( backgroundImagePath );
149 Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( backgroundImage );
150 view.SetBackground( backgroundImageActor );
154 // Connects the orientation signal with the View::OrientationChanged method.
155 //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
157 // Create default ToolBar
158 Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style, textStyle );
160 // Add tool bar layer to the view.
161 view.AddContentLayer( toolBarLayer );
165 // Create a content layer.
166 Dali::Layer contentLayer = Dali::Layer::New();
167 contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
168 contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
169 contentLayer.SetSizeMode( Dali::SIZE_EQUAL_TO_PARENT );
170 view.AddContentLayer( contentLayer );
171 contentLayer.LowerBelow( toolBarLayer );
176 Dali::Layer CreateView( Dali::Application& application,
177 Dali::Toolkit::View& view,
178 Dali::Toolkit::ToolBar& toolBar,
179 const std::string& backgroundImagePath,
180 const std::string& toolbarImagePath,
181 const std::string& title,
182 const ViewStyle& style = DEFAULT_VIEW_STYLE )
184 return CreateView( application, view, toolBar, backgroundImagePath, toolbarImagePath, title, style,
185 GetDefaultTextStyle() );
191 #endif // __DALI_DEMO_HELPER_VIEW_H__