From: Timo Lotterbach Date: Thu, 22 Nov 2012 12:18:11 +0000 (+0100) Subject: LayerManagerControl: added feature "import scene" and "export scene" X-Git-Tag: ivi-layer-management_version_0_9_9_rc X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6c8fd57532afab51382067e32ec76e1735b91c05;p=profile%2Fivi%2Flayer-management.git LayerManagerControl: added feature "import scene" and "export scene" Export scene can be used to write the a (rendered) scene to a file. Current implementation supports a custom format in .txt files, can be extended to xml, json or any other custom format. Import scene can be used to read and restore a scene from a file. Signed-off-by: Timo Lotterbach --- diff --git a/LayerManagerExamples/LayerManagerControl/CMakeLists.txt b/LayerManagerExamples/LayerManagerControl/CMakeLists.txt index c865758..16616ca 100644 --- a/LayerManagerExamples/LayerManagerControl/CMakeLists.txt +++ b/LayerManagerExamples/LayerManagerControl/CMakeLists.txt @@ -40,6 +40,7 @@ add_executable(${PROJECT_NAME} src/ExpressionInterpreter.cpp src/print.cpp src/scatter.cpp + src/sceneio.cpp src/util.cpp ) diff --git a/LayerManagerExamples/LayerManagerControl/include/LMControl.h b/LayerManagerExamples/LayerManagerControl/include/LMControl.h index 0a78642..dbf4cb1 100644 --- a/LayerManagerExamples/LayerManagerControl/include/LMControl.h +++ b/LayerManagerExamples/LayerManagerControl/include/LMControl.h @@ -312,4 +312,25 @@ t_scene_data getScatteredScene(t_scene_data* pInitialScene); void demo(int mode); +//============================================================================= +//sceneio.cpp +//============================================================================= + +/* + * Saves a representation of the current rendered scene to a file + */ +void exportSceneToFile(string filename); + +/* + * Saves an xtext representation of the grammar of the scene + */ +void exportXtext(string fileName, string grammar, string url); + +/* + * Imports a scene from a saved text file + */ +void importSceneFromFile(string filename); + + + #endif diff --git a/LayerManagerExamples/LayerManagerControl/include/SceneStore.h b/LayerManagerExamples/LayerManagerControl/include/SceneStore.h new file mode 100644 index 0000000..db9effd --- /dev/null +++ b/LayerManagerExamples/LayerManagerControl/include/SceneStore.h @@ -0,0 +1,602 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#ifndef __SCENE_STORE_H__ +#define __SCENE_STORE_H__ + + +//========================================================================= +// helper macros fileformat +//========================================================================= +#include + +#include +#include +#include +#include + +using std::string; +using std::stringstream; +using std::ostream; + +#include +using std::list; + +#include +using std::vector; + + + + +template string getPrimitiveType(T var) +{ + return ""; +} + +template string getPrimitiveType(T* var) +{ + T var2=0; + return getPrimitiveType(var2) + "*"; +} + +template<> string getPrimitiveType(bool var) +{ + return "bool"; +} + +template<> string getPrimitiveType(char var) +{ + return "char"; +} + +template<> string getPrimitiveType(signed char var) +{ + return "signed char"; +} + +template<> string getPrimitiveType(unsigned char var) +{ + return "unsigned char"; +} + +template<> string getPrimitiveType(wchar_t var) +{ + return "wchar_t"; +} + +template<> string getPrimitiveType(short int var) +{ + return "short int"; +} + +template<> string getPrimitiveType(unsigned short int var) +{ + return "unsigned short int"; +} + +template<> string getPrimitiveType(long int var) +{ + return "long int"; +} + +template<> string getPrimitiveType(unsigned long int var) +{ + return "unsigned long int"; +} + +template<> string getPrimitiveType(int var) +{ + return "int"; +} + +template<> string getPrimitiveType(unsigned int var) +{ + return "unsigned int"; +} + +template<> string getPrimitiveType(float var) +{ + return "float"; +} + +template<> string getPrimitiveType(double var) +{ + return "double"; +} + +template<> string getPrimitiveType(long double var) +{ + return "long double"; +} + +template<> string getPrimitiveType(string var) +{ + return "string"; +} + + +#if defined(__GXX_EXPERIMENTAL_CXX0X) || __cplusplus >= 201103L +template<> string getPrimitiveType(char16_t var) +{ + return "char16_t"; +} + +template<> string getPrimitiveType(char32_t var) +{ + return "char32_t"; +} + +template<> string getPrimitiveType(long long int var) +{ + return "long long int"; +} + +template<> string getPrimitiveType(unsigned long long int var) +{ + return "unsigned long long int"; +} +#endif + + +class WrapperHelper +{ +public: + const string mType; + WrapperHelper(string t) : + mType(t) + { + + } + + virtual ~WrapperHelper() + { + + } + + virtual void fromString(string s) + { + + } + + virtual string asString() + { + return ""; + } + + virtual void toStringMapTree(StringMapTree* parent) + { + + } + + virtual void toGrammarMapTree(StringMapTree* tree) + { + + } + + virtual WrapperHelper* tryClone(string type, StringMapTree* tree) + { + return NULL; + } + + virtual void addToComplexWrapper(WrapperHelper* wrapper) + { + + } + + virtual string getWrapperPrimitiveType() + { + return ""; + } +}; + +template +class ComplexWrapper: public WrapperHelper +{ +public: + list components; + + ComplexWrapper(string t) : + WrapperHelper(t) + { + + } + + virtual void toStringMapTree(StringMapTree* parent) + { + for(typename list::iterator it = components.begin(); it != components.end(); ++it) + { + StringMapTree* node = new StringMapTree; + (*it)->toStringMapTree(node); + parent->mChildren.push_back(node); + } + } +}; + +template +class BasicWrapper: public WrapperHelper +{ +public: + T value; + + BasicWrapper(string t) : + WrapperHelper(t) + { + + } + + virtual void fromString(string s) + { + stringstream ss; + ss.str(s); + ss >> std::skipws>> value; + } + + virtual string asString() + { + stringstream ss; + ss << value; + return ss.str(); + } + + virtual string getWrapperPrimitiveType() + { + return getPrimitiveType(value); + } + +}; + +template<> +class BasicWrapper: public WrapperHelper +{ +public: + string value; + + BasicWrapper(string t) : + WrapperHelper(t) + { + + } + + virtual void fromString(string s) + { + value = s; + } + + virtual string asString() + { + return value; + } + + virtual string getWrapperPrimitiveType() + { + return getPrimitiveType(value); + } +}; + +template<> +class BasicWrapper: public WrapperHelper +{ +public: + const char* value; + + BasicWrapper(char* t) : + WrapperHelper(t) + { + + } + + virtual void fromString(string s) + { + value = s.c_str(); + } + + virtual string asString() + { + return value; + } + + virtual string getWrapperPrimitiveType() + { + return getPrimitiveType(value); + } +}; + +template +class DummyWrapper: public WrapperHelper +{ +public: + T value; + + DummyWrapper(string t) : + WrapperHelper(t) + { + + } + + + virtual WrapperHelper* tryClone(string type, StringMapTree* tree) + { + if (type == mType) + { + DummyWrapper* newWrapper = new DummyWrapper(type); + newWrapper->value.fromStringMapTree(tree); + return newWrapper; + } + + return NULL; + } + + virtual void toGrammarMapTree(StringMapTree* tree) + { + value.toGrammarMapTree(tree); + } + + + virtual void addToComplexWrapper(WrapperHelper* wrapper) + { + ComplexWrapper* complexWrapper = static_cast* >(wrapper); + complexWrapper->components.push_back(&value); + } +}; + +map _globalTypeIndexdToType; + +#define OBJECT(class_name) \ +class class_name;\ +ostream& operator<<(ostream& out, class_name& obj )\ +{\ + return out;\ +}\ +istream& operator>>(istream& in, class_name& obj)\ +{\ + return in;\ +}\ +class class_name { \ +public:\ + map properties;\ + map typesMap;\ + map components;\ + map dummyComponentClones;\ +public:\ + const static int classNameIndex = __COUNTER__;\ + static int getClassNameIndex()\ + {\ + return classNameIndex;\ + }\ + \ + string mClassName;\ + template bool get(string key, T* p)\ + {\ + if(properties.find(key) != properties.end())\ + {\ + BasicWrapper* obj = (static_cast*> (properties[key]));\ + if(obj) *p = obj->value;\ + return obj != NULL;\ + }\ + return false;\ + }\ + template bool get(int* count, T*** p)\ + {\ + string type = _globalTypeIndexdToType[T::getClassNameIndex()];\ + if(components.find(type) != components.end())\ + {\ + ComplexWrapper* obj = static_cast* >(components[type]);\ + if(obj){\ + vector* temp = new vector(obj->components.begin(), obj->components.end());\ + *p = temp->data();\ + *count = obj->components.size();\ + }\ + return obj != NULL;\ + }\ + return false;\ + }\ + template bool get(list* p)\ + {\ + string type = _globalTypeIndexdToType[T::getClassNameIndex()];\ + if(components.find(type) != components.end())\ + {\ + ComplexWrapper* obj = static_cast* >(components[type]);\ + if(obj){\ + *p = obj->components;\ + }\ + return obj != NULL;\ + }\ + return false;\ + }\ + template bool set(string key, const T& p)\ + {\ + BasicWrapper* obj = (static_cast*> (properties[key]));\ + if(obj) obj->value = p;\ + return obj != NULL;\ + }\ + template bool set(string key, T* p)\ + {\ + BasicWrapper* obj = (static_cast*> (properties[key]));\ + if(obj) obj->value = p;\ + return obj != NULL;\ + }\ + string getType(string key)\ + {\ + return typesMap[key];\ + }\ + template bool add(T* obj)\ + {\ + if(components.find(obj->mClassName) != components.end())\ + {\ + (static_cast* > (components[obj->mClassName])->components).push_back(obj);\ + return true;\ + }\ + return false;\ + }\ + template bool unwrapAndAdd(DummyWrapper* obj)\ + {\ + if(components.find(obj->mClassName) != components.end())\ + {\ + (static_cast* > (components[obj->mClassName])->components).push_back(obj);\ + return true;\ + }\ + return false;\ + }\ + template bool remove(T* obj)\ + {\ + if(components.find(obj->mClassName) != components.end())\ + {\ + (static_cast* > (components[obj->mClassName])->components).remove(obj);\ + return true;\ + }\ + return false;\ + }\ + ~class_name()\ + {\ + for(map::iterator it = properties.begin(); it != properties.end();++it)\ + delete (*it).second;\ + for(map::iterator it = components.begin(); it != components.end();++it)\ + delete (*it).second;\ + }\ + class_name():mClassName(#class_name)\ + {\ + _globalTypeIndexdToType[getClassNameIndex()] = #class_name; + +#define PROPERTY(type, name) \ + properties[#name] = new BasicWrapper(#type);\ + typesMap[#name] = #type; + + +#define CONTAINS(type) \ + components[#type] = new ComplexWrapper(#type);\ + dummyComponentClones[#type]= (new DummyWrapper(#type)); + + +#define OBJECTEND \ + }\ + void toGrammarMapTree(StringMapTree* node)\ + {\ + node->mNodeLabel = mClassName;\ + for(map::iterator it = properties.begin(); it != properties.end();++it)\ + {\ + node->mNodeValues[(*it).first] = make_pair((*it).second->mType, it->second->getWrapperPrimitiveType());\ + }\ + for(map::iterator it = dummyComponentClones.begin(); it != dummyComponentClones.end();++it)\ + {\ + StringMapTree* child = new StringMapTree;\ + node->mChildren.push_back(child);\ + it->second->toGrammarMapTree(child);\ + }\ + }\ + void toStringMapTree(StringMapTree* node)\ + {\ + node->mNodeLabel = mClassName;\ + for(map::iterator it = properties.begin(); it != properties.end();++it)\ + {\ + node->mNodeValues[(*it).first] = make_pair((*it).second->mType, (*it).second->asString());\ + }\ + for(map::iterator it = components.begin(); it != components.end();++it)\ + {\ + it->second->toStringMapTree(node);\ + }\ + }\ + void fromStringMapTree(StringMapTree* node)\ + {\ + mClassName = node->mNodeLabel;\ + for(map >::iterator it = node->mNodeValues.begin(); it != node->mNodeValues.end();++it)\ + {\ + properties[it->first]->fromString(it->second.second);\ + }\ + for(list::iterator it = node->mChildren.begin(); it != node->mChildren.end(); ++it )\ + {\ + string type = (*it)->mNodeLabel;\ + WrapperHelper* wrapper = dummyComponentClones[type]->tryClone(type, (*it));\ + WrapperHelper* complexWrapper = components[type];\ + wrapper->addToComplexWrapper(complexWrapper);\ + }\ + }\ +}; + + + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//**************************************************************************************************************************// +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//**************************************************************************************************************************// +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +OBJECT(IlmSurface) + PROPERTY(t_ilm_surface, id) + PROPERTY(t_ilm_float, opacity) + PROPERTY(t_ilm_uint, sourceX) + PROPERTY(t_ilm_uint, sourceY) + PROPERTY(t_ilm_uint, sourceWidth) + PROPERTY(t_ilm_uint, sourceHeight) + PROPERTY(t_ilm_uint, origSourceWidth) + PROPERTY(t_ilm_uint, origSourceHeight) + PROPERTY(t_ilm_uint, destX) + PROPERTY(t_ilm_uint, destY) + PROPERTY(t_ilm_uint, destWidth) + PROPERTY(t_ilm_uint, destHeight) + PROPERTY(int, orientation) + PROPERTY(t_ilm_bool, visibility) + PROPERTY(t_ilm_uint, frameCounter) + PROPERTY(t_ilm_uint, drawCounter) + PROPERTY(t_ilm_uint, updateCounter) + PROPERTY(t_ilm_uint, pixelformat) + PROPERTY(t_ilm_uint, nativeSurface) + PROPERTY(ilmInputDevice, inputDevicesAcceptance) + /***/ +OBJECTEND + +OBJECT(IlmLayer) + PROPERTY(t_ilm_layer, id) + PROPERTY(t_ilm_float, opacity) + PROPERTY(t_ilm_uint, sourceX) + PROPERTY(t_ilm_uint, sourceY) + PROPERTY(t_ilm_uint, sourceWidth) + PROPERTY(t_ilm_uint, sourceHeight) + PROPERTY(t_ilm_uint, origSourceWidth) + PROPERTY(t_ilm_uint, origSourceHeight) + PROPERTY(t_ilm_uint, destX) + PROPERTY(t_ilm_uint, destY) + PROPERTY(t_ilm_uint, destWidth) + PROPERTY(t_ilm_uint, destHeight) + PROPERTY(int, orientation) + PROPERTY(t_ilm_bool, visibility) + PROPERTY(t_ilm_uint, type) + /***/ + CONTAINS(IlmSurface) +OBJECTEND + +OBJECT(IlmDisplay) + PROPERTY(t_ilm_display, id) + PROPERTY(t_ilm_uint, width) + PROPERTY(t_ilm_uint, height) + /***/ + CONTAINS(IlmLayer) +OBJECTEND + +OBJECT(IlmScene) + /***/ + CONTAINS(IlmSurface) + CONTAINS(IlmLayer) + CONTAINS(IlmDisplay) +OBJECTEND + +#endif diff --git a/LayerManagerExamples/LayerManagerControl/include/StringMapTree.h b/LayerManagerExamples/LayerManagerControl/include/StringMapTree.h new file mode 100644 index 0000000..7ca5599 --- /dev/null +++ b/LayerManagerExamples/LayerManagerControl/include/StringMapTree.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + + +#ifndef __STRING_MAP_TREE_H__ +#define __STRING_MAP_TREE_H__ + +#include +#include +#include + +using std::map; +using std::list; +using std::string; + +class StringMapTree +{ +public: + string mNodeLabel; + map > mNodeValues;//key: property name, value= pair(type, property value) + list mChildren;// pair(type, child component node) + + string toString(string prefix="") + { + string result; + result += prefix + mNodeLabel+ ":{\n"; + for(map >::iterator it = mNodeValues.begin(); it != mNodeValues.end() ; ++it) + { + result += prefix + "\t[" + it->first + ":"+ it->second.first + "]=[" + it->second.second + "]\n"; + } + for(list::iterator it = mChildren.begin(); it != mChildren.end(); ++it) + { + result += (*it)->toString(prefix + "\t") +"\n"; + } + result += prefix + "}"; + + return result; + } + + virtual ~StringMapTree() + { + for(list::iterator it = mChildren.begin(); it != mChildren.end(); ++it) + { + delete *it; + } + } +}; + + + +#endif diff --git a/LayerManagerExamples/LayerManagerControl/src/commands.cpp b/LayerManagerExamples/LayerManagerControl/src/commands.cpp index eaa81df..86a5ca9 100644 --- a/LayerManagerExamples/LayerManagerControl/src/commands.cpp +++ b/LayerManagerExamples/LayerManagerControl/src/commands.cpp @@ -564,3 +564,29 @@ COMMAND("demo ") int mode = (int) input->getInt("animation_mode"); demo(mode); } + +//============================================================================= +COMMAND("export scene to ") +//============================================================================= +{ + string filename = (string) input->getString("filename"); + exportSceneToFile(filename); +} + +//============================================================================= +COMMAND("export xtext to ") +//============================================================================= +{ + string filename = (string) input->getString("filename"); + string grammar = (string) input->getString("grammar"); + string url = (string) input->getString("url"); + exportXtext(filename, grammar, url); +} + +//============================================================================= +COMMAND("import scene from ") +//============================================================================= +{ + string filename = (string) input->getString("filename"); + importSceneFromFile(filename); +} diff --git a/LayerManagerExamples/LayerManagerControl/src/sceneio.cpp b/LayerManagerExamples/LayerManagerControl/src/sceneio.cpp new file mode 100644 index 0000000..3011a82 --- /dev/null +++ b/LayerManagerExamples/LayerManagerControl/src/sceneio.cpp @@ -0,0 +1,727 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either inputess or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#include "ilm_client.h" +#include "LMControl.h" +#include "Expression.h" +#include "ExpressionInterpreter.h" +#include "SceneStore.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +namespace { + +void captureSceneDataHelper(t_ilm_surface surfaceId, t_scene_data* pSceneData, IlmSurface* pSurface) +{ + ilmSurfaceProperties& props = pSceneData->surfaceProperties[surfaceId]; + + pSurface->set("id", surfaceId); + pSurface->set("destHeight", props.destHeight); + pSurface->set("destWidth", props.destWidth); + pSurface->set("destX", props.destX); + pSurface->set("destY", props.destY); + pSurface->set("drawCounter", props.drawCounter); + pSurface->set("frameCounter", props.frameCounter); + pSurface->set("inputDevicesAcceptance", props.inputDevicesAcceptance); + pSurface->set("nativeSurface", props.nativeSurface); + pSurface->set("opacity", props.opacity); + pSurface->set("orientation", props.orientation); + pSurface->set("origSourceHeight", props.origSourceHeight); + pSurface->set("origSourceWidth", props.origSourceWidth); + pSurface->set("pixelformat", props.pixelformat); + pSurface->set("sourceHeight", props.sourceHeight); + pSurface->set("sourceWidth", props.sourceWidth); + pSurface->set("sourceX", props.sourceX); + pSurface->set("sourceY", props.sourceY); + pSurface->set("updateCounter", props.updateCounter); + pSurface->set("visibility", props.visibility); +} + +void captureSceneDataHelper(t_ilm_layer layerId, t_scene_data* pSceneData, IlmLayer* pLayer) +{ + ilmLayerProperties& props = pSceneData->layerProperties[layerId]; + pLayer->set("id", layerId); + pLayer->set("destHeight", props.destHeight); + pLayer->set("destWidth", props.destWidth); + pLayer->set("destX", props.destX); + pLayer->set("destY", props.destY); + pLayer->set("opacity", props.opacity); + pLayer->set("orientation", props.orientation); + pLayer->set("origSourceHeight", props.origSourceHeight); + pLayer->set("origSourceWidth", props.origSourceWidth); + pLayer->set("sourceHeight", props.sourceHeight); + pLayer->set("sourceWidth", props.sourceWidth); + pLayer->set("sourceX", props.sourceX); + pLayer->set("sourceY", props.sourceY); + pLayer->set("type", props.type); + pLayer->set("visibility", props.visibility); + + if (pSceneData->layerSurfaces.find(layerId) != pSceneData->layerSurfaces.end()) + { + for (vector::iterator it = pSceneData->layerSurfaces[layerId].begin(); + it != pSceneData->layerSurfaces[layerId].end(); ++it) + { + IlmSurface* pIlmsurface = new IlmSurface; + captureSceneDataHelper(*it, pSceneData, pIlmsurface); + pLayer->add(pIlmsurface); + } + } +} + +void captureSceneData(IlmScene* scene) +{ + t_scene_data sceneStruct; + captureSceneData(&sceneStruct); + + for (vector::iterator it = sceneStruct.screens.begin(); + it != sceneStruct.screens.end(); ++it) + { + t_ilm_display displayId = *it; + IlmDisplay* pIlmdisplay = new IlmDisplay; + pIlmdisplay->set("id", displayId); + pIlmdisplay->set("width", sceneStruct.screenWidth); + pIlmdisplay->set("height", sceneStruct.screenHeight); + + if (sceneStruct.screenLayers.find(displayId) != sceneStruct.screenLayers.end()) + { + for (vector::iterator it = sceneStruct.screenLayers[displayId].begin(); + it != sceneStruct.screenLayers[displayId].end(); ++it) + { + IlmLayer* pIlmlayer = new IlmLayer; + captureSceneDataHelper(*it, &sceneStruct, pIlmlayer); + + pIlmdisplay->add(pIlmlayer); + } + } + + scene->add(pIlmdisplay); + } + + for (vector::iterator it = sceneStruct.layers.begin(); + it != sceneStruct.layers.end(); ++it) + { + if (sceneStruct.layerScreen.find(*it) == sceneStruct.layerScreen.end()) + { + IlmLayer* pIlmlayer = new IlmLayer; + captureSceneDataHelper(*it, &sceneStruct, pIlmlayer); + + scene->add(pIlmlayer); + } + } + + for (vector::iterator it = sceneStruct.surfaces.begin(); + it != sceneStruct.surfaces.end(); ++it) + { + if (sceneStruct.surfaceLayer.find(*it) == sceneStruct.surfaceLayer.end()) + { + IlmSurface* pIlmsurface = new IlmSurface; + captureSceneDataHelper(*it, &sceneStruct, pIlmsurface); + + scene->add(pIlmsurface); + } + } +} + +string encodeEscapesequences(string s) +{ + map code; + + code["\\"] = "\\[\\]"; + code["\n"] = "\\[n]"; + code["\t"] = "\\[t]"; + code["\v"] = "\\[v]"; + code["\b"] = "\\[b]"; + code["\f"] = "\\[f]"; + code["\r"] = "\\[r]"; + + return replaceAll(s, code); +} + +void exportSceneToTXTHelper(ostream& stream, StringMapTree* tree, string prefix = "") +{ + stream << prefix << encodeEscapesequences(tree->mNodeLabel) + ":{\n"; + for (map >::iterator it = tree->mNodeValues.begin(); + it != tree->mNodeValues.end(); ++it) + { + stream << prefix + "\t[" + encodeEscapesequences(it->first) + ":" + + encodeEscapesequences(it->second.first) + "]=[" + + encodeEscapesequences(it->second.second) + "]\n"; + } + + for (list::iterator it = tree->mChildren.begin(); it != tree->mChildren.end(); ++it) + { + exportSceneToTXTHelper(stream, *it, prefix + "\t"); + stream << "\n"; + } + + stream << prefix + "}"; +} + +void importSceneFromXMLHelper(istream& stream, StringMapTree* node) +{ + +} + + +string decodeEscapesequences(string s) +{ + map code; + code["\\[\\]"] = "\\"; + code["\\[n]"] = "\n"; + code["\\[t]"] = "\t"; + code["\\[v]"] = "\v"; + code["\\[b]"] = "\b"; + code["\\[f]"] = "\f"; + code["\\[r]"] = "\r"; + return replaceAll(s, code); +} + +void importSceneFromTXTHelper(istream& stream, StringMapTree* node) +{ + string in; + //Type + getline(stream, in); + int typeSize = in.find(":") - in.find_first_not_of('\t'); + int typeStart = in.find_first_not_of('\t'); + node->mNodeLabel = in.substr(typeStart, typeSize); + while (true) + { + long streamPosition = stream.tellg(); + getline(stream, in); + in = rtrim(in); + + //end of object + if (in.substr(0, 1) == "}") + return; + + //start of object property + if (in.substr(0, 1) == "[") + { + int startIndex = in.find('[') + 1; + int endIndex = in.find(":"); + string propertyName = in.substr(startIndex, endIndex - startIndex); + propertyName = decodeEscapesequences(propertyName); + + startIndex = endIndex + 1; + endIndex = in.find("]"); + string propertyType = in.substr(startIndex, endIndex - startIndex); + propertyType = decodeEscapesequences(propertyType); + + startIndex = in.find('[', endIndex) + 1; + endIndex = in.find_last_of(']', startIndex) - 1; + string propertyValue = in.substr(startIndex, endIndex - startIndex); + propertyValue = decodeEscapesequences(propertyValue); + + node->mNodeValues[propertyName] = make_pair(propertyType, propertyValue); + } + else + { + stream.seekg(streamPosition); + StringMapTree* child = new StringMapTree; + node->mChildren.push_back(child); + importSceneFromTXTHelper(stream, child); + } + } +} + +string makeValidXMLCharacters(string s) +{ + map code; + code["<"] = "<"; + code[">"] = ">"; + code["&"] = "&"; + code["\'"] = "'"; + code["\""] = """; + return replaceAll(s, code); +} + +void exportSceneToXMLHelper(ostream& stream, StringMapTree* tree, string prefix = "") +{ + stream << prefix << "<" << tree->mNodeLabel << ">\n"; + + for (map >::iterator it = tree->mNodeValues.begin(); + it != tree->mNodeValues.end(); ++it) + { + stream << prefix + "\tfirst << "\" type=\"" << it->second.first << "\">\n"; + stream << prefix << "\t\t" << makeValidXMLCharacters(it->second.second) + "\n"; + stream << prefix << "\t\n"; + } + + for (list::iterator it = tree->mChildren.begin(); + it != tree->mChildren.end(); ++it) + { + exportSceneToXMLHelper(stream, *it, prefix + "\t"); + stream << "\n"; + } + + stream << prefix << "mNodeLabel << ">\n"; +} + +ilmPixelFormat toPixelFormat(t_ilm_int format) +{ + switch (format) + { + case 0: + return ILM_PIXELFORMAT_R_8; + case 1: + return ILM_PIXELFORMAT_RGB_888; + case 2: + return ILM_PIXELFORMAT_RGBA_8888; + case 3: + return ILM_PIXELFORMAT_RGB_565; + case 4: + return ILM_PIXELFORMAT_RGBA_5551; + case 5: + return ILM_PIXELFORMAT_RGBA_6661; + case 6: + return ILM_PIXELFORMAT_RGBA_4444; + } + + return ILM_PIXEL_FORMAT_UNKNOWN; +} + +ilmSurfaceProperties getSurfaceProperties(IlmSurface* pIlmsurface) +{ + ilmSurfaceProperties props; + + pIlmsurface->get("destHeight", &(props.destHeight)); + pIlmsurface->get("destWidth", &(props.destWidth)); + pIlmsurface->get("destX", &(props.destX)); + pIlmsurface->get("destY", &(props.destY)); + pIlmsurface->get("drawCounter", &(props.drawCounter)); + pIlmsurface->get("frameCounter", &(props.frameCounter)); + pIlmsurface->get("inputDevicesAcceptance", &(props.inputDevicesAcceptance)); + pIlmsurface->get("nativeSurface", &(props.nativeSurface)); + pIlmsurface->get("opacity", &(props.opacity)); + pIlmsurface->get("orientation", &(props.orientation)); + pIlmsurface->get("origSourceHeight", &(props.origSourceHeight)); + pIlmsurface->get("origSourceWidth", &(props.origSourceWidth)); + pIlmsurface->get("pixelformat", &(props.pixelformat)); + pIlmsurface->get("sourceHeight", &(props.sourceHeight)); + pIlmsurface->get("sourceWidth", &(props.sourceWidth)); + pIlmsurface->get("sourceX", &(props.sourceX)); + pIlmsurface->get("sourceY", &(props.sourceY)); + pIlmsurface->get("updateCounter", &(props.updateCounter)); + pIlmsurface->get("visibility", &(props.visibility)); + + return props; +} + +ilmLayerProperties getLayerProperties(IlmLayer* pIlmlayer) +{ + ilmLayerProperties props; + pIlmlayer->get("destHeight", &(props.destHeight)); + pIlmlayer->get("destWidth", &(props.destWidth)); + pIlmlayer->get("destX", &(props.destX)); + pIlmlayer->get("destY", &(props.destY)); + pIlmlayer->get("opacity", &(props.opacity)); + pIlmlayer->get("orientation", &(props.orientation)); + pIlmlayer->get("origSourceHeight", &(props.origSourceHeight)); + pIlmlayer->get("origSourceHeight", &(props.origSourceHeight)); + pIlmlayer->get("origSourceWidth", &(props.origSourceWidth)); + pIlmlayer->get("sourceHeight", &(props.sourceHeight)); + pIlmlayer->get("sourceWidth", &(props.sourceWidth)); + pIlmlayer->get("sourceX", &(props.sourceX)); + pIlmlayer->get("sourceY", &(props.sourceY)); + pIlmlayer->get("type", &(props.type)); + pIlmlayer->get("visibility", &(props.visibility)); + + return props; +} + +void createSceneContentsHelper(IlmSurface* pIlmsurface) +{ + t_ilm_surface surfaceId; + pIlmsurface->get("id", &surfaceId); + ilmSurfaceProperties props = getSurfaceProperties(pIlmsurface); + + //if surface does not exist: create it + t_ilm_int surfaceCount; + t_ilm_surface* surfaceArray; + ilm_getSurfaceIDs(&surfaceCount, &surfaceArray); + + if (find(surfaceArray, surfaceArray + surfaceCount, surfaceId) + == surfaceArray + surfaceCount) + { + ilmPixelFormat pixelFormat; + pixelFormat = toPixelFormat(props.pixelformat); + ilm_surfaceCreate(props.nativeSurface, props.origSourceWidth, props.origSourceHeight, pixelFormat, &surfaceId); + } +} + +void createSceneContentsHelper(IlmLayer* pIlmlayer) +{ + t_ilm_layer layerId; + pIlmlayer->get("id", &layerId); + + ilmLayerProperties props = getLayerProperties(pIlmlayer); + + //create layer if does not exist + t_ilm_int layerCount; + t_ilm_layer* layerArray; + ilm_getLayerIDs(&layerCount, &layerArray); + + if (find(layerArray, layerArray + layerCount, layerId) == layerArray + layerCount) + { + ilm_layerCreateWithDimension(&layerId, props.origSourceWidth, props.origSourceHeight); + ilm_commitChanges(); + } + + list surfaceList; + pIlmlayer->get(&surfaceList); + vector renderOrder; + for (list::iterator it = surfaceList.begin(); + it != surfaceList.end(); ++it) + { + t_ilm_surface surfaceId; + (*it)->get("id", &surfaceId); + renderOrder.push_back(surfaceId); + + createSceneContentsHelper(*it); + } + + ilm_layerSetRenderOrder(layerId, renderOrder.data(), renderOrder.size()); + ilm_commitChanges(); +} + +void createSceneContentsHelper(IlmDisplay* pIlmdisplay) +{ + t_ilm_display displayId; + pIlmdisplay->get("id", &displayId); + + list layerList; + pIlmdisplay->get(&layerList); + vector renderOrder; + for (list::iterator it = layerList.begin(); it != layerList.end(); ++it) + { + t_ilm_layer layerId; + (*it)->get("id", &layerId); + renderOrder.push_back(layerId); + + createSceneContentsHelper(*it); + ilm_commitChanges(); + } + + ilm_displaySetRenderOrder(displayId, renderOrder.data(), renderOrder.size()); + ilm_commitChanges(); +} + +void createSceneContents(IlmScene* pIlmscene) +{ + list surfaceList; + pIlmscene->get(&surfaceList); + for (list::iterator it = surfaceList.begin(); it != surfaceList.end(); ++it) + { + createSceneContentsHelper(*it); + } + + list layerList; + pIlmscene->get(&layerList); + for (list::iterator it = layerList.begin(); + it != layerList.end(); ++it) + { + createSceneContentsHelper(*it); + } + + list displayList; + pIlmscene->get(&displayList); + for (list::iterator it = displayList.begin(); it != displayList.end(); ++it) + { + createSceneContentsHelper(*it); + } +} + +void restoreSceneHelper(IlmSurface* pIlmsurface) +{ + t_ilm_surface surfaceId; + pIlmsurface->get("id", &surfaceId); + ilmSurfaceProperties props = getSurfaceProperties(pIlmsurface); + + ilmPixelFormat pixelFormat; + pixelFormat = toPixelFormat(props.pixelformat); + ilm_surfaceSetNativeContent(props.nativeSurface, props.origSourceWidth, props.origSourceHeight, pixelFormat, surfaceId); + + ilm_commitChanges(); + + ilm_surfaceSetOpacity(surfaceId, props.opacity); + ilm_commitChanges(); + ilm_surfaceSetOrientation(surfaceId, props.orientation); + ilm_commitChanges(); + ilm_surfaceSetSourceRectangle(surfaceId, props.sourceX, props.sourceY, props.sourceWidth, props.sourceHeight); + ilm_commitChanges(); + ilm_surfaceSetDestinationRectangle(surfaceId, props.destX, props.destY, props.destWidth, props.destHeight); + ilm_commitChanges(); + ilm_surfaceSetVisibility(surfaceId, props.visibility); + ilm_commitChanges(); +} + +void restoreSceneHelper(IlmLayer* pIlmlayer) +{ + t_ilm_layer layerId; + pIlmlayer->get("id", &layerId); + + ilmLayerProperties props = getLayerProperties(pIlmlayer); + + //set layer properties + ilm_layerSetDestinationRectangle(layerId, props.destX, props.destY, props.destWidth, props.destHeight); + ilm_commitChanges(); + ilm_layerSetOpacity(layerId, props.opacity); + ilm_commitChanges(); + ilm_layerSetOrientation(layerId, props.orientation); + ilm_commitChanges(); + ilm_layerSetSourceRectangle(layerId, props.sourceX, props.sourceY, props.sourceWidth, props.sourceHeight); + ilm_commitChanges(); + ilm_layerSetVisibility(layerId, props.visibility); + ilm_commitChanges(); + + list surfaceList; + pIlmlayer->get(&surfaceList); + + //restore surfaces + for (list::iterator it = surfaceList.begin(); it != surfaceList.end(); ++it) + { + t_ilm_surface surfaceId; + (*it)->get("id", &surfaceId); + + restoreSceneHelper(*it); + } +} + +void restoreSceneHelper(IlmDisplay* pIlmdisplay) +{ + t_ilm_display displayId; + pIlmdisplay->get("id", &displayId); + + list layerList; + pIlmdisplay->get(&layerList); + for (list::iterator it = layerList.begin(); it != layerList.end(); ++it) + { + t_ilm_layer layerId; + (*it)->get("id", &layerId); + + restoreSceneHelper(*it); + ilm_commitChanges(); + } +} + +void restoreScene(IlmScene* pIlmscene) +{ + t_scene_data currentScene; + captureSceneData(¤tScene); + + //remove all surfaces from all layers + for (map::iterator it = currentScene.surfaceLayer.begin(); + it != currentScene.surfaceLayer.end(); ++it) + { + ilm_layerRemoveSurface(it->second, it->first); + } + + ilm_commitChanges(); + + //create scene contents + createSceneContents(pIlmscene); + + //restore scene + list surfaceList; + pIlmscene->get(&surfaceList); + for (list::iterator it = surfaceList.begin(); it != surfaceList.end(); ++it) + { + restoreSceneHelper(*it); + } + + list layerList; + pIlmscene->get(&layerList); + for (list::iterator it = layerList.begin(); it != layerList.end(); ++it) + { + restoreSceneHelper(*it); + } + + list displayList; + pIlmscene->get(&displayList); + for (list::iterator it = displayList.begin(); it != displayList.end(); ++it) + { + restoreSceneHelper(*it); + } +} + +} //end of anonymous namespace + + +void exportSceneToFile(string filename) +{ + IlmScene ilmscene; + IlmScene* pScene = &ilmscene; + captureSceneData(&ilmscene); + stringstream buffer; + StringMapTree sceneTree; + pScene->toStringMapTree(&sceneTree); + + //check extension + if (filename.find_last_of(".") != string::npos) + { + string extension = filename.substr(filename.find_last_of(".")); + cout << extension << endl; + + if (extension == ".xml") + { + buffer << "<\?xml version=\"1.0\"\?>\n"; + exportSceneToXMLHelper(buffer, &sceneTree); + cout << "DONE WRITING XML" << endl; + } + else if (extension == ".txt") + { + exportSceneToTXTHelper(buffer, &sceneTree); + cout << "DONE WRITING TXT" << endl; + } + } + else + { + //defult: + exportSceneToTXTHelper(buffer, &sceneTree); + cout << "DONE WRITING TXT" << endl; + } + + fstream stream(filename.c_str(), ios::out); + + cout<fromStringMapTree(&sceneTree); + cout << "Scene successfully created from tree" << endl; + + restoreScene(pScene); + cout << "Scene restored successfully" << endl; +} + +void exportXtext(string fileName, string grammar, string url) +{ + string name = grammar.substr(grammar.find_last_of('.') + 1); + //make sure first character is lower case + std::transform(name.begin(), ++(name.begin()), name.begin(), ::tolower); + + IlmScene scene; + StringMapTree grammarTree; + scene.toGrammarMapTree(&grammarTree); + + //writing to file + stringstream buffer; + buffer << "grammar " << grammar << " with org.eclipse.xtext.common.Terminals" << endl; + buffer << "generate " << name << " \"" << url << "\"" << endl; + + list doneTypes; + list waitingNodes; + waitingNodes.push_back(&grammarTree); + while (waitingNodes.size() != 0) + { + //pop first element of the waiting types + StringMapTree* typeNode = *(waitingNodes.begin()); + waitingNodes.pop_front(); + string type = typeNode->mNodeLabel; + + //if the type was not printed before + if (find(doneTypes.begin(), doneTypes.end(), type) == doneTypes.end()) + { + doneTypes.push_back(type); + + buffer << type << ":" << endl; + buffer << "\t\'" << type << ":\'" << endl; + + for (map >::iterator it = typeNode->mNodeValues.begin(); + it != typeNode->mNodeValues.end(); ++it) + { + buffer << "\t\t\'" << it->first << ":\' " << it->first << "=" << + (it->second.second.size() > 0 ? it->second.second : it->second.first) << endl; + } + + for (list::iterator it = typeNode->mChildren.begin(); + it != typeNode->mChildren.end(); ++it) + { + waitingNodes.push_back(*it); + string childName = (*it)->mNodeLabel; + //make lower case + std::transform(childName.begin(), childName.end(), childName.begin(), ::tolower); + childName += "s"; + + buffer << "\t\t" << childName << "+=" << (*it)->mNodeLabel << "*" << endl; + } + + buffer << ";" << endl; + } + } + + cout << "Xtext:[" << buffer.str() << "]" << endl; + + fstream fileout(fileName.c_str(), ios::out); + fileout << buffer.str(); + fileout.flush(); + fileout.close(); +}