1 #ifndef DALI_DEMO_HELPER_VIEW_H
2 #define DALI_DEMO_HELPER_VIEW_H
5 * Copyright (c) 2020 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>
22 #include <dali-toolkit/devel-api/controls/tool-bar/tool-bar.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)
39 float mToolBarButtonPercentage; ///< The tool bar button width is a percentage of the tool bar width.
40 float mToolBarTitlePercentage; ///< The tool bar title width is a percentage of the tool bar width.
41 float mToolBarHeight; ///< The tool bar height (in pixels).
42 float mToolBarPadding; ///< The tool bar padding (in pixels)..
45 const ViewStyle DEFAULT_VIEW_STYLE(0.1f, 0.7f, 80.f, 4.f);
47 const char* DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
48 const float DEFAULT_TEXT_STYLE_POINT_SIZE(8.0f);
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);
53 Dali::Layer CreateToolbar(Dali::Toolkit::ToolBar& toolBar,
54 const std::string& toolbarImagePath,
55 const std::string& title,
56 const ViewStyle& style)
58 Dali::Layer toolBarLayer = Dali::Layer::New();
59 toolBarLayer.SetProperty(Dali::Actor::Property::NAME, "TOOLBAR_LAYER");
60 toolBarLayer.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::TOP_CENTER);
61 toolBarLayer.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::TOP_CENTER);
62 toolBarLayer.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::WIDTH);
63 toolBarLayer.SetProperty(Dali::Actor::Property::SIZE, Dali::Vector2(0.0f, style.mToolBarHeight));
65 // Raise tool bar layer to the top.
66 toolBarLayer.RaiseToTop();
69 toolBar = Dali::Toolkit::ToolBar::New();
70 toolBar.SetProperty(Dali::Actor::Property::NAME, "TOOLBAR");
71 Dali::Property::Map background;
72 background["url"] = toolbarImagePath;
73 toolBar.SetProperty(Dali::Toolkit::Control::Property::BACKGROUND, background);
74 toolBar.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::TOP_CENTER);
75 toolBar.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::TOP_CENTER);
76 toolBar.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS);
78 // Add the tool bar to the tool bar layer.
79 toolBarLayer.Add(toolBar);
84 Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New();
85 label.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::TOP_LEFT);
86 label.SetStyleName("ToolbarLabel");
87 label.SetProperty(Dali::Toolkit::TextLabel::Property::TEXT, title);
88 label.SetProperty(Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
89 label.SetProperty(Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
90 label.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT);
92 // Add title to the tool bar.
93 const float padding(style.mToolBarPadding);
94 toolBar.AddControl(label, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HORIZONTAL_CENTER, Dali::Toolkit::Alignment::Padding(padding, padding, padding, padding));
100 Dali::Layer CreateView(Dali::Application& application,
101 Dali::Toolkit::Control& view,
102 Dali::Toolkit::ToolBar& toolBar,
103 const std::string& backgroundImagePath,
104 const std::string& toolbarImagePath,
105 const std::string& title,
106 const ViewStyle& style = DEFAULT_VIEW_STYLE)
108 Dali::Window window = application.GetWindow();
110 // Create default View.
111 view = Dali::Toolkit::Control::New();
112 view.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::CENTER);
113 view.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::CENTER);
114 view.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS);
116 // Add the view to the window before setting the background.
119 // Set background image, loading it at screen resolution:
120 if(!backgroundImagePath.empty())
122 Dali::Property::Map map;
123 map[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
124 map[Dali::Toolkit::ImageVisual::Property::URL] = backgroundImagePath;
125 map[Dali::Toolkit::ImageVisual::Property::DESIRED_WIDTH] = window.GetSize().GetWidth();
126 map[Dali::Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = window.GetSize().GetHeight();
127 map[Dali::Toolkit::ImageVisual::Property::FITTING_MODE] = Dali::FittingMode::SCALE_TO_FILL;
128 map[Dali::Toolkit::ImageVisual::Property::SAMPLING_MODE] = Dali::SamplingMode::BOX_THEN_LINEAR;
129 map[Dali::Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING] = true;
130 view.SetProperty(Dali::Toolkit::Control::Property::BACKGROUND, map);
133 // Create default ToolBar
134 Dali::Layer toolBarLayer = CreateToolbar(toolBar, toolbarImagePath, title, style);
136 // Add tool bar layer to the view.
137 view.Add(toolBarLayer);
139 // Create a content layer.
140 Dali::Layer contentLayer = Dali::Layer::New();
141 contentLayer.SetProperty(Dali::Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::CENTER);
142 contentLayer.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::CENTER);
143 contentLayer.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS);
144 view.Add(contentLayer);
145 contentLayer.LowerBelow(toolBarLayer);
150 Dali::Toolkit::TextLabel CreateToolBarLabel(const std::string& text)
152 Dali::Toolkit::TextLabel label = Dali::Toolkit::TextLabel::New(text);
153 label.SetStyleName("ToolbarLabel");
154 label.SetProperty(Dali::Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
155 label.SetProperty(Dali::Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
156 label.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT);
161 } // namespace DemoHelper
163 #endif // DALI_DEMO_HELPER_VIEW_H