Changes after table-view moved to public-api
[platform/core/uifw/dali-demo.git] / examples / text-label-emojis / text-label-emojis.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 // 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   {
43     std::cout << "EmoticonController::EmoticonController" << std::endl;
44
45     // Connect to the Application's Init signal
46     mApplication.InitSignal().Connect( this, &EmojiExample::Create );
47   }
48
49   ~EmojiExample()
50   {
51     // Nothing to do here;
52   }
53
54   // The Init signal is received once (only) during the Application lifetime
55   void Create( Application& application )
56   {
57     Stage stage = Stage::GetCurrent();
58     stage.SetBackgroundColor( Color::WHITE );
59     stage.KeyEventSignal().Connect(this, &EmojiExample::OnKeyEvent);
60
61     mTableView = Toolkit::TableView::New( NUMBER_OF_EMOJIS, 1 );
62     mTableView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
63     mTableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
64     mTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
65     mTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
66     mTableView.TouchedSignal().Connect( this, &EmojiExample::OnTouchEvent );
67     stage.Add( mTableView );
68
69     for( unsigned int index = 0u; index < NUMBER_OF_EMOJIS; ++index )
70     {
71       const Emoji& emoji = EMOJIS[index];
72       const std::string text = emoji.mUTF8 + " " + emoji.mDescription;
73
74       TextLabel label = TextLabel::New( text );
75       label.SetParentOrigin( ParentOrigin::TOP_CENTER );
76       label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
77       label.SetProperty( TextLabel::Property::MULTI_LINE, true );
78
79       mTableView.SetFitHeight( index );
80       mTableView.AddChild( label, Toolkit::TableView::CellPosition( index, 0 ) );
81     }
82   }
83
84   bool OnTouchEvent( Actor actor, const TouchEvent& event )
85   {
86     if( 1u == event.GetPointCount() )
87     {
88       const TouchPoint::State state = event.GetPoint(0u).state;
89
90       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
91       const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
92
93       if( TouchPoint::Down == state )
94       {
95         mLastPoint = localPoint;
96         mAnimation = Animation::New( 0.25f );
97       }
98       else if( TouchPoint::Motion == state )
99       {
100         if( mAnimation )
101         {
102           mAnimation.AnimateBy( Property(mTableView, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
103           mAnimation.Play();
104           mLastPoint = localPoint;
105         }
106       }
107     }
108
109     return true;
110   }
111
112   /**
113    * Main key event handler
114    */
115   void OnKeyEvent(const KeyEvent& event)
116   {
117     if(event.state == KeyEvent::Down)
118     {
119       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
120       {
121         mApplication.Quit();
122       }
123     }
124   }
125
126
127 private:
128   Application&  mApplication;
129   TableView      mTableView;
130   Animation      mAnimation;
131   float          mLastPoint;
132 };
133
134 void RunTest( Application& application )
135 {
136   EmojiExample test( application );
137
138   application.MainLoop();
139 }
140
141 // Entry point for Linux & SLP applications
142 //
143 int main( int argc, char **argv )
144 {
145   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
146
147   RunTest( application );
148
149   return 0;
150 }