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 "builder-api.h"
22 #include <dali/integration-api/debug.h>
29 #include <object/property-value-wrapper.h>
30 #include <object/handle-wrapper.h>
31 #include <toolkit/builder/builder-wrapper.h>
32 #include <actors/actor-wrapper.h>
33 #include <image/image-wrapper.h>
34 #include <animation/animation-wrapper.h>
35 #include <shader-effects/shader-effect-wrapper.h>
36 #include <rendering/shader-wrapper.h>
45 namespace // un named namespace
49 Dali::Toolkit::Builder GetBuilder( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
51 v8::HandleScope handleScope( isolate );
53 v8::Local<v8::Object> object = args.This();
54 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField( 0 ) );
55 void* ptr = field->Value();
57 BuilderWrapper* wrapper = static_cast<BuilderWrapper *>( ptr );
58 return wrapper->GetBuilder();
62 void GetMapAndString( v8::Isolate* isolate,
63 std::string stringKey,
64 std::string& stringValue,
66 Dali::Property::Map& map,
67 v8::Local<v8::Object> object)
69 v8::Local<v8::Value> stringNameValue = object->Get( v8::String::NewFromUtf8( isolate, stringKey.c_str() ) );
70 if( !stringNameValue->IsString() )
72 std::string message = "missing field :"+stringKey;
73 DALI_SCRIPT_EXCEPTION( isolate, message);
76 stringValue = V8Utils::v8StringToStdString( stringNameValue );
78 // constants is optional
80 v8::Local<v8::Value> constants = object->Get( v8::String::NewFromUtf8( isolate, mapKey.c_str() ) );
81 if( constants->IsObject() )
83 map = V8Utils::GetPropertyMapFromObject( isolate, constants->ToObject() );
88 } // un-named namespace
91 Dali::Toolkit::Builder BuilderApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
93 v8::Isolate* isolate = args.GetIsolate();
94 v8::HandleScope handleScope( isolate );
96 return Dali::Toolkit::Builder::New();
99 void BuilderApi::LoadFromString( const v8::FunctionCallbackInfo< v8::Value >& args )
101 v8::Isolate* isolate = args.GetIsolate();
102 v8::HandleScope handleScope( isolate );
104 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
107 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
110 builder.LoadFromString( str );
114 DALI_SCRIPT_EXCEPTION( isolate, "missing string parameter");
118 void BuilderApi::LoadFromFile( const v8::FunctionCallbackInfo< v8::Value >& args )
120 v8::Isolate* isolate = args.GetIsolate();
121 v8::HandleScope handleScope( isolate );
123 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
126 std::string fileName= V8Utils::GetStringParameter( 0, found, isolate, args );
129 DALI_SCRIPT_EXCEPTION( isolate, "missing string parameter");
133 std::ifstream fileStream( fileName.c_str() );
134 if( !fileStream.is_open() )
136 std::string message = "failed to open JSON file" + fileName;
137 DALI_SCRIPT_EXCEPTION( isolate, message );
141 //single-pass input iterator that reads successive characters from the input stream
142 std::string json = std::string( (std::istreambuf_iterator< char >( fileStream )), std::istreambuf_iterator< char >() );
144 builder.LoadFromString( json );
148 void BuilderApi::AddConstants( const v8::FunctionCallbackInfo< v8::Value >& args )
150 v8::Isolate* isolate = args.GetIsolate();
151 v8::HandleScope handleScope( isolate );
153 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
156 Dali::Property::Map map = V8Utils::GetPropertyMapParameter( 0,found, isolate, args );
160 builder.AddConstants( map );
164 DALI_SCRIPT_EXCEPTION( isolate, "no constants found" );
169 void BuilderApi::GetConstants( const v8::FunctionCallbackInfo< v8::Value >& args )
171 v8::Isolate* isolate = args.GetIsolate();
172 v8::HandleScope handleScope( isolate );
174 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
176 const Dali::Property::Map& map = builder.GetConstants();
178 v8::Local<v8::Object> object = v8::Object::New( isolate );
180 V8Utils::CreatePropertyMap( isolate, map, object );
182 args.GetReturnValue().Set( object );
185 void BuilderApi::CreateAnimation( const v8::FunctionCallbackInfo< v8::Value >& args )
187 v8::Isolate* isolate = args.GetIsolate();
188 v8::HandleScope handleScope( isolate );
198 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
200 if( !args[0]->IsObject() )
202 DALI_SCRIPT_EXCEPTION( isolate, "invalid property map, expecting { animation: x, constants: y, actor: z}" );
205 v8::Local<v8::Object> object = args[0]->ToObject();
207 std::string animationName;
208 Dali::Property::Map map;
210 // get the animation name and constant map
212 GetMapAndString( isolate,
221 v8::Local<v8::Value> actorValue = object->Get( v8::String::NewFromUtf8( isolate, "actor" ) );
222 if( actorValue->IsObject() )
224 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, actorValue->ToObject() );
227 actor = Actor::DownCast( handleWrapper->mHandle );
233 // Animation CreateAnimation( const std::string& animationName, const Dali::Property::Map& map, Dali::Actor sourceActor );
234 anim = builder.CreateAnimation( animationName, map, actor );
238 anim = builder.CreateAnimation( animationName, map);
240 v8::Local<v8::Object> localObject = AnimationWrapper::WrapAnimation( isolate, anim );
241 args.GetReturnValue().Set( localObject );
244 void BuilderApi::Create( const v8::FunctionCallbackInfo< v8::Value >& args )
246 v8::Isolate* isolate = args.GetIsolate();
247 v8::HandleScope handleScope( isolate );
249 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
251 if( !args[0]->IsObject() )
253 DALI_SCRIPT_EXCEPTION( isolate, "invalid param");
256 v8::Local<v8::Object> object = args[0]->ToObject();
260 // template: "myTemplate",
261 // constants: { IMAGE_DIR: "/usr/apps" , SHADER_DIR: "/usr/apps/.."}
264 std::string templateName;
265 Dali::Property::Map map;
267 GetMapAndString( isolate,
274 BaseHandle handle = builder.Create( templateName, map );
275 std::string typeName = handle.GetTypeName();
276 ActorWrapper::ActorType actorType = ActorWrapper::GetActorType( typeName );
278 if( actorType != ActorWrapper::UNKNOWN_ACTOR )
280 v8::Local<v8::Object> actorObject = ActorWrapper::WrapActor(isolate, Actor::DownCast(handle),actorType);
281 args.GetReturnValue().Set( actorObject );
285 if( typeName == "Animation")
287 v8::Local<v8::Object> animation = AnimationWrapper::WrapAnimation(isolate, Animation::DownCast(handle) );
288 args.GetReturnValue().Set( animation );
291 if( typeName == "Image")
293 v8::Local<v8::Object> image = ImageWrapper::WrapImage(isolate, Image::DownCast(handle) );
294 args.GetReturnValue().Set( image );
296 if( typeName == "ShaderEffect")
298 v8::Local<v8::Object> shaderEffect = ShaderEffectWrapper::WrapShaderEffect(isolate, ShaderEffect::DownCast( handle ));
299 args.GetReturnValue().Set( shaderEffect );
301 if( typeName == "Shader")
303 v8::Local<v8::Object> shader = ShaderWrapper::WrapShader(isolate, Shader::DownCast( handle ));
304 args.GetReturnValue().Set( shader );
307 void BuilderApi::ApplyStyle( const v8::FunctionCallbackInfo< v8::Value >& args )
309 v8::Isolate* isolate = args.GetIsolate();
310 v8::HandleScope handleScope( isolate );
311 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
313 // bool ApplyStyle( const std::string& styleName, Handle& handle );
316 std::string styleName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args);
319 DALI_SCRIPT_EXCEPTION( isolate, "missing style name" );
323 Dali::Handle handle = V8Utils::GetHandleParameter( PARAMETER_1, found, isolate, args );
326 DALI_SCRIPT_EXCEPTION( isolate, "missing handle parameter" );
330 builder.ApplyStyle( styleName, handle );
334 void BuilderApi::ApplyFromJson( const v8::FunctionCallbackInfo< v8::Value >& args )
336 v8::Isolate* isolate = args.GetIsolate();
337 v8::HandleScope handleScope( isolate );
338 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
342 Dali::Handle handle = V8Utils::GetHandleParameter( PARAMETER_0, found, isolate, args );
345 DALI_SCRIPT_EXCEPTION( isolate, "missing handle parameter" );
348 std::string jsonString = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args);
351 DALI_SCRIPT_EXCEPTION( isolate, "missing JSON string" );
354 builder.ApplyFromJson( handle, jsonString );
357 void BuilderApi::AddActors( const v8::FunctionCallbackInfo< v8::Value >& args )
359 v8::Isolate* isolate = args.GetIsolate();
360 v8::HandleScope handleScope( isolate );
361 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
370 if( !args[0]->IsObject() )
372 DALI_SCRIPT_EXCEPTION( isolate, "invalid param");
375 v8::Local<v8::Object> object = args[0]->ToObject();
377 v8::Local<v8::Value> actorValue = object->Get( v8::String::NewFromUtf8( isolate, "actor" ) );
378 if( actorValue->IsObject() )
380 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, actorValue->ToObject() );
383 actor = Actor::DownCast( handleWrapper->mHandle );
388 DALI_SCRIPT_EXCEPTION( isolate, "actor field not found in param");
392 v8::Local<v8::Value> stringNameValue = object->Get( v8::String::NewFromUtf8( isolate, "section" ) );
393 if( !stringNameValue->IsString() )
395 std::string message = "missing field: section";
396 DALI_SCRIPT_EXCEPTION( isolate, message);
399 std::string sectionName = V8Utils::v8StringToStdString( stringNameValue );
400 if( !sectionName.empty() )
402 builder.AddActors( sectionName, actor );
406 builder.AddActors( actor );
411 void BuilderApi::CreateRenderTask( const v8::FunctionCallbackInfo< v8::Value >& args )
413 v8::Isolate* isolate = args.GetIsolate();
414 v8::HandleScope handleScope( isolate );
415 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
417 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
421 builder.CreateRenderTask( str );
425 DALI_SCRIPT_EXCEPTION( isolate, "render task name");
429 void BuilderApi::GetShaderEffect( const v8::FunctionCallbackInfo< v8::Value >& args )
431 v8::Isolate* isolate = args.GetIsolate();
432 v8::HandleScope handleScope( isolate );
433 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
436 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
439 ShaderEffect handle = builder.GetShaderEffect( str );
440 v8::Local<v8::Object> shader = ShaderEffectWrapper::WrapShaderEffect(isolate, handle);
441 args.GetReturnValue().Set( shader );
445 DALI_SCRIPT_EXCEPTION( isolate, "missing shader name");
449 void BuilderApi::GetFrameBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
451 v8::Isolate* isolate = args.GetIsolate();
452 v8::HandleScope handleScope( isolate );
453 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
456 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
460 // @TODO Test this, may need to implement a Framebuffer image wrapper.?
461 v8::Local<v8::Object> localObject = ImageWrapper::WrapImage( isolate, builder.GetFrameBufferImage(str) );
462 args.GetReturnValue().Set( localObject );
466 DALI_SCRIPT_EXCEPTION( isolate, "frame buffer name");
471 } // namespace V8Plugin