[dali_1.2.60] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / text-label-emojis / text-label-emojis.cpp
1 /*
2  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <iostream>
21
22 // INTERNAL INCLUDES
23 #include "emoji-strings.h"
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27 using namespace EmojiStrings;
28
29  // TODO Need to expose Text::RENDER.....
30 const int ATLAS_RENDERER = 0;
31
32 // This example shows how to create and display Hello World! using a simple TextActor
33 //
34 class EmojiExample : public ConnectionTracker
35 {
36 public:
37
38   typedef uint32_t SizeType;
39
40   EmojiExample( Application& application )
41   : mApplication( application ),
42     mLastPoint( 0.0f )
43   {
44     std::cout << "EmoticonController::EmoticonController" << std::endl;
45
46     // Connect to the Application's Init signal
47     mApplication.InitSignal().Connect( this, &EmojiExample::Create );
48   }
49
50   ~EmojiExample()
51   {
52     // Nothing to do here;
53   }
54
55   // The Init signal is received once (only) during the Application lifetime
56   void Create( Application& application )
57   {
58     Stage stage = Stage::GetCurrent();
59     stage.SetBackgroundColor( Color::WHITE );
60     stage.KeyEventSignal().Connect(this, &EmojiExample::OnKeyEvent);
61
62     mTableView = Toolkit::TableView::New( NUMBER_OF_EMOJIS, 1 );
63     mTableView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
64     mTableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
65     mTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
66     mTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
67     mTableView.TouchSignal().Connect( this, &EmojiExample::OnTouch );
68     stage.Add( mTableView );
69
70     for( unsigned int index = 0u; index < NUMBER_OF_EMOJIS; ++index )
71     {
72       const Emoji& emoji = EMOJIS[index];
73       const std::string text = emoji.mUTF8 + " " + emoji.mDescription;
74
75       TextLabel label = TextLabel::New( text );
76       label.SetParentOrigin( ParentOrigin::TOP_CENTER );
77       label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
78       label.SetProperty( TextLabel::Property::MULTI_LINE, true );
79
80       mTableView.SetFitHeight( index );
81       mTableView.AddChild( label, Toolkit::TableView::CellPosition( index, 0 ) );
82     }
83   }
84
85   bool OnTouch( Actor actor, const TouchData& event )
86   {
87     if( 1u == event.GetPointCount() )
88     {
89       const PointState::Type state = event.GetState( 0 );
90
91       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
92       const float localPoint = static_cast<float>( static_cast<int>( event.GetLocalPosition( 0 ).y ) );
93
94       if( PointState::DOWN == state )
95       {
96         mLastPoint = localPoint;
97         mAnimation = Animation::New( 0.25f );
98       }
99       else if( PointState::MOTION == state )
100       {
101         if( mAnimation )
102         {
103           mAnimation.AnimateBy( Property(mTableView, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
104           mAnimation.Play();
105           mLastPoint = localPoint;
106         }
107       }
108     }
109
110     return true;
111   }
112
113   /**
114    * Main key event handler
115    */
116   void OnKeyEvent(const KeyEvent& event)
117   {
118     if(event.state == KeyEvent::Down)
119     {
120       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
121       {
122         mApplication.Quit();
123       }
124     }
125   }
126
127
128 private:
129   Application&  mApplication;
130   TableView      mTableView;
131   Animation      mAnimation;
132   float          mLastPoint;
133 };
134
135 void RunTest( Application& application )
136 {
137   EmojiExample test( application );
138
139   application.MainLoop();
140 }
141
142 // Entry point for Linux & SLP applications
143 //
144 int DALI_EXPORT_API main( int argc, char **argv )
145 {
146   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
147
148   RunTest( application );
149
150   return 0;
151 }