| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #include "BsGUIElement.h"
- #include "BsGUIWidget.h"
- #include "BsGUISkin.h"
- #include "BsGUILayout.h"
- #include "BsGUIManager.h"
- #include "CmException.h"
- using namespace CamelotFramework;
- namespace BansheeEngine
- {
- GUIElement::GUIElement(GUIWidget& parent, const GUIElementStyle* style, const GUILayoutOptions& layoutOptions, bool acceptsKeyboardFocus)
- :mParent(&parent), mLayoutOptions(layoutOptions), mWidth(0), mHeight(0), mDepth(0), mStyle(style),
- mAcceptsKeyboardFocus(acceptsKeyboardFocus), mIsDestroyed(false)
- {
- mParent->registerElement(this);
- }
- GUIElement::~GUIElement()
- {
- if(mParentElement != nullptr)
- mParentElement->_unregisterChildElement(this);
- }
- void GUIElement::updateRenderElements()
- {
- updateRenderElementsInternal();
- _markAsClean();
- }
- void GUIElement::updateRenderElementsInternal()
- {
- updateClippedBounds();
- }
- void GUIElement::setLayoutOptions(const GUILayoutOptions& layoutOptions)
- {
- if(layoutOptions.maxWidth < layoutOptions.minWidth)
- {
- CM_EXCEPT(InvalidParametersException, "Maximum width is less than minimum width! Max width: " +
- toString(layoutOptions.maxWidth) + ". Min width: " + toString(layoutOptions.minWidth));
- }
- if(layoutOptions.maxHeight < layoutOptions.minHeight)
- {
- CM_EXCEPT(InvalidParametersException, "Maximum height is less than minimum height! Max height: " +
- toString(layoutOptions.maxHeight) + ". Min height: " + toString(layoutOptions.minHeight));
- }
- mLayoutOptions = layoutOptions;
- }
- bool GUIElement::mouseEvent(const GUIMouseEvent& ev)
- {
- return false;
- }
- bool GUIElement::textInputEvent(const GUITextInputEvent& ev)
- {
- return false;
- }
- bool GUIElement::commandEvent(const GUICommandEvent& ev)
- {
- return false;
- }
- void GUIElement::_setWidgetDepth(UINT8 depth)
- {
- mDepth |= depth << 24;
- markMeshAsDirty();
- }
- void GUIElement::_setAreaDepth(UINT16 depth)
- {
- mDepth |= depth << 8;
- markMeshAsDirty();
- }
- void GUIElement::_setOffset(const CM::Vector2I& offset)
- {
- if(mOffset != offset)
- {
- markMeshAsDirty();
- mOffset = offset;
- updateClippedBounds();
- }
- }
- void GUIElement::_setWidth(UINT32 width)
- {
- if(mWidth != width)
- markContentAsDirty();
- mWidth = width;
- }
- void GUIElement::_setHeight(UINT32 height)
- {
- if(mHeight != height)
- markContentAsDirty();
- mHeight = height;
- }
- void GUIElement::_setClipRect(const CM::RectI& clipRect)
- {
- if(mClipRect != clipRect)
- {
- markMeshAsDirty();
- mClipRect = clipRect;
- updateClippedBounds();
- }
- }
- void GUIElement::_changeParentWidget(GUIWidget* widget)
- {
- if(mParent != widget)
- {
- if(mParent != nullptr)
- mParent->unregisterElement(this);
- if(widget != nullptr)
- widget->registerElement(this);
- mParent = widget;
- }
- GUIElementBase::_changeParentWidget(widget);
- }
- RectI GUIElement::getBounds() const
- {
- return RectI(mOffset.x, mOffset.y, mWidth, mHeight);
- }
- RectI GUIElement::getVisibleBounds() const
- {
- RectI bounds = _getClippedBounds();
-
- bounds.x += mStyle->margins.left;
- bounds.y += mStyle->margins.top;
- bounds.width = (UINT32)std::max(0, (INT32)bounds.width - (INT32)(mStyle->margins.left + mStyle->margins.right));
- bounds.height = (UINT32)std::max(0, (INT32)bounds.height - (INT32)(mStyle->margins.top + mStyle->margins.bottom));
- return bounds;
- }
- RectI GUIElement::getContentBounds() const
- {
- RectI bounds;
- bounds.x = mOffset.x + mStyle->margins.left + mStyle->contentOffset.left;
- bounds.y = mOffset.y + mStyle->margins.top + mStyle->contentOffset.top;
- bounds.width = (UINT32)std::max(0, (INT32)mWidth -
- (INT32)(mStyle->margins.left + mStyle->margins.right + mStyle->contentOffset.left + mStyle->contentOffset.right));
- bounds.height = (UINT32)std::max(0, (INT32)mHeight -
- (INT32)(mStyle->margins.top + mStyle->margins.bottom + mStyle->contentOffset.top + mStyle->contentOffset.bottom));
- return bounds;
- }
- RectI GUIElement::getContentClipRect() const
- {
- RectI contentBounds = getContentBounds();
-
- // Transform into element space so we can clip it using the element clip rectangle
- Vector2I offsetDiff = Vector2I(contentBounds.x - mOffset.x, contentBounds.y - mOffset.y);
- RectI contentClipRect(offsetDiff.x, offsetDiff.y, contentBounds.width, contentBounds.height);
- contentClipRect.clip(mClipRect);
- // Transform into content sprite space
- contentClipRect.x -= offsetDiff.x;
- contentClipRect.y -= offsetDiff.y;
- return contentClipRect;
- }
- bool GUIElement::_isInBounds(const CM::Vector2I position) const
- {
- RectI contentBounds = getVisibleBounds();
- return contentBounds.contains(position);
- }
- void GUIElement::destroy(GUIElement* element)
- {
- if(element->mIsDestroyed)
- return;
- if(element->mParent != nullptr)
- element->mParent->unregisterElement(element);
- element->mIsDestroyed = true;
- GUIManager::instance().queueForDestroy(element);
- }
- }
|