Theme.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. }
  239. unsigned int fontSize;
  240. if (innerSpace->exists("fontSize"))
  241. {
  242. fontSize = innerSpace->getInt("fontSize");
  243. }
  244. else
  245. {
  246. fontSize = normal->getFontSize();
  247. }
  248. const char* textAlignmentString = innerSpace->getString("textAlignment");
  249. Font::Justify textAlignment;
  250. if (textAlignmentString)
  251. {
  252. textAlignment = Font::getJustify(textAlignmentString);
  253. }
  254. else
  255. {
  256. textAlignment = normal->getTextAlignment();
  257. }
  258. bool rightToLeft;
  259. if (innerSpace->exists("rightToLeft"))
  260. {
  261. rightToLeft = innerSpace->getBool("rightToLeft");
  262. }
  263. else
  264. {
  265. rightToLeft = normal->getTextRightToLeft();
  266. }
  267. float opacity;
  268. if (innerSpace->exists("opacity"))
  269. {
  270. opacity = innerSpace->getFloat("opacity");
  271. }
  272. else
  273. {
  274. opacity = normal->getOpacity();
  275. }
  276. ImageList* imageList = NULL;
  277. ThemeImage* cursor = NULL;
  278. Skin* skin = NULL;
  279. theme->lookUpSprites(innerSpace, &imageList, &cursor, &skin);
  280. if (!imageList)
  281. {
  282. imageList = normal->getImageList();
  283. }
  284. if (!cursor)
  285. {
  286. cursor = normal->getCursor();
  287. }
  288. if (!skin)
  289. {
  290. skin = normal->getSkin();
  291. }
  292. if (strcmp(innerSpacename, "stateFocus") == 0)
  293. {
  294. focus = Theme::Style::Overlay::create();
  295. GP_ASSERT(focus);
  296. focus->setSkin(skin);
  297. focus->setCursor(cursor);
  298. focus->setImageList(imageList);
  299. focus->setTextColor(textColor);
  300. focus->setFont(font);
  301. focus->setFontSize(fontSize);
  302. focus->setTextAlignment(textAlignment);
  303. focus->setTextRightToLeft(rightToLeft);
  304. focus->setOpacity(opacity);
  305. if (font)
  306. theme->_fonts.insert(font);
  307. }
  308. else if (strcmp(innerSpacename, "stateActive") == 0)
  309. {
  310. active = Theme::Style::Overlay::create();
  311. GP_ASSERT(active);
  312. active->setSkin(skin);
  313. active->setCursor(cursor);
  314. active->setImageList(imageList);
  315. active->setTextColor(textColor);
  316. active->setFont(font);
  317. active->setFontSize(fontSize);
  318. active->setTextAlignment(textAlignment);
  319. active->setTextRightToLeft(rightToLeft);
  320. active->setOpacity(opacity);
  321. if (font)
  322. theme->_fonts.insert(font);
  323. }
  324. else if (strcmp(innerSpacename, "stateDisabled") == 0)
  325. {
  326. disabled = Theme::Style::Overlay::create();
  327. GP_ASSERT(disabled);
  328. disabled->setSkin(skin);
  329. disabled->setCursor(cursor);
  330. disabled->setImageList(imageList);
  331. disabled->setTextColor(textColor);
  332. disabled->setFont(font);
  333. disabled->setFontSize(fontSize);
  334. disabled->setTextAlignment(textAlignment);
  335. disabled->setTextRightToLeft(rightToLeft);
  336. disabled->setOpacity(opacity);
  337. if (font)
  338. theme->_fonts.insert(font);
  339. }
  340. }
  341. innerSpace = space->getNextNamespace();
  342. }
  343. if (!focus)
  344. {
  345. focus = normal;
  346. focus->addRef();
  347. }
  348. if (!active)
  349. {
  350. active = normal;
  351. active->addRef();
  352. }
  353. if (!disabled)
  354. {
  355. disabled = normal;
  356. disabled->addRef();
  357. }
  358. Theme::Style* s = new Theme::Style(theme, space->getId(), tw, th, margin, padding, normal, focus, active, disabled);
  359. GP_ASSERT(s);
  360. theme->_styles.push_back(s);
  361. }
  362. space = themeProperties->getNextNamespace();
  363. }
  364. // Add this theme to the cache.
  365. __themeCache.push_back(theme);
  366. SAFE_DELETE(properties);
  367. return theme;
  368. }
  369. Theme::Style* Theme::getStyle(const char* name) const
  370. {
  371. GP_ASSERT(name);
  372. for (unsigned int i = 0, count = _styles.size(); i < count; ++i)
  373. {
  374. GP_ASSERT(_styles[i]);
  375. if (strcmp(name, _styles[i]->getId()) == 0)
  376. {
  377. return _styles[i];
  378. }
  379. }
  380. return NULL;
  381. }
  382. void Theme::setProjectionMatrix(const Matrix& matrix)
  383. {
  384. GP_ASSERT(_spriteBatch);
  385. _spriteBatch->setProjectionMatrix(matrix);
  386. // Set the matrix on each Font used by the style.
  387. std::set<Font*>::const_iterator it;
  388. for (it = _fonts.begin(); it != _fonts.end(); ++it)
  389. {
  390. Font* font = *it;
  391. GP_ASSERT(font);
  392. GP_ASSERT(font->getSpriteBatch());
  393. font->getSpriteBatch()->setProjectionMatrix(matrix);
  394. }
  395. }
  396. SpriteBatch* Theme::getSpriteBatch() const
  397. {
  398. return _spriteBatch;
  399. }
  400. /**************
  401. * Theme::UVs *
  402. **************/
  403. Theme::UVs::UVs()
  404. : u1(0), v1(0), u2(0), v2(0)
  405. {
  406. }
  407. Theme::UVs::UVs(float u1, float v1, float u2, float v2)
  408. : u1(u1), v1(v1), u2(u2), v2(v2)
  409. {
  410. }
  411. const Theme::UVs& Theme::UVs::empty()
  412. {
  413. static UVs empty(0, 0, 0, 0);
  414. return empty;
  415. }
  416. /**********************
  417. * Theme::SideRegions *
  418. **********************/
  419. const Theme::SideRegions& Theme::SideRegions::empty()
  420. {
  421. static SideRegions empty;
  422. return empty;
  423. }
  424. /*********************
  425. * Theme::ThemeImage *
  426. *********************/
  427. Theme::ThemeImage::ThemeImage(float tw, float th, const Rectangle& region, const Vector4& color)
  428. : _region(region), _color(color)
  429. {
  430. generateUVs(tw, th, region.x, region.y, region.width, region.height, &_uvs);
  431. }
  432. Theme::ThemeImage::~ThemeImage()
  433. {
  434. }
  435. Theme::ThemeImage* Theme::ThemeImage::create(float tw, float th, Properties* properties, const Vector4& defaultColor)
  436. {
  437. GP_ASSERT(properties);
  438. Vector4 regionVector;
  439. properties->getVector4("region", &regionVector);
  440. const Rectangle region(regionVector.x, regionVector.y, regionVector.z, regionVector.w);
  441. Vector4 color;
  442. if (properties->exists("color"))
  443. {
  444. properties->getColor("color", &color);
  445. }
  446. else
  447. {
  448. color.set(defaultColor);
  449. }
  450. ThemeImage* image = new ThemeImage(tw, th, region, color);
  451. const char* id = properties->getId();
  452. if (id)
  453. {
  454. image->_id = id;
  455. }
  456. return image;
  457. }
  458. const char* Theme::ThemeImage::getId() const
  459. {
  460. return _id.c_str();
  461. }
  462. const Theme::UVs& Theme::ThemeImage::getUVs() const
  463. {
  464. return _uvs;
  465. }
  466. const Rectangle& Theme::ThemeImage::getRegion() const
  467. {
  468. return _region;
  469. }
  470. const Vector4& Theme::ThemeImage::getColor() const
  471. {
  472. return _color;
  473. }
  474. /********************
  475. * Theme::ImageList *
  476. ********************/
  477. Theme::ImageList::ImageList(const Vector4& color) : _color(color)
  478. {
  479. }
  480. Theme::ImageList::ImageList(const ImageList& copy)
  481. {
  482. _id = copy._id;
  483. _color = copy._color;
  484. std::vector<ThemeImage*>::const_iterator it;
  485. for (it = copy._images.begin(); it != copy._images.end(); it++)
  486. {
  487. ThemeImage* image = *it;
  488. GP_ASSERT(image);
  489. _images.push_back(new ThemeImage(*image));
  490. }
  491. }
  492. Theme::ImageList::~ImageList()
  493. {
  494. std::vector<ThemeImage*>::const_iterator it;
  495. for (it = _images.begin(); it != _images.end(); it++)
  496. {
  497. ThemeImage* image = *it;
  498. SAFE_RELEASE(image);
  499. }
  500. }
  501. Theme::ImageList* Theme::ImageList::create(float tw, float th, Properties* properties)
  502. {
  503. GP_ASSERT(properties);
  504. Vector4 color(1, 1, 1, 1);
  505. if (properties->exists("color"))
  506. {
  507. properties->getColor("color", &color);
  508. }
  509. ImageList* imageList = new ImageList(color);
  510. const char* id = properties->getId();
  511. if (id)
  512. {
  513. imageList->_id = id;
  514. }
  515. Properties* space = properties->getNextNamespace();
  516. while (space != NULL)
  517. {
  518. ThemeImage* image = ThemeImage::create(tw, th, space, color);
  519. GP_ASSERT(image);
  520. imageList->_images.push_back(image);
  521. space = properties->getNextNamespace();
  522. }
  523. return imageList;
  524. }
  525. const char* Theme::ImageList::getId() const
  526. {
  527. return _id.c_str();
  528. }
  529. Theme::ThemeImage* Theme::ImageList::getImage(const char* imageId) const
  530. {
  531. GP_ASSERT(imageId);
  532. std::vector<ThemeImage*>::const_iterator it;
  533. for (it = _images.begin(); it != _images.end(); it++)
  534. {
  535. ThemeImage* image = *it;
  536. GP_ASSERT(image);
  537. GP_ASSERT(image->getId());
  538. if (strcmp(image->getId(), imageId) == 0)
  539. {
  540. return image;
  541. }
  542. }
  543. return NULL;
  544. }
  545. /***************
  546. * Theme::Skin *
  547. ***************/
  548. Theme::Skin* Theme::Skin::create(const char* id, float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  549. {
  550. Skin* skin = new Skin(tw, th, region, border, color);
  551. if (id)
  552. {
  553. skin->_id = id;
  554. }
  555. return skin;
  556. }
  557. Theme::Skin::Skin(float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  558. : _border(border), _color(color), _region(region)
  559. {
  560. setRegion(region, tw, th);
  561. }
  562. Theme::Skin::~Skin()
  563. {
  564. }
  565. const char* Theme::Skin::getId() const
  566. {
  567. return _id.c_str();
  568. }
  569. const Theme::Border& Theme::Skin::getBorder() const
  570. {
  571. return _border;
  572. }
  573. const Rectangle& Theme::Skin::getRegion() const
  574. {
  575. return _region;
  576. }
  577. void Theme::Skin::setRegion(const Rectangle& region, float tw, float th)
  578. {
  579. // Can calculate all measurements in advance.
  580. float leftEdge = region.x * tw;
  581. float rightEdge = (region.x + region.width) * tw;
  582. float leftBorder = (region.x + _border.left) * tw;
  583. float rightBorder = (region.x + region.width - _border.right) * tw;
  584. float topEdge = 1.0f - (region.y * th);
  585. float bottomEdge = 1.0f - ((region.y + region.height) * th);
  586. float topBorder = 1.0f - ((region.y + _border.top) * th);
  587. float bottomBorder = 1.0f - ((region.y + region.height - _border.bottom) * th);
  588. // There are 9 sets of UVs to set.
  589. _uvs[TOP_LEFT].u1 = leftEdge;
  590. _uvs[TOP_LEFT].v1 = topEdge;
  591. _uvs[TOP_LEFT].u2 = leftBorder;
  592. _uvs[TOP_LEFT].v2 = topBorder;
  593. _uvs[TOP].u1 = leftBorder;
  594. _uvs[TOP].v1 = topEdge;
  595. _uvs[TOP].u2 = rightBorder;
  596. _uvs[TOP].v2 = topBorder;
  597. _uvs[TOP_RIGHT].u1 = rightBorder;
  598. _uvs[TOP_RIGHT].v1 = topEdge;
  599. _uvs[TOP_RIGHT].u2 = rightEdge;
  600. _uvs[TOP_RIGHT].v2 = topBorder;
  601. _uvs[LEFT].u1 = leftEdge;
  602. _uvs[LEFT].v1 = topBorder;
  603. _uvs[LEFT].u2 = leftBorder;
  604. _uvs[LEFT].v2 = bottomBorder;
  605. _uvs[CENTER].u1 = leftBorder;
  606. _uvs[CENTER].v1 = topBorder;
  607. _uvs[CENTER].u2 = rightBorder;
  608. _uvs[CENTER].v2 = bottomBorder;
  609. _uvs[RIGHT].u1 = rightBorder;
  610. _uvs[RIGHT].v1 = topBorder;
  611. _uvs[RIGHT].u2 = rightEdge;
  612. _uvs[RIGHT].v2 = bottomBorder;
  613. _uvs[BOTTOM_LEFT].u1 = leftEdge;
  614. _uvs[BOTTOM_LEFT].v1 = bottomBorder;
  615. _uvs[BOTTOM_LEFT].u2 = leftBorder;
  616. _uvs[BOTTOM_LEFT].v2 = bottomEdge;
  617. _uvs[BOTTOM].u1 = leftBorder;
  618. _uvs[BOTTOM].v1 = bottomBorder;
  619. _uvs[BOTTOM].u2 = rightBorder;
  620. _uvs[BOTTOM].v2 = bottomEdge;
  621. _uvs[BOTTOM_RIGHT].u1 = rightBorder;
  622. _uvs[BOTTOM_RIGHT].v1 = bottomBorder;
  623. _uvs[BOTTOM_RIGHT].u2 = rightEdge;
  624. _uvs[BOTTOM_RIGHT].v2 = bottomEdge;
  625. }
  626. const Theme::UVs& Theme::Skin::getUVs(SkinArea area) const
  627. {
  628. return _uvs[area];
  629. }
  630. const Vector4& Theme::Skin::getColor() const
  631. {
  632. return _color;
  633. }
  634. /**
  635. * Theme utility methods.
  636. */
  637. void Theme::generateUVs(float tw, float th, float x, float y, float width, float height, UVs* uvs)
  638. {
  639. GP_ASSERT(uvs);
  640. uvs->u1 = x * tw;
  641. uvs->u2 = (x + width) * tw;
  642. uvs->v1 = 1.0f - (y * th);
  643. uvs->v2 = 1.0f - ((y + height) * th);
  644. }
  645. void Theme::lookUpSprites(const Properties* overlaySpace, ImageList** imageList, ThemeImage** cursor, Skin** skin)
  646. {
  647. GP_ASSERT(overlaySpace);
  648. const char* imageListString = overlaySpace->getString("imageList");
  649. if (imageListString)
  650. {
  651. for (unsigned int i = 0; i < _imageLists.size(); ++i)
  652. {
  653. GP_ASSERT(_imageLists[i]);
  654. GP_ASSERT(_imageLists[i]->getId());
  655. if (strcmp(_imageLists[i]->getId(), imageListString) == 0)
  656. {
  657. GP_ASSERT(imageList);
  658. *imageList = _imageLists[i];
  659. break;
  660. }
  661. }
  662. }
  663. const char* cursorString = overlaySpace->getString("cursor");
  664. if (cursorString)
  665. {
  666. for (unsigned int i = 0; i < _images.size(); ++i)
  667. {
  668. GP_ASSERT(_images[i]);
  669. GP_ASSERT(_images[i]->getId());
  670. if (strcmp(_images[i]->getId(), cursorString) == 0)
  671. {
  672. GP_ASSERT(cursor);
  673. *cursor = _images[i];
  674. break;
  675. }
  676. }
  677. }
  678. const char* skinString = overlaySpace->getString("skin");
  679. if (skinString)
  680. {
  681. for (unsigned int i = 0; i < _skins.size(); ++i)
  682. {
  683. GP_ASSERT(_skins[i]);
  684. GP_ASSERT(_skins[i]->getId());
  685. if (strcmp(_skins[i]->getId(), skinString) == 0)
  686. {
  687. GP_ASSERT(skin);
  688. *skin = _skins[i];
  689. break;
  690. }
  691. }
  692. }
  693. }
  694. }