Skip to content

Commit ce76c18

Browse files
authored
Merge pull request #36 from scratchcpp/rotation_style
Implement rotation style
2 parents d08695e + b9da390 commit ce76c18

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

ScratchCPPGui/ProjectPlayer.qml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ProjectScene {
4141
id: target
4242
engine: loader.engine
4343
spriteModel: modelData
44+
transform: Scale { xScale: mirrorHorizontally ? -1 : 1 }
4445
Component.onCompleted: modelData.renderedTarget = this
4546
}
4647
}

ScratchCPPGui/renderedtarget.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,34 @@ void RenderedTarget::loadProperties()
3636
m_visible = sprite->visible();
3737

3838
if (m_visible) {
39+
// Direction
40+
switch (sprite->rotationStyle()) {
41+
case Sprite::RotationStyle::AllAround:
42+
m_rotation = sprite->direction() - 90;
43+
m_newMirrorHorizontally = false;
44+
45+
break;
46+
47+
case Sprite::RotationStyle::LeftRight: {
48+
m_rotation = 0;
49+
m_newMirrorHorizontally = (sprite->direction() < 0);
50+
51+
break;
52+
}
53+
54+
case Sprite::RotationStyle::DoNotRotate:
55+
m_rotation = 0;
56+
m_newMirrorHorizontally = false;
57+
break;
58+
}
59+
3960
// Coordinates
4061
double size = sprite->size() / 100;
41-
m_x = m_engine->stageWidth() / 2 + sprite->x() - m_costume->rotationCenterX() * size / 2;
62+
m_x = m_engine->stageWidth() / 2 + sprite->x() - m_costume->rotationCenterX() * size / 2 * (m_newMirrorHorizontally ? -1 : 1);
4263
m_y = m_engine->stageHeight() / 2 - sprite->y() - m_costume->rotationCenterY() * size / 2;
4364
m_originX = m_costume->rotationCenterX() * size / 2.0;
4465
m_originY = m_costume->rotationCenterY() * size / 2.0;
4566

46-
// Direction
47-
m_rotation = sprite->direction() - 90;
48-
4967
// Layer
5068
m_z = sprite->layerOrder();
5169
}
@@ -102,6 +120,11 @@ void RenderedTarget::updateProperties()
102120
setRotation(m_rotation);
103121
setTransformOriginPoint(QPointF(m_originX, m_originY));
104122

123+
if (m_newMirrorHorizontally != m_mirrorHorizontally) {
124+
m_mirrorHorizontally = m_newMirrorHorizontally;
125+
emit mirrorHorizontallyChanged();
126+
}
127+
105128
if (m_imageChanged) {
106129
update();
107130
m_imageChanged = false;
@@ -199,3 +222,8 @@ const QString &RenderedTarget::bitmapUniqueKey() const
199222
{
200223
return m_bitmapUniqueKey;
201224
}
225+
226+
bool RenderedTarget::mirrorHorizontally() const
227+
{
228+
return m_mirrorHorizontally;
229+
}

ScratchCPPGui/renderedtarget.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RenderedTarget : public QNanoQuickItem
3030
Q_PROPERTY(libscratchcpp::IEngine *engine READ engine WRITE setEngine NOTIFY engineChanged)
3131
Q_PROPERTY(StageModel *stageModel READ stageModel WRITE setStageModel NOTIFY stageModelChanged)
3232
Q_PROPERTY(SpriteModel *spriteModel READ spriteModel WRITE setSpriteModel NOTIFY spriteModelChanged)
33+
Q_PROPERTY(bool mirrorHorizontally READ mirrorHorizontally NOTIFY mirrorHorizontallyChanged)
3334

3435
public:
3536
RenderedTarget(QNanoQuickItem *parent = nullptr);
@@ -54,11 +55,15 @@ class RenderedTarget : public QNanoQuickItem
5455
QBuffer *bitmapBuffer();
5556
const QString &bitmapUniqueKey() const;
5657

58+
bool mirrorHorizontally() const;
59+
5760
signals:
5861
void engineChanged();
5962
void stageModelChanged();
6063
void spriteModelChanged();
6164

65+
void mirrorHorizontallyChanged();
66+
6267
protected:
6368
QNanoQuickItemPainter *createItemPainter() const override;
6469

@@ -81,6 +86,8 @@ class RenderedTarget : public QNanoQuickItem
8186
double m_y = 0;
8287
double m_z = 0;
8388
double m_rotation = 0;
89+
bool m_mirrorHorizontally = false;
90+
bool m_newMirrorHorizontally = false;
8491
double m_originX = 0;
8592
double m_originY = 0;
8693
};

0 commit comments

Comments
 (0)