Drawable2D.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "Camera.h"
  24. #include "Context.h"
  25. #include "Drawable2D.h"
  26. #include "Geometry.h"
  27. #include "Log.h"
  28. #include "Material.h"
  29. #include "Node.h"
  30. #include "Renderer2D.h"
  31. #include "ResourceCache.h"
  32. #include "Scene.h"
  33. #include "Sprite2D.h"
  34. #include "SpriteSheet2D.h"
  35. #include "Technique.h"
  36. #include "Texture2D.h"
  37. #include "VertexBuffer.h"
  38. #include "DebugNew.h"
  39. namespace Urho3D
  40. {
  41. const float PIXEL_SIZE = 0.01f;
  42. extern const char* blendModeNames[];
  43. Drawable2D::Drawable2D(Context* context) :
  44. Drawable(context, DRAWABLE_GEOMETRY),
  45. layer_(0),
  46. orderInLayer_(0),
  47. blendMode_(BLEND_ALPHA),
  48. verticesDirty_(true),
  49. materialUpdatePending_(false),
  50. visibility_(true)
  51. {
  52. }
  53. Drawable2D::~Drawable2D()
  54. {
  55. }
  56. void Drawable2D::RegisterObject(Context* context)
  57. {
  58. ACCESSOR_ATTRIBUTE("Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE("Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  60. ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendModeAttr, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  61. COPY_BASE_ATTRIBUTES(Drawable);
  62. }
  63. void Drawable2D::SetLayer(int layer)
  64. {
  65. if (layer == layer_)
  66. return;
  67. layer_ = layer;
  68. MarkNetworkUpdate();
  69. }
  70. void Drawable2D::SetOrderInLayer(int orderInLayer)
  71. {
  72. if (orderInLayer == orderInLayer_)
  73. return;
  74. orderInLayer_ = orderInLayer;
  75. MarkNetworkUpdate();
  76. }
  77. void Drawable2D::SetTexture(Texture2D* texture)
  78. {
  79. if (texture == texture_)
  80. return;
  81. texture_ = texture;
  82. verticesDirty_ = true;
  83. defaultMaterial_ = 0;
  84. OnMarkedDirty(node_);
  85. MarkNetworkUpdate();
  86. }
  87. void Drawable2D::SetBlendMode(BlendMode blendMode)
  88. {
  89. if (blendMode == blendMode_)
  90. return;
  91. blendMode_ = blendMode;
  92. defaultMaterial_ = 0;
  93. MarkNetworkUpdate();
  94. }
  95. Texture2D* Drawable2D::GetTexture() const
  96. {
  97. return texture_;
  98. }
  99. void Drawable2D::SetDefaultMaterial(Material* material)
  100. {
  101. defaultMaterial_ = material;
  102. }
  103. Material* Drawable2D::GetDefaultMaterial() const
  104. {
  105. return defaultMaterial_;
  106. }
  107. const Vector<Vertex2D>& Drawable2D::GetVertices()
  108. {
  109. if (verticesDirty_)
  110. UpdateVertices();
  111. return vertices_;
  112. }
  113. void Drawable2D::SetBlendModeAttr(BlendMode mode)
  114. {
  115. // Delay applying material update
  116. materialUpdatePending_ = true;
  117. SetBlendMode(mode);
  118. }
  119. void Drawable2D::OnNodeSet(Node* node)
  120. {
  121. Drawable::OnNodeSet(node);
  122. if (node)
  123. {
  124. Scene* scene = GetScene();
  125. if (scene)
  126. scene->GetOrCreateComponent<Renderer2D>();
  127. }
  128. }
  129. void Drawable2D::OnMarkedDirty(Node* node)
  130. {
  131. Drawable::OnMarkedDirty(node);
  132. verticesDirty_ = true;
  133. }
  134. }