Theme.cpp 27 KB

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