4a8e3b0914d0cf3050317a1788cd0ba48f6523e3
[platform/core/uifw/dali-demo.git] / examples / text-message-field / text-message-field-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 message-field-example.cpp
20  * @brief Basic usage of TextField control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31   const char* DESKTOP_IMAGE( DEMO_IMAGE_DIR "woodEffect.jpg" );
32   const Vector2 DESKTOP_SIZE( Vector2( 1440, 1600 ) );
33   const Vector2 PHOTOBOX_SIZE( Vector2(330.0f, 80.0f ) );
34   const float MAX_OFFSCREEN_RENDERING_SIZE = 2048.f;
35   const float SCREEN_BORDER = 5.0f; // Border around screen that Popups and handles will not exceed
36 }
37 /**
38  * @brief The main class of the demo.
39  */
40 class TextMessageFieldExample : public ConnectionTracker
41 {
42 public:
43
44   TextMessageFieldExample( Application& application )
45   : mApplication( application ),
46     mTargetActorPosition(),
47     mTargetActorSize()
48   {
49     // Connect to the Application's Init signal
50     mApplication.InitSignal().Connect( this, &TextMessageFieldExample::Create );
51   }
52
53   ~TextMessageFieldExample()
54   {
55     // Nothing to do here.
56   }
57
58   /**
59    * One-time setup in response to Application InitSignal.
60    */
61   void Create( Application& application )
62   {
63     Stage stage = Stage::GetCurrent();
64     mStageSize = stage.GetSize();
65
66     stage.KeyEventSignal().Connect(this, &TextMessageFieldExample::OnKeyEvent);
67
68     // Create Root actor
69     Actor rootActor = Actor::New();
70     rootActor.SetName("rootActor");
71     rootActor.SetResizePolicy( ResizePolicy::FIXED,  Dimension::ALL_DIMENSIONS );
72     rootActor.SetSize( mStageSize );
73     rootActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
74
75     stage.Add( rootActor );
76
77     const Size mTargetActorSize( mStageSize.width, DESKTOP_SIZE.height );
78
79     // Create Desktop
80     ImageView desktop = ImageView::New( DESKTOP_IMAGE );
81     desktop.SetName("desktopActor");
82     desktop.SetAnchorPoint( AnchorPoint::TOP_LEFT );
83     desktop.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
84     desktop.SetSize( mTargetActorSize );
85
86     rootActor.Add( desktop ); // Add desktop (content) to offscreen actor
87
88     // Create Photo Box A
89     Control photoBoxA = Control::New();
90
91     Dali::Property::Map border;
92     border.Insert( "rendererType",  "borderRenderer" );
93     border.Insert( "borderColor",  Color::WHITE );
94     border.Insert( "borderSize",  1.f );
95     photoBoxA.SetProperty( Control::Property::BACKGROUND, border );
96
97     photoBoxA.SetName("photoBoxA");
98     photoBoxA.SetAnchorPoint( AnchorPoint::CENTER );
99     photoBoxA.SetParentOrigin( ParentOrigin::CENTER );
100     photoBoxA.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
101     photoBoxA.SetSize( PHOTOBOX_SIZE );
102     photoBoxA.SetPosition( 0.0f, -500.0f, 1.0f );
103     desktop.Add( photoBoxA );
104
105     // Create TextField
106     TextField field = TextField::New();
107     field.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
108     field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
109     field.SetPadding( Padding( 1.0f, 1.0f, 1.0f, 1.0f ) );
110     field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
111     field.SetProperty( TextField::Property::TEXT, "Enter Title name"  );
112     field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( SCREEN_BORDER, SCREEN_BORDER, mStageSize.width - SCREEN_BORDER*2, mStageSize.height - SCREEN_BORDER*2 ) );
113     photoBoxA.Add( field );
114
115     mPanGestureDetector = PanGestureDetector::New();
116     mPanGestureDetector.DetectedSignal().Connect(this, &TextMessageFieldExample::OnPanGesture );
117     mPanGestureDetector.Attach( desktop );
118   }
119
120   /**
121    * Main key event handler
122    */
123   void OnKeyEvent(const KeyEvent& event)
124   {
125     if(event.state == KeyEvent::Down)
126     {
127       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
128       {
129         mApplication.Quit();
130       }
131     }
132   }
133
134   void OnPanGesture( Actor actor, const PanGesture& gesture )
135   {
136     if( gesture.state == Gesture::Continuing )
137     {
138       Vector2 position = Vector2( gesture.displacement );
139       mTargetActorPosition.y = mTargetActorPosition.y + position.y;
140       mTargetActorPosition.y = std::min( mTargetActorPosition.y, -mTargetActorSize.height );
141       mTargetActorPosition.y = std::max( mTargetActorPosition.y, ( mTargetActorSize.height - mStageSize.height*0.25f ) );
142       actor.SetPosition( 0.0f, mTargetActorPosition.y );
143     }
144   }
145
146 private:
147
148   Application& mApplication;
149   PanGestureDetector mPanGestureDetector;
150
151   Vector2 mTargetActorPosition;
152   Vector2 mTargetActorSize;
153   Vector2 mStageSize;
154 };
155
156 void RunTest( Application& application )
157 {
158   TextMessageFieldExample test( application );
159
160   application.MainLoop();
161 }
162
163 /** Entry point for Linux & Tizen applications */
164 int main( int argc, char **argv )
165 {
166   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
167
168   RunTest( application );
169
170   return 0;
171 }