Merge "Remove sys-string and use dali-toolkit po files (sys-string package will be...
[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   v8::Isolate* isolate = args.GetIsolate();
93   v8::HandleScope handleScope( isolate );
94   return TextureSet();
95 }
96
97
98 /**
99  * Sets the image to be used by a given texture
100  * @method setImage
101  * @for TextureSet
102  * @param {integer} index The index of the texture in the array of textures
103  * @param {Object} image The image used by this sampler
104  */
105 void TextureSetApi::SetImage( const v8::FunctionCallbackInfo< v8::Value >& args )
106 {
107   v8::Isolate* isolate = args.GetIsolate();
108   v8::HandleScope handleScope( isolate );
109
110   TextureSet textureSet = GetTextureSet( isolate, args );
111
112   bool found( false );
113   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
114   if( !found )
115   {
116     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
117     return;
118   }
119
120   found = false;
121   Image image = V8Utils::GetImageParameter( PARAMETER_1, found, isolate, args );
122   if( !found )
123   {
124     DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 1" );
125   }
126   else
127   {
128     textureSet.SetImage(index, image);
129   }
130 }
131
132 /**
133  * Set the sampler used by a given texture
134  * @method setSampler
135  * @for TextureSet
136  * @param {integer} index The index of the texture in the array of textures
137  * @param {Object} sampler The new sampler
138  */
139 void TextureSetApi::SetSampler( const v8::FunctionCallbackInfo< v8::Value >& args )
140 {
141   v8::Isolate* isolate = args.GetIsolate();
142   v8::HandleScope handleScope( isolate );
143
144   TextureSet textureSet = GetTextureSet( isolate, args );
145
146   bool found( false );
147   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
148   if( !found )
149   {
150     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
151     return;
152   }
153
154   found = false;
155   Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_1, found, isolate, args );
156   if( !found )
157   {
158     DALI_SCRIPT_EXCEPTION( isolate, "missing sampler from param 1" );
159   }
160   else
161   {
162     textureSet.SetSampler(index, sampler);
163   }
164 }
165
166 } // namespace V8Plugin
167
168 } // namespace Dali