Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / simple-bitmap-font-text-label / simple-text-label-example.cpp
1 /*
2  * Copyright (c) 2020 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-toolkit/devel-api/text/bitmap-font.h>
27 #include <dali/devel-api/text-abstraction/bitmap-font.h>
28 #include <dali/devel-api/text-abstraction/font-client.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   SimpleTextLabelExample(Application& application)
40   : mApplication(application)
41   {
42     // Connect to the Application's Init signal
43     mApplication.InitSignal().Connect(this, &SimpleTextLabelExample::Create);
44   }
45
46   ~SimpleTextLabelExample()
47   {
48     // Nothing to do here.
49   }
50
51   /**
52    * One-time setup in response to Application InitSignal.
53    */
54   void Create(Application& application)
55   {
56     Window window = application.GetWindow();
57
58     window.KeyEventSignal().Connect(this, &SimpleTextLabelExample::OnKeyEvent);
59
60     TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
61
62     DevelText::BitmapFontDescription fontDescription;
63     fontDescription.name               = "Digits";
64     fontDescription.underlinePosition  = 0.f;
65     fontDescription.underlineThickness = 0.f;
66
67     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0030.png", "0", 34.f, 0.f});
68     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0031.png", "1", 34.f, 0.f});
69     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0032.png", "2", 34.f, 0.f});
70     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0033.png", "3", 34.f, 0.f});
71     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0034.png", "4", 34.f, 0.f});
72     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0035.png", "5", 34.f, 0.f});
73     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0036.png", "6", 34.f, 0.f});
74     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0037.png", "7", 34.f, 0.f});
75     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0038.png", "8", 34.f, 0.f});
76     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0039.png", "9", 34.f, 0.f});
77     fontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u003a.png", ":", 34.f, 0.f});
78
79     DevelText::BitmapFontDescription colorFontDescription;
80     colorFontDescription.name               = "DigitsColor";
81     colorFontDescription.underlinePosition  = 0.f;
82     colorFontDescription.underlineThickness = 0.f;
83     colorFontDescription.isColorFont        = true;
84
85     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0030_color.png", "0", 34.f, 0.f});
86     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0031_color.png", "1", 34.f, 0.f});
87     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0032_color.png", "2", 34.f, 0.f});
88     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0033_color.png", "3", 34.f, 0.f});
89     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0034_color.png", "4", 34.f, 0.f});
90     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0035_color.png", "5", 34.f, 0.f});
91     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0036_color.png", "6", 34.f, 0.f});
92     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0037_color.png", "7", 34.f, 0.f});
93     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0038_color.png", "8", 34.f, 0.f});
94     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u0039_color.png", "9", 34.f, 0.f});
95     colorFontDescription.glyphs.push_back({DEMO_IMAGE_DIR "u003a_color.png", ":", 34.f, 0.f});
96
97     TextAbstraction::BitmapFont bitmapFont;
98     TextAbstraction::BitmapFont bitmapColorFont;
99
100     DevelText::CreateBitmapFont(fontDescription, bitmapFont);
101     DevelText::CreateBitmapFont(colorFontDescription, bitmapColorFont);
102
103     fontClient.GetFontId(bitmapFont);
104     fontClient.GetFontId(bitmapColorFont);
105
106     TextLabel label01 = TextLabel::New();
107     label01.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
108     label01.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
109     label01.SetProperty(Actor::Property::SIZE, Vector2(400.f, 50.f));
110     label01.SetProperty(Actor::Property::POSITION, Vector2(0.f, -100.f));
111     label01.SetProperty(TextLabel::Property::MULTI_LINE, true);
112
113     label01.SetProperty(TextLabel::Property::ENABLE_MARKUP, true);
114     label01.SetProperty(TextLabel::Property::TEXT, "012<color 'value'='green'>345</color>6789:");
115     label01.SetProperty(TextLabel::Property::TEXT_COLOR, Color::RED);
116     label01.SetProperty(TextLabel::Property::FONT_FAMILY, "Digits");
117
118     label01.SetBackgroundColor(Color::BLACK);
119
120     window.Add(label01);
121
122     TextLabel label02 = TextLabel::New();
123     label02.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
124     label02.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
125     label02.SetProperty(Actor::Property::SIZE, Vector2(400.f, 50.f));
126     label02.SetProperty(Actor::Property::POSITION, Vector2(0.f, -50.f));
127     label02.SetProperty(TextLabel::Property::MULTI_LINE, true);
128
129     label02.SetProperty(TextLabel::Property::TEXT, "0123456789:");
130     label02.SetProperty(TextLabel::Property::TEXT_COLOR, Color::WHITE);
131     label02.SetProperty(TextLabel::Property::FONT_FAMILY, "DigitsColor");
132
133     label02.SetBackgroundColor(Color::BLACK);
134
135     window.Add(label02);
136
137     TextLabel label03 = TextLabel::New();
138     label03.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
139     label03.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
140     label03.SetProperty(Actor::Property::SIZE, Vector2(400.f, 50.f));
141     label03.SetProperty(Actor::Property::POSITION, Vector2(0.f, 0.f));
142     label03.SetProperty(TextLabel::Property::MULTI_LINE, true);
143
144     label03.SetProperty(TextLabel::Property::TEXT, "0123456789:");
145     label03.SetProperty(TextLabel::Property::FONT_FAMILY, "Digits");
146
147     label03.SetBackgroundColor(Color::WHITE);
148
149     window.Add(label03);
150
151     TextLabel label04 = TextLabel::New();
152     label04.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
153     label04.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
154     label04.SetProperty(Actor::Property::SIZE, Vector2(400.f, 50.f));
155     label04.SetProperty(Actor::Property::POSITION, Vector2(0.f, 50.f));
156     label04.SetProperty(TextLabel::Property::MULTI_LINE, true);
157
158     label04.SetProperty(TextLabel::Property::TEXT, "0123456789:");
159     label04.SetProperty(TextLabel::Property::FONT_FAMILY, "Digits");
160     label04.SetProperty(TextLabel::Property::TEXT_COLOR, Color::WHITE);
161
162     label04.SetBackgroundColor(Color::BLACK);
163
164     window.Add(label04);
165   }
166
167   /**
168    * Main key event handler
169    */
170   void OnKeyEvent(const KeyEvent& event)
171   {
172     if(event.GetState() == KeyEvent::DOWN)
173     {
174       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
175       {
176         mApplication.Quit();
177       }
178     }
179   }
180
181 private:
182   Application& mApplication;
183 };
184
185 void RunTest(Application& application)
186 {
187   SimpleTextLabelExample test(application);
188
189   application.MainLoop();
190 }
191
192 /** Entry point for Linux & Tizen applications */
193 int DALI_EXPORT_API main(int argc, char** argv)
194 {
195   Application application = Application::New(&argc, &argv);
196
197   RunTest(application);
198
199   return 0;
200 }