UIImage.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. 0
  42. };
  43. UIImage::UIImage(Context* context) :
  44. Drawable2D(context),
  45. color_(Color::WHITE),
  46. drawMode_(UIIDM_SIMPLE),
  47. xSliceSize_(4),
  48. ySliceSize_(4)
  49. {
  50. }
  51. UIImage::~UIImage()
  52. {
  53. }
  54. void UIImage::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<UIImage>(UIX_CATEGORY);
  57. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  58. MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_FILE);
  60. ENUM_ACCESSOR_ATTRIBUTE("Draw Mode", GetDrawMode, SetDrawMode, UIImageDrawMode, uiImageDrawModes, UIIDM_SIMPLE, AM_FILE);
  61. ACCESSOR_ATTRIBUTE("X Slice Size", GetXSliceSize, SetXSliceSize, int, 4, AM_FILE);
  62. ACCESSOR_ATTRIBUTE("Y Slice Size", GetYSliceSize, SetYSliceSize, int, 4, AM_FILE);
  63. COPY_BASE_ATTRIBUTES(Drawable2D);
  64. }
  65. void UIImage::SetSprite(Sprite2D* sprite)
  66. {
  67. if (sprite == sprite_)
  68. return;
  69. sprite_ = sprite;
  70. SetTexture(sprite_ ? sprite_->GetTexture() : 0);
  71. }
  72. void UIImage::SetColor(const Color& color)
  73. {
  74. if (color == color_)
  75. return;
  76. color_ = color;
  77. verticesDirty_ = true;
  78. }
  79. void UIImage::SetDrawMode(UIImageDrawMode mode)
  80. {
  81. if (mode == drawMode_)
  82. return;
  83. drawMode_ = mode;
  84. verticesDirty_ = true;
  85. }
  86. void UIImage::SetXSliceSize(int size)
  87. {
  88. if (size < 0 || size == xSliceSize_)
  89. return;
  90. xSliceSize_ = size;
  91. if (drawMode_ == UIIDM_SLICED)
  92. verticesDirty_ = true;
  93. }
  94. void UIImage::SetYSliceSize(int size)
  95. {
  96. if (size < 0 || size == ySliceSize_)
  97. return;
  98. ySliceSize_ = size;
  99. if (drawMode_ == UIIDM_SLICED)
  100. verticesDirty_ = true;
  101. }
  102. Sprite2D* UIImage::GetSprite() const
  103. {
  104. return sprite_;
  105. }
  106. void UIImage::SetSpriteAttr(const ResourceRef& value)
  107. {
  108. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  109. if (sprite)
  110. SetSprite(sprite);
  111. }
  112. ResourceRef UIImage::GetSpriteAttr() const
  113. {
  114. return Sprite2D::SaveToResourceRef(sprite_);
  115. }
  116. void UIImage::OnNodeSet(Node* node)
  117. {
  118. Drawable2D::OnNodeSet(node);
  119. if (node)
  120. {
  121. uiRect_ = node->GetComponent<UIRect>();
  122. if (uiRect_)
  123. SubscribeToEvent(uiRect_, E_UIRECTDIRTIED, HANDLER(UIImage, HandleRectDirtied));
  124. else
  125. LOGERROR("UIRect must by added first");
  126. }
  127. }
  128. void UIImage::OnWorldBoundingBoxUpdate()
  129. {
  130. boundingBox_.Clear();
  131. if (uiRect_)
  132. {
  133. const Rect& rect = uiRect_->GetRect();
  134. boundingBox_.Merge(rect.min_);
  135. boundingBox_.Merge(rect.max_);
  136. }
  137. worldBoundingBox_ = boundingBox_;
  138. }
  139. void UIImage::UpdateVertices()
  140. {
  141. if (!verticesDirty_)
  142. return;
  143. vertices_.Clear();
  144. if (!uiRect_)
  145. return;
  146. if (!texture_ || !sprite_)
  147. return;
  148. const IntRect& rect = sprite_->GetRectangle();
  149. if (rect.Width() == 0 || rect.Height() == 0)
  150. return;
  151. switch (drawMode_)
  152. {
  153. case UIIDM_SIMPLE:
  154. UpdateVerticesSimpleMode();
  155. break;
  156. case UIIDM_TILED:
  157. UpdateVerticesTiledMode();
  158. break;
  159. case UIIDM_SLICED:
  160. UpdateVerticesSlicedMode();
  161. break;
  162. }
  163. verticesDirty_ = false;
  164. }
  165. void UIImage::UpdateVerticesSimpleMode()
  166. {
  167. float x = uiRect_->GetX();
  168. float y = uiRect_->GetY();
  169. const IntRect& intRect = sprite_->GetRectangle();
  170. float halfWidth = (float)intRect.Width() * PIXEL_SIZE * 0.5f;
  171. float halfHeight = (float)intRect.Height() * PIXEL_SIZE * 0.5f;
  172. float left = x - halfWidth;
  173. float right = x + halfWidth;
  174. float top = y + halfHeight;
  175. float bottom = y - halfHeight;
  176. float uLeft, uRight, vTop, vBottom;
  177. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  178. AddQuad(left, right, top, bottom, uLeft, uRight, vTop, vBottom);
  179. }
  180. void UIImage::UpdateVerticesTiledMode()
  181. {
  182. const Rect& rect = uiRect_->GetRect();
  183. float xStart = rect.min_.x_;
  184. float xEnd = rect.max_.x_;
  185. float yStart = rect.min_.y_;
  186. float yEnd = rect.max_.y_;
  187. const IntRect& intRect = sprite_->GetRectangle();
  188. float tileWidth = intRect.Width() * PIXEL_SIZE;
  189. float tileHeight = intRect.Height() * PIXEL_SIZE;
  190. float uLeft, uRight, vTop, vBottom;
  191. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  192. for (float left = xStart; left < xEnd; left += tileWidth)
  193. {
  194. for (float top = yEnd; top > yStart; top -= tileHeight)
  195. {
  196. float right = left + tileWidth;
  197. float bottom = top - tileHeight;
  198. float uRight2 = uRight;
  199. float vBottom2 = vBottom;
  200. if (right > xEnd)
  201. {
  202. right = xEnd;
  203. uRight2 = uLeft + (right - left) / tileWidth * (uRight - uLeft);
  204. }
  205. if (bottom < yStart)
  206. {
  207. bottom = yStart;
  208. vBottom2 = vTop - (top - bottom) / tileHeight * (vTop - vBottom);
  209. }
  210. AddQuad(left, right, top, bottom, uLeft, uRight2, vTop, vBottom2);
  211. }
  212. }
  213. }
  214. void UIImage::UpdateVerticesSlicedMode()
  215. {
  216. if (xSliceSize_ == 0 && ySliceSize_ == 0)
  217. {
  218. UpdateVerticesSimpleMode();
  219. return;
  220. }
  221. const IntRect& intRect = sprite_->GetRectangle();
  222. int xSliceSize = Min(xSliceSize_, intRect.Width() / 2);
  223. int ySliceSize = Min(ySliceSize_, intRect.Height() / 2);
  224. float left = uiRect_->GetLeft();
  225. float right = uiRect_->GetRight();
  226. float top = uiRect_->GetTop();
  227. float bottom = uiRect_->GetBottom();
  228. float xDelta = (float)xSliceSize * PIXEL_SIZE;
  229. float yDelta = (float)ySliceSize * PIXEL_SIZE;
  230. float uLeft, uRight, vTop, vBottom;
  231. GetSpriteTextureCoords(uLeft, uRight, vTop, vBottom);
  232. float uDelta = (float)xSliceSize / (float)texture_->GetWidth();
  233. float vDelta = (float)ySliceSize / (float)texture_->GetHeight();
  234. float x1 = left + xDelta;
  235. float x2 = right - xDelta;
  236. float y1 = top - yDelta;
  237. float y2 = bottom + yDelta;
  238. float u1 = uLeft + uDelta;
  239. float u2 = uRight - uDelta;
  240. float v1 = vTop + vDelta;
  241. float v2 = vBottom - vDelta;
  242. AddQuad(left, x1 , top, y1, uLeft, u1 , vTop, v1);
  243. AddQuad(x1 , x2 , top, y1, u1 , u2 , vTop, v1);
  244. AddQuad(x2 , right, top, y1, u2 , uRight, vTop, v1);
  245. AddQuad(left, x1 , y1, y2, uLeft, u1 , v1, v2);
  246. AddQuad(x1 , x2 , y1, y2, u1 , u2 , v1, v2);
  247. AddQuad(x2 , right, y1, y2, u2 , uRight, v1, v2);
  248. AddQuad(left, x1 , y2, bottom, uLeft, u1 , v2, vBottom);
  249. AddQuad(x1 , x2 , y2, bottom, u1 , u2 , v2, vBottom);
  250. AddQuad(x2 , right, y2, bottom, u2 , uRight, v2, vBottom);
  251. }
  252. void UIImage::GetSpriteTextureCoords(float& left, float& right, float& top, float& bottom) const
  253. {
  254. Texture2D* texture = GetTexture();
  255. float invTexW = 1.0f / (float)texture->GetWidth();
  256. float invTexH = 1.0f / (float)texture->GetHeight();
  257. const IntRect& intRect = sprite_->GetRectangle();
  258. left = (float)intRect.left_ * invTexW;
  259. right = (float)intRect.right_ * invTexW;
  260. top = (float)intRect.top_ * invTexH;
  261. bottom = (float)intRect.bottom_ * invTexH;
  262. }
  263. void UIImage::AddQuad(float left, float right, float top, float bottom, float uLeft, float uRight, float vTop, float vBottom)
  264. {
  265. if (right - left <= 0.005f || top - bottom <= 0.005f)
  266. return;
  267. /*
  268. V1---------V2
  269. | / |
  270. | / |
  271. | / |
  272. | / |
  273. | / |
  274. V0---------V3
  275. */
  276. Vertex2D vertex0;
  277. Vertex2D vertex1;
  278. Vertex2D vertex2;
  279. Vertex2D vertex3;
  280. vertex0.position_ = Vector3(left, bottom);
  281. vertex1.position_ = Vector3(left, top);
  282. vertex2.position_ = Vector3(right, top);
  283. vertex3.position_ = Vector3(right, bottom);
  284. vertex0.uv_ = Vector2(uLeft, vBottom);
  285. vertex1.uv_ = Vector2(uLeft, vTop);
  286. vertex2.uv_ = Vector2(uRight, vTop);
  287. vertex3.uv_ = Vector2(uRight, vBottom);
  288. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  289. vertices_.Push(vertex0);
  290. vertices_.Push(vertex1);
  291. vertices_.Push(vertex2);
  292. vertices_.Push(vertex3);
  293. }
  294. void UIImage::HandleRectDirtied(StringHash eventType, VariantMap& eventData)
  295. {
  296. worldBoundingBoxDirty_ = true;
  297. verticesDirty_ = true;
  298. }
  299. }