JavaScript binding for new mesh APIs
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / sampler-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 // CLASS HEADER
19 #include "sampler-api.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23
24 // INTERNAL INCLUDES
25 #include <v8-utils.h>
26 #include <rendering/sampler-wrapper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 /**
35  * ## Sampler API
36  *
37  * Sampler is a handle to an object that can be used to provide the sampling parameters to sample textures
38  *
39  * @class Sampler
40  */
41
42 Sampler SamplerApi::GetSampler( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
43 {
44   v8::HandleScope handleScope( isolate );
45
46   v8::Local<v8::Object> object = args.This();
47   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
48   void* ptr = field->Value();
49
50   SamplerWrapper* wrapper = static_cast< SamplerWrapper *>(ptr);
51   return wrapper->GetSampler();
52 }
53
54 Sampler SamplerApi::GetSamplerFromParams( int paramIndex,
55                                              bool& found,
56                                              v8::Isolate* isolate,
57                                              const v8::FunctionCallbackInfo< v8::Value >& args )
58 {
59   found = false;
60
61   v8::HandleScope handleScope( isolate );
62   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::SAMPLER, isolate, args );
63   if( wrappedObject )
64   {
65     found = true;
66     SamplerWrapper* wrapper = static_cast< SamplerWrapper *>(wrappedObject);
67     return wrapper->GetSampler();
68   }
69   else
70   {
71     return Sampler();
72   }
73 }
74
75 /**
76  * Create a new sampler object.
77  *
78  * @constructor
79  * @method Sampler
80  * @for Sampler
81  * @return {Object} Sampler
82  */
83 Sampler SamplerApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
84 {
85   return Sampler::New();
86 }
87
88 /**
89  * Set the filter modes for this sampler
90  * @method setFilterMode
91  * @for Sampler
92  * @param {integer} minFilter The minification filter that will be used
93  * @param {integer} magFilter The magnification filter that will be used
94  * @example
95  *      // filter mode is one of the following
96  *      dali.FILTER_MODE_NONE             // Use GL system defaults (minification NEAREST_MIPMAP_LINEAR, magnification LINEAR)
97  *      dali.FILTER_MODE_DEFAULT          // Use dali defaults (minification LINEAR, magnification LINEAR)
98  *      dali.FILTER_MODE_NEAREST          // Filter nearest
99  *      dali.FILTER_MODE_LINEAR           // Filter linear
100  *
101  *      sampler.setFilterMode( dali.FILTER_MODE_DEFAULT, dali.FILTER_MODE_LINEAR );
102  */
103 void SamplerApi::SetFilterMode( const v8::FunctionCallbackInfo< v8::Value >& args )
104 {
105   v8::Isolate* isolate = args.GetIsolate();
106   v8::HandleScope handleScope( isolate );
107
108   Sampler sampler = GetSampler( isolate, args );
109
110   bool found( false );
111   int minFilter = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
112   if( !found )
113   {
114     DALI_SCRIPT_EXCEPTION( isolate, "missing minification filter from param 0" );
115     return;
116   }
117
118   found = false;
119   int magFilter = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 /* default */);
120   if( !found )
121   {
122     DALI_SCRIPT_EXCEPTION( isolate, "missing magnification filter from param 1" );
123   }
124   else
125   {
126     sampler.SetFilterMode(static_cast<FilterMode::Type>(minFilter), static_cast<FilterMode::Type>(magFilter));
127   }
128 }
129
130 /**
131  * Set the wrap modes for this sampler
132  * @method setWrapMode
133  * @for Sampler
134  * @param {integer} uWrap Wrap mode for u coordinates
135  * @param {integer} vWrap Wrap mode for v coordinates
136  * @example
137  *      // wrap mode is one of the following
138  *      dali.WRAP_MODE_DEFAULT
139  *      dali.WRAP_MODE_CLAMP_TO_EDGE
140  *      dali.WRAP_MODE_REPEAT
141  *      dali.WRAP_MODE_MIRRORED_REPEAT
142  *
143  *      sampler.setWrapMode( dali.WRAP_MODE_CLAMP_TO_EDGE, dali.WRAP_MODE_REPEAT );
144  */
145 void SamplerApi::SetWrapMode( const v8::FunctionCallbackInfo< v8::Value >& args )
146 {
147   v8::Isolate* isolate = args.GetIsolate();
148   v8::HandleScope handleScope( isolate );
149
150   Sampler sampler = GetSampler( isolate, args );
151
152   bool found( false );
153   int uWrap = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
154   if( !found )
155   {
156     DALI_SCRIPT_EXCEPTION( isolate, "missing wrap mode for u coordinates from param 0" );
157     return;
158   }
159
160   found = false;
161   int vWrap = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 /* default */);
162   if( !found )
163   {
164     DALI_SCRIPT_EXCEPTION( isolate, "missing wrap mode for v coordinates from param 1" );
165   }
166   else
167   {
168     sampler.SetWrapMode(static_cast<WrapMode::Type>(uWrap), static_cast<WrapMode::Type>(vWrap));
169   }
170 }
171
172 } // namespace V8Plugin
173
174 } // namespace Dali