2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "scroll-view-api.h"
25 #include <actors/actor-wrapper.h>
26 #include <controls/control-wrapper.h>
34 namespace // unanmed namespace
37 Toolkit::ScrollView GetScrollView( v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args )
39 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
40 return Toolkit::ScrollView::DownCast( handleWrapper->mHandle );
45 /***************************************
46 * SCROLLVIEW API FUNCTIONS
47 ***************************************/
55 * @return {Object} scrollView
57 Toolkit::Control ScrollViewApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
59 v8::Isolate* isolate = args.GetIsolate();
60 v8::HandleScope handleScope( isolate );
62 Toolkit::ScrollView scrollView = Toolkit::ScrollView::New();
67 * Set the scroll mode of ScrollView.
69 * This defines whether scrolling is enabled horizontally or vertically, how
70 * scrolling is snapped, and the boundary in which the scroll view can pan.
72 * When no specific scroll mode is set, scroll view can scroll to any position
73 * both horizontally and vertically and no snapping is enabled.
75 * Example of setting the scroll boundary of scroll view in the X axis to
76 * three pages (page size equals to the width of scroll view) and allowing
77 * snapping between pages, and disabling scrolling in the Y axis.
81 * xAxisScrollEnabled : true,
82 * xAxisSnapToInterval : scrollView.sizeWidth,
83 * xAxisScrollBoundary : scrollView.sizeWidth * 3,
84 * yAxisScrollEnabled : false
87 * scrollView.setScrollMode(scrollMode);
91 * @method setScrollMode
92 * @param {Object} scrollMode
93 * @param {Boolean} scrollMode.xAxisScrollEnabled True if the content can be scrolled in X axis or false if not.
94 * @param {Float} [scrollMode.xAxisSnapToInterval] When set, causes scroll view to snap to multiples of the value of the interval in the X axis while flicking. (by default no snapping)
95 * @param {Float} [scrollMode.xAxisScrollBoundary] When set, causes scroll view unable to scroll beyond the value of the boundary in the X axis (by default no boundary)
96 * @param {Boolean} scrollMode.yAxisScrollEnabled True if the content can be scrolled in Y axis or false if not.
97 * @param {Float} [scrollMode.yAxisSnapToInterval] When set, causes scroll view to snap to multiples of the value of the interval in the Y axis while flicking. (by default no snapping)
98 * @param {Float} [scrollMode.yAxisScrollBoundary] When set, causes scroll view unable to scroll beyond the value of the boundary in the Y axis (by default no boundary)
100 void ScrollViewApi::SetScrollMode( const v8::FunctionCallbackInfo< v8::Value >& args)
102 v8::Isolate* isolate = args.GetIsolate();
103 v8::HandleScope handleScope( isolate );
105 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
107 v8::Local<v8::Value> scrollMode( args[0] );
108 if( !scrollMode->IsObject() )
110 DALI_SCRIPT_EXCEPTION( isolate, "invalid scroll mode parameter" );
114 v8::Local<v8::Object> scrollModeObj = scrollMode->ToObject();
116 Toolkit::RulerPtr rulerX, rulerY;
118 // Check the scroll mode in the X axis
119 bool xAxisScrollEnabled = true;
120 v8::Local<v8::Value> xAxisScrollEnabledValue= scrollModeObj->Get( v8::String::NewFromUtf8( isolate, "xAxisScrollEnabled" ) );
121 if( xAxisScrollEnabledValue->IsBoolean() )
123 xAxisScrollEnabled = xAxisScrollEnabledValue->ToBoolean()->Value();
127 DALI_SCRIPT_EXCEPTION( isolate, "Missing xAxisScrollEnabled");
131 if(!xAxisScrollEnabled)
133 // Default ruler and disabled
134 rulerX = new Toolkit::DefaultRuler();
139 v8::Local<v8::Value> xAxisSnapToIntervalValue= scrollModeObj->Get( v8::String::NewFromUtf8( isolate, "xAxisSnapToInterval" ) );
140 if( xAxisSnapToIntervalValue->IsNumber() )
142 // Fixed ruler and enabled
143 float xAxisSnapToInterval = xAxisSnapToIntervalValue->ToNumber()->Value();
144 rulerX = new Toolkit::FixedRuler(xAxisSnapToInterval);
148 // Default ruler and enabled
149 rulerX = new Toolkit::DefaultRuler();
152 v8::Local<v8::Value> xAxisScrollBoundaryValue= scrollModeObj->Get( v8::String::NewFromUtf8( isolate, "xAxisScrollBoundary" ) );
153 if( xAxisScrollBoundaryValue->IsNumber() )
155 // By default ruler domain is disabled unless set
156 float xAxisScrollBoundary = xAxisScrollBoundaryValue->ToNumber()->Value();
157 rulerX->SetDomain( Toolkit::RulerDomain( 0, xAxisScrollBoundary, true ) );
161 // Check the scroll mode in the Y axis
162 bool yAxisScrollEnabled = true;
163 v8::Local<v8::Value> yAxisScrollEnabledValue= scrollModeObj->Get( v8::String::NewFromUtf8( isolate, "yAxisScrollEnabled" ) );
164 if( yAxisScrollEnabledValue->IsBoolean() )
166 yAxisScrollEnabled = yAxisScrollEnabledValue->ToBoolean()->Value();
170 DALI_SCRIPT_EXCEPTION( isolate, "Missing yAxisScrollEnabled");
174 if(!yAxisScrollEnabled)
176 // Default ruler and disabled
177 rulerY = new Toolkit::DefaultRuler();
182 v8::Local<v8::Value> yAxisSnapToIntervalValue= scrollModeObj->Get( v8::String::NewFromUtf8( isolate, "yAxisSnapToInterval" ) );
183 if( yAxisSnapToIntervalValue->IsNumber() )
185 // Fixed ruler and enabled
186 float yAxisSnapToInterval = yAxisSnapToIntervalValue->ToNumber()->Value();
187 rulerY = new Toolkit::FixedRuler(yAxisSnapToInterval);
191 // Default ruler and enabled
192 rulerY = new Toolkit::DefaultRuler();
195 v8::Local<v8::Value> yAxisScrollBoundaryValue= scrollModeObj->Get( v8::String::NewFromUtf8( isolate, "yAxisScrollBoundary" ) );
196 if( yAxisScrollBoundaryValue->IsNumber() )
198 // By default ruler domain is disabled unless set
199 float yAxisScrollBoundary = yAxisScrollBoundaryValue->ToNumber()->Value();
200 rulerY->SetDomain( Toolkit::RulerDomain( 0, yAxisScrollBoundary, true ) );
204 scrollView.SetRulerX(rulerX);
205 scrollView.SetRulerY(rulerY);
209 * Retrieves current scroll page based on the defined snap interval being the
210 * size of one page, and all pages laid out in a grid fashion, increasing from
211 * left to right until the end of the scroll boundary. Pages start from 0 as the
214 * If no snap interval is defined, this API will return undefined value.
217 * @method getCurrentPage
218 * @return {Integer} The index of current page in scroll view
220 void ScrollViewApi::GetCurrentPage( const v8::FunctionCallbackInfo< v8::Value >& args)
222 v8::Isolate* isolate = args.GetIsolate();
223 v8::HandleScope handleScope( isolate );
225 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
226 args.GetReturnValue().Set( v8::Integer::New( isolate, scrollView.GetCurrentPage() ) );
230 * Scrolls the contents to the given position.
232 * Position 0,0 is the origin. Increasing X scrolls contents left, while
233 * increasing Y scrolls contents up. If Rulers have been applied to the axes,
234 * then the contents will scroll until reaching the scroll boundary.
235 * Contents will not snap.
237 * The biasing parameters are provided such that in scenarios with 2 or 2x2 pages
238 * in wrap mode, the application developer can decide whether to scroll left or
239 * right to get to the target page.
242 * @method scrollToPosition
243 * @param {Array} position The position to scroll to.
244 * @param {Float} [durationSeconds] The duration of the scroll animation in seconds (default value is scrollView.scrollSnapDuration)
245 * @param {Integer} [alphaFunction] The alpha function to use.
246 * @param {Integer} [horizontalBias] Whether to bias scrolling to left or right (by default no bias).
247 * @param {Integer} [verticalBias] Whether to bias scrolling to top or bottom (by default no bias).
249 * // scroll direction bias is one of the following
250 * dali.DIRECTION_BIAS_NONE // Don't bias scroll snap
251 * dali.DIRECTION_BIAS_LEFT // Bias scroll snap to Left
252 * dali.DIRECTION_BIAS_RIGHT // Bias scroll snap to Right
254 * scrollView.scrollToPosition( [150.0, 100.0], 0.5, dali.ALPHA_FUNCTION_EASE_IN_OUT, dali.DIRECTION_BIAS_LEFT, dali.DIRECTION_BIAS_NONE );
256 void ScrollViewApi::ScrollToPosition( const v8::FunctionCallbackInfo< v8::Value >& args)
258 v8::Isolate* isolate = args.GetIsolate();
259 v8::HandleScope handleScope( isolate );
261 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
264 Vector2 position = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
267 DALI_SCRIPT_EXCEPTION( isolate, "bad position parameter" );
271 float durationSeconds = V8Utils::GetFloatParameter( PARAMETER_1, found, isolate, args, scrollView.GetScrollSnapDuration() );
273 AlphaFunction alphaFunction = scrollView.GetScrollSnapAlphaFunction();
276 int alpha = V8Utils::GetIntegerParameter( PARAMETER_2, found, isolate, args, 0 );
279 alphaFunction = static_cast<AlphaFunction::BuiltinFunction>(alpha);
282 Toolkit::DirectionBias horizontalBias = static_cast<Toolkit::DirectionBias>( V8Utils::GetIntegerParameter( PARAMETER_3, found, isolate, args, Toolkit::DirectionBiasNone ) );
283 Toolkit::DirectionBias verticalBias = static_cast<Toolkit::DirectionBias>( V8Utils::GetIntegerParameter( PARAMETER_4, found, isolate, args, Toolkit::DirectionBiasNone ) );
285 scrollView.ScrollTo( position, durationSeconds, alphaFunction, horizontalBias, verticalBias );
289 * Scrolls the contents to the page with the given index.
291 * This is based on assumption that the page index starts from 0 and the
292 * position of each page is: [pageIndex * snapToInterval, 0].
294 * If no snap interval is defined, calling this API will cause unexpected
297 * The biasing parameter is provided such that in scenarios with 2 pages
298 * in wrap mode, the application developer can decide whether to scroll
299 * left or right to get to the target page.
302 * @method scrollToPage
303 * @param {Integer} pageIndex The index of the page to scroll to.
304 * @param {Float} [durationSeconds] The duration of the scroll animation in seconds (default value is scrollView.scrollSnapDuration)
305 * @param {Integer} [bias] Whether to bias scrolling to left or right (by default no bias).
307 * // scroll direction bias is one of the following
308 * dali.DIRECTION_BIAS_NONE // Don't bias scroll snap
309 * dali.DIRECTION_BIAS_LEFT // Bias scroll snap to Left
310 * dali.DIRECTION_BIAS_RIGHT // Bias scroll snap to Right
312 * scrollView.scrollToPage( 1, 0.5, dali.DIRECTION_BIAS_RIGHT );
314 void ScrollViewApi::ScrollToPage( const v8::FunctionCallbackInfo< v8::Value >& args)
316 v8::Isolate* isolate = args.GetIsolate();
317 v8::HandleScope handleScope( isolate );
319 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
322 int pageIndex = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
325 DALI_SCRIPT_EXCEPTION( isolate, "bad page index parameter" );
329 float durationSeconds = V8Utils::GetFloatParameter( PARAMETER_1, found, isolate, args, scrollView.GetScrollSnapDuration() );
330 Toolkit::DirectionBias bias = static_cast<Toolkit::DirectionBias>( V8Utils::GetIntegerParameter( PARAMETER_2, found, isolate, args, Toolkit::DirectionBiasNone ) );
332 scrollView.ScrollTo( pageIndex, durationSeconds, bias );
336 * Scrolls the contents such that the given actor appears in the center of
339 * The actor must be a direct child of scroll view.
342 * @method scrollToActor
343 * @param {Object} actor The actor to scroll to.
344 * @param {Float} [durationSeconds] The duration of the scroll animation in seconds (default value is scrollView.scrollSnapDuration)
346 * scrollView.scrollToActor( childActor, 0.5 );
348 void ScrollViewApi::ScrollToActor( const v8::FunctionCallbackInfo< v8::Value >& args)
350 v8::Isolate* isolate = args.GetIsolate();
351 v8::HandleScope handleScope( isolate );
353 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
356 Actor actor = V8Utils::GetActorParameter( 0, found, isolate, args );
359 DALI_SCRIPT_EXCEPTION( isolate, "invalid actor parameter" );
363 float durationSeconds = V8Utils::GetFloatParameter( PARAMETER_1, found, isolate, args, scrollView.GetScrollSnapDuration() );
365 scrollView.ScrollTo( actor, durationSeconds );
369 * Scrolls the content to the nearest snap point as specified by the snap interval.
370 * If already at snap points, it will not scroll.
373 * @method scrollToSnapInterval
374 * @return {Boolean} True if snapping is needed or false if already at snap points
376 * var success = scrollView.scrollToSnapInterval();
378 void ScrollViewApi::ScrollToSnapInterval( const v8::FunctionCallbackInfo< v8::Value >& args)
380 v8::Isolate* isolate = args.GetIsolate();
381 v8::HandleScope handleScope( isolate );
383 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
385 args.GetReturnValue().Set( v8::Boolean::New( isolate, scrollView.ScrollToSnapPoint() ) );
389 * Set the alpha function of flick animation.
392 * @method setScrollFlickAlphaFunction
393 * @param {Integer} alphaFunction The alpha function to use.
395 * scrollView.setScrollFlickAlphaFunction( dali.ALPHA_FUNCTION_EASE_IN_OUT );
397 void ScrollViewApi::SetScrollFlickAlphaFunction( const v8::FunctionCallbackInfo< v8::Value >& args)
399 v8::Isolate* isolate = args.GetIsolate();
400 v8::HandleScope handleScope( isolate );
402 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
405 int alphaFunction = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
408 DALI_SCRIPT_EXCEPTION( isolate, "invalid alpha function parameter" );
413 scrollView.SetScrollFlickAlphaFunction( static_cast<AlphaFunction::BuiltinFunction>(alphaFunction) );
418 * Set the alpha function of snap animation.
421 * @method setScrollSnapAlphaFunction
422 * @param {String} alphaFunction The alpha function to use.
424 * scrollView.setScrollSnapAlphaFunction( dali.ALPHA_FUNCTION_EASE_IN_OUT );
426 void ScrollViewApi::SetScrollSnapAlphaFunction( const v8::FunctionCallbackInfo< v8::Value >& args)
428 v8::Isolate* isolate = args.GetIsolate();
429 v8::HandleScope handleScope( isolate );
431 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
434 int alphaFunction = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
437 DALI_SCRIPT_EXCEPTION( isolate, "invalid alpha function parameter" );
442 scrollView.SetScrollSnapAlphaFunction( static_cast<AlphaFunction::BuiltinFunction>(alphaFunction) );
447 * Set the alpha function of overshoot snap animation.
450 * @method setSnapOvershootAlphaFunction
451 * @param {String} alphaFunction The alpha function to use.
453 * scrollView.setSnapOvershootAlphaFunction( dali.ALPHA_FUNCTION_EASE_IN_OUT );
455 void ScrollViewApi::SetSnapOvershootAlphaFunction( const v8::FunctionCallbackInfo< v8::Value >& args)
457 v8::Isolate* isolate = args.GetIsolate();
458 v8::HandleScope handleScope( isolate );
460 Toolkit::ScrollView scrollView = GetScrollView( isolate, args );
463 int alphaFunction = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
466 DALI_SCRIPT_EXCEPTION( isolate, "invalid alpha function parameter" );
471 scrollView.SetSnapOvershootAlphaFunction( static_cast<AlphaFunction::BuiltinFunction>(alphaFunction) );
475 } // namespace V8Plugin