nux-1.16.0
Theme.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "NuxCore/TinyXML/tinyxml.h"
00025 #include "Theme.h"
00026 
00027 
00028 namespace nux
00029 {
00030 
00031 #define INT_TO_ENUM_ELEMENT(a) {a, #a} // --->>> {a, "a"}
00032 
00033   typedef struct
00034   {
00035     UXStyleImageRef value;
00036     const char *style;
00037   } UXStyle;
00038 
00039   UXStyle UXStyleArray [] =
00040   {
00041     INT_TO_ENUM_ELEMENT (eIMAGE_STYLE_NONE),
00042     INT_TO_ENUM_ELEMENT (eSTROKE_CORNER_SQUARE),
00043     INT_TO_ENUM_ELEMENT (eSTROKE_CORNER_ROUND1),
00044     INT_TO_ENUM_ELEMENT (eSTROKE_CORNER_ROUND2),
00045     INT_TO_ENUM_ELEMENT (eSTROKE_CORNER_ROUND4),
00046     INT_TO_ENUM_ELEMENT (eSTROKE_CORNER_ROUND10),
00047 
00048     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_ROUND1),
00049     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_ROUND2),
00050     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_ROUND4),
00051     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_ROUND10),
00052 
00053     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_ROUND10_SHADOW),
00054     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_ROUND4_SHADOW),
00055     INT_TO_ENUM_ELEMENT (eSHAPE_CORNER_SHADOW),
00056 
00057     INT_TO_ENUM_ELEMENT (eTRIANGLE_UP),
00058     INT_TO_ENUM_ELEMENT (eTRIANGLE_DOWN),
00059     INT_TO_ENUM_ELEMENT (eTRIANGLE_LEFT),
00060     INT_TO_ENUM_ELEMENT (eTRIANGLE_RIGHT),
00061 
00062     INT_TO_ENUM_ELEMENT (eWINDOW_SIZEGRIP),
00063 
00064     INT_TO_ENUM_ELEMENT (eSHAPE_CHECK_MARK),
00065     INT_TO_ENUM_ELEMENT (eSHAPE_CHECK_BOX),
00066 
00067     INT_TO_ENUM_ELEMENT (eCHECKBOX_NORMAL_ON),
00068     INT_TO_ENUM_ELEMENT (eCHECKBOX_NORMAL_OFF),
00069     INT_TO_ENUM_ELEMENT (eCHECKBOX_FOCUS_ON),
00070     INT_TO_ENUM_ELEMENT (eCHECKBOX_FOCUS_OFF),
00071     INT_TO_ENUM_ELEMENT (eCHECKBOX_PRELIGHT_ON),
00072     INT_TO_ENUM_ELEMENT (eCHECKBOX_PRELIGHT_OFF),
00073 
00074     INT_TO_ENUM_ELEMENT (eRADIO_NORMAL_ON),
00075     INT_TO_ENUM_ELEMENT (eRADIO_NORMAL_OFF),
00076     INT_TO_ENUM_ELEMENT (eRADIO_FOCUS_ON),
00077     INT_TO_ENUM_ELEMENT (eRADIO_FOCUS_OFF),
00078     INT_TO_ENUM_ELEMENT (eRADIO_PRELIGHT_ON),
00079     INT_TO_ENUM_ELEMENT (eRADIO_PRELIGHT_OFF),
00080 
00081     INT_TO_ENUM_ELEMENT (eBUTTON_NORMAL),
00082     INT_TO_ENUM_ELEMENT (eBUTTON_FOCUS),
00083     INT_TO_ENUM_ELEMENT (eBUTTON_PRELIGHT),
00084     INT_TO_ENUM_ELEMENT (eSPINNERUP),
00085     INT_TO_ENUM_ELEMENT (eSPINNERDOWN),
00086 
00087     INT_TO_ENUM_ELEMENT (eCOMBOBOX_OPEN_BUTTON),
00088     INT_TO_ENUM_ELEMENT (eTAB_LEFT),
00089     INT_TO_ENUM_ELEMENT (eTAB_RIGHT),
00090     INT_TO_ENUM_ELEMENT (eSPINER_UP),
00091     INT_TO_ENUM_ELEMENT (eSPINER_DOWN),
00092     INT_TO_ENUM_ELEMENT (eTREE_NODE_OPEN),
00093     INT_TO_ENUM_ELEMENT (eTREE_NODE_CLOSE),
00094 
00095     INT_TO_ENUM_ELEMENT (eSCROLLBAR_TRIANGLE_UP),
00096     INT_TO_ENUM_ELEMENT (eSCROLLBAR_TRIANGLE_DOWN),
00097     INT_TO_ENUM_ELEMENT (eSCROLLBAR_TRIANGLE_LEFT),
00098     INT_TO_ENUM_ELEMENT (eSCROLLBAR_TRIANGLE_RIGHT),
00099 
00100     INT_TO_ENUM_ELEMENT (eVALUATORVERTICALMOVE),
00101     INT_TO_ENUM_ELEMENT (eVALUATORHORIZONTALMOVE),
00102     INT_TO_ENUM_ELEMENT (eVALUATORMOVE),
00103 
00104     INT_TO_ENUM_ELEMENT (eVECTORXLABEL),
00105     INT_TO_ENUM_ELEMENT (eVECTORYLABEL),
00106     INT_TO_ENUM_ELEMENT (eVECTORZLABEL),
00107     INT_TO_ENUM_ELEMENT (eVECTORWLABEL),
00108 
00109 
00110     INT_TO_ENUM_ELEMENT (eHSCROLLBAR),
00111     INT_TO_ENUM_ELEMENT (eVSCROLLBAR),
00112 
00113     INT_TO_ENUM_ELEMENT (eMATRIX3PREVIEW),
00114     INT_TO_ENUM_ELEMENT (eMATRIX4PREVIEW),
00115     INT_TO_ENUM_ELEMENT (eDOT6x6),
00116 
00117     INT_TO_ENUM_ELEMENT (eGraphIcon),
00118     INT_TO_ENUM_ELEMENT (eGraphBarIcon),
00119 
00120     INT_TO_ENUM_ELEMENT (eWindowCloseButton),
00121 
00122     {eIMAGE_STYLE_NONE, 0}
00123   };
00124 
00125 
00126   /*static unsigned long axtoi(const char *s)
00127   {
00128       int n = 0;         // position in string
00129       int m = 0;         // position in digit[] to shift
00130       int count;         // loop index
00131       unsigned long intValue = 0;  // integer value of hex string
00132       int digit[16];      // hold values to convert
00133 
00134       const char *hexStg = s;
00135       if((s[0] == '0') && ((s[1] == 'X') || (s[1] == 'x')))
00136       {
00137           hexStg = s+2;
00138       }
00139 
00140       while (n < 16)
00141       {
00142           if (hexStg[n]=='\0')
00143               break;
00144           if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
00145               digit[n] = hexStg[n] & 0x0f;            //convert to int
00146           else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
00147               digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
00148           else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
00149               digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
00150           else break;
00151           n++;
00152       }
00153       count = n;
00154       m = n - 1;
00155       n = 0;
00156       while(n < count)
00157       {
00158           // digit[n] is value of hex digit at position n
00159           // (m << 2) is the number of positions to shift
00160           // OR the bits into return value
00161           intValue = intValue | (digit[n] << (m << 2));
00162           m--;   // adjust the position to set
00163           n++;   // next digit to process
00164       }
00165       return (intValue);
00166   }*/
00167 
00168   /*static unsigned int ReadXMLColorAttribute(TiXmlElement* element, const char* attribute_name)
00169   {
00170       unsigned int retvalue = 0;
00171       if(element)
00172       {
00173           retvalue = axtoi(element->Attribute(attribute_name));
00174       }
00175       return retvalue;
00176   }*/
00177 
00178   static UXStyleImageRef GetStyleImageRef (const TCHAR *style_name)
00179   {
00180     int i = 0;
00181 
00182     while (UXStyleArray[i].style != 0)
00183     {
00184       if (strcmp (UXStyleArray[i].style, style_name) == 0)
00185       {
00186         return UXStyleArray[i].value;
00187       }
00188 
00189       i++;
00190     }
00191 
00192     return eIMAGE_STYLE_NONE;
00193   }
00194 
00195   UXTheme::UXTheme()
00196   {
00197     LoadPainterImages();
00198   }
00199 
00200   UXTheme::~UXTheme()
00201   {
00202     std::list<PainterImage*>::iterator it;
00203     for (it = m_PainterImageList.begin(); it != m_PainterImageList.end(); it++)
00204     {
00205       (*it)->texture->UnReference ();
00206       delete (*it);
00207     }
00208     m_PainterImageList.clear ();
00209   }
00210 
00211   void UXTheme::LoadPainterImages()
00212   {
00213     NString file_search = TEXT ("Painter.xml");
00214     NString painter_filename = NUX_FIND_RESOURCE_LOCATION_NOFAIL (file_search.GetTCharPtr() );
00215 
00216     if (painter_filename == TEXT ("") )
00217     {
00218       nuxCriticalMsg (TEXT ("[GraphicsEngine::LoadPainterImages] Can't find Painter.xml file."));
00219       return;
00220     }
00221 
00222     TiXmlDocument doc (painter_filename.GetTCharPtr() );
00223     doc.LoadFile();
00224 
00225     TiXmlHandle docHandle ( &doc );
00226     TiXmlElement *data = docHandle.FirstChild (TCHARToUTF8 (TEXT ("PaintData"))).Element();
00227     TiXmlElement *image = 0;
00228 
00229     for (image = data->FirstChildElement (TCHARToUTF8 (TEXT ("Image"))); image; image = image->NextSiblingElement (TCHARToUTF8 (TEXT ("Image"))))
00230     {
00231       PainterImage *pimage = new PainterImage;
00232       Memset (pimage, 0, sizeof (PainterImage) );
00233 
00234       NString style = image->Attribute (TCHARToUTF8 (TEXT ("style")));
00235 
00236       pimage->style = GetStyleImageRef (style.GetTCharPtr());
00237 
00238       // If the attributes border_left, border_right, border_top, border_bottom are not present, assume they are equal to 0;
00239       pimage->border_left = pimage->border_right = pimage->border_top = pimage->border_bottom = 0;
00240 
00241       image->Attribute (TCHARToUTF8 (TEXT ("border_left") ), &pimage->border_left);
00242       image->Attribute (TCHARToUTF8 (TEXT ("border_right") ), &pimage->border_right);
00243       image->Attribute (TCHARToUTF8 (TEXT ("border_top") ),  &pimage->border_top);
00244       image->Attribute (TCHARToUTF8 (TEXT ("border_bottom") ), &pimage->border_bottom);
00245 
00246 
00247       const char *draw_borders_only = image->Attribute (TCHARToUTF8 (TEXT ("border_only")));
00248 
00249       if (draw_borders_only == 0)
00250       {
00251         pimage->draw_borders_only = true;
00252       }
00253       else
00254       {
00255         if (strcmp (TCHARToUTF8 (TEXT ("false")), draw_borders_only) == 0)
00256         {
00257           pimage->draw_borders_only = false;
00258         }
00259         else
00260         {
00261           pimage->draw_borders_only = true;
00262         }
00263       }
00264 
00265       if (1)
00266       {
00267         BaseTexture* device_texture;
00268 
00269         NString filename = image->Attribute (TCHARToUTF8 (TEXT ("Name")));
00270         NString texture_filename = NUX_FIND_RESOURCE_LOCATION_NOFAIL (filename.GetTCharPtr());
00271         device_texture = Load2DTextureFile (texture_filename.GetTCharPtr());
00272 
00273         pimage->texture = device_texture;
00274       }
00275       else
00276       {
00277         NString filename = image->Attribute (TCHARToUTF8 (TEXT ("Name")));
00278         NString texture_filename = NUX_FIND_RESOURCE_LOCATION_NOFAIL (filename.GetTCharPtr());
00279         pimage->texture = Load2DTextureFile (texture_filename.GetTCharPtr());
00280       }
00281 
00282       m_PainterImageList.push_back (pimage);
00283     }
00284   }
00285 
00286   const PainterImage *UXTheme::GetImage (UXStyleImageRef style)
00287   {
00288     std::list<PainterImage *>::iterator it;
00289 
00290     for (it = m_PainterImageList.begin(); it != m_PainterImageList.end(); it++)
00291     {
00292       if ( (*it)->style == style)
00293       {
00294         return (*it);
00295       }
00296     }
00297 
00298     return 0;
00299   }
00300 
00301   Rect UXTheme::GetImageGeometry (UXStyleImageRef style)
00302   {
00303     std::list<PainterImage *>::iterator it;
00304 
00305     for (it = m_PainterImageList.begin(); it != m_PainterImageList.end(); it++)
00306     {
00307       if ( (*it)->style == style)
00308       {
00309         t_u32 width = (*it)->texture->GetWidth();
00310         t_u32 height = (*it)->texture->GetHeight();
00311         return Rect (0, 0, width, height);
00312       }
00313     }
00314 
00315     nuxDebugMsg (TEXT ("[GraphicsEngine::GetImageGeometry] Cannot find UXStyleImageRef") );
00316     return Rect (0, 0, 0, 0);
00317   }
00318 
00319   BaseTexture *UXTheme::Load2DTextureFile (const char *filename)
00320   {
00321     BaseTexture* texture2D = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture ();
00322     NBitmapData *BitmapData = LoadImageFile (filename);
00323 
00324     if (BitmapData)
00325     {
00326       texture2D->Update (BitmapData);
00327       delete BitmapData;
00328     }
00329     return texture2D;
00330   }
00331 
00332   BaseTexture *UXTheme::Load2DTextureFileGenerateAlpha (const char *filename, int red, int green, int blue)
00333   {
00334     return 0;
00335   }
00336 
00337 }
00338 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends