Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / text-fonts / text-fonts-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 text-fonts-example.cpp
20  * @brief Example of various TextLabel each with different font set ups,
21  *        enables Testing of Font when the system font changes.
22           The first label is free, with no font family set, it could use the default system font and change as it changes.
23           The second label has it's font family set via the demo json file. It should not change when the system font changes.
24           The third label has it's font family set in code via SetProperty. It also should not change when the system font changes.
25           The forth label is not shown until the button along the bottom is pressed, it has no font set so the newly created label should use the system font,
26           Pressing the button again resets and unparents that button and then re-adds it.
27  */
28
29 // EXTERNAL INCLUDES
30 #include <dali-toolkit/dali-toolkit.h>
31 #include <iostream>
32
33 // INTERNAL INCLUDES
34 #include "shared/multi-language-strings.h"
35 #include "shared/view.h"
36
37 using namespace Dali;
38 using namespace Dali::Toolkit;
39 using namespace MultiLanguageStrings;
40
41 namespace
42 {
43 const char* const LABEL_TEXT        = "A Quick Fox";
44 const char* const LABEL_TEXT_MIXED  = "Fox 구미호";
45 const char* const LABEL_TEXT_KOREAN = "구미호";
46 } // namespace
47
48 /**
49  * @brief The main class of the demo.
50  */
51 class TextFontsExample : public ConnectionTracker
52 {
53 public:
54   TextFontsExample(Application& application)
55   : mApplication(application),
56     mToggle(true)
57   {
58     // Connect to the Application's Init signal
59     mApplication.InitSignal().Connect(this, &TextFontsExample::Create);
60   }
61
62   ~TextFontsExample()
63   {
64     // Nothing to do here.
65   }
66
67   void CreateTextLabel(TextLabel& textLabel, std::string textString, const Vector4& color, bool infoLabel = false)
68   {
69     textLabel = TextLabel::New(textString);
70     textLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
71     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
72     textLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
73     textLabel.SetProperty(TextLabel::Property::MULTI_LINE, true);
74     if(infoLabel)
75     {
76       textLabel.SetProperty(TextLabel::Property::TEXT_COLOR, Color::WHITE);
77       textLabel.SetProperty(TextLabel::Property::POINT_SIZE, 12.0f);
78       textLabel.SetProperty(TextLabel::Property::FONT_FAMILY, "SamsungOneUI");
79     }
80     else
81     {
82       Property::Map shadowMap;
83       shadowMap.Insert("color", Color::BLACK);
84       shadowMap.Insert("offset", Vector2(0.3f, 0.3f));
85       textLabel.SetProperty(TextLabel::Property::SHADOW, shadowMap);
86       textLabel.SetProperty(TextLabel::Property::TEXT_COLOR, Color::BLUE);
87     }
88     textLabel.SetBackgroundColor(color);
89   }
90
91   void CreateContainer(Control& container, const Vector2 size)
92   {
93     container = Control::New();
94     container.SetProperty(Actor::Property::SIZE, size);
95     container.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
96     container.SetProperty(Actor::Property::DRAW_MODE, DrawMode::OVERLAY_2D);
97   }
98
99   void CreateFolderButton(PushButton& button)
100   {
101     button = PushButton::New();
102     button.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
103     button.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
104     button.SetProperty(Actor::Property::SIZE, Vector2(50.0f, 50.0f));
105   }
106
107   bool OnButtonClicked(Toolkit::Button button)
108   {
109     if(mLabel4)
110     {
111       UnparentAndReset(mLabel4);
112     }
113
114     if(!mContainer4)
115     {
116       CreateContainer(mContainer4, mLayoutSize);
117       Window  window     = mApplication.GetWindow();
118       Vector2 windowSize = window.GetSize();
119       mContainer4.SetProperty(Actor::Property::POSITION, Vector2(0, windowSize.height * 0.25f * 3));
120       window.Add(mContainer4);
121       // Info
122       CreateContainer(mContainer4Info, mLayoutSize);
123       mContainer4Info.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT);
124       mContainer4.Add(mContainer4Info);
125       CreateTextLabel(mLabel4Info, "system free", Color::BLACK, true);
126       mContainer4Info.Add(mLabel4Info);
127     }
128
129     if(mToggle)
130     {
131       CreateTextLabel(mLabel4, LABEL_TEXT_KOREAN, Color::WHITE);
132       mToggle = false;
133     }
134     else
135     {
136       CreateTextLabel(mLabel4, LABEL_TEXT_MIXED, Color::WHITE);
137       mToggle = true;
138     }
139
140     mContainer4.Add(mLabel4);
141
142     return true;
143   }
144
145   /**
146    * One-time setup in response to Application InitSignal.
147    */
148   void Create(Application& application)
149   {
150     Window  window     = application.GetWindow();
151     Vector2 windowSize = window.GetSize();
152
153     window.KeyEventSignal().Connect(this, &TextFontsExample::OnKeyEvent);
154
155     CreateFolderButton(mButton);
156     mButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
157     mButton.ClickedSignal().Connect(this, &TextFontsExample::OnButtonClicked);
158     window.Add(mButton);
159
160     mLayoutSize = Vector2(windowSize.width * 0.5f, windowSize.height * 0.10f);
161     CreateContainer(mContainer, mLayoutSize);
162     CreateContainer(mContainer2, mLayoutSize);
163     CreateContainer(mContainer3, mLayoutSize);
164
165     // Info about Text Label and if font should be fixed or free to change with system
166     CreateContainer(mContainerInfo, mLayoutSize);
167     CreateContainer(mContainer2Info, mLayoutSize);
168     CreateContainer(mContainer3Info, mLayoutSize);
169     mContainerInfo.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT);
170     mContainer2Info.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT);
171     mContainer3Info.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT);
172     mContainer.Add(mContainerInfo);
173     mContainer2.Add(mContainer2Info);
174     mContainer3.Add(mContainer3Info);
175     CreateTextLabel(mLabelInfo, "system free", Color::BLACK, true);
176     CreateTextLabel(mLabel2Info, "json fixed", Color::BLACK, true);
177     CreateTextLabel(mLabel3Info, "SetProp fixed", Color::BLACK, true);
178     mContainerInfo.Add(mLabelInfo);
179     mContainer2Info.Add(mLabel2Info);
180     mContainer3Info.Add(mLabel3Info);
181
182     window.Add(mContainer);
183     window.Add(mContainer2);
184     window.Add(mContainer3);
185
186     CreateTextLabel(mLabel, LABEL_TEXT, Color::WHITE);
187
188     CreateTextLabel(mLabel2, LABEL_TEXT, Color::WHITE);
189     mLabel2.SetStyleName("TextLabelRosemary");
190
191     CreateTextLabel(mLabel3, LABEL_TEXT, Color::WHITE);
192     mLabel3.SetProperty(TextLabel::Property::FONT_FAMILY, "SamsungOneUI");
193
194     mContainer.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
195     mContainer2.SetProperty(Actor::Property::POSITION, Vector2(0, windowSize.height * 0.25f));
196     mContainer3.SetProperty(Actor::Property::POSITION, Vector2(0, windowSize.height * 0.25f * 2));
197
198     mContainer.Add(mLabel);
199     mContainer2.Add(mLabel2);
200     mContainer3.Add(mLabel3);
201   }
202
203   /**
204    * Main key event handler
205    */
206   void OnKeyEvent(const KeyEvent& event)
207   {
208     if(event.GetState() == KeyEvent::DOWN)
209     {
210       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
211       {
212         mApplication.Quit();
213       }
214     }
215   }
216
217 private:
218   Application& mApplication;
219
220   PushButton mButton;
221
222   TextLabel mLabel;
223   TextLabel mLabel2;
224   TextLabel mLabel3;
225
226   TextLabel mLabel4;
227
228   Control mContainer;
229   Control mContainer2;
230   Control mContainer3;
231   Control mContainer4;
232
233   Control mContainerInfo;
234   Control mContainer2Info;
235   Control mContainer3Info;
236   Control mContainer4Info;
237
238   TextLabel mLabelInfo;
239   TextLabel mLabel2Info;
240   TextLabel mLabel3Info;
241   TextLabel mLabel4Info;
242
243   Vector2 mLayoutSize;
244
245   bool mToggle;
246 };
247
248 int DALI_EXPORT_API main(int argc, char** argv)
249 {
250   Application      application = Application::New(&argc, &argv, DEMO_THEME_PATH);
251   TextFontsExample test(application);
252   application.MainLoop();
253   return 0;
254 }