void GUIButtonBase::updateRenderElementsInternal() { mImageDesc.width = mLayoutData.area.width; mImageDesc.height = mLayoutData.area.height; const HSpriteTexture& activeTex = getActiveTexture(); if (SpriteTexture::checkIsLoaded(activeTex)) mImageDesc.texture = activeTex.getInternalPtr(); else mImageDesc.texture = nullptr; mImageDesc.borderLeft = _getStyle()->border.left; mImageDesc.borderRight = _getStyle()->border.right; mImageDesc.borderTop = _getStyle()->border.top; mImageDesc.borderBottom = _getStyle()->border.bottom; mImageDesc.color = getTint(); mImageSprite->update(mImageDesc, (UINT64)_getParentWidget()); mTextSprite->update(getTextDesc(), (UINT64)_getParentWidget()); if(mContentImageSprite != nullptr) { Rect2I contentBounds = getCachedContentBounds(); HSpriteTexture image = mContent.getImage(mActiveState); UINT32 contentWidth = image->getWidth(); UINT32 contentHeight = image->getHeight(); UINT32 contentMaxWidth = std::min((UINT32)contentBounds.width, contentWidth); UINT32 contentMaxHeight = std::min((UINT32)contentBounds.height, contentHeight); float horzRatio = contentMaxWidth / (float)contentWidth; float vertRatio = contentMaxHeight / (float)contentHeight; if (horzRatio < vertRatio) { contentWidth = Math::roundToInt(contentWidth * horzRatio); contentHeight = Math::roundToInt(contentHeight * horzRatio); } else { contentWidth = Math::roundToInt(contentWidth * vertRatio); contentHeight = Math::roundToInt(contentHeight * vertRatio); } IMAGE_SPRITE_DESC contentImgDesc; contentImgDesc.texture = image.getInternalPtr(); contentImgDesc.width = contentWidth; contentImgDesc.height = contentHeight; contentImgDesc.color = getTint(); mContentImageSprite->update(contentImgDesc, (UINT64)_getParentWidget()); } GUIElement::updateRenderElementsInternal(); }
Rect2I GUIElement::getCachedContentClipRect() const { Rect2I contentBounds = getCachedContentBounds(); // Transform into element space so we can clip it using the element clip rectangle Vector2I offsetDiff = Vector2I(contentBounds.x - mLayoutData.area.x, contentBounds.y - mLayoutData.area.y); Rect2I contentClipRect(offsetDiff.x, offsetDiff.y, contentBounds.width, contentBounds.height); contentClipRect.clip(mLayoutData.getLocalClipRect()); // Transform into content sprite space contentClipRect.x -= offsetDiff.x; contentClipRect.y -= offsetDiff.y; return contentClipRect; }
TEXT_SPRITE_DESC GUIButtonBase::getTextDesc() const { TEXT_SPRITE_DESC textDesc; textDesc.text = mContent.getText(); textDesc.font = _getStyle()->font; textDesc.fontSize = _getStyle()->fontSize; textDesc.color = getTint() * getActiveTextColor(); Rect2I textBounds = getCachedContentBounds(); textDesc.width = textBounds.width; textDesc.height = textBounds.height; textDesc.horzAlign = _getStyle()->textHorzAlign; textDesc.vertAlign = _getStyle()->textVertAlign; return textDesc; }
void GUIButtonBase::_fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 vertexOffset, UINT32 indexOffset, UINT32 maxNumVerts, UINT32 maxNumIndices, UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const { UINT32 textSpriteIdx = mImageSprite->getNumRenderElements(); UINT32 contentImgSpriteIdx = textSpriteIdx + mTextSprite->getNumRenderElements(); if(renderElementIdx < textSpriteIdx) { Vector2I offset(mLayoutData.area.x, mLayoutData.area.y); mImageSprite->fillBuffer(vertices, uv, indices, vertexOffset, indexOffset, maxNumVerts, maxNumIndices, vertexStride, indexStride, renderElementIdx, offset, mLayoutData.getLocalClipRect()); return; } Rect2I contentBounds = getCachedContentBounds(); Rect2I contentClipRect = getCachedContentClipRect(); Rect2I textBounds = mTextSprite->getBounds(Vector2I(), Rect2I()); Vector2I textOffset; Rect2I textClipRect; Vector2I imageOffset; Rect2I imageClipRect; if(mContentImageSprite != nullptr) { Rect2I imageBounds = mContentImageSprite->getBounds(Vector2I(), Rect2I()); INT32 imageXOffset = 0; INT32 textImageSpacing = 0; if (textBounds.width == 0) { UINT32 freeWidth = (UINT32)std::max(0, contentBounds.width - textBounds.width - imageBounds.width); imageXOffset = (INT32)(freeWidth / 2); } else textImageSpacing = GUIContent::IMAGE_TEXT_SPACING; if(_getStyle()->imagePosition == GUIImagePosition::Right) { INT32 imageReservedWidth = std::max(0, contentBounds.width - textBounds.width); textOffset = Vector2I(contentBounds.x, contentBounds.y); textClipRect = contentClipRect; textClipRect.width = std::min(contentBounds.width - imageReservedWidth, textClipRect.width); imageOffset = Vector2I(contentBounds.x + textBounds.width + imageXOffset + textImageSpacing, contentBounds.y); imageClipRect = contentClipRect; imageClipRect.x -= textBounds.width + imageXOffset; } else { INT32 imageReservedWidth = imageBounds.width + imageXOffset; imageOffset = Vector2I(contentBounds.x + imageXOffset, contentBounds.y); imageClipRect = contentClipRect; imageClipRect.x -= imageXOffset; imageClipRect.width = std::min(imageReservedWidth, imageClipRect.width); textOffset = Vector2I(contentBounds.x + imageReservedWidth + textImageSpacing, contentBounds.y); textClipRect = contentClipRect; textClipRect.x -= imageReservedWidth; } INT32 imageYOffset = (contentBounds.height - imageBounds.height) / 2; imageClipRect.y -= imageYOffset; imageOffset.y += imageYOffset; } else { textOffset = Vector2I(contentBounds.x, contentBounds.y); textClipRect = contentClipRect; } if(renderElementIdx >= contentImgSpriteIdx) { mContentImageSprite->fillBuffer(vertices, uv, indices, vertexOffset, indexOffset, maxNumVerts, maxNumIndices, vertexStride, indexStride, contentImgSpriteIdx - renderElementIdx, imageOffset, imageClipRect); } else { mTextSprite->fillBuffer(vertices, uv, indices, vertexOffset, indexOffset, maxNumVerts, maxNumIndices, vertexStride, indexStride, textSpriteIdx - renderElementIdx, textOffset, textClipRect); } }