Merge remote-tracking branch 'origin/tizen' into new_text
[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 // INTERNAL INCLUDES
19
20 #include "vertical-layout.h"
21 #include "emoji-strings.h"
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/public-api/text-abstraction/text-abstraction.h>
26 #include <iostream>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30 using namespace EmojiStrings;
31
32  // TODO Need to expose Text::RENDER.....
33 const int ATLAS_RENDERER = 0;
34
35 // This example shows how to create and display Hello World! using a simple TextActor
36 //
37 class EmojiExample : public ConnectionTracker
38 {
39 public:
40
41   typedef uint32_t SizeType;
42
43   EmojiExample( Application& application )
44   : mApplication( application )
45   {
46     std::cout << "EmoticonController::EmoticonController" << std::endl;
47
48     // Connect to the Application's Init signal
49     mApplication.InitSignal().Connect( this, &EmojiExample::Create );
50   }
51
52   ~EmojiExample()
53   {
54     // Nothing to do here;
55   }
56
57   // The Init signal is received once (only) during the Application lifetime
58   void Create( Application& application )
59   {
60
61     mLayout = VerticalLayout::New();
62     mLayout.SetParentOrigin( ParentOrigin::TOP_LEFT );
63     mLayout.SetAnchorPoint( AnchorPoint::TOP_LEFT );
64     Stage stage = Stage::GetCurrent();
65     stage.Add( mLayout );
66     stage.KeyEventSignal().Connect(this, &EmojiExample::OnKeyEvent);
67
68     for( unsigned int index = 0u; index < NUMBER_OF_EMOJIS; ++index )
69     {
70       const Emoji& emoji = EMOJIS[index];
71       const std::string text = emoji.mUTF8 + " " + emoji.mDescription;
72       TextLabel label = TextLabel::New();
73       label.SetParentOrigin( ParentOrigin::TOP_CENTER );
74       label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
75       label.SetProperty( TextLabel::Property::MULTI_LINE, true );
76       label.SetProperty( TextLabel::Property::TEXT, text );
77       mLayout.AddLabel( label );
78       mLayout.TouchedSignal().Connect( this, &EmojiExample::OnTouchEvent );
79     }
80
81     const Vector2& size = stage.GetSize();
82     const float height = mLayout.GetHeightForWidth( size.width );
83     mLayout.SetSize( Size( size.width, height ) );
84     mLayout.TouchedSignal().Connect( this, &EmojiExample::OnTouchEvent );
85
86   }
87
88   bool OnTouchEvent( Actor actor, const TouchEvent& event )
89   {
90     if( 1u == event.GetPointCount() )
91     {
92       const TouchPoint::State state = event.GetPoint(0u).state;
93
94       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
95       const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
96
97       if( TouchPoint::Down == state )
98       {
99         mLastPoint = localPoint;
100         mAnimation = Animation::New( 0.25f );
101       }
102       else if( TouchPoint::Motion == state )
103       {
104         if( mAnimation )
105         {
106           mAnimation.MoveBy( mLayout, Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunctions::Linear );
107           mAnimation.Play();
108           mLastPoint = localPoint;
109         }
110       }
111     }
112
113     return true;
114   }
115
116   /**
117    * Main key event handler
118    */
119   void OnKeyEvent(const KeyEvent& event)
120   {
121     if(event.state == KeyEvent::Down)
122     {
123       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
124       {
125         mApplication.Quit();
126       }
127     }
128   }
129
130
131 private:
132   Application&  mApplication;
133   VerticalLayout mLayout;
134   Animation      mAnimation;
135   float          mLastPoint;
136 };
137
138 void RunTest( Application& application )
139 {
140   EmojiExample test( application );
141
142   application.MainLoop();
143 }
144
145 // Entry point for Linux & SLP applications
146 //
147 int main( int argc, char **argv )
148 {
149   Application application = Application::New( &argc, &argv );
150
151   RunTest( application );
152
153   return 0;
154 }