Merge "Changed all property & signal names to lowerCamelCase" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / stage / stage-api.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 // CLASS HEADER
19 #include "stage-api.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <object/property-value-wrapper.h>
24 #include <actors/actor-wrapper.h>
25 #include <actors/actor-api.h>
26 #include <render-tasks/render-task-list-wrapper.h>
27
28
29 namespace Dali
30 {
31
32 namespace V8Plugin
33 {
34
35
36 /***************************************
37  * STAGE FUNCTIONS
38  *
39  ****************************************/
40 Dali::Stage StageApi::GetStage(v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args)
41 {
42   // the stage object should be in args, but for now just use GetCurrent
43   return Stage::GetCurrent();
44 }
45
46 /**
47  * Adds a child Actor to the Stage.
48  * @method add
49  * @for Stage
50  * @param {Object} Actor
51  */
52 void StageApi::Add( const v8::FunctionCallbackInfo< v8::Value >& args )
53 {
54   v8::Isolate* isolate = args.GetIsolate();
55   v8::HandleScope handleScope( isolate );
56   Stage stage = GetStage( isolate, args );
57   bool found( false );
58   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
59   if( found )
60   {
61     stage.Add( actor );
62   }
63   else
64   {
65     DALI_SCRIPT_EXCEPTION( isolate, "missing actor parameter");
66   }
67 }
68
69 /**
70  * Removes a child Actor to the Stage.
71  * @method remove
72  * @for Stage
73  * @param {Object} Actor
74  */
75 void StageApi::Remove( const v8::FunctionCallbackInfo< v8::Value >& args )
76 {
77   v8::Isolate* isolate = args.GetIsolate();
78   v8::HandleScope handleScope( isolate );
79   Stage stage = GetStage( isolate, args );
80   bool found( false );
81
82   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
83   if( found )
84   {
85     stage.Remove( actor );
86   }
87   else
88   {
89     DALI_SCRIPT_EXCEPTION( isolate, "missing actor parameter");
90   }
91 }
92 /**
93  * Get the size of the stage
94  * @method getSize
95  * @for Stage
96  * @return {Object} size with properties { x: , y: }
97  */
98 void StageApi::GetSize( const v8::FunctionCallbackInfo< v8::Value >& args )
99 {
100   v8::Isolate* isolate = args.GetIsolate();
101   v8::HandleScope handleScope( isolate );
102   Stage stage = GetStage( isolate, args );
103   Vector2 size( stage.GetSize() );
104
105   v8::Local<v8::Object> sizeObject = v8::Object::New( isolate );
106   sizeObject->Set( v8::String::NewFromUtf8( isolate, "x"), v8::Number::New( isolate,size.width ));
107   sizeObject->Set( v8::String::NewFromUtf8( isolate, "y"), v8::Number::New( isolate,size.height ));
108
109   // set return value
110   args.GetReturnValue().Set( sizeObject );
111
112 }
113 /**
114  * Retrieve the list of render-tasks.
115  * @method getRenderTaskList
116  * @for Stage
117  * @return {Object} render task list
118  */
119 void StageApi::GetRenderTaskList( const v8::FunctionCallbackInfo< v8::Value >& args  )
120 {
121   v8::Isolate* isolate = args.GetIsolate();
122   v8::HandleScope handleScope( isolate );
123   Stage stage = GetStage( isolate, args );
124
125   RenderTaskList taskList = stage.GetRenderTaskList();
126
127   v8::Local <v8::Object> object = RenderTaskListWrapper::WrapRenderTaskList( isolate, taskList );
128
129   args.GetReturnValue().Set(  object );
130 }
131 /**
132  * Query the number of on-stage layers.
133  * Note that a default layer is always provided (count >= 1).
134  * @method getLayerCount
135  * @for Stage
136  * @return {Number} number of layers
137  */
138 void StageApi::GetLayerCount( const v8::FunctionCallbackInfo<v8::Value>& args)
139 {
140   v8::Isolate* isolate = args.GetIsolate();
141   v8::HandleScope handleScope( isolate );
142   Stage stage = GetStage( isolate, args );
143
144   args.GetReturnValue().Set( v8::Integer::New( isolate, stage.GetLayerCount() ) );
145
146 }
147 /**
148  * Retrieve the list of render-tasks.
149  * @method getLayer
150  * @for Stage
151  * @param {Integer} depth
152  * @return {Object} layer
153  */
154 void StageApi::GetLayer( const v8::FunctionCallbackInfo< v8::Value >& args  )
155 {
156   v8::Isolate* isolate = args.GetIsolate();
157   v8::HandleScope handleScope( isolate );
158   Stage stage = GetStage( isolate, args );
159
160   bool found( false );
161   unsigned int depth = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
162   if( !found )
163   {
164     DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
165     return;
166   }
167   Layer rootLayer = stage.GetLayer( depth );
168
169   v8::Handle < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, rootLayer );
170   args.GetReturnValue().Set( wrappedLayer );
171 }
172 /**
173  * Returns the Stage's Root Layer.
174  * @method getRootLayer
175  * @for Stage
176  * @return {Object} root layer
177  */
178 void StageApi::GetRootLayer( const v8::FunctionCallbackInfo<v8::Value>& args)
179 {
180   v8::Isolate* isolate = args.GetIsolate();
181   v8::HandleScope handleScope( isolate );
182   Stage stage = GetStage( isolate, args );
183
184   Layer rootLayer = stage.GetRootLayer();
185
186   v8::Handle < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, rootLayer );
187   args.GetReturnValue().Set( wrappedLayer );
188 }
189
190 /**
191  * Set the background color of the stage
192  * @method setBackgroundColor
193  * @for Stage
194  * @param {Object} Array of [Red,Green,Blue,Alpha], e.g. dali.COLOR_RED, or [1,0,0,1] (full red and alpha);
195  * @example
196  *     dali.stage.setBackgroundColor( dali.COLOR_RED );
197  *
198  *     dali.stage.setBackgroundColor( [0.5,0.5,0.5,1 ] ); // set the background to grey
199  */
200 void StageApi::SetBackgroundColor( const v8::FunctionCallbackInfo<v8::Value>& args)
201 {
202   v8::Isolate* isolate = args.GetIsolate();
203   v8::HandleScope handleScope( isolate );
204   Stage stage = GetStage( isolate, args );
205   bool found( false );
206
207   Vector4 backgroundColor = V8Utils::GetVector4Parameter( PARAMETER_0, found, isolate, args);
208   if (!found)
209   {
210     DALI_SCRIPT_EXCEPTION( isolate, "no parameter not found" );
211     return;
212   }
213   stage.SetBackgroundColor( backgroundColor );
214 }
215 /**
216  * Get the background color of the stage
217  * @method getBackgroundColor
218  * @for Stage
219  * @return {Object} object that contains the properties [red:,green:,blue,alpha:]
220  */
221 void StageApi::GetBackgroundColor( const v8::FunctionCallbackInfo<v8::Value>& args)
222 {
223   v8::Isolate* isolate = args.GetIsolate();
224   v8::HandleScope handleScope( isolate );
225   Stage stage = GetStage( isolate, args );
226
227   Vector4 color = stage.GetBackgroundColor();
228
229   v8::Local<v8::Object> colorObject = v8::Object::New( isolate );
230
231   colorObject->Set( v8::String::NewFromUtf8( isolate, "red"),   v8::Number::New( isolate,color.r));
232   colorObject->Set( v8::String::NewFromUtf8( isolate, "green"), v8::Number::New( isolate,color.g));
233   colorObject->Set( v8::String::NewFromUtf8( isolate, "blue"),  v8::Number::New( isolate,color.b));
234   colorObject->Set( v8::String::NewFromUtf8( isolate, "alpha"), v8::Number::New( isolate,color.a));
235
236   // set return value
237   args.GetReturnValue().Set( colorObject );
238
239
240 }
241 /**
242  * Retrieve the DPI of the display device to which the stage is connected.
243  * @method getDpi
244  * @for Stage
245  * @return {Object} object that contains the properties [x:,y:]
246  */
247 void StageApi::GetDpi( const v8::FunctionCallbackInfo<v8::Value>& args)
248 {
249   v8::Isolate* isolate = args.GetIsolate();
250   v8::HandleScope handleScope( isolate );
251   Stage stage = GetStage( isolate, args );
252
253   Vector2 dpi( stage.GetDpi() );
254
255   v8::Local<v8::Object> dpiObject = v8::Object::New( isolate );
256   dpiObject->Set( v8::String::NewFromUtf8( isolate, "x"),   v8::Number::New( isolate,dpi.x));
257   dpiObject->Set( v8::String::NewFromUtf8( isolate, "y"), v8::Number::New( isolate,dpi.y));
258
259
260   args.GetReturnValue().Set(  dpiObject );
261 }
262
263
264 } // namespace V8Plugin
265
266 } // namespace Dali