Merge "Add more shared C++/JavaScript docs and add JavaScript wrapping guide" into...
[platform/core/uifw/dali-toolkit.git] / texture-atlas-exporter / dali-exporter / dali3d_exporter.cpp
1 // Created with TexturePacker (http://www.codeandweb.com/texturepacker)
2 // DALi Exporter: nick.holland@partner.samsung.com
3 //
4 // {{smartUpdateKey}}
5
6 // For your application cut and paste either:
7 //
8 // 1. Lookup table.
9 // 2. Constants.
10 // 3. JavaScript property map for using with DALi JS.
11
12 // Note: If you use one option, then delete code for the other two
13
14
15
16
17 //
18 // 1.  ------  lookup table method  ------
19 //
20 // Handy if you want to get image with a postfix, e.g. image_1, image_2, image_3
21 // Or if some of the image names contain special characters which are not allowed
22 // in constant definitions ( e.g. spaces and full stops).
23
24
25 const char* ATLAS_FILE_NAME( "{{texture.fullName}}" );  ///< Atlas image filename
26
27
28 /**
29  * Structure to hold image name and position within the atlas.
30  *
31  */
32 struct ImageInfo
33 {
34   const char* name;
35   unsigned int x,y,w,h;
36   Dali::BlendingMode::Type blendMode;  // only enable blending if image has alpha
37 };
38
39 /**
40  * lookup table
41  */
42 const ImageInfo ImageAtlas[]=
43 {
44 {% for sprite in allSprites %} { "{{sprite.trimmedName}}", {{sprite.frameRect.x}}, {{sprite.frameRect.y}}, {{sprite.frameRect.width}}, {{sprite.frameRect.height}}, {%if sprite.isSolid %}BlendingMode::OFF{% else%}BlendingMode::ON{% endif %} }{% if not forloop.last %},{% endif %}
45 {% endfor %}
46 };
47
48 const unsigned int ATLAS_IMAGE_COUNT = sizeof(ImageAtlas)/sizeof(ImageAtlas[0]);
49
50 // Example function on how to get an image info from the table
51 //
52 // std::string fileName = std::string( DALI_IMAGE_DIR ) + ATLAS_FILE_NAME;
53 // Image imageAtlas = Image::New( fileName );
54 //
55 //
56 //  const ImageInfo* info = GetImageInfo("left_icon");
57 //
58 //  if( info)
59 //  {
60 //     ImageActor myActor = ImageActor::New( imageAtlas, ImageActor::PixelArea( info->x, info->y, info->w, info->h) );
61 //     myActor->SetBlendMode( info->blendMode );
62 //
63 //
64
65 const ImageInfo* GetImageInfo(const char* name)
66 {
67   typedef std::map< const char*, const ImageInfo* > LookupMap;
68   static LookupMap lookup;
69   if( lookup.empty() )
70   {
71     for( unsigned int i = 0; i < ATLAS_IMAGE_COUNT; ++i)
72     {
73       lookup[ ImageAtlas[i].name ] =  &ImageAtlas[i];
74     }
75   }
76   LookupMap::const_iterator iter = lookup.find(name);
77   if( iter != lookup.end() )
78   {
79    return (*iter).second;
80   }
81   DALI_ASSERT_ALWAYS(0 && "image name not found in atlas");
82   return NULL;
83 }
84
85 //
86 //
87 // 2. ------ constants code ------
88 //
89 //
90
91 const char* ATLAS_FILE_NAME( "{{texture.fullName}}" );
92
93 /**
94  * Structure to hold position / blend mode within the atlas.
95  *
96  */
97 struct ImageInfo
98 {
99   ImageInfo(unsigned int x,unsigned int y,unsigned int w,unsigned int h,  Dali::BlendingMode::Type mode)
100   :pixelArea(x,y,w,h),blendMode(mode)
101   {}
102   ImageActor::PixelArea pixelArea;
103   Dali::BlendingMode::Type blendMode;  // only enable blending if image had alpha
104 };
105
106 {% for sprite in allSprites %}const ImageInfo {{ sprite.trimmedName|upper}}( {{sprite.frameRect.x}}, {{sprite.frameRect.y}}, {{sprite.frameRect.width}}, {{sprite.frameRect.height}} ,{%if sprite.isSolid %}BlendingMode::OFF{% else%}BlendingMode::ON{% endif %} );
107 {% endfor %}
108
109
110 // Example on using the Atlas, please delete this code.
111 void LoadAtlasImages()
112 {
113   std::string fileName = std::string(DALI_IMAGE_DIR) + ATLAS_FILE_NAME;
114   Image atlasImage = Image::New( fileName );
115   {% for sprite in allSprites %}ImageActor {{sprite.trimmedName|capfirst}} = ImageActor::New( atlasImage,  {{sprite.trimmedName|upper}}.pixelArea);
116   {{sprite.trimmedName|capfirst}}.SetBlendMode( {{sprite.trimmedName|upper}}.blendMode );
117
118   {% endfor %}
119 }
120
121 //
122 //
123 // 3.  ------  JavaScript key/value lookup table   ------
124 //
125 //
126 //
127
128 ATLAS_IMAGE_LIST : [
129 {% for sprite in allSprites %} { name: "{{sprite.trimmedName}}", x: {{sprite.frameRect.x}}, y:{{sprite.frameRect.y}}, w:{{sprite.frameRect.width}}, h:{{sprite.frameRect.height}}, blendMode:{%if sprite.isSolid %}dali.BLENDING_OFF{% else%}dali.BLENDING_ON{% endif %}  }{% if not forloop.last %},{% endif %}
130 {% endfor %}
131 ]