Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / text-label-emojis / text-label-emojis.cpp
1 /*
2  * Copyright (c) 2020 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 <dali-toolkit/devel-api/controls/table-view/table-view.h>
21 #include <iostream>
22
23 // INTERNAL INCLUDES
24 #include "emoji-strings.h"
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28 using namespace EmojiStrings;
29
30  // TODO Need to expose Text::RENDER.....
31 const int ATLAS_RENDERER = 0;
32
33 // This example shows how to create and display Hello World! using a simple TextActor
34 //
35 class EmojiExample : public ConnectionTracker
36 {
37 public:
38
39   typedef uint32_t SizeType;
40
41   EmojiExample( Application& application )
42   : mApplication( application ),
43     mLastPoint( 0.0f )
44   {
45     std::cout << "EmoticonController::EmoticonController" << std::endl;
46
47     // Connect to the Application's Init signal
48     mApplication.InitSignal().Connect( this, &EmojiExample::Create );
49   }
50
51   ~EmojiExample()
52   {
53     // Nothing to do here;
54   }
55
56   // The Init signal is received once (only) during the Application lifetime
57   void Create( Application& application )
58   {
59     Window window = application.GetWindow();
60     window.SetBackgroundColor( Color::WHITE );
61     window.KeyEventSignal().Connect(this, &EmojiExample::OnKeyEvent);
62
63     mTableView = Toolkit::TableView::New( NUMBER_OF_EMOJIS, 1 );
64     mTableView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
65     mTableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
66     mTableView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
67     mTableView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
68     mTableView.TouchedSignal().Connect( this, &EmojiExample::OnTouch );
69     window.Add( mTableView );
70
71     for( unsigned int index = 0u; index < NUMBER_OF_EMOJIS; ++index )
72     {
73       const Emoji& emoji = EMOJIS[index];
74       const std::string text = emoji.mUTF8 + " " + emoji.mDescription;
75
76       TextLabel label = TextLabel::New( text );
77       label.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
78       label.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
79       label.SetProperty( TextLabel::Property::MULTI_LINE, true );
80
81       mTableView.SetFitHeight( index );
82       mTableView.AddChild( label, Toolkit::TableView::CellPosition( index, 0 ) );
83     }
84   }
85
86   bool OnTouch( Actor actor, const TouchEvent& event )
87   {
88     if( 1u == event.GetPointCount() )
89     {
90       const PointState::Type state = event.GetState( 0 );
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.GetLocalPosition( 0 ).y ) );
94
95       if( PointState::DOWN == state )
96       {
97         mLastPoint = localPoint;
98         mAnimation = Animation::New( 0.25f );
99       }
100       else if( PointState::MOTION == state )
101       {
102         if( mAnimation )
103         {
104           mAnimation.AnimateBy( Property(mTableView, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::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.GetState() == KeyEvent::DOWN)
120     {
121       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
122       {
123         mApplication.Quit();
124       }
125     }
126   }
127
128
129 private:
130   Application&  mApplication;
131   TableView      mTableView;
132   Animation      mAnimation;
133   float          mLastPoint;
134 };
135
136 int DALI_EXPORT_API main( int argc, char **argv )
137 {
138   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
139   EmojiExample test( application );
140   application.MainLoop();
141   return 0;
142 }