Move more public-api headers to devel-api. PART 3
[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 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
26
27 // INTERNAL INCLUDES
28 #include "shared/multi-language-strings.h"
29 #include "shared/view.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     DemoHelper::RequestThemeChange();
61
62     Stage stage = Stage::GetCurrent();
63
64     stage.KeyEventSignal().Connect(this, &TextLabelMultiLanguageExample::OnKeyEvent);
65     stage.SetBackgroundColor( Color::WHITE );
66
67     mTableView = Toolkit::TableView::New( NUMBER_OF_LANGUAGES, 1 );
68     mTableView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
69     mTableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
70     mTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
71     mTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
72     mTableView.TouchedSignal().Connect( this, &TextLabelMultiLanguageExample::OnTouchEvent );
73     stage.Add( mTableView );
74
75     for( unsigned int index = 0u; index < NUMBER_OF_LANGUAGES; ++index )
76     {
77       const Language& language = LANGUAGES[index];
78
79       TextLabel label = TextLabel::New();
80       label.SetProperty( TextLabel::Property::MULTI_LINE, true );
81
82       const std::string text = language.languageName + " " + language.languageRomanName + " " + language.text;
83       label.SetProperty( TextLabel::Property::TEXT, text );
84
85       mTableView.SetFitHeight( index );
86       mTableView.AddChild( label, Toolkit::TableView::CellPosition( index, 0 ) );
87     }
88   }
89
90   bool OnTouchEvent( Actor actor, const TouchEvent& event )
91   {
92     if( 1u == event.GetPointCount() )
93     {
94       const TouchPoint::State state = event.GetPoint(0u).state;
95
96       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
97       const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
98
99       if( TouchPoint::Down == state )
100       {
101         mLastPoint = localPoint;
102         mAnimation = Animation::New( 0.25f );
103       }
104       else if( TouchPoint::Motion == state )
105       {
106         if( mAnimation )
107         {
108           mAnimation.AnimateBy( Property(mTableView, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
109           mAnimation.Play();
110           mLastPoint = localPoint;
111         }
112       }
113     }
114
115     return true;
116   }
117
118   /**
119    * Main key event handler
120    */
121   void OnKeyEvent(const KeyEvent& event)
122   {
123     if(event.state == KeyEvent::Down)
124     {
125       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
126       {
127         mApplication.Quit();
128       }
129     }
130   }
131
132 private:
133
134   Application&   mApplication;
135   TableView      mTableView;
136   Animation      mAnimation;
137   float          mLastPoint;
138 };
139
140 void RunTest( Application& application )
141 {
142   TextLabelMultiLanguageExample test( application );
143
144   application.MainLoop();
145 }
146
147 /** Entry point for Linux & Tizen applications */
148 int main( int argc, char **argv )
149 {
150   Application application = Application::New( &argc, &argv );
151
152   RunTest( application );
153
154   return 0;
155 }