Drawable2D.cpp 3.5 KB

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