[dali_1.4.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / stage / stage-wrapper.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-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali-wrapper.h>
24 #include <stage/stage-api.h>
25 #include <shared/api-function.h>
26 #include <shared/object-template-helper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 namespace
35 {
36
37 /**
38  * Contains a list of all functions that can be called on the stage
39  */
40 const ApiFunction StageFunctionTable[]=
41 {
42     /**************************************
43     * Stage API (in order of stage.h)
44     * Any properties that have accessor functions are ignored to avoid duplication
45     **************************************/
46     { "Add",                  StageApi::Add       },
47     { "Remove",               StageApi::Remove    },
48     { "GetSize",              StageApi::GetSize   },
49     { "GetRenderTaskList",    StageApi::GetRenderTaskList   },
50     { "GetLayerCount",        StageApi::GetLayerCount   },
51     { "GetLayer",             StageApi::GetLayer        },
52     { "GetRootLayer",         StageApi::GetRootLayer   },
53     { "SetBackgroundColor",   StageApi::SetBackgroundColor   },
54     { "GetBackgroundColor",   StageApi::GetBackgroundColor   },
55     { "GetDpi",               StageApi::GetDpi       },
56 };
57
58 const unsigned int StageFunctionTableCount = sizeof(StageFunctionTable)/sizeof(StageFunctionTable[0]);
59 } //un-named space
60
61
62 StageWrapper::StageWrapper( const Dali::Stage& stage, GarbageCollectorInterface& gc )
63 : BaseWrappedObject( BaseWrappedObject::STAGE , gc )
64 {
65     mStage = stage;
66 }
67
68 v8::Handle<v8::Object> StageWrapper::WrapStage(v8::Isolate* isolate, const Dali::Stage& stage )
69 {
70   v8::EscapableHandleScope handleScope( isolate );
71   v8::Local<v8::ObjectTemplate> objectTemplate;
72
73   objectTemplate = GetStageTemplate( isolate);
74
75   // create an instance of the template
76   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
77
78   // create the Stage wrapper
79   StageWrapper* pointer =  new StageWrapper( stage, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
80
81   // assign the JavaScript object to the wrapper.
82   pointer->SetJavascriptObject( isolate, localObject );
83
84   return handleScope.Escape( localObject );
85 }
86
87 v8::Local<v8::ObjectTemplate> StageWrapper::GetStageTemplate( v8::Isolate* isolate)
88 {
89   v8::EscapableHandleScope handleScope( isolate );
90   v8::Local<v8::ObjectTemplate> objectTemplate;
91   objectTemplate = MakeStageTemplate( isolate );
92   return handleScope.Escape( objectTemplate );
93 }
94
95 v8::Handle<v8::ObjectTemplate> StageWrapper::MakeStageTemplate( v8::Isolate* isolate )
96 {
97   v8::EscapableHandleScope handleScope( isolate );
98
99   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
100
101   // add intercepts for Signals, we can't use HandleWrapper::AddIntercepts because Stage doesn't inherit
102   // from Handle ( just baseHandle)
103   ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate );
104
105   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
106
107   // add our function properties
108   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, StageFunctionTable, StageFunctionTableCount );
109
110   return handleScope.Escape( objTemplate );
111 }
112
113 Stage StageWrapper::GetStage()
114 {
115   return mStage;
116 }
117
118
119 } // namespace V8Plugin
120
121 } // namespace Dali