Drawable2D.cpp 3.4 KB

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