Drawable2D.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright (c) 2008-2015 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 "../Atomic2D/Drawable2D.h"
  29. #include "../Atomic2D/Renderer2D.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. const float PIXEL_SIZE = 0.01f;
  34. SourceBatch2D::SourceBatch2D() : drawOrder_(0)
  35. {
  36. }
  37. Drawable2D::Drawable2D(Context* context) :
  38. Drawable(context, DRAWABLE_GEOMETRY2D),
  39. layer_(0),
  40. orderInLayer_(0),
  41. sourceBatchesDirty_(true)
  42. {
  43. }
  44. Drawable2D::~Drawable2D()
  45. {
  46. if (renderer_)
  47. renderer_->RemoveDrawable(this);
  48. }
  49. void Drawable2D::RegisterObject(Context* context)
  50. {
  51. ACCESSOR_ATTRIBUTE("Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  52. ACCESSOR_ATTRIBUTE("Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  53. }
  54. void Drawable2D::OnSetEnabled()
  55. {
  56. bool enabled = IsEnabledEffective();
  57. if (enabled && renderer_)
  58. renderer_->AddDrawable(this);
  59. else if (!enabled && renderer_)
  60. renderer_->RemoveDrawable(this);
  61. }
  62. void Drawable2D::SetLayer(int layer)
  63. {
  64. if (layer == layer_)
  65. return;
  66. layer_ = layer;
  67. OnDrawOrderChanged();
  68. MarkNetworkUpdate();
  69. }
  70. void Drawable2D::SetOrderInLayer(int orderInLayer)
  71. {
  72. if (orderInLayer == orderInLayer_)
  73. return;
  74. orderInLayer_ = orderInLayer;
  75. OnDrawOrderChanged();
  76. MarkNetworkUpdate();
  77. }
  78. const Vector<SourceBatch2D>& Drawable2D::GetSourceBatches()
  79. {
  80. if (sourceBatchesDirty_)
  81. UpdateSourceBatches();
  82. return sourceBatches_;
  83. }
  84. void Drawable2D::OnNodeSet(Node* node)
  85. {
  86. // Do not call Drawable::OnNodeSet(node)
  87. if (node)
  88. {
  89. Scene* scene = GetScene();
  90. if (scene)
  91. {
  92. renderer_ = scene->GetOrCreateComponent<Renderer2D>();
  93. if (IsEnabledEffective())
  94. renderer_->AddDrawable(this);
  95. }
  96. node->AddListener(this);
  97. }
  98. else
  99. {
  100. if (renderer_)
  101. renderer_->RemoveDrawable(this);
  102. }
  103. }
  104. void Drawable2D::OnMarkedDirty(Node* node)
  105. {
  106. Drawable::OnMarkedDirty(node);
  107. sourceBatchesDirty_ = true;
  108. }
  109. }