Geometry Batching
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / texture-set-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 "texture-set-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <rendering/texture-set-api.h>
24 #include <shared/api-function.h>
25 #include <shared/object-template-helper.h>
26 #include <dali-wrapper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 v8::Persistent<v8::ObjectTemplate> TextureSetWrapper::mTextureSetTemplate;
35
36 namespace // un-named name space
37 {
38
39 /**
40  * Contains a list of all functions that can be called
41  */
42 const ApiFunction TextureSetFunctionTable[]=
43 {
44     /**************************************
45     * TextureSet API (in order of texture-set.h)
46     **************************************/
47
48    { "SetImage"                        , TextureSetApi::SetImage },
49    { "SetSampler"                      , TextureSetApi::SetSampler },
50 };
51
52 const unsigned int TextureSetFunctionTableCount = sizeof(TextureSetFunctionTable)/sizeof(TextureSetFunctionTable[0]);
53 } //un-named space
54
55
56 TextureSetWrapper::TextureSetWrapper( const Dali::TextureSet& textureSet, GarbageCollectorInterface& gc )
57 :  BaseWrappedObject(  BaseWrappedObject::TEXTURE_SET, gc )
58 {
59     mTextureSet = textureSet;
60 }
61
62 v8::Handle<v8::Object> TextureSetWrapper::WrapTextureSet(v8::Isolate* isolate, const Dali::TextureSet& textureSet )
63 {
64   v8::EscapableHandleScope handleScope( isolate );
65   v8::Local<v8::ObjectTemplate> objectTemplate;
66
67   objectTemplate = GetTextureSetTemplate( isolate);
68
69   // create an instance of the template
70   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
71
72   // create the texture set wrapper
73   TextureSetWrapper* pointer =  new TextureSetWrapper( textureSet, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
74
75   // assign the JavaScript object to the wrapper.
76   pointer->SetJavascriptObject( isolate, localObject );
77
78   return handleScope.Escape( localObject );
79 }
80
81 v8::Local<v8::ObjectTemplate> TextureSetWrapper::GetTextureSetTemplate( v8::Isolate* isolate)
82 {
83   v8::EscapableHandleScope handleScope( isolate );
84   v8::Local<v8::ObjectTemplate> objectTemplate;
85
86   if( mTextureSetTemplate.IsEmpty() )
87   {
88     objectTemplate = MakeTextureSetTemplate( isolate );
89     mTextureSetTemplate.Reset( isolate, objectTemplate );
90   }
91   else
92   {
93     // get the object template
94     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mTextureSetTemplate );
95   }
96   return handleScope.Escape( objectTemplate );
97 }
98
99 v8::Handle<v8::ObjectTemplate> TextureSetWrapper::MakeTextureSetTemplate( v8::Isolate* isolate )
100 {
101   v8::EscapableHandleScope handleScope( isolate );
102
103   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
104
105   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
106
107   // add our function properties
108   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, TextureSetFunctionTable, TextureSetFunctionTableCount );
109
110   return handleScope.Escape( objTemplate );
111 }
112
113 void TextureSetWrapper::NewTextureSet( const v8::FunctionCallbackInfo< v8::Value >& args)
114 {
115   v8::Isolate* isolate = args.GetIsolate();
116   v8::HandleScope handleScope( isolate);
117
118   if(!args.IsConstructCall())
119   {
120       DALI_SCRIPT_EXCEPTION( isolate, "TextureSet constructor called without 'new'");
121       return;
122   }
123   Dali::TextureSet textureSet = TextureSetApi::New( args );
124
125   if(textureSet)
126   {
127     v8::Local<v8::Object> localObject = WrapTextureSet( isolate, textureSet );
128     args.GetReturnValue().Set( localObject );
129   }
130 }
131
132
133 TextureSet TextureSetWrapper::GetTextureSet()
134 {
135   return mTextureSet;
136 }
137
138
139 } // namespace V8Plugin
140
141 } // namespace Dali