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 <rendering/shader-wrapper.h>
44 namespace // un named namespace
48 Dali::Toolkit::Builder GetBuilder( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
50 v8::HandleScope handleScope( isolate );
52 v8::Local<v8::Object> object = args.This();
53 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField( 0 ) );
54 void* ptr = field->Value();
56 BuilderWrapper* wrapper = static_cast<BuilderWrapper *>( ptr );
57 return wrapper->GetBuilder();
61 void GetMapAndString( v8::Isolate* isolate,
62 std::string stringKey,
63 std::string& stringValue,
65 Dali::Property::Map& map,
66 v8::Local<v8::Object> object)
68 v8::Local<v8::Value> stringNameValue = object->Get( v8::String::NewFromUtf8( isolate, stringKey.c_str() ) );
69 if( !stringNameValue->IsString() )
71 std::string message = "missing field :"+stringKey;
72 DALI_SCRIPT_EXCEPTION( isolate, message);
75 stringValue = V8Utils::v8StringToStdString( stringNameValue );
77 // constants is optional
79 v8::Local<v8::Value> constants = object->Get( v8::String::NewFromUtf8( isolate, mapKey.c_str() ) );
80 if( constants->IsObject() )
82 map = V8Utils::GetPropertyMapFromObject( isolate, constants->ToObject() );
87 } // un-named namespace
90 Dali::Toolkit::Builder BuilderApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
92 v8::Isolate* isolate = args.GetIsolate();
93 v8::HandleScope handleScope( isolate );
95 return Dali::Toolkit::Builder::New();
98 void BuilderApi::LoadFromString( const v8::FunctionCallbackInfo< v8::Value >& args )
100 v8::Isolate* isolate = args.GetIsolate();
101 v8::HandleScope handleScope( isolate );
103 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
106 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
109 builder.LoadFromString( str );
113 DALI_SCRIPT_EXCEPTION( isolate, "missing string parameter");
117 void BuilderApi::LoadFromFile( const v8::FunctionCallbackInfo< v8::Value >& args )
119 v8::Isolate* isolate = args.GetIsolate();
120 v8::HandleScope handleScope( isolate );
122 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
125 std::string fileName= V8Utils::GetStringParameter( 0, found, isolate, args );
128 DALI_SCRIPT_EXCEPTION( isolate, "missing string parameter");
132 std::ifstream fileStream( fileName.c_str() );
133 if( !fileStream.is_open() )
135 std::string message = "failed to open JSON file" + fileName;
136 DALI_SCRIPT_EXCEPTION( isolate, message );
140 //single-pass input iterator that reads successive characters from the input stream
141 std::string json = std::string( (std::istreambuf_iterator< char >( fileStream )), std::istreambuf_iterator< char >() );
143 builder.LoadFromString( json );
147 void BuilderApi::AddConstants( const v8::FunctionCallbackInfo< v8::Value >& args )
149 v8::Isolate* isolate = args.GetIsolate();
150 v8::HandleScope handleScope( isolate );
152 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
155 Dali::Property::Map map = V8Utils::GetPropertyMapParameter( 0,found, isolate, args );
159 builder.AddConstants( map );
163 DALI_SCRIPT_EXCEPTION( isolate, "no constants found" );
168 void BuilderApi::GetConstants( const v8::FunctionCallbackInfo< v8::Value >& args )
170 v8::Isolate* isolate = args.GetIsolate();
171 v8::HandleScope handleScope( isolate );
173 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
175 const Dali::Property::Map& map = builder.GetConstants();
177 v8::Local<v8::Object> object = v8::Object::New( isolate );
179 V8Utils::CreatePropertyMap( isolate, map, object );
181 args.GetReturnValue().Set( object );
184 void BuilderApi::CreateAnimation( const v8::FunctionCallbackInfo< v8::Value >& args )
186 v8::Isolate* isolate = args.GetIsolate();
187 v8::HandleScope handleScope( isolate );
197 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
199 if( !args[0]->IsObject() )
201 DALI_SCRIPT_EXCEPTION( isolate, "invalid property map, expecting { animation: x, constants: y, actor: z}" );
204 v8::Local<v8::Object> object = args[0]->ToObject();
206 std::string animationName;
207 Dali::Property::Map map;
209 // get the animation name and constant map
211 GetMapAndString( isolate,
220 v8::Local<v8::Value> actorValue = object->Get( v8::String::NewFromUtf8( isolate, "actor" ) );
221 if( actorValue->IsObject() )
223 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, actorValue->ToObject() );
226 actor = Actor::DownCast( handleWrapper->mHandle );
232 // Animation CreateAnimation( const std::string& animationName, const Dali::Property::Map& map, Dali::Actor sourceActor );
233 anim = builder.CreateAnimation( animationName, map, actor );
237 anim = builder.CreateAnimation( animationName, map);
239 v8::Local<v8::Object> localObject = AnimationWrapper::WrapAnimation( isolate, anim );
240 args.GetReturnValue().Set( localObject );
243 void BuilderApi::Create( const v8::FunctionCallbackInfo< v8::Value >& args )
245 v8::Isolate* isolate = args.GetIsolate();
246 v8::HandleScope handleScope( isolate );
248 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
250 if( !args[0]->IsObject() )
252 DALI_SCRIPT_EXCEPTION( isolate, "invalid param");
255 v8::Local<v8::Object> object = args[0]->ToObject();
259 // template: "myTemplate",
260 // constants: { IMAGE_DIR: "/usr/apps" , SHADER_DIR: "/usr/apps/.."}
263 std::string templateName;
264 Dali::Property::Map map;
266 GetMapAndString( isolate,
273 BaseHandle handle = builder.Create( templateName, map );
274 std::string typeName = handle.GetTypeName();
275 ActorWrapper::ActorType actorType = ActorWrapper::GetActorType( typeName );
277 if( actorType != ActorWrapper::UNKNOWN_ACTOR )
279 v8::Local<v8::Object> actorObject = ActorWrapper::WrapActor(isolate, Actor::DownCast(handle),actorType);
280 args.GetReturnValue().Set( actorObject );
284 if( typeName == "Animation")
286 v8::Local<v8::Object> animation = AnimationWrapper::WrapAnimation(isolate, Animation::DownCast(handle) );
287 args.GetReturnValue().Set( animation );
290 if( typeName == "Image")
292 v8::Local<v8::Object> image = ImageWrapper::WrapImage(isolate, Image::DownCast(handle) );
293 args.GetReturnValue().Set( image );
295 if( typeName == "Shader")
297 v8::Local<v8::Object> shader = ShaderWrapper::WrapShader(isolate, Shader::DownCast( handle ));
298 args.GetReturnValue().Set( shader );
301 void BuilderApi::ApplyStyle( const v8::FunctionCallbackInfo< v8::Value >& args )
303 v8::Isolate* isolate = args.GetIsolate();
304 v8::HandleScope handleScope( isolate );
305 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
307 // bool ApplyStyle( const std::string& styleName, Handle& handle );
310 std::string styleName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args);
313 DALI_SCRIPT_EXCEPTION( isolate, "missing style name" );
317 Dali::Handle handle = V8Utils::GetHandleParameter( PARAMETER_1, found, isolate, args );
320 DALI_SCRIPT_EXCEPTION( isolate, "missing handle parameter" );
324 builder.ApplyStyle( styleName, handle );
328 void BuilderApi::ApplyFromJson( const v8::FunctionCallbackInfo< v8::Value >& args )
330 v8::Isolate* isolate = args.GetIsolate();
331 v8::HandleScope handleScope( isolate );
332 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
336 Dali::Handle handle = V8Utils::GetHandleParameter( PARAMETER_0, found, isolate, args );
339 DALI_SCRIPT_EXCEPTION( isolate, "missing handle parameter" );
342 std::string jsonString = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args);
345 DALI_SCRIPT_EXCEPTION( isolate, "missing JSON string" );
348 builder.ApplyFromJson( handle, jsonString );
351 void BuilderApi::AddActors( const v8::FunctionCallbackInfo< v8::Value >& args )
353 v8::Isolate* isolate = args.GetIsolate();
354 v8::HandleScope handleScope( isolate );
355 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
364 if( !args[0]->IsObject() )
366 DALI_SCRIPT_EXCEPTION( isolate, "invalid param");
369 v8::Local<v8::Object> object = args[0]->ToObject();
371 v8::Local<v8::Value> actorValue = object->Get( v8::String::NewFromUtf8( isolate, "actor" ) );
372 if( actorValue->IsObject() )
374 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, actorValue->ToObject() );
377 actor = Actor::DownCast( handleWrapper->mHandle );
382 DALI_SCRIPT_EXCEPTION( isolate, "actor field not found in param");
386 v8::Local<v8::Value> stringNameValue = object->Get( v8::String::NewFromUtf8( isolate, "section" ) );
387 if( !stringNameValue->IsString() )
389 std::string message = "missing field: section";
390 DALI_SCRIPT_EXCEPTION( isolate, message);
393 std::string sectionName = V8Utils::v8StringToStdString( stringNameValue );
394 if( !sectionName.empty() )
396 builder.AddActors( sectionName, actor );
400 builder.AddActors( actor );
405 void BuilderApi::CreateRenderTask( const v8::FunctionCallbackInfo< v8::Value >& args )
407 v8::Isolate* isolate = args.GetIsolate();
408 v8::HandleScope handleScope( isolate );
409 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
411 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
415 builder.CreateRenderTask( str );
419 DALI_SCRIPT_EXCEPTION( isolate, "render task name");
423 void BuilderApi::GetFrameBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
425 v8::Isolate* isolate = args.GetIsolate();
426 v8::HandleScope handleScope( isolate );
427 Dali::Toolkit::Builder builder = GetBuilder( isolate, args );
430 std::string str = V8Utils::GetStringParameter( 0, found, isolate, args );
434 // @TODO Test this, may need to implement a Framebuffer image wrapper.?
435 v8::Local<v8::Object> localObject = ImageWrapper::WrapImage( isolate, builder.GetFrameBufferImage(str) );
436 args.GetReturnValue().Set( localObject );
440 DALI_SCRIPT_EXCEPTION( isolate, "frame buffer name");
445 } // namespace V8Plugin