void RadioButton::updateBounds() { Label::updateBounds(); Vector2 size; if (_selected) { const Rectangle& selectedRegion = getImageRegion("selected", NORMAL); size.set(selectedRegion.width, selectedRegion.height); } else { const Rectangle& unselectedRegion = getImageRegion("unselected", NORMAL); size.set(unselectedRegion.width, unselectedRegion.height); } if (_autoSize & AUTO_SIZE_HEIGHT) { // Text-only width was already measured in Label::update - append image const Theme::Border& border = getBorder(NORMAL); const Theme::Border& padding = getPadding(); setHeightInternal(std::max(_bounds.height, size.y + border.top + border.bottom + padding.top + padding.bottom)); } if (_autoSize & AUTO_SIZE_WIDTH) { // Text-only width was already measured in Label::update - append image setWidthInternal(_bounds.height + 5 + _bounds.width); } }
void CheckBox::update(const Control* container, const Vector2& offset) { Label::update(container, offset); Control::State state = getState(); Vector2 size; if (_imageSize.isZero()) { if (_checked) { const Rectangle& selectedRegion = getImageRegion("checked", state); size.set(selectedRegion.width, selectedRegion.height); } else { const Rectangle& unselectedRegion = getImageRegion("unchecked", state); size.set(unselectedRegion.width, unselectedRegion.height); } } else { size.set(_imageSize); } if (_autoWidth == Control::AUTO_SIZE_FIT) { // Text-only width was already measured in Label::update - append image setWidth(size.x + _bounds.width + 5); } if (_autoHeight == Control::AUTO_SIZE_FIT) { // Text-only width was already measured in Label::update - append image setHeight(std::max(getHeight(), size.y)); } _textBounds.x += size.x + 5; if (_checked) { _image = getImage("checked", state); } else { _image = getImage("unchecked", state); } }
void CheckBox::update(const Control* container, const Vector2& offset) { Label::update(container, offset); Vector2 size; if (_imageSize.isZero()) { if (_checked) { const Rectangle& selectedRegion = getImageRegion("checked", _state); size.set(selectedRegion.width, selectedRegion.height); } else { const Rectangle& unselectedRegion = getImageRegion("unchecked", _state); size.set(unselectedRegion.width, unselectedRegion.height); } } else { size.set(_imageSize); } float iconWidth = size.x; _textBounds.x += iconWidth + 5; _textBounds.width -= iconWidth + 5; if (_checked) { _image = getImage("checked", _state); } else { _image = getImage("unchecked", _state); } }
void Container::updateScroll() { // Update Time. static long lastFrameTime = Game::getGameTime(); long frameTime = Game::getGameTime(); long elapsedTime = (frameTime - lastFrameTime); lastFrameTime = frameTime; const Theme::Border& containerBorder = getBorder(_state); const Theme::Padding& containerPadding = getPadding(); // Calculate total width and height. float totalWidth = 0; float totalHeight = 0; std::vector<Control*> controls = getControls(); unsigned int controlsCount = controls.size(); for (unsigned int i = 0; i < controlsCount; i++) { Control* control = controls.at(i); const Rectangle& bounds = control->getBounds(); const Theme::Margin& margin = control->getMargin(); float newWidth = bounds.x + bounds.width; if (newWidth > totalWidth) { totalWidth = newWidth; } float newHeight = bounds.y + bounds.height; if (newHeight > totalHeight) { totalHeight = newHeight; } } float vWidth = getImageRegion("verticalScrollBar", _state).width; float hHeight = getImageRegion("horizontalScrollBar", _state).height; float clipWidth = _bounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right - vWidth; float clipHeight = _bounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom - hHeight; // Apply and dampen inertia. if (!_scrolling && !_scrollingVelocity.isZero()) { // Calculate the time passed since last update. float elapsedSecs = (float)elapsedTime * 0.001f; _scrollPosition.x += _scrollingVelocity.x * elapsedSecs; _scrollPosition.y += _scrollingVelocity.y * elapsedSecs; float dampening = 1.0f - _scrollingFriction * SCROLL_FRICTION_FACTOR * elapsedSecs; _scrollingVelocity.x *= dampening; _scrollingVelocity.y *= dampening; if (fabs(_scrollingVelocity.x) < 100.0f) _scrollingVelocity.x = 0.0f; if (fabs(_scrollingVelocity.y) < 100.0f) _scrollingVelocity.y = 0.0f; } // Stop scrolling when the far edge is reached. if (-_scrollPosition.x > totalWidth - clipWidth) { _scrollPosition.x = -(totalWidth - clipWidth); _scrollingVelocity.x = 0; } if (-_scrollPosition.y > totalHeight - clipHeight) { _scrollPosition.y = -(totalHeight - clipHeight); _scrollingVelocity.y = 0; } if (_scrollPosition.x > 0) { _scrollPosition.x = 0; _scrollingVelocity.x = 0; } if (_scrollPosition.y > 0) { _scrollPosition.y = 0; _scrollingVelocity.y = 0; } float scrollWidth = 0; if (clipWidth < totalWidth) scrollWidth = (clipWidth / totalWidth) * clipWidth; float scrollHeight = 0; if (clipHeight < totalHeight) scrollHeight = (clipHeight / totalHeight) * clipHeight; _scrollBarBounds.set(((-_scrollPosition.x) / totalWidth) * clipWidth, ((-_scrollPosition.y) / totalHeight) * clipHeight, scrollWidth, scrollHeight); // If scroll velocity is 0 and scrollbars are not always visible, trigger fade-out animation. if (!_scrolling && _scrollingVelocity.isZero() && _scrollBarsAutoHide && _scrollBarOpacity == 1.0f) { float to = 0; _scrollBarOpacity = 0.99f; Animation* animation = createAnimationFromTo("scrollbar-fade-out", ANIMATE_OPACITY, &_scrollBarOpacity, &to, Curve::QUADRATIC_IN_OUT, 500L); _scrollBarOpacityClip = animation->getClip(); _scrollBarOpacityClip->play(); } // Position controls within scroll area. _layout->update(this, _scrollPosition); }