Theme.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. GP_ASSERT(*it);
  391. GP_ASSERT((*it)->getSpriteBatch());
  392. (*it)->getSpriteBatch()->setProjectionMatrix(matrix);
  393. }
  394. }
  395. SpriteBatch* Theme::getSpriteBatch() const
  396. {
  397. return _spriteBatch;
  398. }
  399. /**************
  400. * Theme::UVs *
  401. **************/
  402. Theme::UVs::UVs()
  403. : u1(0), v1(0), u2(0), v2(0)
  404. {
  405. }
  406. Theme::UVs::UVs(float u1, float v1, float u2, float v2)
  407. : u1(u1), v1(v1), u2(u2), v2(v2)
  408. {
  409. }
  410. const Theme::UVs& Theme::UVs::empty()
  411. {
  412. static UVs empty(0, 0, 0, 0);
  413. return empty;
  414. }
  415. /**********************
  416. * Theme::SideRegions *
  417. **********************/
  418. const Theme::SideRegions& Theme::SideRegions::empty()
  419. {
  420. static SideRegions empty;
  421. return empty;
  422. }
  423. /****************
  424. * Theme::ThemeImage *
  425. ****************/
  426. Theme::ThemeImage::ThemeImage(float tw, float th, const Rectangle& region, const Vector4& color)
  427. : _region(region), _color(color)
  428. {
  429. generateUVs(tw, th, region.x, region.y, region.width, region.height, &_uvs);
  430. }
  431. Theme::ThemeImage::~ThemeImage()
  432. {
  433. }
  434. Theme::ThemeImage* Theme::ThemeImage::create(float tw, float th, Properties* properties, const Vector4& defaultColor)
  435. {
  436. GP_ASSERT(properties);
  437. Vector4 regionVector;
  438. properties->getVector4("region", &regionVector);
  439. const Rectangle region(regionVector.x, regionVector.y, regionVector.z, regionVector.w);
  440. Vector4 color;
  441. if (properties->exists("color"))
  442. {
  443. properties->getColor("color", &color);
  444. }
  445. else
  446. {
  447. color.set(defaultColor);
  448. }
  449. ThemeImage* image = new ThemeImage(tw, th, region, color);
  450. const char* id = properties->getId();
  451. if (id)
  452. {
  453. image->_id = id;
  454. }
  455. return image;
  456. }
  457. const char* Theme::ThemeImage::getId() const
  458. {
  459. return _id.c_str();
  460. }
  461. const Theme::UVs& Theme::ThemeImage::getUVs() const
  462. {
  463. return _uvs;
  464. }
  465. const Rectangle& Theme::ThemeImage::getRegion() const
  466. {
  467. return _region;
  468. }
  469. const Vector4& Theme::ThemeImage::getColor() const
  470. {
  471. return _color;
  472. }
  473. /********************
  474. * Theme::ImageList *
  475. ********************/
  476. Theme::ImageList::ImageList(const Vector4& color) : _color(color)
  477. {
  478. }
  479. Theme::ImageList::ImageList(const ImageList& copy)
  480. {
  481. _id = copy._id;
  482. _color = copy._color;
  483. std::vector<ThemeImage*>::const_iterator it;
  484. for (it = copy._images.begin(); it != copy._images.end(); it++)
  485. {
  486. ThemeImage* image = *it;
  487. GP_ASSERT(image);
  488. _images.push_back(new ThemeImage(*image));
  489. }
  490. }
  491. Theme::ImageList::~ImageList()
  492. {
  493. std::vector<ThemeImage*>::const_iterator it;
  494. for (it = _images.begin(); it != _images.end(); it++)
  495. {
  496. ThemeImage* image = *it;
  497. SAFE_RELEASE(image);
  498. }
  499. }
  500. Theme::ImageList* Theme::ImageList::create(float tw, float th, Properties* properties)
  501. {
  502. GP_ASSERT(properties);
  503. Vector4 color(1, 1, 1, 1);
  504. if (properties->exists("color"))
  505. {
  506. properties->getColor("color", &color);
  507. }
  508. ImageList* imageList = new ImageList(color);
  509. const char* id = properties->getId();
  510. if (id)
  511. {
  512. imageList->_id = id;
  513. }
  514. Properties* space = properties->getNextNamespace();
  515. while (space != NULL)
  516. {
  517. ThemeImage* image = ThemeImage::create(tw, th, space, color);
  518. GP_ASSERT(image);
  519. imageList->_images.push_back(image);
  520. space = properties->getNextNamespace();
  521. }
  522. return imageList;
  523. }
  524. const char* Theme::ImageList::getId() const
  525. {
  526. return _id.c_str();
  527. }
  528. Theme::ThemeImage* Theme::ImageList::getImage(const char* imageId) const
  529. {
  530. GP_ASSERT(imageId);
  531. std::vector<ThemeImage*>::const_iterator it;
  532. for (it = _images.begin(); it != _images.end(); it++)
  533. {
  534. ThemeImage* image = *it;
  535. GP_ASSERT(image);
  536. GP_ASSERT(image->getId());
  537. if (strcmp(image->getId(), imageId) == 0)
  538. {
  539. return image;
  540. }
  541. }
  542. return NULL;
  543. }
  544. /***************
  545. * Theme::Skin *
  546. ***************/
  547. Theme::Skin* Theme::Skin::create(const char* id, float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  548. {
  549. Skin* skin = new Skin(tw, th, region, border, color);
  550. if (id)
  551. {
  552. skin->_id = id;
  553. }
  554. return skin;
  555. }
  556. Theme::Skin::Skin(float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  557. : _border(border), _color(color), _region(region)
  558. {
  559. setRegion(region, tw, th);
  560. }
  561. Theme::Skin::~Skin()
  562. {
  563. }
  564. const char* Theme::Skin::getId() const
  565. {
  566. return _id.c_str();
  567. }
  568. const Theme::Border& Theme::Skin::getBorder() const
  569. {
  570. return _border;
  571. }
  572. const Rectangle& Theme::Skin::getRegion() const
  573. {
  574. return _region;
  575. }
  576. void Theme::Skin::setRegion(const Rectangle& region, float tw, float th)
  577. {
  578. // Can calculate all measurements in advance.
  579. float leftEdge = region.x * tw;
  580. float rightEdge = (region.x + region.width) * tw;
  581. float leftBorder = (region.x + _border.left) * tw;
  582. float rightBorder = (region.x + region.width - _border.right) * tw;
  583. float topEdge = 1.0f - (region.y * th);
  584. float bottomEdge = 1.0f - ((region.y + region.height) * th);
  585. float topBorder = 1.0f - ((region.y + _border.top) * th);
  586. float bottomBorder = 1.0f - ((region.y + region.height - _border.bottom) * th);
  587. // There are 9 sets of UVs to set.
  588. _uvs[TOP_LEFT].u1 = leftEdge;
  589. _uvs[TOP_LEFT].v1 = topEdge;
  590. _uvs[TOP_LEFT].u2 = leftBorder;
  591. _uvs[TOP_LEFT].v2 = topBorder;
  592. _uvs[TOP].u1 = leftBorder;
  593. _uvs[TOP].v1 = topEdge;
  594. _uvs[TOP].u2 = rightBorder;
  595. _uvs[TOP].v2 = topBorder;
  596. _uvs[TOP_RIGHT].u1 = rightBorder;
  597. _uvs[TOP_RIGHT].v1 = topEdge;
  598. _uvs[TOP_RIGHT].u2 = rightEdge;
  599. _uvs[TOP_RIGHT].v2 = topBorder;
  600. _uvs[LEFT].u1 = leftEdge;
  601. _uvs[LEFT].v1 = topBorder;
  602. _uvs[LEFT].u2 = leftBorder;
  603. _uvs[LEFT].v2 = bottomBorder;
  604. _uvs[CENTER].u1 = leftBorder;
  605. _uvs[CENTER].v1 = topBorder;
  606. _uvs[CENTER].u2 = rightBorder;
  607. _uvs[CENTER].v2 = bottomBorder;
  608. _uvs[RIGHT].u1 = rightBorder;
  609. _uvs[RIGHT].v1 = topBorder;
  610. _uvs[RIGHT].u2 = rightEdge;
  611. _uvs[RIGHT].v2 = bottomBorder;
  612. _uvs[BOTTOM_LEFT].u1 = leftEdge;
  613. _uvs[BOTTOM_LEFT].v1 = bottomBorder;
  614. _uvs[BOTTOM_LEFT].u2 = leftBorder;
  615. _uvs[BOTTOM_LEFT].v2 = bottomEdge;
  616. _uvs[BOTTOM].u1 = leftBorder;
  617. _uvs[BOTTOM].v1 = bottomBorder;
  618. _uvs[BOTTOM].u2 = rightBorder;
  619. _uvs[BOTTOM].v2 = bottomEdge;
  620. _uvs[BOTTOM_RIGHT].u1 = rightBorder;
  621. _uvs[BOTTOM_RIGHT].v1 = bottomBorder;
  622. _uvs[BOTTOM_RIGHT].u2 = rightEdge;
  623. _uvs[BOTTOM_RIGHT].v2 = bottomEdge;
  624. }
  625. const Theme::UVs& Theme::Skin::getUVs(SkinArea area) const
  626. {
  627. return _uvs[area];
  628. }
  629. const Vector4& Theme::Skin::getColor() const
  630. {
  631. return _color;
  632. }
  633. /**
  634. * Theme utility methods.
  635. */
  636. void Theme::generateUVs(float tw, float th, float x, float y, float width, float height, UVs* uvs)
  637. {
  638. GP_ASSERT(uvs);
  639. uvs->u1 = x * tw;
  640. uvs->u2 = (x + width) * tw;
  641. uvs->v1 = 1.0f - (y * th);
  642. uvs->v2 = 1.0f - ((y + height) * th);
  643. }
  644. void Theme::lookUpSprites(const Properties* overlaySpace, ImageList** imageList, ThemeImage** cursor, Skin** skin)
  645. {
  646. GP_ASSERT(overlaySpace);
  647. const char* imageListString = overlaySpace->getString("imageList");
  648. if (imageListString)
  649. {
  650. for (unsigned int i = 0; i < _imageLists.size(); ++i)
  651. {
  652. GP_ASSERT(_imageLists[i]);
  653. GP_ASSERT(_imageLists[i]->getId());
  654. if (strcmp(_imageLists[i]->getId(), imageListString) == 0)
  655. {
  656. GP_ASSERT(imageList);
  657. *imageList = _imageLists[i];
  658. break;
  659. }
  660. }
  661. }
  662. const char* cursorString = overlaySpace->getString("cursor");
  663. if (cursorString)
  664. {
  665. for (unsigned int i = 0; i < _images.size(); ++i)
  666. {
  667. GP_ASSERT(_images[i]);
  668. GP_ASSERT(_images[i]->getId());
  669. if (strcmp(_images[i]->getId(), cursorString) == 0)
  670. {
  671. GP_ASSERT(cursor);
  672. *cursor = _images[i];
  673. break;
  674. }
  675. }
  676. }
  677. const char* skinString = overlaySpace->getString("skin");
  678. if (skinString)
  679. {
  680. for (unsigned int i = 0; i < _skins.size(); ++i)
  681. {
  682. GP_ASSERT(_skins[i]);
  683. GP_ASSERT(_skins[i]->getId());
  684. if (strcmp(_skins[i]->getId(), skinString) == 0)
  685. {
  686. GP_ASSERT(skin);
  687. *skin = _skins[i];
  688. break;
  689. }
  690. }
  691. }
  692. }
  693. }