Drawable2D.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (c) 2008-2016 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 "../Core/Context.h"
  24. #include "../Graphics/Camera.h"
  25. #include "../Graphics/Material.h"
  26. #include "../Graphics/Texture2D.h"
  27. #include "../Scene/Scene.h"
  28. #include "../Urho2D/Drawable2D.h"
  29. #include "../Urho2D/Renderer2D.h"
  30. #include "../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. const float PIXEL_SIZE = 0.01f;
  34. SourceBatch2D::SourceBatch2D() :
  35. distance_(0.0f),
  36. drawOrder_(0)
  37. {
  38. }
  39. Drawable2D::Drawable2D(Context* context) :
  40. Drawable(context, DRAWABLE_GEOMETRY2D),
  41. layer_(0),
  42. orderInLayer_(0),
  43. sourceBatchesDirty_(true)
  44. {
  45. }
  46. Drawable2D::~Drawable2D()
  47. {
  48. if (renderer_)
  49. renderer_->RemoveDrawable(this);
  50. }
  51. void Drawable2D::RegisterObject(Context* context)
  52. {
  53. URHO3D_ACCESSOR_ATTRIBUTE("Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  54. URHO3D_ACCESSOR_ATTRIBUTE("Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  55. URHO3D_ATTRIBUTE("View Mask", int, viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  56. }
  57. void Drawable2D::OnSetEnabled()
  58. {
  59. bool enabled = IsEnabledEffective();
  60. if (enabled && renderer_)
  61. renderer_->AddDrawable(this);
  62. else if (!enabled && renderer_)
  63. renderer_->RemoveDrawable(this);
  64. }
  65. void Drawable2D::SetLayer(int layer)
  66. {
  67. if (layer == layer_)
  68. return;
  69. layer_ = layer;
  70. OnDrawOrderChanged();
  71. MarkNetworkUpdate();
  72. }
  73. void Drawable2D::SetOrderInLayer(int orderInLayer)
  74. {
  75. if (orderInLayer == orderInLayer_)
  76. return;
  77. orderInLayer_ = orderInLayer;
  78. OnDrawOrderChanged();
  79. MarkNetworkUpdate();
  80. }
  81. const Vector<SourceBatch2D>& Drawable2D::GetSourceBatches()
  82. {
  83. if (sourceBatchesDirty_)
  84. UpdateSourceBatches();
  85. return sourceBatches_;
  86. }
  87. void Drawable2D::OnSceneSet(Scene* scene)
  88. {
  89. // Do not call Drawable::OnSceneSet(node), as 2D drawable components should not be added to the octree
  90. // but are instead rendered through Renderer2D
  91. if (scene)
  92. {
  93. renderer_ = scene->GetOrCreateComponent<Renderer2D>();
  94. if (IsEnabledEffective())
  95. renderer_->AddDrawable(this);
  96. }
  97. else
  98. {
  99. if (renderer_)
  100. renderer_->RemoveDrawable(this);
  101. }
  102. }
  103. void Drawable2D::OnMarkedDirty(Node* node)
  104. {
  105. Drawable::OnMarkedDirty(node);
  106. sourceBatchesDirty_ = true;
  107. }
  108. }