0bdcc9bac0210ae1418379d68c40a3e87afbe47a
[platform/core/uifw/dali-demo.git] / examples / simple-bitmap-font-text-label / simple-text-label-example.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 /**
19  * @file simple-text-label-example.cpp
20  * @brief Basic usage of SimpleTextLabel control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25
26 #include <dali/devel-api/text-abstraction/font-client.h>
27 #include <dali/devel-api/text-abstraction/bitmap-font.h>
28 #include <dali-toolkit/devel-api/text/bitmap-font.h>
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 /**
34  * @brief The main class of the demo.
35  */
36 class SimpleTextLabelExample : public ConnectionTracker
37 {
38 public:
39
40   SimpleTextLabelExample( Application& application )
41   : mApplication( application )
42   {
43     // Connect to the Application's Init signal
44     mApplication.InitSignal().Connect( this, &SimpleTextLabelExample::Create );
45   }
46
47   ~SimpleTextLabelExample()
48   {
49     // Nothing to do here.
50   }
51
52   /**
53    * One-time setup in response to Application InitSignal.
54    */
55   void Create( Application& application )
56   {
57     Stage stage = Stage::GetCurrent();
58
59     stage.KeyEventSignal().Connect(this, &SimpleTextLabelExample::OnKeyEvent);
60
61     TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
62
63     DevelText::BitmapFontDescription fontDescription;
64     fontDescription.name = "Digits";
65     fontDescription.underlinePosition = 0.f;
66     fontDescription.underlineThickness = 0.f;
67
68     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0030.png", "0", 34.f, 0.f } );
69     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0031.png", "1", 34.f, 0.f } );
70     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0032.png", "2", 34.f, 0.f } );
71     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0033.png", "3", 34.f, 0.f } );
72     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0034.png", "4", 34.f, 0.f } );
73     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0035.png", "5", 34.f, 0.f } );
74     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0036.png", "6", 34.f, 0.f } );
75     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0037.png", "7", 34.f, 0.f } );
76     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0038.png", "8", 34.f, 0.f } );
77     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0039.png", "9", 34.f, 0.f } );
78     fontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u003a.png", ":", 34.f, 0.f } );
79
80
81     DevelText::BitmapFontDescription colorFontDescription;
82     colorFontDescription.name = "DigitsColor";
83     colorFontDescription.underlinePosition = 0.f;
84     colorFontDescription.underlineThickness = 0.f;
85     colorFontDescription.isColorFont = true;
86
87     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0030_color.png", "0", 34.f, 0.f } );
88     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0031_color.png", "1", 34.f, 0.f } );
89     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0032_color.png", "2", 34.f, 0.f } );
90     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0033_color.png", "3", 34.f, 0.f } );
91     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0034_color.png", "4", 34.f, 0.f } );
92     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0035_color.png", "5", 34.f, 0.f } );
93     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0036_color.png", "6", 34.f, 0.f } );
94     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0037_color.png", "7", 34.f, 0.f } );
95     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0038_color.png", "8", 34.f, 0.f } );
96     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u0039_color.png", "9", 34.f, 0.f } );
97     colorFontDescription.glyphs.push_back( { DEMO_IMAGE_DIR "u003a_color.png", ":", 34.f, 0.f } );
98
99     TextAbstraction::BitmapFont bitmapFont;
100     TextAbstraction::BitmapFont bitmapColorFont;
101
102     DevelText::CreateBitmapFont( fontDescription, bitmapFont );
103     DevelText::CreateBitmapFont( colorFontDescription, bitmapColorFont );
104
105     fontClient.GetFontId( bitmapFont );
106     fontClient.GetFontId( bitmapColorFont );
107
108     TextLabel label01 = TextLabel::New();
109     label01.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
110     label01.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
111     label01.SetSize( 400.f, 50.f );
112     label01.SetPosition( 0.f, -100.f );
113     label01.SetProperty( TextLabel::Property::MULTI_LINE, true );
114
115     label01.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
116     label01.SetProperty( TextLabel::Property::TEXT, "012<color 'value'='green'>345</color>6789:" );
117     label01.SetProperty( TextLabel::Property::TEXT_COLOR, Color::RED );
118     label01.SetProperty( TextLabel::Property::FONT_FAMILY, "Digits" );
119
120     label01.SetBackgroundColor( Color::BLACK );
121
122     stage.Add( label01 );
123
124
125     TextLabel  label02 = TextLabel::New();
126     label02.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
127     label02.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
128     label02.SetSize( 400.f, 50.f );
129     label02.SetPosition( 0.f, -50.f );
130     label02.SetProperty( TextLabel::Property::MULTI_LINE, true );
131
132     label02.SetProperty( TextLabel::Property::TEXT, "0123456789:" );
133     label02.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
134     label02.SetProperty( TextLabel::Property::FONT_FAMILY, "DigitsColor" );
135
136     label02.SetBackgroundColor( Color::BLACK );
137
138     stage.Add( label02 );
139
140     TextLabel  label03 = TextLabel::New();
141     label03.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
142     label03.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
143     label03.SetSize( 400.f, 50.f );
144     label03.SetPosition( 0.f, 0.f );
145     label03.SetProperty( TextLabel::Property::MULTI_LINE, true );
146
147     label03.SetProperty( TextLabel::Property::TEXT, "0123456789:" );
148     label03.SetProperty( TextLabel::Property::FONT_FAMILY, "Digits" );
149
150     label03.SetBackgroundColor( Color::WHITE );
151
152     stage.Add( label03 );
153
154     TextLabel  label04 = TextLabel::New();
155     label04.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
156     label04.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
157     label04.SetSize( 400.f, 50.f );
158     label04.SetPosition( 0.f, 50.f );
159     label04.SetProperty( TextLabel::Property::MULTI_LINE, true );
160
161     label04.SetProperty( TextLabel::Property::TEXT, "0123456789:" );
162     label04.SetProperty( TextLabel::Property::FONT_FAMILY, "Digits" );
163     label04.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
164
165     label04.SetBackgroundColor( Color::BLACK );
166
167     stage.Add( label04 );
168  }
169
170   /**
171    * Main key event handler
172    */
173   void OnKeyEvent(const KeyEvent& event)
174   {
175     if(event.state == KeyEvent::Down)
176     {
177       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
178       {
179         mApplication.Quit();
180       }
181     }
182   }
183
184 private:
185
186   Application& mApplication;
187 };
188
189 void RunTest( Application& application )
190 {
191   SimpleTextLabelExample test( application );
192
193   application.MainLoop();
194 }
195
196 /** Entry point for Linux & Tizen applications */
197 int DALI_EXPORT_API main( int argc, char **argv )
198 {
199   Application application = Application::New( &argc, &argv );
200
201   RunTest( application );
202
203   return 0;
204 }