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 <text/font-wrapper.h>
36 #include <shader-effects/shader-effect-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: "my-temppalte",
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 == "Font ")
298 v8::Local<v8::Object> font = FontWrapper::WrapFont(isolate, Font::DownCast(handle) );
299 args.GetReturnValue().Set( font );
301 if( typeName == "Shader")
303 v8::Local<v8::Object> shader = ShaderEffectWrapper::WrapShaderEffect(isolate, ShaderEffect::DownCast( handle ));
304 args.GetReturnValue().Set( shader );
308 void BuilderApi::ApplyStyle( const v8::FunctionCallbackInfo< v8::Value >& args )
310 v8::Isolate* isolate = args.GetIsolate();
311 v8::HandleScope handleScope( isolate );
312 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
314 // bool ApplyStyle( const std::string& styleName, Handle& handle );
317 std::string styleName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args);
320 DALI_SCRIPT_EXCEPTION( isolate, "missing style name" );
324 Dali::Handle handle = V8Utils::GetHandleParameter( PARAMETER_1, found, isolate, args );
327 DALI_SCRIPT_EXCEPTION( isolate, "missing handle parameter" );
331 builder.ApplyStyle( styleName, handle );
335 void BuilderApi::ApplyFromJson( const v8::FunctionCallbackInfo< v8::Value >& args )
337 v8::Isolate* isolate = args.GetIsolate();
338 v8::HandleScope handleScope( isolate );
339 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
343 Dali::Handle handle = V8Utils::GetHandleParameter( PARAMETER_0, found, isolate, args );
346 DALI_SCRIPT_EXCEPTION( isolate, "missing handle parameter" );
349 std::string jsonString = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args);
352 DALI_SCRIPT_EXCEPTION( isolate, "missing JSON string" );
355 builder.ApplyFromJson( handle, jsonString );
358 void BuilderApi::AddActors( const v8::FunctionCallbackInfo< v8::Value >& args )
360 v8::Isolate* isolate = args.GetIsolate();
361 v8::HandleScope handleScope( isolate );
362 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
371 if( !args[0]->IsObject() )
373 DALI_SCRIPT_EXCEPTION( isolate, "invalid param");
376 v8::Local<v8::Object> object = args[0]->ToObject();
378 v8::Local<v8::Value> actorValue = object->Get( v8::String::NewFromUtf8( isolate, "actor" ) );
379 if( actorValue->IsObject() )
381 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, actorValue->ToObject() );
384 actor = Actor::DownCast( handleWrapper->mHandle );
389 DALI_SCRIPT_EXCEPTION( isolate, "actor field not found in param");
393 v8::Local<v8::Value> stringNameValue = object->Get( v8::String::NewFromUtf8( isolate, "section" ) );
394 if( !stringNameValue->IsString() )
396 std::string message = "missing field: section";
397 DALI_SCRIPT_EXCEPTION( isolate, message);
400 std::string sectionName = V8Utils::v8StringToStdString( stringNameValue );
401 if( !sectionName.empty() )
403 builder.AddActors( sectionName, actor );
407 builder.AddActors( actor );
412 void BuilderApi::CreateRenderTask( const v8::FunctionCallbackInfo< v8::Value >& args )
414 v8::Isolate* isolate = args.GetIsolate();
415 v8::HandleScope handleScope( isolate );
416 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
418 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
422 builder.CreateRenderTask( str );
426 DALI_SCRIPT_EXCEPTION( isolate, "render task name");
430 void BuilderApi::GetShaderEffect( const v8::FunctionCallbackInfo< v8::Value >& args )
432 v8::Isolate* isolate = args.GetIsolate();
433 v8::HandleScope handleScope( isolate );
434 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
437 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
440 ShaderEffect handle = builder.GetShaderEffect( str );
441 v8::Local<v8::Object> shader = ShaderEffectWrapper::WrapShaderEffect(isolate, handle);
442 args.GetReturnValue().Set( shader );
446 DALI_SCRIPT_EXCEPTION( isolate, "missing shader name");
450 void BuilderApi::GetFrameBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
452 v8::Isolate* isolate = args.GetIsolate();
453 v8::HandleScope handleScope( isolate );
454 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
457 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
461 // @TODO Test this, may need to implement a Framebuffer image wrapper.?
462 v8::Local<v8::Object> localObject = ImageWrapper::WrapImage( isolate, builder.GetFrameBufferImage(str) );
463 args.GetReturnValue().Set( localObject );
467 DALI_SCRIPT_EXCEPTION( isolate, "frame buffer name");
472 } // namespace V8Plugin