void UIButton::correctSize() { Rect32 rect; BaseSprite *img = nullptr; if (_image) { img = _image; } else if (_imageDisable) { img = _imageDisable; } else if (_imageHover) { img = _imageHover; } else if (_imagePress) { img = _imagePress; } else if (_imageFocus) { img = _imageFocus; } if (_width <= 0) { if (img) { img->getBoundingRect(&rect, 0, 0); _width = rect.right - rect.left; } else { _width = 100; } } if (_height <= 0) { if (img) { img->getBoundingRect(&rect, 0, 0); _height = rect.bottom - rect.top; } } if (_text) { int textHeight; if (_font) { textHeight = _font->getTextHeight((byte *)_text, _width); } else { textHeight = _gameRef->getSystemFont()->getTextHeight((byte *)_text, _width); } if (textHeight > _height) { _height = textHeight; } } if (_height <= 0) { _height = 100; } if (_back) { _back->correctSize(&_width, &_height); } }
bool UIButton::display(int offsetX, int offsetY) { if (!_visible) { return STATUS_OK; } UITiledImage *back = nullptr; BaseSprite *image = nullptr; BaseFont *font = 0; //RECT rect; //BasePlatform::setRect(&rect, OffsetX + _posX, OffsetY + _posY, OffsetX+_posX+_width, OffsetY+_posY+_height); //_hover = (!_disable && BasePlatform::ptInRect(&rect, _gameRef->_mousePos)!=FALSE); _hover = (!_disable && _gameRef->_activeObject == this && (_gameRef->_interactive || _gameRef->_state == GAME_SEMI_FROZEN)); if ((_press && _hover && !_gameRef->_mouseLeftDown) || (_oneTimePress && g_system->getMillis() - _oneTimePressTime >= 100)) { press(); } if (_disable) { if (_backDisable) { back = _backDisable; } if (_imageDisable) { image = _imageDisable; } if (_text && _fontDisable) { font = _fontDisable; } } else if (_press || _oneTimePress || _stayPressed) { if (_backPress) { back = _backPress; } if (_imagePress) { image = _imagePress; } if (_text && _fontPress) { font = _fontPress; } } else if (_hover) { if (_backHover) { back = _backHover; } if (_imageHover) { image = _imageHover; } if (_text && _fontHover) { font = _fontHover; } } else if (_canFocus && isFocused()) { if (_backFocus) { back = _backFocus; } if (_imageFocus) { image = _imageFocus; } if (_text && _fontFocus) { font = _fontFocus; } } if (!back && _back) { back = _back; } if (!image && _image) { image = _image; } if (_text && !font) { if (_font) { font = _font; } else { font = _gameRef->getSystemFont(); } } int imageX = offsetX + _posX; int imageY = offsetY + _posY; if (image && _centerImage) { Rect32 rc; image->getBoundingRect(&rc, 0, 0); imageX += (_width - (rc.right - rc.left)) / 2; imageY += (_height - (rc.bottom - rc.top)) / 2; } if (back) { back->display(offsetX + _posX, offsetY + _posY, _width, _height); } //if (image) image->Draw(ImageX +((_press||_oneTimePress)&&back?1:0), ImageY +((_press||_oneTimePress)&&back?1:0), nullptr); if (image) { image->draw(imageX + ((_press || _oneTimePress) && back ? 1 : 0), imageY + ((_press || _oneTimePress) && back ? 1 : 0), _pixelPerfect ? this : nullptr); } if (font && _text) { int text_offset = (_height - font->getTextHeight((byte *)_text, _width)) / 2; font->drawText((byte *)_text, offsetX + _posX + ((_press || _oneTimePress) ? 1 : 0), offsetY + _posY + text_offset + ((_press || _oneTimePress) ? 1 : 0), _width, _align); } if (!_pixelPerfect || !_image) { _gameRef->_renderer->addRectToList(new BaseActiveRect(_gameRef, this, nullptr, offsetX + _posX, offsetY + _posY, _width, _height, 100, 100, false)); } // reset unused sprites if (_image && _image != image) { _image->reset(); } if (_imageDisable && _imageDisable != image) { _imageDisable->reset(); } if (_imageFocus && _imageFocus != image) { _imageFocus->reset(); } if (_imagePress && _imagePress != image) { _imagePress->reset(); } if (_imageHover && _imageHover != image) { _imageHover->reset(); } _press = _hover && _gameRef->_mouseLeftDown && _gameRef->_capturedObject == this; return STATUS_OK; }