Drawable2D.cpp 3.5 KB

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