Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-demo.git] / examples / text-label-multi-language / text-label-multi-language-example.cpp
1 /*
2  * Copyright (c) 2015 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-label-example.cpp
20  * @brief Basic usage of TextLabel control
21  */
22
23 // INTERNAL INCLUDES
24 #include "vertical-layout.h"
25 #include "shared/multi-language-strings.h"
26
27 // EXTERNAL INCLUDES
28 #include <dali-toolkit/dali-toolkit.h>
29 #include <dali/public-api/text-abstraction/text-abstraction.h>
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
34
35 /**
36  * @brief The main class of the demo.
37  */
38 class TextLabelMultiLanguageExample : public ConnectionTracker
39 {
40 public:
41
42   TextLabelMultiLanguageExample( Application& application )
43   : mApplication( application ),
44     mLastPoint( 0.f )
45   {
46     // Connect to the Application's Init signal
47     mApplication.InitSignal().Connect( this, &TextLabelMultiLanguageExample::Create );
48   }
49
50   ~TextLabelMultiLanguageExample()
51   {
52     // Nothing to do here.
53   }
54
55   /**
56    * One-time setup in response to Application InitSignal.
57    */
58   void Create( Application& application )
59   {
60     Stage stage = Stage::GetCurrent();
61
62     stage.KeyEventSignal().Connect(this, &TextLabelMultiLanguageExample::OnKeyEvent);
63
64     mLayout = VerticalLayout::New();
65     mLayout.SetParentOrigin( ParentOrigin::TOP_LEFT );
66     mLayout.SetAnchorPoint( AnchorPoint::TOP_LEFT );
67
68     stage.Add( mLayout );
69
70     for( unsigned int index = 0u; index < NUMBER_OF_LANGUAGES; ++index )
71     {
72       const Language& language = LANGUAGES[index];
73
74       TextLabel label = TextLabel::New();
75       label.SetParentOrigin( ParentOrigin::TOP_CENTER );
76       label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
77
78       label.SetProperty( TextLabel::Property::MULTI_LINE, true );
79
80       const std::string text = language.languageName + " " + language.languageRomanName + " " + language.text;
81
82       label.SetProperty( TextLabel::Property::TEXT, text );
83       mLayout.AddLabel( label );
84
85       mLayout.TouchedSignal().Connect( this, &TextLabelMultiLanguageExample::OnTouchEvent );
86     }
87
88     const Vector2& size = Stage::GetCurrent().GetSize();
89     const float height = mLayout.GetHeightForWidth( size.width );
90     mLayout.SetSize( Size( size.width, height ) );
91   }
92
93   bool OnTouchEvent( Actor actor, const TouchEvent& event )
94   {
95     if( 1u == event.GetPointCount() )
96     {
97       const TouchPoint::State state = event.GetPoint(0u).state;
98
99       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
100       const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
101
102       if( TouchPoint::Down == state )
103       {
104         mLastPoint = localPoint;
105         mAnimation = Animation::New( 0.25f );
106       }
107       else if( TouchPoint::Motion == state )
108       {
109         if( mAnimation )
110         {
111           mAnimation.MoveBy( mLayout, Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunctions::Linear );
112           mAnimation.Play();
113           mLastPoint = localPoint;
114         }
115       }
116     }
117
118     return true;
119   }
120
121   /**
122    * Main key event handler
123    */
124   void OnKeyEvent(const KeyEvent& event)
125   {
126     if(event.state == KeyEvent::Down)
127     {
128       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
129       {
130         mApplication.Quit();
131       }
132     }
133   }
134
135 private:
136
137   Application&   mApplication;
138   VerticalLayout mLayout;
139   Animation      mAnimation;
140   float          mLastPoint;
141 };
142
143 void RunTest( Application& application )
144 {
145   TextLabelMultiLanguageExample test( application );
146
147   application.MainLoop();
148 }
149
150 /** Entry point for Linux & Tizen applications */
151 int main( int argc, char **argv )
152 {
153   Application application = Application::New( &argc, &argv );
154
155   RunTest( application );
156
157   return 0;
158 }