Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / inc / FUiEffects_ParserEffectParser.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        FUiEffects_ParserEffectParser.h
19  * @brief       This file contains a declaration of EffectParser class
20  */
21
22 #ifndef _FUI_EFFECTS_INTERNAL_PARSER_EFFECTPARSER_H_
23 #define _FUI_EFFECTS_INTERNAL_PARSER_EFFECTPARSER_H_
24
25 #include <vector>
26 #include <string>
27 #include <map>
28 #include <utils/FUiEffects_Utils.h>
29 #include <FUiEffects_ParserXMLParser.h>
30 #include <FUiEffects_PeElementSurface.h>
31 #include <FUiEffects_RuntimeEffectModel.h>
32 #include <FUiEffects_RuntimeRenderDataScene.h>
33 #include <FUiEffects_RuntimeModelSurface.h>
34 #include <FUiEffects_RuntimeGraphicalSurfaceNurbs.h>
35 #include <FUiEffects_PePointSurface.h>
36 #include <FUiEffects_PeSpringSurface.h>
37 #include <FUiEffects_PeRodSurface.h>
38 #include <FUiEffects_PeRodSurfaceNURBS.h>
39 #include <FUiEffects_RuntimeIEffectModelListener.h>
40 #include <FBaseDataType.h>
41 #include <unique_ptr.h>
42
43 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer {
44 class EffectRenderer;
45 } } } }
46
47 /**
48  * @namespace   _Parser
49  * @brief               This namespace contains classes that provide methods for parsing an effect file
50  *                              and building the effect
51 */
52 namespace Tizen { namespace Ui { namespace Effects { namespace _Parser
53 {
54
55 /**
56  * @class       EffectParser
57  * @brief       A static method only class that performs unzipping of an effect file,
58  *                      parsing of xml-files of the model and may create or not the model itself
59  *
60  * @since 2.0
61  *
62  */
63 class EffectParser
64 {
65 public:
66         /**
67          * Handles an open-tag of an XML-element in a file being parsed by XMLParser
68          *
69          * @since 2.0
70          *
71          * @param               [in] elementName - the name of the XML-element
72          * @exception   E_OUT_OF_MEMORY         Insufficient memory
73          * @remarks             Can only be called from XMLParser and from friends
74          */
75         void StartElement(const char* pElementName);
76
77         /**
78          * Handles an close-tag of an XML-element in a file being parsed by XMLParser
79          *
80          * @since 2.0
81          *
82          * @param[in]   elementName      the name of the XML-element
83          * @exception   E_INVALID_STATE         The instance buffers occurred to be in an invalid state
84          * @exception   E_INVALID_ARG           An unexpected argument passed
85          * @remarks             Can only be called from XMLParser and from friends
86          */
87         void EndElement(const char* pElementName);
88
89         /**
90          * Handles presence of an attribute in an XML-element
91          *                              in a file being parsed by XMLParser
92          *
93          * @since 2.0
94          *
95          * @param[in]   attributeName - the name of the attribute of an XML-element
96          * @param[in]   attributeValue - the value of the attribute of an XML-element
97          * @exception   E_INVALID_STATE         The instance buffers occurred to be in an invalid state
98          * @remarks             Can only be called from XMLParser and from friends
99          */
100         void Attribute(const char* pAttributeName, const char* pAttributeValue);
101
102         /**
103          * Unzips and checks content of an effect file, performs XML-parsing
104          *                              of the corresponding files
105          *
106          * @since 2.0
107          *
108          * @return              A boolean result of both: effect file parsing and XML-parsing
109          * @param[in]   path     a full path to the input effect file (a zip-archive)
110          * @exception   OUT_OF_MEMORY           Insufficient memory
111          * @exception   E_INVALID_ARG           An invalid argument passed to the XmlParser instantiator
112          * @exception   E_ALREADY_BOUND         The passed parent doesn't match the current parent
113          *                                                                      of a created XmlParser singleton object
114          * @exception   E_OPERATION_FAILED      Extraction of model files from an effect file hasn't finished successfully
115          */
116         static bool ParseEffectFile(const char* pEffectFilePath, const char* pModelFilesDirectoryPath);
117
118         /**
119          * Performs unziping of an effect file, checks out its content,performs
120          *                              XML-parsing and constructs according to the effect file EffectInstance object
121          *
122          * @since 2.0
123          *
124          * @return              A pointer to the constructed _EffectInstance or null if parsing failed
125          *                              at some point
126          * @param[in]   path - a full path to the input effect file (a zip-archive)
127          * @param[in]   effectsManager  a pointer to the object that should call this method
128          * @param[in]   effectModelID    an ID of the effect to be constructed
129          * @exception   OUT_OF_MEMORY           Insufficient memory
130          * @exception   E_INVALID_ARG           An invalid argument passed to the XmlParser instantiator
131          * @exception   E_ALREADY_BOUND         The passed parent doesn't match the current parent
132          *                                                                      of a created XmlParser singleton object
133          * @exception   E_OPERATION_FAILED      Extraction of model files from an effect file hasn't finished successfully
134          * @exception   E_PARSING_FAILED        XML parsing hasn't been successful
135          * @exception   E_KEY_NOT_FOUND         Missing an attribute or an error model attributes
136          * @exception   E_INVALID_CONDITION     A member variable of effect parser was not initialized
137          * @exception   E_INVALID_FORMAT        An error in the logic of effect parser occurred
138          */
139         static result ParseEffectFileLoadModel(const char* pEffectFilePath, const char* pModelFilesDirectoryPath,
140                                                                                         Tizen::Ui::Effects::_Runtime::IEffectModelListener* pEffectManager, long effectModelID,
141                                                                                         Tizen::Ui::Effects::_Runtime::IEffectModelManager*& pEffectModel, Tizen::Ui::Effects::_Renderer::EffectRenderer*& pEffectRenderer, int& modelTimerTimeout);
142
143 private:
144         /**
145          * @brief               class constructor
146          * @remarks             accessible only from static methods and from friends
147          */
148         EffectParser(void);
149
150         /**
151          * @brief               class destructor
152          * @remarks             accessible only from static methods and from friends
153          */
154         ~EffectParser(void);
155
156         // Hidden copy constructor
157         EffectParser(const EffectParser& rhs);
158
159         // Hidden assignment operator
160         EffectParser& operator=(const EffectParser& rhs);
161
162         // Container of attibutes of an xml element
163         typedef std::map<std::string, std::string> TypeXmlElementAttributes;
164
165         // type of an xml element containing its name and attributes
166         struct XmlElement
167         {
168                 std::string     name;
169                 TypeXmlElementAttributes        attributes;
170         };
171
172         struct SceneData
173         {
174                 unsigned int timerTimeout;
175                 Tizen::Ui::Effects::_Utils::Vec3f ambientColor;
176                 float intensity;
177                 float attenuation;
178                 SceneData();
179         };
180
181         struct ViewportData
182         {
183                 float   x;
184                 float   y;
185                 float   z;
186                 float   width;
187                 float   height;
188                 float   near;
189                 float   far;
190                 float   angle;
191                 bool    perspective;
192                 ViewportData();
193         };
194
195         // Container of pointers to objects of ElementSurface
196         typedef std::vector<Tizen::Ui::Effects::_PhysicsEngine::ElementSurface*> TypePElementSurfaceCollection;
197
198         // Container of pointers to objects of UnitLight
199         typedef std::vector<Tizen::Ui::Effects::_Runtime::UnitLight*> TypePUnitLightCollection;
200
201         // Container of objects of XmlElement
202         typedef std::vector<XmlElement> TypeXmlElementCollection;
203
204         // Container that maps indices of PointSurfaceNurbs objects in the model file to indices of those in
205         // the appropriate container of this class
206         typedef std::map<std::pair<unsigned int,unsigned int>, unsigned int> TypeNurbsPointIndexDistributor;
207
208         // Container of pointers to objects of ModelSurface
209         typedef std::vector<Tizen::Ui::Effects::_Runtime::ModelSurface*> TypePModelSurfaceCollection;
210
211         bool ExtractModelFiles(const char* pPath);
212         void ClearModelFiles(void);
213         void ClearBuffers(void);
214
215         Tizen::Ui::Effects::_PhysicsEngine::PointSurface* FindPointSurfaceById(long pointId) const;
216         Tizen::Ui::Effects::_PhysicsEngine::PointSurfaceNurbs* FindPointSurfaceNurbsByIndices(unsigned int row, unsigned int col) const;
217
218         bool CreatePointLight(XmlElement&);
219         bool CreateSpotLight(XmlElement&);
220         bool CreateDirectionalLight(XmlElement&);
221         bool CreatePointSurface(XmlElement&);
222         bool CreateSpringSurface(XmlElement&);
223         bool CreateRodSurface(XmlElement&);
224         bool CreateRodSurfaceNurbs(XmlElement&);
225         bool CreateGraphicalSurface(XmlElement&);
226         bool CreatePointSurfaceNurbs(XmlElement&);
227         bool CreateGraphicalSurfaceKnot(XmlElement&);
228         bool PriorReadModelSurfaceData(void);
229         bool PriorReadGraphicalSurfaceData(void);
230         bool CreateModelSurface(XmlElement&);
231         bool CreateEffectModel(XmlElement&);
232         bool CreateGravityForce(XmlElement&);
233         bool ReadViewportData(XmlElement&);
234         bool PriorReadSceneData(void);
235         bool ReadManifestData(XmlElement&);
236         Tizen::Ui::Effects::_Utils::Vec3f StringHexToRgb(const std::string&);
237
238 private:
239
240         static EffectParser*  __pInstance;
241         std::unique_ptr<XmlParser> __pXmlParser;
242
243         std::string __currentModelFilesDirectoryPath;
244         static const std::string __MANIFEST_FILE_NAME;
245         static const std::string __SCENE_FILE_NAME;
246         static const std::string __SCRIPT_FILE_NAME;
247
248         TypeXmlElementCollection __elementBuffer;
249         TypeNurbsPointIndexDistributor __pointSurfaceNurbsIndices;
250         TypePElementSurfaceCollection __elementSurfaceBuffer;
251         TypePUnitLightCollection __unitLightBuffer;
252         std::unique_ptr<Tizen::Ui::Effects::_Runtime::GraphicalSurface> __pCurrentGraphicalSurface;
253         Tizen::Ui::Effects::_Utils::Vec3f __currentGravityForce;
254         float __currentGravityForceValue;
255         std::unique_ptr<Tizen::Ui::Effects::_Runtime::TypeKnots> __pCurrentVectorXBuffer;
256         std::unique_ptr<Tizen::Ui::Effects::_Runtime::TypeKnots> __pCurrentVectorYBuffer;
257         TypePModelSurfaceCollection __modelSurfaceBuffer;
258
259         int __lastSceneXMLElementIndex;
260         int __lastModelSurfaceXMLElementIndex;
261         int __lastGraphicalSurfaceXMLElementIndex;
262         std::unique_ptr<Tizen::Ui::Effects::_Runtime::EffectModel> __pCurrentEffectModel;
263         Tizen::Ui::Effects::_Runtime::IEffectModelListener* __pCurrentEffectManager;    //agrigation pointer
264
265         long __currentEffectId;
266         Tizen::Ui::Effects::EffectType __currentModelBehaviourType;
267         Tizen::Ui::Effects::_Runtime::TypeTimeEffect __currentEffectDuration;
268         std::string __currentEffectInstanceName;
269
270         SceneData __currentSceneData;
271         ViewportData __currentViewportData;
272
273         float __currentModelSurfaceX;
274         float __currentModelSurfaceY;
275         unsigned int __currentGraphicalSurfaceDimCtrlX;
276         unsigned int __currentGraphicalSurfaceDimCtrlY;
277
278         bool __insideGraphicalSurface;
279         bool __rightAfterSceneStart;
280         bool __rightAfterModelSurfaceStart;
281         bool __rightAfterGraphicalSurfaceStart;
282         bool __insideVectorX;
283         bool __insideVectorY;
284         bool __buildingModelFailed;
285         bool __insideElementsPool;
286 }; // EffectParser
287
288 } } } } // Tizen::Ui::Effects::_Parser
289
290 #endif // _FUI_EFFECTS_INTERNAL_PARSER_EFFECTPARSER_H_