| 173 | } |
| 174 | |
| 175 | void ScrollView::setInnerContainerSize(const Vec2& size) |
| 176 | { |
| 177 | float innerSizeWidth = _contentSize.width; |
| 178 | float innerSizeHeight = _contentSize.height; |
| 179 | Vec2 originalInnerSize = _innerContainer->getContentSize(); |
| 180 | if (size.width >= innerSizeWidth) |
| 181 | { |
| 182 | innerSizeWidth = size.width; |
| 183 | } |
| 184 | if (size.height >= innerSizeHeight) |
| 185 | { |
| 186 | innerSizeHeight = size.height; |
| 187 | } |
| 188 | _innerContainer->setContentSize(Vec2(innerSizeWidth, innerSizeHeight)); |
| 189 | |
| 190 | // Calculate and set the position of the inner container. |
| 191 | Vec2 pos = _innerContainer->getPosition(); |
| 192 | const auto boundingBox = _innerContainer->getBoundingBox(); |
| 193 | const auto rBoundary = boundingBox.origin.x + boundingBox.size.width; |
| 194 | const auto bBoundary = boundingBox.origin.y; |
| 195 | if (rBoundary < _contentSize.width) |
| 196 | { |
| 197 | // When the right border does not fit to the width, |
| 198 | // consider left-side fitting or right-side fitting. |
| 199 | const auto lBoundary = boundingBox.origin.x; |
| 200 | if (rBoundary - lBoundary > _contentSize.width) |
| 201 | { |
| 202 | // The width of _innerContainer is greater than _contentSize.width,use right-side fitting. |
| 203 | pos.x = _contentSize.width - |
| 204 | (1.f - _innerContainer->getAnchorPoint().x) * _innerContainer->getContentSize().width; |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | // or use left-side fitting. |
| 209 | pos.x = _innerContainer->getAnchorPoint().x * _innerContainer->getContentSize().width; |
| 210 | } |
| 211 | } |
| 212 | if (bBoundary > 0.f) |
| 213 | { |
| 214 | // Same as horizontal direction. |
| 215 | const auto tBoundary = boundingBox.origin.y + boundingBox.size.height; |
| 216 | if (tBoundary - bBoundary > _contentSize.height) |
| 217 | { |
| 218 | pos.y = _innerContainer->getAnchorPoint().y * _innerContainer->getContentSize().height; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | pos.y = _contentSize.height - |
| 223 | (1.0f - _innerContainer->getAnchorPoint().y) * _innerContainer->getContentSize().height; |
| 224 | } |
| 225 | } |
| 226 | // In the vertical direction, if the _innerContainer initially contains content larger than _contentSize.height, |
| 227 | // this will result in bottom alignment.Obviously this is not what I want, |
| 228 | // so here is the rule : when the height of _innerContainer and _contentSize are equal, |
| 229 | // if the position of _innerContainer needs to be changed at this point,align _innerContainer to the top. |
| 230 | if (Direction::VERTICAL == _direction && originalInnerSize.height == _contentSize.height) |
| 231 | { |
| 232 | pos.y = _contentSize.height - innerSizeHeight; |