Theme.cpp 25 KB

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