| 94 | } |
| 95 | |
| 96 | bool ButtonSprite::initWithSprite(Sprite* iconSprite, int width, int minWidth, float height, float scale, bool absoluteWidth, std::string_view texture, bool dontUseScale9) |
| 97 | { |
| 98 | if (!Sprite::init()) |
| 99 | return false; |
| 100 | |
| 101 | _subSprite = iconSprite; |
| 102 | _hasTextProvided = false; |
| 103 | _scale = scale; |
| 104 | _width = static_cast<float>(width); |
| 105 | _absoluteWidth = absoluteWidth; |
| 106 | _minWidth = static_cast<float>(minWidth); |
| 107 | |
| 108 | this->addChild(_subSprite, 1); |
| 109 | |
| 110 | if (dontUseScale9) |
| 111 | { |
| 112 | _subBGSprite = Sprite::create(texture); |
| 113 | this->addChild(_subBGSprite, 0); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | _buttonTexture = ui::Scale9Sprite::create(texture, ax::Rect(0, 0, 40, 40)); |
| 118 | _buttonTexture->setContentSize({ 16,16 }); |
| 119 | this->addChild(_buttonTexture, 0); |
| 120 | } |
| 121 | |
| 122 | if (_width > 0) |
| 123 | { |
| 124 | if (_subSprite->getContentSize().width > _width) |
| 125 | { |
| 126 | _subSprite->setScale(_width / _subSprite->getContentSize().width); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (height > 0 && _subSprite->getContentSize().height > height) |
| 131 | { |
| 132 | float newSpriteScale = (height / _subSprite->getContentSize().height); |
| 133 | if (newSpriteScale < _subSprite->getScale()) |
| 134 | { |
| 135 | _subSprite->setScale(newSpriteScale); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (_subSprite->getScale() >= _scale) |
| 140 | _subSprite->setScale(_scale); |
| 141 | |
| 142 | this->updateSpriteBGSize(); |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | void ButtonSprite::updateBGImage(std::string_view file) |
| 148 | { |
no test coverage detected