Theme.cpp 26 KB

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