UIImage.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "DebugRenderer.h"
  25. #include "Log.h"
  26. #include "Node.h"
  27. #include "Sprite2D.h"
  28. #include "Texture2D.h"
  29. #include "UIImage.h"
  30. #include "UIRect.h"
  31. #include "UIXEvents.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. extern const char* UIX_CATEGORY;
  36. static const char* uiImageDrawModes[] =
  37. {
  38. "Simple",
  39. "Tiled",
  40. "Sliced",
  41. "Filled",
  42. 0
  43. };
  44. static const char* uiFillTypes[] =
  45. {
  46. "Horizontal",
  47. "Vertical",
  48. "Radial",
  49. 0
  50. };
  51. UIImage::UIImage(Context* context) :
  52. Drawable2D(context),
  53. color_(Color::WHITE),
  54. drawMode_(UIDM_SIMPLE),
  55. horizontalSliceSize_(0),
  56. verticalSliceSize_(0),
  57. fillType_(UIFT_HORIZONTAL),
  58. fillAmount_(1.0f),
  59. fillInverse_(false)
  60. {
  61. }
  62. UIImage::~UIImage()
  63. {
  64. }
  65. void UIImage::RegisterObject(Context* context)
  66. {
  67. context->RegisterFactory<UIImage>(UIX_CATEGORY);
  68. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  69. MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  70. ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_FILE);
  71. ENUM_ACCESSOR_ATTRIBUTE("Draw Mode", GetDrawMode, SetDrawMode, UIDrawMode, uiImageDrawModes, UIDM_SIMPLE, AM_FILE);
  72. ACCESSOR_ATTRIBUTE("Horizontal Slice Size", GetHorizontalSliceSize, SetHorizontalSliceSize, int, 0, AM_FILE);
  73. ACCESSOR_ATTRIBUTE("Vertical Slice Size", GetVerticalSliceSize, SetVerticalSliceSize, int, 0, AM_FILE);
  74. ENUM_ACCESSOR_ATTRIBUTE("Fill Type", GetFillType, SetFillType, UIFillType, uiFillTypes, UIFT_HORIZONTAL, AM_FILE);
  75. ACCESSOR_ATTRIBUTE("Fill Amount", GetFillAmount, SetFillAmount, float, 1.0f, AM_FILE);
  76. ACCESSOR_ATTRIBUTE("Fill Inverse", IsFillInverse, SetFillInverse, bool, false, AM_FILE);
  77. COPY_BASE_ATTRIBUTES(Drawable2D);
  78. }
  79. void UIImage::SetSprite(Sprite2D* sprite)
  80. {
  81. if (sprite == sprite_)
  82. return;
  83. sprite_ = sprite;
  84. SetTexture(sprite_ ? sprite_->GetTexture() : 0);
  85. }
  86. void UIImage::SetColor(const Color& color)
  87. {
  88. if (color == color_)
  89. return;
  90. color_ = color;
  91. verticesDirty_ = true;
  92. }
  93. void UIImage::SetDrawMode(UIDrawMode mode)
  94. {
  95. if (mode == drawMode_)
  96. return;
  97. drawMode_ = mode;
  98. verticesDirty_ = true;
  99. }
  100. void UIImage::SetHorizontalSliceSize(int size)
  101. {
  102. if (size < 0 || size == horizontalSliceSize_)
  103. return;
  104. horizontalSliceSize_ = size;
  105. if (drawMode_ == UIDM_SLICED)
  106. verticesDirty_ = true;
  107. }
  108. void UIImage::SetVerticalSliceSize(int size)
  109. {
  110. if (size < 0 || size == verticalSliceSize_)
  111. return;
  112. verticalSliceSize_ = size;
  113. if (drawMode_ == UIDM_SLICED)
  114. verticesDirty_ = true;
  115. }
  116. void UIImage::SetFillType(UIFillType fillType)
  117. {
  118. if (fillType == fillType_)
  119. return;
  120. fillType_ = fillType;
  121. if (drawMode_ == UIDM_FILLED)
  122. verticesDirty_ = true;
  123. }
  124. void UIImage::SetFillAmount(float amount)
  125. {
  126. amount = Clamp(amount, 0.0f, 1.0f);
  127. if (amount == fillAmount_)
  128. return;
  129. fillAmount_ = amount;
  130. if (drawMode_ == UIDM_FILLED)
  131. verticesDirty_ = true;
  132. }
  133. void UIImage::SetFillInverse(bool inverse)
  134. {
  135. if (inverse == fillInverse_)
  136. return;
  137. fillInverse_ = inverse;
  138. if (drawMode_ == UIDM_FILLED)
  139. verticesDirty_ = true;
  140. }
  141. Sprite2D* UIImage::GetSprite() const
  142. {
  143. return sprite_;
  144. }
  145. void UIImage::SetSpriteAttr(const ResourceRef& value)
  146. {
  147. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  148. if (sprite)
  149. SetSprite(sprite);
  150. }
  151. ResourceRef UIImage::GetSpriteAttr() const
  152. {
  153. return Sprite2D::SaveToResourceRef(sprite_);
  154. }
  155. void UIImage::OnNodeSet(Node* node)
  156. {
  157. Drawable2D::OnNodeSet(node);
  158. if (node)
  159. {
  160. uiRect_ = node->GetComponent<UIRect>();
  161. if (uiRect_)
  162. SubscribeToEvent(uiRect_, E_UIRECTDIRTIED, HANDLER(UIImage, HandleRectDirtied));
  163. else
  164. LOGERROR("UIRect must by added first");
  165. }
  166. }
  167. void UIImage::OnWorldBoundingBoxUpdate()
  168. {
  169. boundingBox_.Clear();
  170. if (uiRect_)
  171. {
  172. const Rect& rect = uiRect_->GetRect();
  173. boundingBox_.Merge(rect.min_);
  174. boundingBox_.Merge(rect.max_);
  175. }
  176. worldBoundingBox_ = boundingBox_;
  177. }
  178. void UIImage::UpdateVertices()
  179. {
  180. if (!verticesDirty_)
  181. return;
  182. vertices_.Clear();
  183. if (!uiRect_)
  184. return;
  185. if (!texture_ || !sprite_)
  186. return;
  187. const IntRect& rect = sprite_->GetRectangle();
  188. if (rect.Width() == 0 || rect.Height() == 0)
  189. return;
  190. switch (drawMode_)
  191. {
  192. case UIDM_SIMPLE:
  193. UpdateVerticesSimpleMode();
  194. break;
  195. case UIDM_TILED:
  196. UpdateVerticesTiledMode();
  197. break;
  198. case UIDM_SLICED:
  199. UpdateVerticesSlicedMode();
  200. break;
  201. case UIDM_FILLED:
  202. UpdateVerticesFilledMode();
  203. break;
  204. }
  205. verticesDirty_ = false;
  206. }
  207. void UIImage::UpdateVerticesSimpleMode()
  208. {
  209. float x = uiRect_->GetX();
  210. float y = uiRect_->GetY();
  211. const IntRect& intRect = sprite_->GetRectangle();
  212. float halfWidth = (float)intRect.Width() * PIXEL_SIZE * 0.5f;
  213. float halfHeight = (float)intRect.Height() * PIXEL_SIZE * 0.5f;
  214. float left = x - halfWidth;
  215. float right = x + halfWidth;
  216. float top = y + halfHeight;
  217. float bottom = y - halfHeight;
  218. float uLeft, uRight, vTop, vBottom;
  219. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  220. AddQuad(left, right, top, bottom, uLeft, uRight, vTop, vBottom);
  221. }
  222. void UIImage::UpdateVerticesTiledMode()
  223. {
  224. const Rect& rect = uiRect_->GetRect();
  225. float xStart = rect.min_.x_;
  226. float xEnd = rect.max_.x_;
  227. float yStart = rect.min_.y_;
  228. float yEnd = rect.max_.y_;
  229. const IntRect& intRect = sprite_->GetRectangle();
  230. float tileWidth = intRect.Width() * PIXEL_SIZE;
  231. float tileHeight = intRect.Height() * PIXEL_SIZE;
  232. float uLeft, uRight, vTop, vBottom;
  233. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  234. for (float left = xStart; left < xEnd; left += tileWidth)
  235. {
  236. for (float top = yEnd; top > yStart; top -= tileHeight)
  237. {
  238. float right = left + tileWidth;
  239. float bottom = top - tileHeight;
  240. float uRight2 = uRight;
  241. float vBottom2 = vBottom;
  242. if (right > xEnd)
  243. {
  244. right = xEnd;
  245. uRight2 = uLeft + (right - left) / tileWidth * (uRight - uLeft);
  246. }
  247. if (bottom < yStart)
  248. {
  249. bottom = yStart;
  250. vBottom2 = vTop - (top - bottom) / tileHeight * (vTop - vBottom);
  251. }
  252. AddQuad(left, right, top, bottom, uLeft, uRight2, vTop, vBottom2);
  253. }
  254. }
  255. }
  256. void UIImage::UpdateVerticesSlicedMode()
  257. {
  258. if (horizontalSliceSize_ == 0 && verticalSliceSize_ == 0)
  259. {
  260. float left = uiRect_->GetLeft();
  261. float right = uiRect_->GetRight();
  262. float top = uiRect_->GetTop();
  263. float bottom = uiRect_->GetBottom();
  264. float uLeft, uRight, vTop, vBottom;
  265. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  266. AddQuad(left, right, top, bottom, uLeft, uRight, vTop, vBottom);
  267. return;
  268. }
  269. const IntRect& intRect = sprite_->GetRectangle();
  270. int xSliceSize = Min(horizontalSliceSize_, intRect.Width() / 2);
  271. int ySliceSize = Min(verticalSliceSize_, intRect.Height() / 2);
  272. float left = uiRect_->GetLeft();
  273. float right = uiRect_->GetRight();
  274. float top = uiRect_->GetTop();
  275. float bottom = uiRect_->GetBottom();
  276. float xDelta = (float)xSliceSize * PIXEL_SIZE;
  277. float yDelta = (float)ySliceSize * PIXEL_SIZE;
  278. float uLeft, uRight, vTop, vBottom;
  279. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  280. float uDelta = (float)xSliceSize / (float)texture_->GetWidth();
  281. float vDelta = (float)ySliceSize / (float)texture_->GetHeight();
  282. float x1 = left + xDelta;
  283. float x2 = right - xDelta;
  284. float y1 = top - yDelta;
  285. float y2 = bottom + yDelta;
  286. float u1 = uLeft + uDelta;
  287. float u2 = uRight - uDelta;
  288. float v1 = vTop + vDelta;
  289. float v2 = vBottom - vDelta;
  290. AddQuad(left, x1 , top, y1, uLeft, u1 , vTop, v1);
  291. AddQuad(x1 , x2 , top, y1, u1 , u2 , vTop, v1);
  292. AddQuad(x2 , right, top, y1, u2 , uRight, vTop, v1);
  293. AddQuad(left, x1 , y1, y2, uLeft, u1 , v1, v2);
  294. AddQuad(x1 , x2 , y1, y2, u1 , u2 , v1, v2);
  295. AddQuad(x2 , right, y1, y2, u2 , uRight, v1, v2);
  296. AddQuad(left, x1 , y2, bottom, uLeft, u1 , v2, vBottom);
  297. AddQuad(x1 , x2 , y2, bottom, u1 , u2 , v2, vBottom);
  298. AddQuad(x2 , right, y2, bottom, u2 , uRight, v2, vBottom);
  299. }
  300. void UIImage::UpdateVerticesFilledMode()
  301. {
  302. if (fillType_ != UIFT_RADIAL)
  303. {
  304. float left = uiRect_->GetLeft();
  305. float right = uiRect_->GetRight();
  306. float top = uiRect_->GetTop();
  307. float bottom = uiRect_->GetBottom();
  308. float uLeft, uRight, vTop, vBottom;
  309. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  310. if (fillType_ == UIFT_HORIZONTAL)
  311. {
  312. if (!fillInverse_)
  313. {
  314. right = left + (right - left) * fillAmount_;
  315. uRight = uLeft + (uRight - uLeft) * fillAmount_;
  316. }
  317. else
  318. {
  319. left = right - (right - left) * fillAmount_;
  320. uLeft = uRight - (uRight - uLeft) * fillAmount_;
  321. }
  322. }
  323. else
  324. {
  325. if (!fillInverse_)
  326. {
  327. top = bottom + (top - bottom) * fillAmount_;
  328. vTop = vBottom + (vTop - vBottom) * fillAmount_;
  329. }
  330. else
  331. {
  332. bottom = top - (top - bottom) * fillAmount_;
  333. vBottom = vTop - (vTop - vBottom) * fillAmount_;
  334. }
  335. }
  336. AddQuad(left, right, top, bottom, uLeft, uRight, vTop, vBottom);
  337. }
  338. else
  339. UpdateVerticesFilledModeRadial();
  340. }
  341. void UIImage::UpdateVerticesFilledModeRadial()
  342. {
  343. float angle = 360.0f * fillAmount_;
  344. if (angle < 1.0f)
  345. return;
  346. float x = uiRect_->GetX();
  347. float y = uiRect_->GetY();
  348. float left = uiRect_->GetLeft();
  349. float right = uiRect_->GetRight();
  350. float top = uiRect_->GetTop();
  351. float bottom = uiRect_->GetBottom();
  352. unsigned color = color_.ToUInt();
  353. float uLeft, uRight, vTop, vBottom;
  354. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  355. float u = (uLeft + uRight) * 0.5f;
  356. float v = (vTop + vBottom) * 0.5f;
  357. // Calculate vertex
  358. float realAngle = fillInverse_ ? -angle : angle;
  359. float s = Sin(realAngle);
  360. float c = Cos(realAngle);
  361. float width = uiRect_->GetWidth();
  362. float height = uiRect_->GetHeight();
  363. float halfWidth = width * 0.5f;
  364. float halfHeight = height * 0.5f;
  365. float dx = -halfHeight * s / Max(Abs(c), M_EPSILON);
  366. dx = Clamp(dx, -halfWidth, halfWidth);
  367. float dy = halfWidth * c / Max(Abs(s), M_EPSILON);
  368. dy = Clamp(dy, -halfHeight, halfHeight);
  369. Vertex2D vertex;
  370. vertex.position_ = Vector3(x + dx, y + dy);
  371. vertex.color_ = color;
  372. vertex.uv_ = Vector2(uLeft + (uRight - uLeft) / width * (vertex.position_.x_ - left), vBottom + (vTop - vBottom) / height * (vertex.position_.y_ - bottom));
  373. // 2----1----5
  374. // | \a1| /|
  375. // | \ | / |
  376. // | 0 |
  377. // | / \ |
  378. // |/ \|
  379. // 3---------4
  380. Vertex2D vertices[6] =
  381. {
  382. { Vector3(x , y ), color, Vector2(u , v ) }, // 0
  383. { Vector3(x , top ), color, Vector2(u , vTop ) }, // 1
  384. { Vector3(left , top ), color, Vector2(uLeft , vTop ) }, // 2
  385. { Vector3(left , bottom), color, Vector2(uLeft , vBottom) }, // 3
  386. { Vector3(right, bottom), color, Vector2(uRight, vBottom) }, // 4
  387. { Vector3(right, top ), color, Vector2(uRight, vTop ) }, // 5
  388. };
  389. // Swap vertices when fillInverse_ is true
  390. if (fillInverse_)
  391. {
  392. Swap(vertices[2], vertices[5]);
  393. Swap(vertices[3], vertices[4]);
  394. }
  395. // Calculate value of a1
  396. float angle1 = Atan2(width, height);
  397. // Begin build quad0
  398. vertices_.Push(vertices[0]);
  399. vertices_.Push(vertices[1]);
  400. if (angle <= angle1)
  401. {
  402. vertices_.Push(vertex);
  403. vertices_.Push(vertex);
  404. // End build quad0 (v0, v1, vertex, vertex)
  405. return;
  406. }
  407. if (angle <= 180.0f - angle1)
  408. {
  409. vertices_.Push(vertices[2]);
  410. vertices_.Push(vertex);
  411. // End build quad0 (v0, v1, v2, vertex)
  412. return;
  413. }
  414. vertices_.Push(vertices[2]);
  415. vertices_.Push(vertices[3]);
  416. // End build quad0 (v0, v1, v2, v3)
  417. // Begin build quad1
  418. vertices_.Push(vertices[0]);
  419. vertices_.Push(vertices[3]);
  420. if (angle <= 180.0f + angle1)
  421. {
  422. vertices_.Push(vertex);
  423. vertices_.Push(vertex);
  424. // End build quad1 (v0, v3, vertex, vertex)
  425. return;
  426. }
  427. vertices_.Push(vertices[4]);
  428. if (angle <= 360.0f - angle1)
  429. {
  430. vertices_.Push(vertex);
  431. // End build quad1 (v0, v3, v4, vertex)
  432. return;
  433. }
  434. vertices_.Push(vertices[5]);
  435. // End build quad1 (v0, v3, v4, v5)
  436. // Begin build quad2
  437. vertices_.Push(vertices[0]);
  438. vertices_.Push(vertices[5]);
  439. vertices_.Push(vertex);
  440. vertices_.Push(vertex);
  441. // End build quad2 (v0, v5, vertex, vertex)
  442. }
  443. void UIImage::GetSpriteTextureCoords(float& left, float& right, float& top, float& bottom) const
  444. {
  445. Texture2D* texture = GetTexture();
  446. float invTexW = 1.0f / (float)texture->GetWidth();
  447. float invTexH = 1.0f / (float)texture->GetHeight();
  448. const IntRect& intRect = sprite_->GetRectangle();
  449. left = (float)intRect.left_ * invTexW;
  450. right = (float)intRect.right_ * invTexW;
  451. top = (float)intRect.top_ * invTexH;
  452. bottom = (float)intRect.bottom_ * invTexH;
  453. }
  454. void UIImage::AddQuad(float left, float right, float top, float bottom, float uLeft, float uRight, float vTop, float vBottom)
  455. {
  456. if (right - left <= 0.005f || top - bottom <= 0.005f)
  457. return;
  458. // V1---------V2
  459. // | / |
  460. // | / |
  461. // | / |
  462. // | / |
  463. // | / |
  464. // V0---------V3
  465. Vertex2D vertex0;
  466. Vertex2D vertex1;
  467. Vertex2D vertex2;
  468. Vertex2D vertex3;
  469. vertex0.position_ = Vector3(left, bottom);
  470. vertex1.position_ = Vector3(left, top);
  471. vertex2.position_ = Vector3(right, top);
  472. vertex3.position_ = Vector3(right, bottom);
  473. vertex0.uv_ = Vector2(uLeft, vBottom);
  474. vertex1.uv_ = Vector2(uLeft, vTop);
  475. vertex2.uv_ = Vector2(uRight, vTop);
  476. vertex3.uv_ = Vector2(uRight, vBottom);
  477. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  478. vertices_.Push(vertex0);
  479. vertices_.Push(vertex1);
  480. vertices_.Push(vertex2);
  481. vertices_.Push(vertex3);
  482. }
  483. void UIImage::HandleRectDirtied(StringHash eventType, VariantMap& eventData)
  484. {
  485. worldBoundingBoxDirty_ = true;
  486. verticesDirty_ = true;
  487. }
  488. }