Allow to extract string value from a property with type of property map in JavaScript
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / object / property-value-wrapper.h
1 #ifndef __DALI_V8PLUGIN_PROPERTYVALUE_WRAPPER_H__
2 #define __DALI_V8PLUGIN_PROPERTYVALUE_WRAPPER_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <v8.h>
23 #include <dali/public-api/object/property-value.h>
24
25 // INTERNAL INCLUDES
26 #include <shared/base-wrapped-object.h>
27
28
29 namespace Dali
30 {
31
32 namespace V8Plugin
33 {
34
35
36 /**
37  * An PropertyValue wrapper.
38  * Provides access to Property Value specific functionality and V8 memory handling.
39  *
40  * Only wraps complex types.
41  * Native JavaScript Property Value types such as float, bool string etc are handled by the actor wrapper.
42  * This supports sub property access for maps and vector.x etc and math functionality not easily mapped
43  * to JavaScript objects.
44  *
45  */
46 class PropertyValueWrapper : public BaseWrappedObject
47 {
48 public:
49
50   /**
51    * @brief Constructor
52    * @param[in] value property value to wrap
53    * @param[in] gc Dali garbage collector interface
54    */
55   explicit PropertyValueWrapper( const Dali::Property::Value& value, GarbageCollectorInterface& gc );
56
57   /**
58    * @brief Destructor
59    */
60   virtual ~PropertyValueWrapper();
61
62   /**
63    * @brief Creates a new Rotation wrapped inside a Javascript Object.
64    * @param[in] args v8 function call arguments interpreted as (x=0,y=0)
65    */
66   static void NewRotation( const v8::FunctionCallbackInfo< v8::Value >& args);
67
68   /**
69    * @brief Creates a new Matrix wrapped inside a Javascript Object.
70    * @param[in] args v8 function call arguments can be a 4 x 4 matrix or a 3 x 3 matrix
71    */
72   static void NewMatrix( const v8::FunctionCallbackInfo< v8::Value >& args);
73
74   /**
75    * @brief Wraps a dali property value inside a JavaScript object
76    * @return JavaScript object
77    */
78   static v8::Handle<v8::Object> WrapDaliProperty(v8::Isolate* isolate, const Dali::Property::Value &value);
79
80   /**
81    * @brief Extract a property value wrapper from a javascript object
82    * @return property value wrapper
83    */
84   static PropertyValueWrapper*  Unwrap( v8::Isolate* isolate, v8::Handle< v8::Object> obj);
85
86   /**
87    * Extract a property value from a javascript object, of a given type
88    * @return property value
89    */
90   static Dali::Property::Value ExtractPropertyValue( v8::Isolate* isolate, v8::Local< v8::Value> v8Value, Dali::Property::Type type);
91
92   /**
93    * @brief Extract a vector or a matrix from a JavaScript array
94    * @return property value ( vector or matrix)
95    */
96   static Dali::Property::Value VectorOrMatrixFromV8Array( v8::Isolate* isolate, const v8::Local<v8::Value>& v8Value);
97
98   /**
99    * @brief Extract a Dali Property::Array from a JavaScript array
100    * @return array property value ( vector or matrix)
101    */
102   static Dali::Property::Value ArrayFromV8Array( v8::Isolate* isolate, const v8::Local<v8::Value>& v8Value);
103
104   /**
105    * @return property value
106    */
107    Dali::Property::Value GetValue()
108    {
109      return mValue;
110    }
111
112 private:
113
114
115    /**
116     * @brief get the value for a property for JavaScript object than contains a Dali object.
117     * E.g. Get( "x", JavaScript object that wraps a Dali Vector2 )
118     * @param[in] propertyName property name
119     * @param[in] info reference to PropertyCallbackInfo structure (contains the Javascript
120     * object and the return value).
121     */
122    static void PropertyGet( v8::Local<v8::String> propertyName,
123                              const v8::PropertyCallbackInfo<v8::Value>& info);
124
125
126    /**
127     * @brief Set the value for a property for JavaScript object than contains a Dali object.
128     * E.g. Set( "x", 103, JavaScript object that wraps a Dali Vector2 )
129     * @param[in] propertyName property name
130     * @param[in] javaScriptValue javascript value to set, this is typically a number
131     * @param[in] info reference to PropertyCallbackInfo structure (contains the Javascript
132     * object).
133     */
134    static void PropertySet( v8::Local<v8::String> propertyName,
135                      v8::Local<v8::Value> javaScriptValue,
136                      const v8::PropertyCallbackInfo<v8::Value>& info);
137
138
139
140
141   // The Property Value ObjectTemplates
142   static v8::Persistent<v8::ObjectTemplate> mTemplatePrimitive;
143
144   /**
145    * @brief used to make Vectors, rotation and rect template
146    * Just produces an object template with 2 internal fields
147    * A type and a pointer to the dali object
148    */
149   static v8::Handle<v8::ObjectTemplate> MakeDaliPropertyTemplate( v8::Isolate* isolate );
150
151 private:
152
153   Dali::Property::Value mValue;     ///< property value
154 };
155
156 } // V8Plugin
157
158 } // Dali
159
160 #endif // header