Theme.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. #include "Base.h"
  2. #include "Theme.h"
  3. #include "ThemeStyle.h"
  4. namespace gameplay
  5. {
  6. static std::vector<Theme*> __themeCache;
  7. Theme::Theme()
  8. {
  9. }
  10. Theme::~Theme()
  11. {
  12. // Destroy all the cursors, styles and , fonts.
  13. for (size_t i = 0, count = _styles.size(); i < count; ++i)
  14. {
  15. Style* style = _styles[i];
  16. SAFE_DELETE(style);
  17. }
  18. for (size_t i = 0, count = _images.size(); i < count; ++i)
  19. {
  20. ThemeImage* image = _images[i];
  21. SAFE_RELEASE(image);
  22. }
  23. for (size_t i = 0, count = _imageLists.size(); i < count; ++i)
  24. {
  25. ImageList* imageList = _imageLists[i];
  26. SAFE_RELEASE(imageList);
  27. }
  28. for (size_t i = 0, count = _skins.size(); i < count; ++i)
  29. {
  30. Skin* skin = _skins[i];
  31. SAFE_RELEASE(skin);
  32. }
  33. SAFE_DELETE(_spriteBatch);
  34. SAFE_RELEASE(_texture);
  35. // Remove ourself from the theme cache.
  36. std::vector<Theme*>::iterator itr = std::find(__themeCache.begin(), __themeCache.end(), this);
  37. if (itr != __themeCache.end())
  38. {
  39. __themeCache.erase(itr);
  40. }
  41. }
  42. Theme* Theme::create(const char* url)
  43. {
  44. GP_ASSERT(url);
  45. // Search theme cache first.
  46. for (size_t i = 0, count = __themeCache.size(); i < count; ++i)
  47. {
  48. Theme* t = __themeCache[i];
  49. if (t->_url == url)
  50. {
  51. // Found a match.
  52. t->addRef();
  53. return t;
  54. }
  55. }
  56. // Load theme properties from file path.
  57. Properties* properties = Properties::create(url);
  58. GP_ASSERT(properties);
  59. if (properties == NULL)
  60. {
  61. return NULL;
  62. }
  63. // Check if the Properties is valid and has a valid namespace.
  64. Properties* themeProperties = (strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace();
  65. GP_ASSERT(themeProperties);
  66. if (!themeProperties || !(strcmp(themeProperties->getNamespace(), "theme") == 0))
  67. {
  68. SAFE_DELETE(properties);
  69. return NULL;
  70. }
  71. // Create a new theme.
  72. Theme* theme = new Theme();
  73. theme->_url = url;
  74. // Parse the Properties object and set up the theme.
  75. std::string textureFile;
  76. themeProperties->getPath("texture", &textureFile);
  77. theme->_texture = Texture::create(textureFile.c_str(), false);
  78. GP_ASSERT(theme->_texture);
  79. theme->_spriteBatch = SpriteBatch::create(theme->_texture);
  80. GP_ASSERT(theme->_spriteBatch);
  81. theme->_spriteBatch->getSampler()->setFilterMode(Texture::LINEAR, Texture::LINEAR);
  82. theme->_spriteBatch->getSampler()->setWrapMode(Texture::CLAMP, Texture::CLAMP);
  83. float tw = 1.0f / theme->_texture->getWidth();
  84. float th = 1.0f / theme->_texture->getHeight();
  85. Properties* space = themeProperties->getNextNamespace();
  86. while (space != NULL)
  87. {
  88. // First load all cursors, checkboxes etc. that can be referred to by styles.
  89. const char* spacename = space->getNamespace();
  90. if (strcmp(spacename, "image") == 0)
  91. {
  92. theme->_images.push_back(ThemeImage::create(tw, th, space, Vector4::one()));
  93. }
  94. else if (strcmp(spacename, "imageList") == 0)
  95. {
  96. theme->_imageLists.push_back(ImageList::create(tw, th, space));
  97. }
  98. else if (strcmp(spacename, "skin") == 0)
  99. {
  100. Theme::Border border;
  101. Properties* innerSpace = space->getNextNamespace();
  102. if (innerSpace)
  103. {
  104. const char* innerSpacename = innerSpace->getNamespace();
  105. if (strcmp(innerSpacename, "border") == 0)
  106. {
  107. border.top = innerSpace->getFloat("top");
  108. border.bottom = innerSpace->getFloat("bottom");
  109. border.left = innerSpace->getFloat("left");
  110. border.right = innerSpace->getFloat("right");
  111. }
  112. }
  113. Vector4 regionVector;
  114. space->getVector4("region", &regionVector);
  115. const Rectangle region(regionVector.x, regionVector.y, regionVector.z, regionVector.w);
  116. Vector4 color(1, 1, 1, 1);
  117. if (space->exists("color"))
  118. {
  119. space->getColor("color", &color);
  120. }
  121. Skin* skin = Skin::create(space->getId(), tw, th, region, border, color);
  122. GP_ASSERT(skin);
  123. theme->_skins.push_back(skin);
  124. }
  125. space = themeProperties->getNextNamespace();
  126. }
  127. themeProperties->rewind();
  128. space = themeProperties->getNextNamespace();
  129. while (space != NULL)
  130. {
  131. const char* spacename = space->getNamespace();
  132. if (strcmp(spacename, "style") == 0)
  133. {
  134. // Each style contains up to MAX_OVERLAYS overlays,
  135. // as well as Border and Padding namespaces.
  136. Theme::Margin margin;
  137. Theme::Padding padding;
  138. Theme::Style::Overlay* normal = NULL;
  139. Theme::Style::Overlay* focus = NULL;
  140. Theme::Style::Overlay* active = NULL;
  141. Theme::Style::Overlay* disabled = NULL;
  142. Theme::Style::Overlay* hover = NULL;
  143. // Need to load OVERLAY_NORMAL first so that the other overlays can inherit from it.
  144. Properties* innerSpace = space->getNextNamespace();
  145. while (innerSpace != NULL)
  146. {
  147. const char* innerSpacename = innerSpace->getNamespace();
  148. if (strcmp(innerSpacename, "stateNormal") == 0)
  149. {
  150. Vector4 textColor(0, 0, 0, 1);
  151. if (innerSpace->exists("textColor"))
  152. {
  153. innerSpace->getColor("textColor", &textColor);
  154. }
  155. Font* font = NULL;
  156. std::string fontPath;
  157. if (innerSpace->getPath("font", &fontPath))
  158. {
  159. font = Font::create(fontPath.c_str());
  160. }
  161. unsigned int fontSize = innerSpace->getInt("fontSize");
  162. const char* textAlignmentString = innerSpace->getString("textAlignment");
  163. Font::Justify textAlignment = Font::ALIGN_TOP_LEFT;
  164. if (textAlignmentString)
  165. {
  166. textAlignment = Font::getJustify(textAlignmentString);
  167. }
  168. bool rightToLeft = innerSpace->getBool("rightToLeft");
  169. float opacity = 1.0f;
  170. if (innerSpace->exists("opacity"))
  171. {
  172. opacity = innerSpace->getFloat("opacity");
  173. }
  174. ImageList* imageList = NULL;
  175. ThemeImage* cursor = NULL;
  176. Skin* skin = NULL;
  177. theme->lookUpSprites(innerSpace, &imageList, &cursor, &skin);
  178. normal = Theme::Style::Overlay::create();
  179. GP_ASSERT(normal);
  180. normal->setSkin(skin);
  181. normal->setCursor(cursor);
  182. normal->setImageList(imageList);
  183. normal->setTextColor(textColor);
  184. normal->setFont(font);
  185. normal->setFontSize(fontSize);
  186. normal->setTextAlignment(textAlignment);
  187. normal->setTextRightToLeft(rightToLeft);
  188. normal->setOpacity(opacity);
  189. if (font)
  190. {
  191. theme->_fonts.insert(font);
  192. font->release();
  193. }
  194. // Done with this pass.
  195. break;
  196. }
  197. innerSpace = space->getNextNamespace();
  198. }
  199. // At least the OVERLAY_NORMAL is required.
  200. if (!normal)
  201. GP_ERROR("All themes require the normal state overlay to be defined.");
  202. space->rewind();
  203. innerSpace = space->getNextNamespace();
  204. while (innerSpace != NULL)
  205. {
  206. const char* innerSpacename = innerSpace->getNamespace();
  207. if (strcmp(innerSpacename, "margin") == 0)
  208. {
  209. margin.top = innerSpace->getFloat("top");
  210. margin.bottom = innerSpace->getFloat("bottom");
  211. margin.left = innerSpace->getFloat("left");
  212. margin.right = innerSpace->getFloat("right");
  213. }
  214. else if (strcmp(innerSpacename, "padding") == 0)
  215. {
  216. padding.top = innerSpace->getFloat("top");
  217. padding.bottom = innerSpace->getFloat("bottom");
  218. padding.left = innerSpace->getFloat("left");
  219. padding.right = innerSpace->getFloat("right");
  220. }
  221. else if (strcmp(innerSpacename, "stateNormal") != 0)
  222. {
  223. // Either OVERLAY_FOCUS or OVERLAY_ACTIVE.
  224. // If a property isn't specified, it inherits from OVERLAY_NORMAL.
  225. Vector4 textColor;
  226. if (!innerSpace->getColor("textColor", &textColor))
  227. {
  228. textColor.set(normal->getTextColor());
  229. }
  230. Font* font = NULL;
  231. std::string fontPath;
  232. if (innerSpace->getPath("font", &fontPath))
  233. {
  234. font = Font::create(fontPath.c_str());
  235. }
  236. if (!font)
  237. {
  238. font = normal->getFont();
  239. if (font)
  240. font->addRef();
  241. }
  242. unsigned int fontSize;
  243. if (innerSpace->exists("fontSize"))
  244. {
  245. fontSize = innerSpace->getInt("fontSize");
  246. }
  247. else
  248. {
  249. fontSize = normal->getFontSize();
  250. }
  251. const char* textAlignmentString = innerSpace->getString("textAlignment");
  252. Font::Justify textAlignment;
  253. if (textAlignmentString)
  254. {
  255. textAlignment = Font::getJustify(textAlignmentString);
  256. }
  257. else
  258. {
  259. textAlignment = normal->getTextAlignment();
  260. }
  261. bool rightToLeft;
  262. if (innerSpace->exists("rightToLeft"))
  263. {
  264. rightToLeft = innerSpace->getBool("rightToLeft");
  265. }
  266. else
  267. {
  268. rightToLeft = normal->getTextRightToLeft();
  269. }
  270. float opacity;
  271. if (innerSpace->exists("opacity"))
  272. {
  273. opacity = innerSpace->getFloat("opacity");
  274. }
  275. else
  276. {
  277. opacity = normal->getOpacity();
  278. }
  279. ImageList* imageList = NULL;
  280. ThemeImage* cursor = NULL;
  281. Skin* skin = NULL;
  282. theme->lookUpSprites(innerSpace, &imageList, &cursor, &skin);
  283. if (!imageList)
  284. {
  285. imageList = normal->getImageList();
  286. }
  287. if (!cursor)
  288. {
  289. cursor = normal->getCursor();
  290. }
  291. if (!skin)
  292. {
  293. skin = normal->getSkin();
  294. }
  295. if (strcmp(innerSpacename, "stateFocus") == 0)
  296. {
  297. focus = Theme::Style::Overlay::create();
  298. GP_ASSERT(focus);
  299. focus->setSkin(skin);
  300. focus->setCursor(cursor);
  301. focus->setImageList(imageList);
  302. focus->setTextColor(textColor);
  303. focus->setFont(font);
  304. focus->setFontSize(fontSize);
  305. focus->setTextAlignment(textAlignment);
  306. focus->setTextRightToLeft(rightToLeft);
  307. focus->setOpacity(opacity);
  308. if (font)
  309. {
  310. theme->_fonts.insert(font);
  311. font->release();
  312. }
  313. }
  314. else if (strcmp(innerSpacename, "stateActive") == 0)
  315. {
  316. active = Theme::Style::Overlay::create();
  317. GP_ASSERT(active);
  318. active->setSkin(skin);
  319. active->setCursor(cursor);
  320. active->setImageList(imageList);
  321. active->setTextColor(textColor);
  322. active->setFont(font);
  323. active->setFontSize(fontSize);
  324. active->setTextAlignment(textAlignment);
  325. active->setTextRightToLeft(rightToLeft);
  326. active->setOpacity(opacity);
  327. if (font)
  328. {
  329. theme->_fonts.insert(font);
  330. font->release();
  331. }
  332. }
  333. else if (strcmp(innerSpacename, "stateDisabled") == 0)
  334. {
  335. disabled = Theme::Style::Overlay::create();
  336. GP_ASSERT(disabled);
  337. disabled->setSkin(skin);
  338. disabled->setCursor(cursor);
  339. disabled->setImageList(imageList);
  340. disabled->setTextColor(textColor);
  341. disabled->setFont(font);
  342. disabled->setFontSize(fontSize);
  343. disabled->setTextAlignment(textAlignment);
  344. disabled->setTextRightToLeft(rightToLeft);
  345. disabled->setOpacity(opacity);
  346. if (font)
  347. {
  348. theme->_fonts.insert(font);
  349. font->release();
  350. }
  351. }
  352. else if (strcmp(innerSpacename, "stateHover") == 0)
  353. {
  354. hover = Theme::Style::Overlay::create();
  355. GP_ASSERT(hover);
  356. hover->setSkin(skin);
  357. hover->setCursor(cursor);
  358. hover->setImageList(imageList);
  359. hover->setTextColor(textColor);
  360. hover->setFont(font);
  361. hover->setFontSize(fontSize);
  362. hover->setTextAlignment(textAlignment);
  363. hover->setTextRightToLeft(rightToLeft);
  364. hover->setOpacity(opacity);
  365. if (font)
  366. {
  367. theme->_fonts.insert(font);
  368. font->release();
  369. }
  370. }
  371. }
  372. innerSpace = space->getNextNamespace();
  373. }
  374. if (!focus)
  375. {
  376. focus = normal;
  377. focus->addRef();
  378. }
  379. if (!disabled)
  380. {
  381. disabled = normal;
  382. disabled->addRef();
  383. }
  384. // Note: The hover and active states have their overlay left NULL if unspecified.
  385. // Events will still be triggered, but a control's overlay will not be changed.
  386. Theme::Style* s = new Theme::Style(theme, space->getId(), tw, th, margin, padding, normal, focus, active, disabled, hover);
  387. GP_ASSERT(s);
  388. theme->_styles.push_back(s);
  389. }
  390. space = themeProperties->getNextNamespace();
  391. }
  392. // Add this theme to the cache.
  393. __themeCache.push_back(theme);
  394. SAFE_DELETE(properties);
  395. return theme;
  396. }
  397. Theme::Style* Theme::getStyle(const char* name) const
  398. {
  399. GP_ASSERT(name);
  400. for (size_t i = 0, count = _styles.size(); i < count; ++i)
  401. {
  402. GP_ASSERT(_styles[i]);
  403. if (strcmp(name, _styles[i]->getId()) == 0)
  404. {
  405. return _styles[i];
  406. }
  407. }
  408. return NULL;
  409. }
  410. Theme::Style* Theme::getEmptyStyle()
  411. {
  412. Theme::Style* emptyStyle = getStyle("EMPTY_STYLE");
  413. if (!emptyStyle)
  414. {
  415. Theme::Style::Overlay* overlay = Theme::Style::Overlay::create();
  416. overlay->addRef();
  417. overlay->addRef();
  418. emptyStyle = new Theme::Style(const_cast<Theme*>(this), "EMPTY_STYLE", 1.0f / _texture->getWidth(), 1.0f / _texture->getHeight(),
  419. Theme::Margin::empty(), Theme::Border::empty(), overlay, overlay, NULL, overlay, NULL);
  420. _styles.push_back(emptyStyle);
  421. }
  422. return emptyStyle;
  423. }
  424. void Theme::setProjectionMatrix(const Matrix& matrix)
  425. {
  426. GP_ASSERT(_spriteBatch);
  427. _spriteBatch->setProjectionMatrix(matrix);
  428. }
  429. SpriteBatch* Theme::getSpriteBatch() const
  430. {
  431. return _spriteBatch;
  432. }
  433. /**************
  434. * Theme::UVs *
  435. **************/
  436. Theme::UVs::UVs()
  437. : u1(0), v1(0), u2(0), v2(0)
  438. {
  439. }
  440. Theme::UVs::UVs(float u1, float v1, float u2, float v2)
  441. : u1(u1), v1(v1), u2(u2), v2(v2)
  442. {
  443. }
  444. const Theme::UVs& Theme::UVs::empty()
  445. {
  446. static UVs empty(0, 0, 0, 0);
  447. return empty;
  448. }
  449. const Theme::UVs& Theme::UVs::full()
  450. {
  451. static UVs full(0, 1, 1, 0);
  452. return full;
  453. }
  454. /**********************
  455. * Theme::SideRegions *
  456. **********************/
  457. const Theme::SideRegions& Theme::SideRegions::empty()
  458. {
  459. static SideRegions empty;
  460. return empty;
  461. }
  462. /*********************
  463. * Theme::ThemeImage *
  464. *********************/
  465. Theme::ThemeImage::ThemeImage(float tw, float th, const Rectangle& region, const Vector4& color)
  466. : _region(region), _color(color)
  467. {
  468. generateUVs(tw, th, region.x, region.y, region.width, region.height, &_uvs);
  469. }
  470. Theme::ThemeImage::~ThemeImage()
  471. {
  472. }
  473. Theme::ThemeImage* Theme::ThemeImage::create(float tw, float th, Properties* properties, const Vector4& defaultColor)
  474. {
  475. GP_ASSERT(properties);
  476. Vector4 regionVector;
  477. properties->getVector4("region", &regionVector);
  478. const Rectangle region(regionVector.x, regionVector.y, regionVector.z, regionVector.w);
  479. Vector4 color;
  480. if (properties->exists("color"))
  481. {
  482. properties->getColor("color", &color);
  483. }
  484. else
  485. {
  486. color.set(defaultColor);
  487. }
  488. ThemeImage* image = new ThemeImage(tw, th, region, color);
  489. const char* id = properties->getId();
  490. if (id)
  491. {
  492. image->_id = id;
  493. }
  494. return image;
  495. }
  496. const char* Theme::ThemeImage::getId() const
  497. {
  498. return _id.c_str();
  499. }
  500. const Theme::UVs& Theme::ThemeImage::getUVs() const
  501. {
  502. return _uvs;
  503. }
  504. const Rectangle& Theme::ThemeImage::getRegion() const
  505. {
  506. return _region;
  507. }
  508. const Vector4& Theme::ThemeImage::getColor() const
  509. {
  510. return _color;
  511. }
  512. /********************
  513. * Theme::ImageList *
  514. ********************/
  515. Theme::ImageList::ImageList(const Vector4& color) : _color(color)
  516. {
  517. }
  518. Theme::ImageList::ImageList(const ImageList& copy)
  519. : _id(copy._id), _color(copy._color)
  520. {
  521. std::vector<ThemeImage*>::const_iterator it;
  522. for (it = copy._images.begin(); it != copy._images.end(); ++it)
  523. {
  524. ThemeImage* image = *it;
  525. GP_ASSERT(image);
  526. _images.push_back(new ThemeImage(*image));
  527. }
  528. }
  529. Theme::ImageList::~ImageList()
  530. {
  531. std::vector<ThemeImage*>::const_iterator it;
  532. for (it = _images.begin(); it != _images.end(); ++it)
  533. {
  534. ThemeImage* image = *it;
  535. SAFE_RELEASE(image);
  536. }
  537. }
  538. Theme::ImageList* Theme::ImageList::create(float tw, float th, Properties* properties)
  539. {
  540. GP_ASSERT(properties);
  541. Vector4 color(1, 1, 1, 1);
  542. if (properties->exists("color"))
  543. {
  544. properties->getColor("color", &color);
  545. }
  546. ImageList* imageList = new ImageList(color);
  547. const char* id = properties->getId();
  548. if (id)
  549. {
  550. imageList->_id = id;
  551. }
  552. Properties* space = properties->getNextNamespace();
  553. while (space != NULL)
  554. {
  555. ThemeImage* image = ThemeImage::create(tw, th, space, color);
  556. GP_ASSERT(image);
  557. imageList->_images.push_back(image);
  558. space = properties->getNextNamespace();
  559. }
  560. return imageList;
  561. }
  562. const char* Theme::ImageList::getId() const
  563. {
  564. return _id.c_str();
  565. }
  566. Theme::ThemeImage* Theme::ImageList::getImage(const char* imageId) const
  567. {
  568. GP_ASSERT(imageId);
  569. std::vector<ThemeImage*>::const_iterator it;
  570. for (it = _images.begin(); it != _images.end(); ++it)
  571. {
  572. ThemeImage* image = *it;
  573. GP_ASSERT(image);
  574. GP_ASSERT(image->getId());
  575. if (strcmp(image->getId(), imageId) == 0)
  576. {
  577. return image;
  578. }
  579. }
  580. return NULL;
  581. }
  582. /***************
  583. * Theme::Skin *
  584. ***************/
  585. Theme::Skin* Theme::Skin::create(const char* id, float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  586. {
  587. Skin* skin = new Skin(tw, th, region, border, color);
  588. if (id)
  589. {
  590. skin->_id = id;
  591. }
  592. return skin;
  593. }
  594. Theme::Skin::Skin(float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  595. : _border(border), _color(color), _region(region)
  596. {
  597. setRegion(region, tw, th);
  598. }
  599. Theme::Skin::~Skin()
  600. {
  601. }
  602. const char* Theme::Skin::getId() const
  603. {
  604. return _id.c_str();
  605. }
  606. const Theme::Border& Theme::Skin::getBorder() const
  607. {
  608. return _border;
  609. }
  610. const Rectangle& Theme::Skin::getRegion() const
  611. {
  612. return _region;
  613. }
  614. void Theme::Skin::setRegion(const Rectangle& region, float tw, float th)
  615. {
  616. // Can calculate all measurements in advance.
  617. float leftEdge = region.x * tw;
  618. float rightEdge = (region.x + region.width) * tw;
  619. float leftBorder = (region.x + _border.left) * tw;
  620. float rightBorder = (region.x + region.width - _border.right) * tw;
  621. float topEdge = 1.0f - (region.y * th);
  622. float bottomEdge = 1.0f - ((region.y + region.height) * th);
  623. float topBorder = 1.0f - ((region.y + _border.top) * th);
  624. float bottomBorder = 1.0f - ((region.y + region.height - _border.bottom) * th);
  625. // There are 9 sets of UVs to set.
  626. _uvs[TOP_LEFT].u1 = leftEdge;
  627. _uvs[TOP_LEFT].v1 = topEdge;
  628. _uvs[TOP_LEFT].u2 = leftBorder;
  629. _uvs[TOP_LEFT].v2 = topBorder;
  630. _uvs[TOP].u1 = leftBorder;
  631. _uvs[TOP].v1 = topEdge;
  632. _uvs[TOP].u2 = rightBorder;
  633. _uvs[TOP].v2 = topBorder;
  634. _uvs[TOP_RIGHT].u1 = rightBorder;
  635. _uvs[TOP_RIGHT].v1 = topEdge;
  636. _uvs[TOP_RIGHT].u2 = rightEdge;
  637. _uvs[TOP_RIGHT].v2 = topBorder;
  638. _uvs[LEFT].u1 = leftEdge;
  639. _uvs[LEFT].v1 = topBorder;
  640. _uvs[LEFT].u2 = leftBorder;
  641. _uvs[LEFT].v2 = bottomBorder;
  642. _uvs[CENTER].u1 = leftBorder;
  643. _uvs[CENTER].v1 = topBorder;
  644. _uvs[CENTER].u2 = rightBorder;
  645. _uvs[CENTER].v2 = bottomBorder;
  646. _uvs[RIGHT].u1 = rightBorder;
  647. _uvs[RIGHT].v1 = topBorder;
  648. _uvs[RIGHT].u2 = rightEdge;
  649. _uvs[RIGHT].v2 = bottomBorder;
  650. _uvs[BOTTOM_LEFT].u1 = leftEdge;
  651. _uvs[BOTTOM_LEFT].v1 = bottomBorder;
  652. _uvs[BOTTOM_LEFT].u2 = leftBorder;
  653. _uvs[BOTTOM_LEFT].v2 = bottomEdge;
  654. _uvs[BOTTOM].u1 = leftBorder;
  655. _uvs[BOTTOM].v1 = bottomBorder;
  656. _uvs[BOTTOM].u2 = rightBorder;
  657. _uvs[BOTTOM].v2 = bottomEdge;
  658. _uvs[BOTTOM_RIGHT].u1 = rightBorder;
  659. _uvs[BOTTOM_RIGHT].v1 = bottomBorder;
  660. _uvs[BOTTOM_RIGHT].u2 = rightEdge;
  661. _uvs[BOTTOM_RIGHT].v2 = bottomEdge;
  662. }
  663. const Theme::UVs& Theme::Skin::getUVs(SkinArea area) const
  664. {
  665. return _uvs[area];
  666. }
  667. const Vector4& Theme::Skin::getColor() const
  668. {
  669. return _color;
  670. }
  671. /**
  672. * Theme utility methods.
  673. */
  674. void Theme::generateUVs(float tw, float th, float x, float y, float width, float height, UVs* uvs)
  675. {
  676. GP_ASSERT(uvs);
  677. uvs->u1 = x * tw;
  678. uvs->u2 = (x + width) * tw;
  679. uvs->v1 = 1.0f - (y * th);
  680. uvs->v2 = 1.0f - ((y + height) * th);
  681. }
  682. void Theme::lookUpSprites(const Properties* overlaySpace, ImageList** imageList, ThemeImage** cursor, Skin** skin)
  683. {
  684. GP_ASSERT(overlaySpace);
  685. const char* imageListString = overlaySpace->getString("imageList");
  686. if (imageListString)
  687. {
  688. for (unsigned int i = 0; i < _imageLists.size(); ++i)
  689. {
  690. GP_ASSERT(_imageLists[i]);
  691. GP_ASSERT(_imageLists[i]->getId());
  692. if (strcmp(_imageLists[i]->getId(), imageListString) == 0)
  693. {
  694. GP_ASSERT(imageList);
  695. *imageList = _imageLists[i];
  696. break;
  697. }
  698. }
  699. }
  700. const char* cursorString = overlaySpace->getString("cursor");
  701. if (cursorString)
  702. {
  703. for (unsigned int i = 0; i < _images.size(); ++i)
  704. {
  705. GP_ASSERT(_images[i]);
  706. GP_ASSERT(_images[i]->getId());
  707. if (strcmp(_images[i]->getId(), cursorString) == 0)
  708. {
  709. GP_ASSERT(cursor);
  710. *cursor = _images[i];
  711. break;
  712. }
  713. }
  714. }
  715. const char* skinString = overlaySpace->getString("skin");
  716. if (skinString)
  717. {
  718. for (unsigned int i = 0; i < _skins.size(); ++i)
  719. {
  720. GP_ASSERT(_skins[i]);
  721. GP_ASSERT(_skins[i]->getId());
  722. if (strcmp(_skins[i]->getId(), skinString) == 0)
  723. {
  724. GP_ASSERT(skin);
  725. *skin = _skins[i];
  726. break;
  727. }
  728. }
  729. }
  730. }
  731. }