text-view-example.cpp
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "shared/view.h"
#include <dali/dali.h>
#include <dali-toolkit/dali-toolkit.h>
using namespace Dali;
namespace
{
const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
const char* const TOOLBAR_TITLE = "Text View";
const int TOOLBAR_HEIGHT = 80;
const int NUM_TABLE_ROWS = 12;
const int NUM_TABLE_COLUMNS = 12;
} // namespace
/**
* Helper structs
*/
struct TableCell
{
unsigned int row;
unsigned int column;
unsigned int rowSpan;
unsigned int columnSpan;
};
struct TableString
{
std::string fontName;
std::string fontStyle;
float fontSize;
Dali::TextStyle::Weight fontWeight;
Vector4 fontColour;
std::string text;
Toolkit::Alignment::Type horizontalAlignment;
Toolkit::Alignment::Type verticalAlignment;
float padding;
float orientation;
TableCell cellPosition;
};
namespace
{
// Font Font style Font point size Font weight Text colour Text Horizontal alignment Vertical alignment Padding Orientation Table cell position and span
const TableString TABLE_STRINGS[] = { { "HelveticaNue", "Regular", 8.0f, Dali::TextStyle::REGULAR, Vector4( 0.5f, 1.0f, 0.0f, 1.0f ), "Howdy", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 0.0f, { 0, 0, 2, 4 } },
{ "HelveticaNue", "Regular", 13.0f, Dali::TextStyle::EXTRABOLD, Vector4( 1.0f, 0.5f, 0.0f, 1.0f ), "Hello!", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 0.0f, { 0, 4, 1, 4 } },
{ "HelveticaNue", "Regular", 18.0f, Dali::TextStyle::REGULAR, Vector4( 1.0f, 0.75f, 0.25f, 1.0f ), "שלום!", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 0.0f, { 1, 4, 1, 4 } },
{ "HelveticaNue", "Regular", 8.0f, Dali::TextStyle::REGULAR, Vector4( 0.5f, 0.0f, 1.0f, 1.0f ), "Hi there", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalBottom, 10.0f, 0.0f, { 0, 8, 2, 4 } },
{ "HelveticaNue", "Regular", 12.0f, Dali::TextStyle::REGULAR, Vector4( 0.5f, 1.0f, 1.0f, 1.0f ), "Hola", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 90.0f, { 2, 0, 4, 2 } },
{ "HelveticaNue", "Regular", 18.0f, Dali::TextStyle::BOLD, Vector4( 0.5f, 1.0f, 0.5f, 1.0f ), "Bonjour", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 0.0f, { 2, 2, 2, 4 } },
{ "HelveticaNue", "Regular", 12.0f, Dali::TextStyle::REGULAR, Vector4( 1.0f, 1.0f, 0.5f, 1.0f ), "Ciao", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 0.0f, { 2, 7, 2, 3 } },
{ "HelveticaNue", "Regular", 26.0f, Dali::TextStyle::EXTRABLACK, Vector4( 0.5f, 0.0f, 0.0f, 1.0f ), "Hello", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 0.0f, { 4, 2, 1, 6 } },
{ "HelveticaNue", "Regular", 8.0f, Dali::TextStyle::DEMIBOLD, Vector4( 0.0f, 0.5f, 0.0f, 1.0f ), "Top of the morning to you", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 90.0f, { 4, 10, 8, 2 } },
{ "HelveticaNue", "Regular", 13.0f, Dali::TextStyle::DEMIBOLD, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ), "हैलो", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 0.0f, { 6, 1, 1, 3 } },
{ "HelveticaNue", "Regular", 8.0f, Dali::TextStyle::DEMIBOLD, Vector4( 1.0f, 1.0f, 0.0f, 1.0f ), "สวัสดี", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 90.0f, { 6, 5, 2, 1 } },
{ "HelveticaNue", "Regular", 18.0f, Dali::TextStyle::REGULAR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ), "你好", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalBottom, 10.0f, 0.0f, { 6, 6, 1, 3 } },
{ "HelveticaNue", "Regular", 34.0f, Dali::TextStyle::REGULAR, Vector4( 0.0f, 0.0f, 1.0f, 1.0f ), "G'day", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 0.0f, { 7, 0, 2, 10 } },
{ "HelveticaNue", "Regular", 16.0f, Dali::TextStyle::EXTRABLACK, Vector4( 0.0f, 0.5f, 1.0f, 1.0f ), "مرحبا", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 0.0f, { 9, 1, 2, 4 } },
{ "HelveticaNue", "Regular", 10.0f, Dali::TextStyle::EXTRABLACK, Vector4( 1.0f, 0.0f, 0.0f, 1.0f ), "こんにちは", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalCenter, 10.0f, 0.0f, { 10, 0, 2, 6 } },
{ "HelveticaNue", "Regular", 14.0f, Dali::TextStyle::REGULAR, Vector4( 0.0f, 1.0f, 0.0f, 1.0f ), "aloha", Toolkit::Alignment::HorizontalCenter, Toolkit::Alignment::VerticalTop, 10.0f, 0.0f, { 10, 6, 2, 4 } }
};
const int NUM_TABLE_STRINGS = sizeof( TABLE_STRINGS ) / sizeof( TABLE_STRINGS[0] );
} // namespace
/**
* This example shows the usage of TextView.
*/
class TextViewController: public ConnectionTracker
{
public:
TextViewController( Application& application )
: mApplication( application )
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &TextViewController::Create );
}
~TextViewController()
{
// Nothing to do here
}
void Create( Application& application )
{
// The Init signal is received once (only) during the Application lifetime
Stage stage = Stage::GetCurrent();
// Respond to key events
stage.KeyEventSignal().Connect(this, &TextViewController::OnKeyEvent);
// Creates a default view with a default tool bar.
// The view is added to the stage.
mContentLayer = DemoHelper::CreateView( application,
mView,
mToolBar,
BACKGROUND_IMAGE,
TOOLBAR_IMAGE,
TOOLBAR_TITLE );
// Create a table view the height of the stage
Toolkit::TableView textContainer = Toolkit::TableView::New( NUM_TABLE_ROWS, NUM_TABLE_COLUMNS );
textContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
textContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
textContainer.SetPosition( 0, TOOLBAR_HEIGHT );
textContainer.SetSize( stage.GetSize().width, stage.GetSize().height - TOOLBAR_HEIGHT );
mContentLayer.Add( textContainer );
// Add data
for( int i = 0; i < NUM_TABLE_STRINGS; ++i )
{
const TableString& tableString = TABLE_STRINGS[ i ];
Dali::TextStyle textStyle;
textStyle.SetFontName( tableString.fontName );
textStyle.SetFontStyle( tableString.fontStyle );
textStyle.SetFontPointSize( Dali::PointSize( DemoHelper::ScalePointSize( tableString.fontSize ) ) );
textStyle.SetWeight( tableString.fontWeight );
textStyle.SetTextColor( tableString.fontColour );
Toolkit::TextView textView = Toolkit::TextView::New( tableString.text );
textView.SetStyleToCurrentText( textStyle );
textView.SetRotation( Dali::Degree( tableString.orientation ), Vector3( 0.0f, 0.0f, 1.0f ) );
Toolkit::Alignment alignmentContainer = Toolkit::Alignment::New( tableString.horizontalAlignment, tableString.verticalAlignment );
alignmentContainer.SetPadding( Toolkit::Alignment::Padding( tableString.padding, tableString.padding, tableString.padding, tableString.padding ) );
alignmentContainer.SetScaling( Toolkit::Alignment::ScaleToFill );
alignmentContainer.Add( textView );
textContainer.AddChild( alignmentContainer, Toolkit::TableView::CellPosition( tableString.cellPosition.row, tableString.cellPosition.column, tableString.cellPosition.rowSpan, tableString.cellPosition.columnSpan ) );
}
}
void OnKeyEvent( const KeyEvent& event )
{
if( event.state == KeyEvent::Down )
{
if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
{
// Exit application when click back or escape.
mApplication.Quit();
}
}
}
private:
Application& mApplication;
Toolkit::View mView; ///< The View instance.
Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
Layer mContentLayer; ///< Content layer
};
void RunTest( Application& application )
{
TextViewController test( application );
application.MainLoop();
}
// Entry point for Linux & Tizen applications
//
int main( int argc, char **argv )
{
Application application = Application::New( &argc, &argv );
RunTest( application );
return 0;
}