Commit 65b6dfc12fd207650e7f6dcc991710b5c020f670

Authored by Agnelo Vaz
1 parent 49fdbca4

TextField Demo to use Popup and Label interaction

Change-Id: Ib515e9d7d5c854408832bd3eb94ee6626d396ad2
Signed-off-by: Agnelo Vaz <agnelo.vaz@samsung.com>
examples/text-field/text-field-example.cpp
... ... @@ -22,7 +22,9 @@
22 22  
23 23 // EXTERNAL INCLUDES
24 24 #include <dali-toolkit/dali-toolkit.h>
  25 +#include <dali-toolkit/devel-api/controls/popup/popup.h>
25 26 #include <iostream>
  27 +#include <dali/public-api/events/touch-point.h>
26 28  
27 29 // INTERNAL INCLUDES
28 30 #include "shared/multi-language-strings.h"
... ... @@ -35,38 +37,11 @@ using namespace MultiLanguageStrings;
35 37 namespace
36 38 {
37 39  
38   - const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
  40 + const char* const FOLDER_ICON_IMAGE = DALI_IMAGE_DIR "folder_appicon_empty_bg.png";
39 41  
40 42 const float BORDER_WIDTH = 4.0f;
41 43  
42   - const unsigned int KEY_ZERO = 10;
43   - const unsigned int KEY_ONE = 11;
44   - const unsigned int KEY_F = 41;
45   - const unsigned int KEY_H = 43;
46   - const unsigned int KEY_V = 55;
47   - const unsigned int KEY_M = 58;
48   - const unsigned int KEY_L = 46;
49   - const unsigned int KEY_S = 39;
50   - const unsigned int KEY_PLUS = 21;
51   - const unsigned int KEY_MINUS = 20;
52   -
53   - const char* H_ALIGNMENT_STRING_TABLE[] =
54   - {
55   - "BEGIN",
56   - "CENTER",
57   - "END"
58   - };
59   -
60   - const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
61   -
62   - const char* V_ALIGNMENT_STRING_TABLE[] =
63   - {
64   - "TOP",
65   - "CENTER",
66   - "BOTTOM"
67   - };
68   -
69   - const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
  44 + const Vector3 POPUP_SIZE_FACTOR_TO_PARENT = Vector3( 0.0, 0.25, 0.0 );
70 45  
71 46 } // unnamed namespace
72 47  
... ... @@ -78,9 +53,7 @@ class TextFieldExample : public ConnectionTracker
78 53 public:
79 54  
80 55 TextFieldExample( Application& application )
81   - : mApplication( application ),
82   - mLanguageId( 0u ),
83   - mAlignment( 0u )
  56 + : mApplication( application )
