1c5ad3987fdf0579f86074200341ee7e6e71f20b
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / texture-set-api.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
19 // CLASS HEADER
20 #include "texture-set-api.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24
25 // INTERNAL INCLUDES
26 #include <v8-utils.h>
27 #include <rendering/texture-set-wrapper.h>
28 #include <rendering/shader-wrapper.h>
29 #include <rendering/shader-api.h>
30 #include <rendering/sampler-wrapper.h>
31 #include <rendering/sampler-api.h>
32 #include <image/image-wrapper.h>
33
34 namespace Dali
35 {
36
37 namespace V8Plugin
38 {
39
40 /**
41  * ## TextureSet API
42  *
43  * TextureSet is a handle to an object that contains the textures used by a renderer
44  *
45  * @class TextureSet
46  * @extends Handle
47  */
48
49 TextureSet TextureSetApi::GetTextureSet( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
50 {
51   v8::HandleScope handleScope( isolate );
52
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();
56
57   TextureSetWrapper* wrapper = static_cast< TextureSetWrapper *>(ptr);
58   return wrapper->GetTextureSet();
59 }
60
61 TextureSet TextureSetApi::GetTextureSetFromParams( int paramIndex,
62                                                bool& found,
63                                                v8::Isolate* isolate,
64                                                const v8::FunctionCallbackInfo< v8::Value >& args )
65 {
66   found = false;
67
68   v8::HandleScope handleScope( isolate );
69   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::TEXTURE_SET, isolate, args );
70   if( wrappedObject )
71   {
72     found = true;
73     TextureSetWrapper* wrapper = static_cast< TextureSetWrapper *>(wrappedObject);
74     return wrapper->GetTextureSet();
75   }
76   else
77   {
78     return TextureSet();
79   }
80 }
81
82 /**
83  * Create a new texture set object.
84  *
85  * @constructor
86  * @method TextureSet
87  * @for TextureSet
88  * @return {Object} TextureSet
89  */
90 TextureSet TextureSetApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
91 {
92   return TextureSet::New();
93 }
94
95
96 /**
97  * Sets the image to be used by a given texture
98  * @method setImage
99  * @for TextureSet
100  * @param {integer} index The index of the texture in the array of textures
101  * @param {Object} image The image used by this sampler
102  */
103 void TextureSetApi::SetImage( const v8::FunctionCallbackInfo< v8::Value >& args )
104 {
105   v8::Isolate* isolate = args.GetIsolate();
106   v8::HandleScope handleScope( isolate );
107
108   TextureSet textureSet = GetTextureSet( isolate, args );
109
110   bool found( false );
111   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
112   if( !found )
113   {
114     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
115     return;
116   }
117
118   found = false;
119   Image image = V8Utils::GetImageParameter( PARAMETER_1, found, isolate, args );
120   if( !found )
121   {
122     DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 1" );
123   }
124   else
125   {
126     textureSet.SetImage(index, image);
127   }
128 }
129
130 /**
131  * Set the sampler used by a given texture
132  * @method setSampler
133  * @for TextureSet
134  * @param {integer} index The index of the texture in the array of textures
135  * @param {Object} sampler The new sampler
136  */
137 void TextureSetApi::SetSampler( const v8::FunctionCallbackInfo< v8::Value >& args )
138 {
139   v8::Isolate* isolate = args.GetIsolate();
140   v8::HandleScope handleScope( isolate );
141
142   TextureSet textureSet = GetTextureSet( isolate, args );
143
144   bool found( false );
145   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
146   if( !found )
147   {
148     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
149     return;
150   }
151
152   found = false;
153   Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_1, found, isolate, args );
154   if( !found )
155   {
156     DALI_SCRIPT_EXCEPTION( isolate, "missing sampler from param 1" );
157   }
158   else
159   {
160     textureSet.SetSampler(index, sampler);
161   }
162 }
163
164 } // namespace V8Plugin
165
166 } // namespace Dali