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