| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- //
- // Copyright (c) 2008-2013 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 "Graphics.h"
- #include "Matrix3x4.h"
- #include "ShaderVariation.h"
- #include "Texture.h"
- #include "UIElement.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- #ifdef USE_OPENGL
- static const float posAdjust = 0.0f;
- #else
- static const float posAdjust = 0.5f;
- #endif
- static const Vector3 posAdjustVec(posAdjust, posAdjust, 0.0f);
- UIBatch::UIBatch() :
- element_(0),
- blendMode_(BLEND_REPLACE),
- texture_(0),
- invTextureSize_(Vector2::ONE),
- fixedColor_(0),
- vertexData_(0),
- vertexStart_(0),
- vertexEnd_(0)
- {
- }
- UIBatch::UIBatch(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) :
- element_(element),
- blendMode_(blendMode),
- scissor_(scissor),
- texture_(texture),
- invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
- fixedColor_(element->GetDerivedColor().ToUInt()),
- vertexData_(vertexData),
- vertexStart_(vertexData->Size()),
- vertexEnd_(vertexData->Size())
- {
- }
- void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight,
- Color* color)
- {
- unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
- if (color || !element_->HasColorGradient())
- {
- unsigned uintColor = color ? color->ToUInt() : fixedColor_;
-
- // If alpha is 0, nothing will be rendered, so do not add the quad
- if (!(uintColor & 0xff000000))
- return;
-
- topLeftColor = uintColor;
- topRightColor = uintColor;
- bottomLeftColor = uintColor;
- bottomRightColor = uintColor;
- }
- else
- {
- topLeftColor = GetInterpolatedColor(x, y);
- topRightColor = GetInterpolatedColor(x + width, y);
- bottomLeftColor = GetInterpolatedColor(x, y + height);
- bottomRightColor = GetInterpolatedColor(x + width, y + height);
- }
-
- const IntVector2& screenPos = element_->GetScreenPosition();
-
- float left = (float)(x + screenPos.x_) - posAdjust;
- float right = left + (float)width;
- float top = (float)(y + screenPos.y_) - posAdjust;
- float bottom = top + (float)height;
-
- float leftUV = texOffsetX * invTextureSize_.x_;
- float topUV = texOffsetY * invTextureSize_.y_;
- float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
- float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
-
- unsigned begin = vertexData_->Size();
- vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
- float* dest = &(vertexData_->At(begin));
- vertexEnd_ = vertexData_->Size();
-
- *dest++ = left; *dest++ = top; *dest++ = 0.0f;
- *((unsigned*)dest) = topLeftColor; dest++;
- *dest++ = leftUV; *dest++ = topUV;
-
- *dest++ = right; *dest++ = top; *dest++ = 0.0f;
- *((unsigned*)dest) = topRightColor; dest++;
- *dest++ = rightUV; *dest++ = topUV;
-
- *dest++ = left; *dest++ = bottom; *dest++ = 0.0f;
- *((unsigned*)dest) = bottomLeftColor; dest++;
- *dest++ = leftUV; *dest++ = bottomUV;
-
- *dest++ = right; *dest++ = top; *dest++ = 0.0f;
- *((unsigned*)dest) = topRightColor; dest++;
- *dest++ = rightUV; *dest++ = topUV;
-
- *dest++ = right; *dest++ = bottom; *dest++ = 0.0f;
- *((unsigned*)dest) = bottomRightColor; dest++;
- *dest++ = rightUV; *dest++ = bottomUV;
- *dest++ = left; *dest++ = bottom; *dest++ = 0.0f;
- *((unsigned*)dest) = bottomLeftColor; dest++;
- *dest++ = leftUV; *dest++ = bottomUV;
- }
- void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
- int texWidth, int texHeight, Color* color)
- {
- unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
-
- if (color || !element_->HasColorGradient())
- {
- unsigned uintColor = color ? color->ToUInt() : fixedColor_;
-
- // If alpha is 0, nothing will be rendered, so do not add the quad
- if (!(uintColor & 0xff000000))
- return;
-
- topLeftColor = uintColor;
- topRightColor = uintColor;
- bottomLeftColor = uintColor;
- bottomRightColor = uintColor;
- }
- else
- {
- topLeftColor = GetInterpolatedColor(x, y);
- topRightColor = GetInterpolatedColor(x + width, y);
- bottomLeftColor = GetInterpolatedColor(x, y + height);
- bottomRightColor = GetInterpolatedColor(x + width, y + height);
- }
-
- Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjustVec;
- Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjustVec;
- Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjustVec;
- Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjustVec;
-
- float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
- float topUV = ((float)texOffsetY) * invTextureSize_.y_;
- float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
- float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
-
- unsigned begin = vertexData_->Size();
- vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
- float* dest = &(vertexData_->At(begin));
- vertexEnd_ = vertexData_->Size();
- *dest++ = v1.x_; *dest++ = v1.y_; *dest++ = 0.0f;
- *((unsigned*)dest) = topLeftColor; dest++;
- *dest++ = leftUV; *dest++ = topUV;
-
- *dest++ = v2.x_; *dest++ = v2.y_; *dest++ = 0.0f;
- *((unsigned*)dest) = topRightColor; dest++;
- *dest++ = rightUV; *dest++ = topUV;
-
- *dest++ = v3.x_; *dest++ = v3.y_; *dest++ = 0.0f;
- *((unsigned*)dest) = bottomLeftColor; dest++;
- *dest++ = leftUV; *dest++ = bottomUV;
-
- *dest++ = v2.x_; *dest++ = v2.y_; *dest++ = 0.0f;
- *((unsigned*)dest) = topRightColor; dest++;
- *dest++ = rightUV; *dest++ = topUV;
-
- *dest++ = v4.x_; *dest++ = v4.y_; *dest++ = 0.0f;
- *((unsigned*)dest) = bottomRightColor; dest++;
- *dest++ = rightUV; *dest++ = bottomUV;
- *dest++ = v3.x_; *dest++ = v3.y_; *dest++ = 0.0f;
- *((unsigned*)dest) = bottomLeftColor; dest++;
- *dest++ = leftUV; *dest++ = bottomUV;
- }
- void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
- {
- if (!(element_->HasColorGradient() || element_->GetDerivedColor().ToUInt() & 0xff000000))
- return; // No gradient and alpha is 0, so do not add the quad
-
- if (!tiled)
- {
- AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
- return;
- }
-
- int tileX = 0;
- int tileY = 0;
- int tileW = 0;
- int tileH = 0;
-
- while (tileY < height)
- {
- tileX = 0;
- tileH = Min(height - tileY, texHeight);
-
- while (tileX < width)
- {
- tileW = Min(width - tileX, texWidth);
-
- AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
-
- tileX += tileW;
- }
-
- tileY += tileH;
- }
- }
- void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight,
- const Color& color)
- {
- Color derivedColor(color.r_, color.g_, color.b_, color.a_ * element_->GetDerivedOpacity());
- AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight, &derivedColor);
- }
- bool UIBatch::Merge(const UIBatch& batch)
- {
- if (batch.blendMode_ != blendMode_ ||
- batch.scissor_ != scissor_ ||
- batch.texture_ != texture_ ||
- batch.vertexData_ != vertexData_ ||
- batch.vertexStart_ != vertexEnd_)
- return false;
-
- vertexEnd_ = batch.vertexEnd_;
- return true;
- }
- unsigned UIBatch::GetInterpolatedColor(int x, int y)
- {
- const IntVector2& size = element_->GetSize();
-
- if (size.x_ && size.y_)
- {
- float cLerpX = Clamp((float)x / (float)size.x_, 0.0f, 1.0f);
- float cLerpY = Clamp((float)y / (float)size.y_, 0.0f, 1.0f);
-
- Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
- Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
- Color color = topColor.Lerp(bottomColor, cLerpY);
- color.a_ *= element_->GetDerivedOpacity();
- return color.ToUInt();
- }
- else
- {
- Color color = element_->GetColor(C_TOPLEFT);
- color.a_ *= element_->GetDerivedOpacity();
- return color.ToUInt();
- }
- }
- void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
- {
- if (batch.vertexEnd_ == batch.vertexStart_)
- return;
-
- if (!batches.Empty() && batches.Back().Merge(batch))
- return;
-
- batches.Push(batch);
- }
- }
|