84 57 {
85 58 // Connect to the Application's Init signal
86 59 mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
... ... @@ -98,40 +71,119 @@ public:
98 71 {
99 72 Stage stage = Stage::GetCurrent();
100 73  
101   - mTapGestureDetector = TapGestureDetector::New();
102   - mTapGestureDetector.Attach( stage.GetRootLayer() );
103   - mTapGestureDetector.DetectedSignal().Connect( this, &TextFieldExample::OnTap );
104   -
  74 + stage.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
105 75 stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
106 76  
  77 + mButton = CreateFolderButton();
  78 + mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
  79 + stage.Add( mButton );
  80 + }
  81 +
  82 + PushButton CreateFolderButton()
  83 + {
  84 + PushButton button = PushButton::New();
  85 + ResourceImage image = ResourceImage::New( FOLDER_ICON_IMAGE );
  86 + ImageActor folderButton = ImageActor::New( image );
  87 + folderButton.SetColor( Color::WHITE );
  88 + button.SetButtonImage( folderButton );
  89 + button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
  90 + button.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
  91 + button.SetSize( image.GetWidth(), image.GetHeight() );
  92 +
  93 + return button;
  94 + }
  95 +
  96 + bool OnButtonClicked( Toolkit::Button button )
  97 + {
  98 + Stage stage = Stage::GetCurrent();
107 99 Vector2 stageSize = stage.GetSize();
108 100  
109   - Control container = Control::New();
110   - container.SetName( "Container" );
111   - container.SetParentOrigin( ParentOrigin::CENTER );
112   - container.SetSize( Vector2(stageSize.width*0.6f, stageSize.width*0.6f) );
113   - container.SetBackgroundColor( Color::WHITE );
114   - container.GetChildAt(0).SetZ(-1.0f);
115   - stage.Add( container );
  101 + // Launch a pop-up containing TextField
  102 + mField = CreateTextField( stageSize, mButtonLabel );
  103 + mPopup = CreatePopup( stageSize.width * 0.8f );
  104 + mPopup.Add( mField );
  105 + mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
  106 + mPopup.Show();
  107 +
  108 + return true;
  109 + }
116 110  
117   - mField = TextField::New();
118   - mField.SetAnchorPoint( AnchorPoint::TOP_LEFT );
119   - mField.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
120   - mField.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
121   - mField.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
122   - mField.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
123   - mField.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
  111 + TextField CreateTextField( const Vector2& stageSize, const std::string& text )
  112 + {
  113 + TextField field = TextField::New();
  114 + field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
  115 + field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
  116 + field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
  117 + field.SetProperty( TextField::Property::TEXT, text );
  118 + field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
  119 + field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
  120 + field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
  121 + field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
  122 +
  123 + return field;
  124 + }
124 125  
125   - container.Add( mField );
  126 + Popup CreatePopup( float width )
  127 + {
  128 + Popup popup = Popup::New();
  129 + popup.SetParentOrigin( ParentOrigin::CENTER );
  130 + popup.SetAnchorPoint( AnchorPoint::CENTER );
  131 + popup.SetSize( width, 0.0f );
  132 + popup.HideTail();
  133 + popup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
  134 + popup.SetSizeModeFactor( POPUP_SIZE_FACTOR_TO_PARENT );
  135 + popup.TouchedSignal().Connect( this, &TextFieldExample::OnPopupTouched );
  136 +
  137 + return popup;
  138 + }
126 139  
127   - Property::Value fieldText = mField.GetProperty( TextField::Property::TEXT );
  140 + void OnPopupOutsideTouched()
  141 + {
  142 + // Update the folder text
  143 + if( mButton && mField )
  144 + {
  145 + Property::Value text = mField.GetProperty( TextField::Property::TEXT );
  146 + mButtonLabel = text.Get< std::string >();
  147 + mButton.SetLabel( mButtonLabel );
  148 + }
128 149  
129   - std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
  150 + // Hide & discard the pop-up
  151 + if( mPopup )
  152 + {
  153 + mPopup.Hide();
  154 + }
  155 + mField.Reset();
  156 + mPopup.Reset();
130 157 }
131 158  
132   - void OnTap( Actor actor, const TapGesture& tapGesture )
  159 + bool OnPopupTouched( Actor actor, const TouchEvent& event )
133 160 {
134   - mField.ClearKeyInputFocus();
  161 + // End edit mode for TextField if parent Popup touched.
  162 + if(event.GetPointCount() > 0)
  163 + {
  164 + const TouchPoint& point = event.GetPoint(0);
  165 + switch(point.state)
  166 + {
  167 + case TouchPoint::Down:
  168 + {
  169 + // Update the folder text and lose focus for Key events
  170 + if( mButton && mField )
  171 + {
  172 + Property::Value text = mField.GetProperty( TextField::Property::TEXT );
  173 + mButtonLabel = text.Get< std::string >();
  174 + mButton.SetLabel( mButtonLabel );
  175 + mField.ClearKeyInputFocus();
  176 + }
  177 + break;
  178 + }
  179 + default:
  180 + {
  181 + break;
  182 + }
  183 + } // end switch
  184 + }
  185 +
  186 + return true;
135 187 }
136 188  
137 189 /**
... ... @@ -145,73 +197,6 @@ public:
145 197 {
146 198 mApplication.Quit();
147 199 }
148   - else if( event.IsCtrlModifier() )
149   - {
150   - switch( event.keyCode )
151   - {
152   - // Select rendering back-end
153   - case KEY_ZERO: // fall through
154   - case KEY_ONE:
155   - {
156   - mField.SetProperty( TextField::Property::RENDERING_BACKEND, event.keyCode - 10 );
157   - break;
158   - }
159   - case KEY_H: // Horizontal alignment
160   - {
161   - if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
162   - {
163   - mAlignment = 0u;
164   - }
165   -
166   - mField.SetProperty( TextField::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
167   - break;
168   - }
169   - case KEY_V: // Vertical alignment
170   - {
171   - if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
172   - {
173   - mAlignment = 0u;
174   - }
175   -
176   - mField.SetProperty( TextField::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
177   - break;
178   - }
179   - case KEY_L: // Language
180   - {
181   - const Language& language = LANGUAGES[ mLanguageId ];
182   -
183   - mField.SetProperty( TextField::Property::TEXT, language.text );
184   -
185   - if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
186   - {
187   - mLanguageId = 0u;
188   - }
189   - break;
190   - }
191   - case KEY_S: // Shadow color
192   - {
193   - if( Color::BLACK == mField.GetProperty<Vector4>( TextField::Property::SHADOW_COLOR ) )
194   - {
195   - mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::RED );
196   - }
197   - else
198   - {
199   - mField.SetProperty( TextField::Property::SHADOW_COLOR, Color::BLACK );
200   - }
201   - break;
202   - }
203   - case KEY_PLUS: // Increase shadow offset
204   - {
205   - mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
206   - break;
207   - }
208   - case KEY_MINUS: // Decrease shadow offset
209   - {
210   - mField.SetProperty( TextField::Property::SHADOW_OFFSET, mField.GetProperty<Vector2>( TextField::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
211   - break;
212   - }
213   - }
214   - }
215 200 }
216 201 }
217 202  
... ... @@ -219,12 +204,13 @@ private:
219 204  
220 205 Application& mApplication;
221 206  
222   - TextField mField;
223   -
224   - TapGestureDetector mTapGestureDetector;
  207 + // This button launches a pop-up containing TextField
  208 + PushButton mButton;
  209 + std::string mButtonLabel;
225 210  
226   - unsigned int mLanguageId;
227   - unsigned int mAlignment;
  211 + // Pop-up contents
  212 + TextField mField;
  213 + Popup mPopup;
228 214 };
229 215  
230 216 void RunTest( Application& application )
... ...