| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "Scene.h"
- #include "Sprite2D.h"
- #include "StaticSprite2D.h"
- #include "Texture2D.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- extern const char* URHO2D_CATEGORY;
- StaticSprite2D::StaticSprite2D(Context* context) :
- Drawable2D(context),
- flipX_(false),
- flipY_(false),
- color_(Color::WHITE),
- useHotSpot_(false),
- hotSpot_(0.5f, 0.5f)
- {
- vertices_.Reserve(6);
- }
- StaticSprite2D::~StaticSprite2D()
- {
- }
- void StaticSprite2D::RegisterObject(Context* context)
- {
- context->RegisterFactory<StaticSprite2D>(URHO2D_CATEGORY);
- ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
- REF_ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
- COPY_BASE_ATTRIBUTES(Drawable2D);
- }
- void StaticSprite2D::SetFlip(bool flipX, bool flipY)
- {
- if (flipX == flipX_ && flipY == flipY_)
- return;
- flipX_ = flipX;
- flipY_ = flipY;
- verticesDirty_ = true;
- MarkNetworkUpdate();
- }
- void StaticSprite2D::SetFlipX(bool flipX)
- {
- SetFlip(flipX, flipY_);
- }
- void StaticSprite2D::SetFlipY(bool flipY)
- {
- SetFlip(flipX_, flipY);
- }
- void StaticSprite2D::SetColor(const Color& color)
- {
- if (color == color_)
- return;
- color_ = color;
- verticesDirty_ = true;
- MarkNetworkUpdate();
- }
- void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
- {
- if (useHotSpot == useHotSpot_)
- return;
- useHotSpot_ = useHotSpot;
- verticesDirty_ = true;
- MarkNetworkUpdate();
- }
- void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
- {
- if (hotspot == hotSpot_)
- return;
- hotSpot_ = hotspot;
- if (useHotSpot_)
- {
- verticesDirty_ = true;
- MarkNetworkUpdate();
- }
- }
- void StaticSprite2D::OnWorldBoundingBoxUpdate()
- {
- boundingBox_.Clear();
- if (sprite_)
- {
- const IntRect& rectangle_ = sprite_->GetRectangle();
- float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
- float height = (float)rectangle_.Height() * PIXEL_SIZE;
- const Vector2& hotSpot = sprite_->GetHotSpot();
- float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
- float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
- float leftX = -width * hotSpotX;
- float rightX = width * (1.0f - hotSpotX);
- float bottomY = -height * hotSpotY;
- float topY = height * (1.0f - hotSpotY);
- boundingBox_.Merge(Vector3(leftX, bottomY, 0.0f));
- boundingBox_.Merge(Vector3(rightX, topY, 0.0f));
- }
- worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
- }
- void StaticSprite2D::UpdateVertices()
- {
- if (!verticesDirty_)
- return;
- vertices_.Clear();
- Texture2D* texture = GetTexture();
- if (!texture)
- return;
- const IntRect& rectangle_ = sprite_->GetRectangle();
- if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
- return;
- /*
- V1---------V2
- | / |
- | / |
- | / |
- | / |
- | / |
- V0---------V3
- */
- Vertex2D vertex0;
- Vertex2D vertex1;
- Vertex2D vertex2;
- Vertex2D vertex3;
- float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
- float height = (float)rectangle_.Height() * PIXEL_SIZE;
- float hotSpotX;
- float hotSpotY;
- if (useHotSpot_)
- {
- hotSpotX = flipX_ ? (1.0f - hotSpot_.x_) : hotSpot_.x_;
- hotSpotY = flipY_ ? (1.0f - hotSpot_.y_) : hotSpot_.y_;
- }
- else
- {
- const Vector2& hotSpot = sprite_->GetHotSpot();
- hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
- hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
- }
- #ifdef URHO3D_OPENGL
- float leftX = -width * hotSpotX;
- float rightX = width * (1.0f - hotSpotX);
- float bottomY = -height * hotSpotY;
- float topY = height * (1.0f - hotSpotY);
- #else
- const float halfPixelOffset = 0.5f * PIXEL_SIZE;
- float leftX = -width * hotSpotX + halfPixelOffset;
- float rightX = width * (1.0f - hotSpotX) + halfPixelOffset;
- float bottomY = -height * hotSpotY + halfPixelOffset;
- float topY = height * (1.0f - hotSpotY) + halfPixelOffset;
- #endif
- const Matrix3x4& worldTransform = node_->GetWorldTransform();
- vertex0.position_ = worldTransform * Vector3(leftX, bottomY, 0.0f);
- vertex1.position_ = worldTransform * Vector3(leftX, topY, 0.0f);
- vertex2.position_ = worldTransform * Vector3(rightX, topY, 0.0f);
- vertex3.position_ = worldTransform * Vector3(rightX, bottomY, 0.0f);
- float invTexW = 1.0f / (float)texture->GetWidth();
- float invTexH = 1.0f / (float)texture->GetHeight();
- float leftU = rectangle_.left_ * invTexW;
- float rightU = rectangle_.right_ * invTexW;
- float topV = rectangle_.top_ * invTexH;
- float bottomV = rectangle_.bottom_ * invTexH;
- vertex0.uv_ = Vector2(leftU, bottomV);
- vertex1.uv_ = Vector2(leftU, topV);
- vertex2.uv_ = Vector2(rightU, topV);
- vertex3.uv_ = Vector2(rightU, bottomV);
- if (flipX_)
- {
- Swap(vertex0.uv_.x_, vertex3.uv_.x_);
- Swap(vertex1.uv_.x_, vertex2.uv_.x_);
- }
-
- if (flipY_)
- {
- Swap(vertex0.uv_.y_, vertex1.uv_.y_);
- Swap(vertex2.uv_.y_, vertex3.uv_.y_);
- }
- vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
- vertices_.Push(vertex0);
- vertices_.Push(vertex1);
- vertices_.Push(vertex2);
- vertices_.Push(vertex3);
- verticesDirty_ = false;
- }
- }
|