Theme.cpp 25 KB

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