image-view-svg-example.cpp
7.17 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
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2016 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.
*
*/
#include <dali-toolkit/dali-toolkit.h>
#include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
#include <dali-toolkit/devel-api/controls/renderer-factory/control-renderer.h>
#include <string.h>
using namespace Dali;
namespace
{
const float MAX_SCALE = 6.f;
const char* SVG_IMAGES[] =
{
DEMO_IMAGE_DIR "Camera.svg",
DEMO_IMAGE_DIR "Contacts.svg",
DEMO_IMAGE_DIR "Mail.svg",
DEMO_IMAGE_DIR "Message.svg",
DEMO_IMAGE_DIR "Phone.svg",
DEMO_IMAGE_DIR "Settings.svg",
DEMO_IMAGE_DIR "World.svg",
DEMO_IMAGE_DIR "Kid1.svg"
};
const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
}
// This example shows how to display svg images with ImageView
//
class ImageSvgController : public ConnectionTracker
{
public:
ImageSvgController( Application& application )
: mApplication( application ),
mScale( 1.f ),
mIndex( 0 )
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &ImageSvgController::Create );
}
~ImageSvgController()
{
}
// The Init signal is received once (only) during the Application lifetime
void Create( Application& application )
{
// Get a handle to the stage
Stage stage = Stage::GetCurrent();
stage.SetBackgroundColor( Color::WHITE );
Vector2 stageSize = stage.GetSize();
mActorSize = stageSize/2.f;
stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
// Background, for receiving gestures
mStageBackground = Actor::New();
mStageBackground.SetAnchorPoint( AnchorPoint::TOP_CENTER );
mStageBackground.SetParentOrigin( ParentOrigin::TOP_CENTER );
mStageBackground.SetSize( stageSize.x, stageSize.y );
stage.Add(mStageBackground);
// Push button, for changing the image set for displaying
Toolkit::PushButton changeButton = Toolkit::PushButton::New();
changeButton.SetLabelText( "Next" );
changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
stage.Add( changeButton );
changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
// Push button, for resetting the actor size and position
Toolkit::PushButton resetButton = Toolkit::PushButton::New();
resetButton.SetLabelText( "Reset" );
resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
stage.Add( resetButton );
resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
// Create and put imageViews to stage
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
mSvgActor[i].SetSize( mActorSize );
stage.Add( mSvgActor[i] );
}
mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
// Connect pan gesture for moving the actors
mPanGestureDetector = PanGestureDetector::New();
mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
mPanGestureDetector.Attach( mStageBackground );
// Connect pinch gesture for resizing the actors
mPinchGestureDetector = PinchGestureDetector::New();
mPinchGestureDetector.Attach( mStageBackground);
mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
}
// Callback of push button, for changing image set
bool OnChangeButtonClicked( Toolkit::Button button )
{
mIndex = (mIndex+4) % NUM_SVG_IMAGES;
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
}
return true;
}
// Callback of push button, for resetting image size and position
bool OnResetButtonClicked( Toolkit::Button button )
{
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i].SetSize(mActorSize);
mSvgActor[i].SetPosition( Vector3::ZERO );
mScale = 1.f;
}
return true;
}
// Callback of pan gesture, for moving the actors
void OnPanGesture( Actor actor, const PanGesture& gesture )
{
if( gesture.state == Gesture::Continuing )
{
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
}
}
}
// Callback of pinch gesture, for resizing the actors
void OnPinch(Actor actor, const PinchGesture& gesture)
{
if (gesture.state == Gesture::Started)
{
mScaleAtPinchStart = mScale;
}
if( gesture.state == Gesture::Finished )
{
mScale = mScaleAtPinchStart * gesture.scale;
mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i].SetSize( mActorSize * mScale);
}
}
}
/**
* Main key event handler
*/
void OnKeyEvent(const KeyEvent& event)
{
if(event.state == KeyEvent::Down)
{
if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
{
mApplication.Quit();
}
else
{
const char* keyName = event.keyPressedName.c_str();
if( strcmp(keyName, "Left") == 0 )
{
mScale /= 1.1f;
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i].SetSize( mActorSize * mScale);
}
}
else if( strcmp(keyName, "Right") == 0 )
{
if( mScale < MAX_SCALE )
{
mScale *= 1.1f;
}
for( unsigned int i=0; i<4u; i++)
{
mSvgActor[i].SetSize( mActorSize * mScale);
}
}
}
}
}
private:
Application& mApplication;
Actor mStageBackground;
PanGestureDetector mPanGestureDetector;
PinchGestureDetector mPinchGestureDetector;
Toolkit::ImageView mSvgActor[4];
Vector2 mActorSize;
float mScale;
float mScaleAtPinchStart;
unsigned int mIndex;
};
void RunTest( Application& application )
{
ImageSvgController test( application );
application.MainLoop();
}
// Entry point for Linux & Tizen applications
//
int main( int argc, char **argv )
{
Application application = Application::New( &argc, &argv );
RunTest( application );
return 0;
}