Changeset View
Changeset View
Standalone View
Standalone View
src/sprite.cpp
Show All 36 Lines | |||||
37 | 37 | | |||
38 | void Sprite::setSpriteSize(int size) | 38 | void Sprite::setSpriteSize(int size) | ||
39 | { | 39 | { | ||
40 | int w = renderer()->defaultSize().width(); | 40 | int w = renderer()->defaultSize().width(); | ||
41 | int h = renderer()->defaultSize().height(); | 41 | int h = renderer()->defaultSize().height(); | ||
42 | 42 | | |||
43 | if (size <= 0 || w <= 0 || h <= 0) return; | 43 | if (size <= 0 || w <= 0 || h <= 0) return; | ||
44 | 44 | | |||
45 | qreal s = ((qreal)size) / ((w > h) ? w : h); | 45 | qreal s = (static_cast<qreal>(size)) / ((w > h) ? w : h); | ||
46 | 46 | | |||
47 | setTransform(QTransform::fromScale(s, s), true); | 47 | setTransform(QTransform::fromScale(s, s), true); | ||
48 | } | 48 | } | ||
49 | 49 | | |||
50 | void Sprite::setAngle(double degrees) | 50 | void Sprite::setAngle(double degrees) | ||
51 | { | 51 | { | ||
52 | resetTransform(); | 52 | resetTransform(); | ||
53 | setSpriteSize(SPRITE_SIZE); | 53 | setSpriteSize(SPRITE_SIZE); | ||
54 | 54 | | |||
55 | // Default rotation is done with the top-left corner of the SVG as the rotation point, | 55 | // Default rotation is done with the top-left corner of the SVG as the rotation point, | ||
56 | // but we want to the rotation to be around the SVG's center... | 56 | // but we want to the rotation to be around the SVG's center... | ||
57 | // This is why this "translation" is needed before the actual rotation. | 57 | // This is why this "translation" is needed before the actual rotation. | ||
58 | QTransform transform = QTransform::fromTranslate( | 58 | QTransform transform = QTransform::fromTranslate( | ||
59 | renderer()->defaultSize().width() * cos((degrees-135) * M_PI/180) * sqrt((double)2.0)/2, | 59 | renderer()->defaultSize().width() * cos((degrees-135) * M_PI/180) * sqrt(static_cast<double>(2.0))/2, | ||
60 | renderer()->defaultSize().height() * sin((degrees-135) * M_PI/180) * sqrt((double)2.0)/2 | 60 | renderer()->defaultSize().height() * sin((degrees-135) * M_PI/180) * sqrt(static_cast<double>(2.0))/2 | ||
yurchor: This might be a stupid question but isn't it enough to write just
```
sqrt(2.0)
```
or
```
sqrt… | |||||
61 | ); | 61 | ); | ||
62 | transform.rotate(degrees); | 62 | transform.rotate(degrees); | ||
63 | setTransform(transform, true); | 63 | setTransform(transform, true); | ||
64 | m_angle = degrees; | 64 | m_angle = degrees; | ||
65 | 65 | | |||
66 | //TODO: Check if the update can be done more efficiently | 66 | //TODO: Check if the update can be done more efficiently | ||
67 | update(); | 67 | update(); | ||
68 | } | 68 | } |
This might be a stupid question but isn't it enough to write just
or
?
Thanks in advance for your answers.