e13be63418bb67737d9604597f53b8abecd676ad
[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 Dali::Vector4           DEFAULT_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.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 float ScalePointSize(int pointSize)
54 {
55   Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
56   float meanDpi = (dpi.height + dpi.width) * 0.5f;
57   return pointSize * 220.0f / meanDpi;        // 220 is the default horizontal DPI defined in adaptor Application
58 }
59
60 Dali::Layer CreateToolbar( Dali::Toolkit::ToolBar& toolBar,
61                            const std::string& toolbarImagePath,
62                            const std::string& title,
63                            const ViewStyle& style )
64 {
65   Dali::Layer toolBarLayer = Dali::Layer::New();
66   toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
67   toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
68   toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::Property::Size, Dali::ParentSource( Dali::Actor::Property::Size ), Dali::SourceWidthFixedHeight( style.mToolBarHeight  ) ) );
69   toolBarLayer.SetSize( 0.0f, style.mToolBarHeight );
70
71   // Raise tool bar layer to the top.
72   toolBarLayer.RaiseToTop();
73
74   // Tool bar
75   Dali::Image image = Dali::ResourceImage::New( toolbarImagePath );
76   Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
77   toolBar = Dali::Toolkit::ToolBar::New();
78   toolBar.SetBackground( toolBarBackground );
79   toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
80   toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
81   toolBar.SetSize( 0.0f, style.mToolBarHeight );
82   toolBar.SetSizeMode( Dali::SIZE_EQUAL_TO_PARENT );
83   toolBarBackground.SetSortModifier(1.0f);
84
85   // Add the tool bar to the too bar layer.
86   toolBarLayer.Add( toolBar );
87
88   // Tool bar text.
89   if( !title.empty() )
90   {
91     // TODO
92   }
93
94   return toolBarLayer;
95 }
96
97 Dali::Layer CreateView( Dali::Application& application,
98                         Dali::Toolkit::View& view,
99                         Dali::Toolkit::ToolBar& toolBar,
100                         const std::string& backgroundImagePath,
101                         const std::string& toolbarImagePath,
102                         const std::string& title,
103                         const ViewStyle& style = DEFAULT_VIEW_STYLE )
104 {
105   Dali::Stage stage = Dali::Stage::GetCurrent();
106
107   // Create default View.
108   view = Dali::Toolkit::View::New();
109
110   // Add the view to the stage before setting the background.
111   stage.Add( view );
112
113   // Set background image.
114   if ( ! backgroundImagePath.empty() )
115   {
116     Dali::Image backgroundImage = Dali::ResourceImage::New( backgroundImagePath );
117     Dali::ImageActor backgroundImageActor = Dali::ImageActor::New( backgroundImage );
118     view.SetBackground( backgroundImageActor );
119   }
120
121   // FIXME
122   // Connects the orientation signal with the View::OrientationChanged method.
123   //application.GetOrientation().ChangedSignal().Connect( &view, &Dali::Toolkit::View::OrientationChanged );
124
125   // Create default ToolBar
126   Dali::Layer toolBarLayer = CreateToolbar( toolBar, toolbarImagePath, title, style );
127
128   // Add tool bar layer to the view.
129   view.AddContentLayer( toolBarLayer );
130
131
132
133   // Create a content layer.
134   Dali::Layer contentLayer = Dali::Layer::New();
135   contentLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
136   contentLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
137   contentLayer.SetSizeMode( Dali::SIZE_EQUAL_TO_PARENT );
138   view.AddContentLayer( contentLayer );
139   contentLayer.LowerBelow( toolBarLayer );
140
141   return contentLayer;
142 }
143
144 } // DemoHelper
145
146 #endif // __DALI_DEMO_HELPER_VIEW_H__