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