Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-demo.git] / examples / text-fonts / text-fonts-example.cpp
1 /*
2  * Copyright (c) 2017 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 }
47
48 /**
49  * @brief The main class of the demo.
50  */
51 class TextFontsExample : public ConnectionTracker
52 {
53 public:
54
55   TextFontsExample( Application& application )
56   : mApplication( application ),
57     mToggle(true)
58   {
59     // Connect to the Application's Init signal
60     mApplication.InitSignal().Connect( this, &TextFontsExample::Create );
61   }
62
63   ~TextFontsExample()
64   {
65     // Nothing to do here.
66   }
67
68   void CreateTextLabel( TextLabel& textLabel, std::string textString, const Vector4& color, bool infoLabel=false )
69   {
70     textLabel = TextLabel::New( textString );
71     textLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
72     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
73     textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
74     textLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
75     if ( infoLabel )
76     {
77       textLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
78       textLabel.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
79       textLabel.SetProperty( TextLabel::Property::FONT_FAMILY, "SamsungOneUI" );
80     }
81     else
82     {
83       Property::Map shadowMap;
84       shadowMap.Insert( "color", Color::BLACK );
85       shadowMap.Insert( "offset", Vector2( 0.3f, 0.3f ) );
86       textLabel.SetProperty( TextLabel::Property::SHADOW, shadowMap );
87       textLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
88     }
89     textLabel.SetBackgroundColor( color );
90   }
91
92   void CreateContainer( Control& container, const Vector2 size )
93   {
94     container = Control::New();
95     container.SetSize( size );
96     container.SetAnchorPoint( AnchorPoint::TOP_LEFT );
97     container.SetDrawMode( DrawMode::OVERLAY_2D );
98   }
99
100   void CreateFolderButton( PushButton& button )
101   {
102     button = PushButton::New();
103     button.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
104     button.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
105     button.SetSize( 50.0f, 50.0f );
106   }
107
108   bool OnButtonClicked( Toolkit::Button button )
109   {
110     if ( mLabel4 )
111     {
112       UnparentAndReset( mLabel4 );
113     }
114
115     if ( !mContainer4 )
116     {
117       CreateContainer ( mContainer4 , mLayoutSize);
118       Stage stage = Stage::GetCurrent();
119       Vector2 stageSize = stage.GetSize();
120       mContainer4.SetPosition( 0, stageSize.height*0.25f*3 );
121       stage.Add( mContainer4 );
122       // Info
123       CreateContainer ( mContainer4Info , mLayoutSize );
124       mContainer4Info.SetParentOrigin( ParentOrigin::TOP_RIGHT );
125       mContainer4.Add( mContainer4Info );
126       CreateTextLabel ( mLabel4Info, "system free", Color::BLACK, true  );
127       mContainer4Info.Add ( mLabel4Info );
128     }
129
130     if ( mToggle )
131     {
132       CreateTextLabel ( mLabel4, LABEL_TEXT_KOREAN, Color::WHITE );
133       mToggle = false;
134     }
135     else
136     {
137       CreateTextLabel ( mLabel4, LABEL_TEXT_MIXED, Color::WHITE );
138       mToggle = true;
139     }
140
141     mContainer4.Add( mLabel4 );
142
143     return true;
144   }
145
146   /**
147    * One-time setup in response to Application InitSignal.
148    */
149   void Create( Application& application )
150   {
151     Stage stage = Stage::GetCurrent();
152     Vector2 stageSize = stage.GetSize();
153
154     stage.KeyEventSignal().Connect(this, &TextFontsExample::OnKeyEvent);
155
156     CreateFolderButton ( mButton );
157     mButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
158     mButton.ClickedSignal().Connect( this, &TextFontsExample::OnButtonClicked );
159     stage.Add( mButton );
160
161     mLayoutSize = Vector2( stageSize.width*0.5f, stageSize.height*0.10f );
162     CreateContainer ( mContainer , mLayoutSize);
163     CreateContainer ( mContainer2 , mLayoutSize );
164     CreateContainer ( mContainer3 , mLayoutSize );
165
166     // Info about Text Label and if font should be fixed or free to change with system
167     CreateContainer ( mContainerInfo , mLayoutSize );
168     CreateContainer ( mContainer2Info , mLayoutSize );
169     CreateContainer ( mContainer3Info , mLayoutSize );
170     mContainerInfo.SetParentOrigin( ParentOrigin::TOP_RIGHT );
171     mContainer2Info.SetParentOrigin( ParentOrigin::TOP_RIGHT );
172     mContainer3Info.SetParentOrigin( ParentOrigin::TOP_RIGHT );
173     mContainer.Add( mContainerInfo );
174     mContainer2.Add( mContainer2Info );
175     mContainer3.Add( mContainer3Info );
176     CreateTextLabel ( mLabelInfo, "system free", Color::BLACK, true  );
177     CreateTextLabel ( mLabel2Info, "json fixed", Color::BLACK, true  );
178     CreateTextLabel ( mLabel3Info, "SetProp fixed", Color::BLACK, true  );
179     mContainerInfo.Add( mLabelInfo );
180     mContainer2Info.Add( mLabel2Info );
181     mContainer3Info.Add( mLabel3Info );
182
183     stage.Add( mContainer );
184     stage.Add( mContainer2 );
185     stage.Add( mContainer3 );
186
187     CreateTextLabel ( mLabel, LABEL_TEXT, Color::WHITE  );
188
189     CreateTextLabel ( mLabel2, LABEL_TEXT, Color::WHITE  );
190     mLabel2.SetStyleName("TextLabelRosemary");
191
192     CreateTextLabel ( mLabel3, LABEL_TEXT, Color::WHITE  );
193     mLabel3.SetProperty( TextLabel::Property::FONT_FAMILY, "SamsungOneUI" );
194
195     mContainer.SetPosition( 0, 0 );
196     mContainer2.SetPosition( 0, stageSize.height*0.25f );
197     mContainer3.SetPosition( 0, stageSize.height*0.25f*2 );
198
199     mContainer.Add( mLabel );
200     mContainer2.Add( mLabel2 );
201     mContainer3.Add( mLabel3 );
202   }
203
204   /**
205    * Main key event handler
206    */
207   void OnKeyEvent(const KeyEvent& event)
208   {
209     if(event.state == KeyEvent::Down)
210     {
211       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
212       {
213         mApplication.Quit();
214       }
215     }
216   }
217
218 private:
219
220   Application& mApplication;
221
222   PushButton mButton;
223
224   TextLabel mLabel;
225   TextLabel mLabel2;
226   TextLabel mLabel3;
227
228   TextLabel mLabel4;
229
230   Control mContainer;
231   Control mContainer2;
232   Control mContainer3;
233   Control mContainer4;
234
235   Control mContainerInfo;
236   Control mContainer2Info;
237   Control mContainer3Info;
238   Control mContainer4Info;
239
240   TextLabel mLabelInfo;
241   TextLabel mLabel2Info;
242   TextLabel mLabel3Info;
243   TextLabel mLabel4Info;
244
245   Vector2 mLayoutSize;
246
247   bool mToggle;
248 };
249
250 int DALI_EXPORT_API main( int argc, char **argv )
251 {
252   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
253   TextFontsExample test( application );
254   application.MainLoop();
255   return 0;
256 }