Skip to content

Commit bfb548d

Browse files
authored
Merge pull request #530 from scratchcpp/sprite_dragging_api
Add API for sprite dragging
2 parents 072a37b + 2290b06 commit bfb548d

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

include/scratchcpp/sprite.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class LIBSCRATCHCPP_EXPORT Sprite
4949

5050
void setPosition(double x, double y);
5151

52+
bool dragging() const;
53+
void startDragging();
54+
void stopDragging();
55+
void dragToPosition(double x, double y);
56+
5257
double size() const;
5358
void setSize(double newSize);
5459

src/scratch/sprite.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ double Sprite::x() const
169169
/*! Sets the X position of the sprite. */
170170
void Sprite::setX(double newX)
171171
{
172+
if (impl->dragging)
173+
return;
174+
172175
setXY(newX, impl->y);
173176

174177
if (impl->iface)
@@ -184,6 +187,9 @@ double Sprite::y() const
184187
/*! Sets the Y position of the sprite. */
185188
void Sprite::setY(double newY)
186189
{
190+
if (impl->dragging)
191+
return;
192+
187193
setXY(impl->x, newY);
188194

189195
if (impl->iface)
@@ -193,6 +199,42 @@ void Sprite::setY(double newY)
193199
/* Sets the position of the sprite. */
194200
void Sprite::setPosition(double x, double y)
195201
{
202+
if (impl->dragging)
203+
return;
204+
205+
setXY(x, y);
206+
207+
if (impl->iface) {
208+
impl->iface->onXChanged(impl->x);
209+
impl->iface->onYChanged(impl->y);
210+
}
211+
}
212+
213+
/*! Returns true if the sprite is being dragged. */
214+
bool Sprite::dragging() const
215+
{
216+
return impl->dragging;
217+
}
218+
219+
/*! Starts dragging. Calls to setX(), setY() and setPosition() will be ignored until stopDragging() is called. */
220+
void Sprite::startDragging()
221+
{
222+
impl->dragging = true;
223+
}
224+
225+
/*! Stops dragging. */
226+
void Sprite::stopDragging()
227+
{
228+
impl->dragging = false;
229+
}
230+
231+
/*!
232+
* Drags the sprite to the given position.
233+
* \note If startDragging() wasn't called before calling this method, dragging will be started automatically.
234+
*/
235+
void Sprite::dragToPosition(double x, double y)
236+
{
237+
impl->dragging = true;
196238
setXY(x, y);
197239

198240
if (impl->iface) {

src/scratch/sprite_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct SpritePrivate
3030
double size = 100;
3131
double direction = 90;
3232
bool draggable = false;
33+
bool dragging = false;
3334
Sprite::RotationStyle rotationStyle = Sprite::RotationStyle::AllAround;
3435
};
3536

test/scratch_classes/sprite_test.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,59 @@ TEST(SpriteTest, XY)
377377
ASSERT_EQ(sprite.y(), -23);
378378
}
379379

380+
TEST(SpriteTest, Dragging)
381+
{
382+
Sprite sprite;
383+
SpriteHandlerMock handler;
384+
EXPECT_CALL(handler, init);
385+
sprite.setInterface(&handler);
386+
ASSERT_FALSE(sprite.dragging());
387+
388+
sprite.startDragging();
389+
ASSERT_TRUE(sprite.dragging());
390+
391+
sprite.setX(10);
392+
ASSERT_EQ(sprite.x(), 0);
393+
sprite.setY(-56);
394+
ASSERT_EQ(sprite.y(), 0);
395+
sprite.setPosition(-2.8, 54.1);
396+
ASSERT_EQ(sprite.x(), 0);
397+
ASSERT_EQ(sprite.y(), 0);
398+
399+
EXPECT_CALL(handler, onMoved);
400+
EXPECT_CALL(handler, onXChanged);
401+
EXPECT_CALL(handler, onYChanged);
402+
sprite.dragToPosition(34.2, -89.7);
403+
ASSERT_EQ(sprite.x(), 34.2);
404+
ASSERT_EQ(sprite.y(), -89.7);
405+
ASSERT_TRUE(sprite.dragging());
406+
407+
sprite.stopDragging();
408+
ASSERT_FALSE(sprite.dragging());
409+
410+
EXPECT_CALL(handler, onMoved);
411+
EXPECT_CALL(handler, onXChanged);
412+
EXPECT_CALL(handler, onYChanged);
413+
sprite.setPosition(-2.8, 54.1);
414+
ASSERT_EQ(sprite.x(), -2.8);
415+
ASSERT_EQ(sprite.y(), 54.1);
416+
417+
EXPECT_CALL(handler, onMoved);
418+
EXPECT_CALL(handler, onXChanged);
419+
EXPECT_CALL(handler, onYChanged);
420+
sprite.dragToPosition(-34.2, 89.7);
421+
ASSERT_EQ(sprite.x(), -34.2);
422+
ASSERT_EQ(sprite.y(), 89.7);
423+
ASSERT_TRUE(sprite.dragging());
424+
425+
sprite.setPosition(-2.8, 54.1);
426+
ASSERT_EQ(sprite.x(), -34.2);
427+
ASSERT_EQ(sprite.y(), 89.7);
428+
429+
sprite.stopDragging();
430+
ASSERT_FALSE(sprite.dragging());
431+
}
432+
380433
TEST(SpriteTest, Size)
381434
{
382435
Sprite sprite;

0 commit comments

Comments
 (0)