Theme.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. const char* textureFile = themeProperties->getString("texture");
  76. theme->_texture = Texture::create(textureFile, false);
  77. GP_ASSERT(theme->_texture);
  78. theme->_spriteBatch = SpriteBatch::create(theme->_texture);
  79. GP_ASSERT(theme->_spriteBatch);
  80. theme->_spriteBatch->getSampler()->setFilterMode(Texture::NEAREST, Texture::NEAREST);
  81. float tw = 1.0f / theme->_texture->getWidth();
  82. float th = 1.0f / theme->_texture->getHeight();
  83. Properties* space = themeProperties->getNextNamespace();
  84. while (space != NULL)
  85. {
  86. // First load all cursors, checkboxes etc. that can be referred to by styles.
  87. const char* spacename = space->getNamespace();
  88. if (strcmp(spacename, "image") == 0)
  89. {
  90. theme->_images.push_back(ThemeImage::create(tw, th, space, Vector4::one()));
  91. }
  92. else if (strcmp(spacename, "imageList") == 0)
  93. {
  94. theme->_imageLists.push_back(ImageList::create(tw, th, space));
  95. }
  96. else if (strcmp(spacename, "skin") == 0)
  97. {
  98. Theme::Border border;
  99. Properties* innerSpace = space->getNextNamespace();
  100. if (innerSpace)
  101. {
  102. const char* innerSpacename = innerSpace->getNamespace();
  103. if (strcmp(innerSpacename, "border") == 0)
  104. {
  105. border.top = innerSpace->getFloat("top");
  106. border.bottom = innerSpace->getFloat("bottom");
  107. border.left = innerSpace->getFloat("left");
  108. border.right = innerSpace->getFloat("right");
  109. }
  110. }
  111. Vector4 regionVector;
  112. space->getVector4("region", &regionVector);
  113. const Rectangle region(regionVector.x, regionVector.y, regionVector.z, regionVector.w);
  114. Vector4 color(1, 1, 1, 1);
  115. if (space->exists("color"))
  116. {
  117. space->getColor("color", &color);
  118. }
  119. Skin* skin = Skin::create(space->getId(), tw, th, region, border, color);
  120. GP_ASSERT(skin);
  121. theme->_skins.push_back(skin);
  122. }
  123. space = themeProperties->getNextNamespace();
  124. }
  125. themeProperties->rewind();
  126. space = themeProperties->getNextNamespace();
  127. while (space != NULL)
  128. {
  129. const char* spacename = space->getNamespace();
  130. if (strcmp(spacename, "style") == 0)
  131. {
  132. // Each style contains up to MAX_OVERLAYS overlays,
  133. // as well as Border and Padding namespaces.
  134. Theme::Margin margin;
  135. Theme::Padding padding;
  136. Theme::Style::Overlay* normal = NULL;
  137. Theme::Style::Overlay* focus = NULL;
  138. Theme::Style::Overlay* active = NULL;
  139. Theme::Style::Overlay* disabled = NULL;
  140. // Need to load OVERLAY_NORMAL first so that the other overlays can inherit from it.
  141. Properties* innerSpace = space->getNextNamespace();
  142. while (innerSpace != NULL)
  143. {
  144. const char* innerSpacename = innerSpace->getNamespace();
  145. if (strcmp(innerSpacename, "stateNormal") == 0)
  146. {
  147. Vector4 textColor(0, 0, 0, 1);
  148. if (innerSpace->exists("textColor"))
  149. {
  150. innerSpace->getColor("textColor", &textColor);
  151. }
  152. const char* fontPath = innerSpace->getString("font");
  153. Font* font = NULL;
  154. if (fontPath)
  155. {
  156. font = Font::create(fontPath);
  157. }
  158. unsigned int fontSize = innerSpace->getInt("fontSize");
  159. const char* textAlignmentString = innerSpace->getString("textAlignment");
  160. Font::Justify textAlignment = Font::ALIGN_TOP_LEFT;
  161. if (textAlignmentString)
  162. {
  163. textAlignment = Font::getJustify(textAlignmentString);
  164. }
  165. bool rightToLeft = innerSpace->getBool("rightToLeft");
  166. float opacity = 1.0f;
  167. if (innerSpace->exists("opacity"))
  168. {
  169. opacity = innerSpace->getFloat("opacity");
  170. }
  171. ImageList* imageList = NULL;
  172. ThemeImage* cursor = NULL;
  173. Skin* skin = NULL;
  174. theme->lookUpSprites(innerSpace, &imageList, &cursor, &skin);
  175. normal = Theme::Style::Overlay::create();
  176. GP_ASSERT(normal);
  177. normal->setSkin(skin);
  178. normal->setCursor(cursor);
  179. normal->setImageList(imageList);
  180. normal->setTextColor(textColor);
  181. normal->setFont(font);
  182. normal->setFontSize(fontSize);
  183. normal->setTextAlignment(textAlignment);
  184. normal->setTextRightToLeft(rightToLeft);
  185. normal->setOpacity(opacity);
  186. if (font)
  187. {
  188. theme->_fonts.insert(font);
  189. font->release();
  190. }
  191. // Done with this pass.
  192. break;
  193. }
  194. innerSpace = space->getNextNamespace();
  195. }
  196. // At least the OVERLAY_NORMAL is required.
  197. if (!normal)
  198. GP_ERROR("All themes require the normal state overlay to be defined.");
  199. space->rewind();
  200. innerSpace = space->getNextNamespace();
  201. while (innerSpace != NULL)
  202. {
  203. const char* innerSpacename = innerSpace->getNamespace();
  204. if (strcmp(innerSpacename, "margin") == 0)
  205. {
  206. margin.top = innerSpace->getFloat("top");
  207. margin.bottom = innerSpace->getFloat("bottom");
  208. margin.left = innerSpace->getFloat("left");
  209. margin.right = innerSpace->getFloat("right");
  210. }
  211. else if (strcmp(innerSpacename, "padding") == 0)
  212. {
  213. padding.top = innerSpace->getFloat("top");
  214. padding.bottom = innerSpace->getFloat("bottom");
  215. padding.left = innerSpace->getFloat("left");
  216. padding.right = innerSpace->getFloat("right");
  217. }
  218. else if (strcmp(innerSpacename, "stateNormal") != 0)
  219. {
  220. // Either OVERLAY_FOCUS or OVERLAY_ACTIVE.
  221. // If a property isn't specified, it inherits from OVERLAY_NORMAL.
  222. Vector4 textColor;
  223. if (!innerSpace->getColor("textColor", &textColor))
  224. {
  225. textColor.set(normal->getTextColor());
  226. }
  227. const char* fontPath = innerSpace->getString("font");
  228. Font* font = NULL;
  229. if (fontPath)
  230. {
  231. font = Font::create(fontPath);
  232. }
  233. if (!font)
  234. {
  235. font = normal->getFont();
  236. if (font)
  237. font->addRef();
  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. {
  307. theme->_fonts.insert(font);
  308. font->release();
  309. }
  310. }
  311. else if (strcmp(innerSpacename, "stateActive") == 0)
  312. {
  313. active = Theme::Style::Overlay::create();
  314. GP_ASSERT(active);
  315. active->setSkin(skin);
  316. active->setCursor(cursor);
  317. active->setImageList(imageList);
  318. active->setTextColor(textColor);
  319. active->setFont(font);
  320. active->setFontSize(fontSize);
  321. active->setTextAlignment(textAlignment);
  322. active->setTextRightToLeft(rightToLeft);
  323. active->setOpacity(opacity);
  324. if (font)
  325. {
  326. theme->_fonts.insert(font);
  327. font->release();
  328. }
  329. }
  330. else if (strcmp(innerSpacename, "stateDisabled") == 0)
  331. {
  332. disabled = Theme::Style::Overlay::create();
  333. GP_ASSERT(disabled);
  334. disabled->setSkin(skin);
  335. disabled->setCursor(cursor);
  336. disabled->setImageList(imageList);
  337. disabled->setTextColor(textColor);
  338. disabled->setFont(font);
  339. disabled->setFontSize(fontSize);
  340. disabled->setTextAlignment(textAlignment);
  341. disabled->setTextRightToLeft(rightToLeft);
  342. disabled->setOpacity(opacity);
  343. if (font)
  344. {
  345. theme->_fonts.insert(font);
  346. font->release();
  347. }
  348. }
  349. }
  350. innerSpace = space->getNextNamespace();
  351. }
  352. if (!focus)
  353. {
  354. focus = normal;
  355. focus->addRef();
  356. }
  357. if (!active)
  358. {
  359. active = normal;
  360. active->addRef();
  361. }
  362. if (!disabled)
  363. {
  364. disabled = normal;
  365. disabled->addRef();
  366. }
  367. Theme::Style* s = new Theme::Style(theme, space->getId(), tw, th, margin, padding, normal, focus, active, disabled);
  368. GP_ASSERT(s);
  369. theme->_styles.push_back(s);
  370. }
  371. space = themeProperties->getNextNamespace();
  372. }
  373. // Add this theme to the cache.
  374. __themeCache.push_back(theme);
  375. SAFE_DELETE(properties);
  376. return theme;
  377. }
  378. Theme::Style* Theme::getStyle(const char* name) const
  379. {
  380. GP_ASSERT(name);
  381. for (size_t i = 0, count = _styles.size(); i < count; ++i)
  382. {
  383. GP_ASSERT(_styles[i]);
  384. if (strcmp(name, _styles[i]->getId()) == 0)
  385. {
  386. return _styles[i];
  387. }
  388. }
  389. return NULL;
  390. }
  391. Theme::Style* Theme::getEmptyStyle()
  392. {
  393. Theme::Style* emptyStyle = getStyle("EMPTY_STYLE");
  394. if (!emptyStyle)
  395. {
  396. Theme::Style::Overlay* overlay = Theme::Style::Overlay::create();
  397. overlay->addRef();
  398. overlay->addRef();
  399. overlay->addRef();
  400. emptyStyle = new Theme::Style(const_cast<Theme*>(this), "EMPTY_STYLE", 1.0f / _texture->getWidth(), 1.0f / _texture->getHeight(),
  401. Theme::Margin::empty(), Theme::Border::empty(), overlay, overlay, overlay, overlay);
  402. _styles.push_back(emptyStyle);
  403. }
  404. return emptyStyle;
  405. }
  406. void Theme::setProjectionMatrix(const Matrix& matrix)
  407. {
  408. GP_ASSERT(_spriteBatch);
  409. _spriteBatch->setProjectionMatrix(matrix);
  410. // Set the matrix on each Font used by the style.
  411. std::set<Font*>::const_iterator it;
  412. for (it = _fonts.begin(); it != _fonts.end(); ++it)
  413. {
  414. Font* font = *it;
  415. GP_ASSERT(font);
  416. GP_ASSERT(font->getSpriteBatch());
  417. font->getSpriteBatch()->setProjectionMatrix(matrix);
  418. }
  419. }
  420. SpriteBatch* Theme::getSpriteBatch() const
  421. {
  422. return _spriteBatch;
  423. }
  424. /**************
  425. * Theme::UVs *
  426. **************/
  427. Theme::UVs::UVs()
  428. : u1(0), v1(0), u2(0), v2(0)
  429. {
  430. }
  431. Theme::UVs::UVs(float u1, float v1, float u2, float v2)
  432. : u1(u1), v1(v1), u2(u2), v2(v2)
  433. {
  434. }
  435. const Theme::UVs& Theme::UVs::empty()
  436. {
  437. static UVs empty(0, 0, 0, 0);
  438. return empty;
  439. }
  440. const Theme::UVs& Theme::UVs::full()
  441. {
  442. static UVs full(0, 1, 1, 0);
  443. return full;
  444. }
  445. /**********************
  446. * Theme::SideRegions *
  447. **********************/
  448. const Theme::SideRegions& Theme::SideRegions::empty()
  449. {
  450. static SideRegions empty;
  451. return empty;
  452. }
  453. /*********************
  454. * Theme::ThemeImage *
  455. *********************/
  456. Theme::ThemeImage::ThemeImage(float tw, float th, const Rectangle& region, const Vector4& color)
  457. : _region(region), _color(color)
  458. {
  459. generateUVs(tw, th, region.x, region.y, region.width, region.height, &_uvs);
  460. }
  461. Theme::ThemeImage::~ThemeImage()
  462. {
  463. }
  464. Theme::ThemeImage* Theme::ThemeImage::create(float tw, float th, Properties* properties, const Vector4& defaultColor)
  465. {
  466. GP_ASSERT(properties);
  467. Vector4 regionVector;
  468. properties->getVector4("region", &regionVector);
  469. const Rectangle region(regionVector.x, regionVector.y, regionVector.z, regionVector.w);
  470. Vector4 color;
  471. if (properties->exists("color"))
  472. {
  473. properties->getColor("color", &color);
  474. }
  475. else
  476. {
  477. color.set(defaultColor);
  478. }
  479. ThemeImage* image = new ThemeImage(tw, th, region, color);
  480. const char* id = properties->getId();
  481. if (id)
  482. {
  483. image->_id = id;
  484. }
  485. return image;
  486. }
  487. const char* Theme::ThemeImage::getId() const
  488. {
  489. return _id.c_str();
  490. }
  491. const Theme::UVs& Theme::ThemeImage::getUVs() const
  492. {
  493. return _uvs;
  494. }
  495. const Rectangle& Theme::ThemeImage::getRegion() const
  496. {
  497. return _region;
  498. }
  499. const Vector4& Theme::ThemeImage::getColor() const
  500. {
  501. return _color;
  502. }
  503. /********************
  504. * Theme::ImageList *
  505. ********************/
  506. Theme::ImageList::ImageList(const Vector4& color) : _color(color)
  507. {
  508. }
  509. Theme::ImageList::ImageList(const ImageList& copy)
  510. : _id(copy._id), _color(copy._color)
  511. {
  512. std::vector<ThemeImage*>::const_iterator it;
  513. for (it = copy._images.begin(); it != copy._images.end(); ++it)
  514. {
  515. ThemeImage* image = *it;
  516. GP_ASSERT(image);
  517. _images.push_back(new ThemeImage(*image));
  518. }
  519. }
  520. Theme::ImageList::~ImageList()
  521. {
  522. std::vector<ThemeImage*>::const_iterator it;
  523. for (it = _images.begin(); it != _images.end(); ++it)
  524. {
  525. ThemeImage* image = *it;
  526. SAFE_RELEASE(image);
  527. }
  528. }
  529. Theme::ImageList* Theme::ImageList::create(float tw, float th, Properties* properties)
  530. {
  531. GP_ASSERT(properties);
  532. Vector4 color(1, 1, 1, 1);
  533. if (properties->exists("color"))
  534. {
  535. properties->getColor("color", &color);
  536. }
  537. ImageList* imageList = new ImageList(color);
  538. const char* id = properties->getId();
  539. if (id)
  540. {
  541. imageList->_id = id;
  542. }
  543. Properties* space = properties->getNextNamespace();
  544. while (space != NULL)
  545. {
  546. ThemeImage* image = ThemeImage::create(tw, th, space, color);
  547. GP_ASSERT(image);
  548. imageList->_images.push_back(image);
  549. space = properties->getNextNamespace();
  550. }
  551. return imageList;
  552. }
  553. const char* Theme::ImageList::getId() const
  554. {
  555. return _id.c_str();
  556. }
  557. Theme::ThemeImage* Theme::ImageList::getImage(const char* imageId) const
  558. {
  559. GP_ASSERT(imageId);
  560. std::vector<ThemeImage*>::const_iterator it;
  561. for (it = _images.begin(); it != _images.end(); ++it)
  562. {
  563. ThemeImage* image = *it;
  564. GP_ASSERT(image);
  565. GP_ASSERT(image->getId());
  566. if (strcmp(image->getId(), imageId) == 0)
  567. {
  568. return image;
  569. }
  570. }
  571. return NULL;
  572. }
  573. /***************
  574. * Theme::Skin *
  575. ***************/
  576. Theme::Skin* Theme::Skin::create(const char* id, float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  577. {
  578. Skin* skin = new Skin(tw, th, region, border, color);
  579. if (id)
  580. {
  581. skin->_id = id;
  582. }
  583. return skin;
  584. }
  585. Theme::Skin::Skin(float tw, float th, const Rectangle& region, const Theme::Border& border, const Vector4& color)
  586. : _border(border), _color(color), _region(region)
  587. {
  588. setRegion(region, tw, th);
  589. }
  590. Theme::Skin::~Skin()
  591. {
  592. }
  593. const char* Theme::Skin::getId() const
  594. {
  595. return _id.c_str();
  596. }
  597. const Theme::Border& Theme::Skin::getBorder() const
  598. {
  599. return _border;
  600. }
  601. const Rectangle& Theme::Skin::getRegion() const
  602. {
  603. return _region;
  604. }
  605. void Theme::Skin::setRegion(const Rectangle& region, float tw, float th)
  606. {
  607. // Can calculate all measurements in advance.
  608. float leftEdge = region.x * tw;
  609. float rightEdge = (region.x + region.width) * tw;
  610. float leftBorder = (region.x + _border.left) * tw;
  611. float rightBorder = (region.x + region.width - _border.right) * tw;
  612. float topEdge = 1.0f - (region.y * th);
  613. float bottomEdge = 1.0f - ((region.y + region.height) * th);
  614. float topBorder = 1.0f - ((region.y + _border.top) * th);
  615. float bottomBorder = 1.0f - ((region.y + region.height - _border.bottom) * th);
  616. // There are 9 sets of UVs to set.
  617. _uvs[TOP_LEFT].u1 = leftEdge;
  618. _uvs[TOP_LEFT].v1 = topEdge;
  619. _uvs[TOP_LEFT].u2 = leftBorder;
  620. _uvs[TOP_LEFT].v2 = topBorder;
  621. _uvs[TOP].u1 = leftBorder;
  622. _uvs[TOP].v1 = topEdge;
  623. _uvs[TOP].u2 = rightBorder;
  624. _uvs[TOP].v2 = topBorder;
  625. _uvs[TOP_RIGHT].u1 = rightBorder;
  626. _uvs[TOP_RIGHT].v1 = topEdge;
  627. _uvs[TOP_RIGHT].u2 = rightEdge;
  628. _uvs[TOP_RIGHT].v2 = topBorder;
  629. _uvs[LEFT].u1 = leftEdge;
  630. _uvs[LEFT].v1 = topBorder;
  631. _uvs[LEFT].u2 = leftBorder;
  632. _uvs[LEFT].v2 = bottomBorder;
  633. _uvs[CENTER].u1 = leftBorder;
  634. _uvs[CENTER].v1 = topBorder;
  635. _uvs[CENTER].u2 = rightBorder;
  636. _uvs[CENTER].v2 = bottomBorder;
  637. _uvs[RIGHT].u1 = rightBorder;
  638. _uvs[RIGHT].v1 = topBorder;
  639. _uvs[RIGHT].u2 = rightEdge;
  640. _uvs[RIGHT].v2 = bottomBorder;
  641. _uvs[BOTTOM_LEFT].u1 = leftEdge;
  642. _uvs[BOTTOM_LEFT].v1 = bottomBorder;
  643. _uvs[BOTTOM_LEFT].u2 = leftBorder;
  644. _uvs[BOTTOM_LEFT].v2 = bottomEdge;
  645. _uvs[BOTTOM].u1 = leftBorder;
  646. _uvs[BOTTOM].v1 = bottomBorder;
  647. _uvs[BOTTOM].u2 = rightBorder;
  648. _uvs[BOTTOM].v2 = bottomEdge;
  649. _uvs[BOTTOM_RIGHT].u1 = rightBorder;
  650. _uvs[BOTTOM_RIGHT].v1 = bottomBorder;
  651. _uvs[BOTTOM_RIGHT].u2 = rightEdge;
  652. _uvs[BOTTOM_RIGHT].v2 = bottomEdge;
  653. }
  654. const Theme::UVs& Theme::Skin::getUVs(SkinArea area) const
  655. {
  656. return _uvs[area];
  657. }
  658. const Vector4& Theme::Skin::getColor() const
  659. {
  660. return _color;
  661. }
  662. /**
  663. * Theme utility methods.
  664. */
  665. void Theme::generateUVs(float tw, float th, float x, float y, float width, float height, UVs* uvs)
  666. {
  667. GP_ASSERT(uvs);
  668. uvs->u1 = x * tw;
  669. uvs->u2 = (x + width) * tw;
  670. uvs->v1 = 1.0f - (y * th);
  671. uvs->v2 = 1.0f - ((y + height) * th);
  672. }
  673. void Theme::lookUpSprites(const Properties* overlaySpace, ImageList** imageList, ThemeImage** cursor, Skin** skin)
  674. {
  675. GP_ASSERT(overlaySpace);
  676. const char* imageListString = overlaySpace->getString("imageList");
  677. if (imageListString)
  678. {
  679. for (unsigned int i = 0; i < _imageLists.size(); ++i)
  680. {
  681. GP_ASSERT(_imageLists[i]);
  682. GP_ASSERT(_imageLists[i]->getId());
  683. if (strcmp(_imageLists[i]->getId(), imageListString) == 0)
  684. {
  685. GP_ASSERT(imageList);
  686. *imageList = _imageLists[i];
  687. break;
  688. }
  689. }
  690. }
  691. const char* cursorString = overlaySpace->getString("cursor");
  692. if (cursorString)
  693. {
  694. for (unsigned int i = 0; i < _images.size(); ++i)
  695. {
  696. GP_ASSERT(_images[i]);
  697. GP_ASSERT(_images[i]->getId());
  698. if (strcmp(_images[i]->getId(), cursorString) == 0)
  699. {
  700. GP_ASSERT(cursor);
  701. *cursor = _images[i];
  702. break;
  703. }
  704. }
  705. }
  706. const char* skinString = overlaySpace->getString("skin");
  707. if (skinString)
  708. {
  709. for (unsigned int i = 0; i < _skins.size(); ++i)
  710. {
  711. GP_ASSERT(_skins[i]);
  712. GP_ASSERT(_skins[i]->getId());
  713. if (strcmp(_skins[i]->getId(), skinString) == 0)
  714. {
  715. GP_ASSERT(skin);
  716. *skin = _skins[i];
  717. break;
  718. }
  719. }
  720. }
  721. }
  722. }