text-field-example.cpp
6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* @file text-field-example.cpp
* @brief Basic usage of TextField control
*/
// EXTERNAL INCLUDES
#include <dali-toolkit/dali-toolkit.h>
#include <dali-toolkit/devel-api/controls/popup/popup.h>
#include <iostream>
#include <dali/public-api/events/touch-point.h>
// INTERNAL INCLUDES
#include "shared/multi-language-strings.h"
#include "shared/view.h"
using namespace Dali;
using namespace Dali::Toolkit;
using namespace MultiLanguageStrings;
namespace
{
const char* const FOLDER_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_bg.png";
const char* const FOLDER_OPEN_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_open_bg.png";
const float BORDER_WIDTH = 4.0f;
const Vector3 POPUP_SIZE_FACTOR_TO_PARENT = Vector3( 0.0, 0.25, 0.0 );
} // unnamed namespace
/**
* @brief The main class of the demo.
*/
class TextFieldExample : public ConnectionTracker
{
public:
TextFieldExample( Application& application )
: mApplication( application )
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
}
~TextFieldExample()
{
// Nothing to do here.
}
/**
* One-time setup in response to Application InitSignal.
*/
void Create( Application& application )
{
Stage stage = Stage::GetCurrent();
stage.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
mButton = CreateFolderButton();
mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
stage.Add( mButton );
}
PushButton CreateFolderButton()
{
PushButton button = PushButton::New();
button.SetUnselectedImage( FOLDER_ICON_IMAGE );
button.SetSelectedImage( FOLDER_OPEN_ICON_IMAGE );
button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
button.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
ImageDimensions imageSize = ResourceImage::GetImageSize( FOLDER_ICON_IMAGE );
button.SetSize( imageSize.GetWidth(), imageSize.GetHeight() );
return button;
}
bool OnButtonClicked( Toolkit::Button button )
{
Stage stage = Stage::GetCurrent();
Vector2 stageSize = stage.GetSize();
// Remove previously hidden pop-up
UnparentAndReset(mPopup);
// Launch a pop-up containing TextField
mField = CreateTextField( stageSize, mButtonLabel );
mPopup = CreatePopup( stageSize.width * 0.8f );
mPopup.Add( mField );
mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
stage.Add( mPopup );
mPopup.SetDisplayState( Popup::SHOWN );
return true;
}
TextField CreateTextField( const Vector2& stageSize, const std::string& text )
{
TextField field = TextField::New();
field.SetName("textField");
field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
field.SetProperty( TextField::Property::TEXT, text );
field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, stageSize.width - BORDER_WIDTH*2, stageSize.height - BORDER_WIDTH*2 ) );
return field;
}
Popup CreatePopup( float width )
{
Popup popup = Popup::New();
popup.SetParentOrigin( ParentOrigin::CENTER );
popup.SetAnchorPoint( AnchorPoint::CENTER );
popup.SetSize( width, 0.0f );
popup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
popup.SetSizeModeFactor( POPUP_SIZE_FACTOR_TO_PARENT );
popup.TouchSignal().Connect( this, &TextFieldExample::OnPopupTouched );
return popup;
}
void OnPopupOutsideTouched()
{
// Update the folder text
if( mButton && mField )
{
Property::Value text = mField.GetProperty( TextField::Property::TEXT );
mButtonLabel = text.Get< std::string >();
mButton.SetLabelText( mButtonLabel );
}
// Hide & discard the pop-up
if( mPopup )
{
mPopup.SetDisplayState( Popup::HIDDEN );
}
mField.Reset();
}
bool OnPopupTouched( Actor actor, const TouchData& event )
{
// End edit mode for TextField if parent Popup touched.
if(event.GetPointCount() > 0)
{
switch( event.GetState( 0 ) )
{
case PointState::DOWN:
{
// Update the folder text and lose focus for Key events
if( mButton && mField )
{
Property::Value text = mField.GetProperty( TextField::Property::TEXT );
mButtonLabel = text.Get< std::string >();
mButton.SetLabelText( mButtonLabel );
mField.ClearKeyInputFocus();
}
break;
}
default:
{
break;
}
} // end switch
}
return true;
}
/**
* Main key event handler
*/
void OnKeyEvent(const KeyEvent& event)
{
if(event.state == KeyEvent::Down)
{
if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
{
mApplication.Quit();
}
}
}
private:
Application& mApplication;
// This button launches a pop-up containing TextField
PushButton mButton;
std::string mButtonLabel;
// Pop-up contents
TextField mField;
Popup mPopup;
};
void RunTest( Application& application )
{
TextFieldExample test( application );
application.MainLoop();
}
/** Entry point for Linux & Tizen applications */
int DALI_EXPORT_API main( int argc, char **argv )
{
// DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
Application application = Application::New( &argc, &argv );
RunTest( application );
return 0;
}