Skip to content

Commit c402c8e

Browse files
committed
Move engine property from Target to Drawable
1 parent d293b86 commit c402c8e

File tree

9 files changed

+42
-32
lines changed

9 files changed

+42
-32
lines changed

include/scratchcpp/drawable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace libscratchcpp
99
{
1010

11+
class IEngine;
12+
1113
class DrawablePrivate;
1214

1315
/*! \brief The Drawable class is the base class of rendered elements (stage, sprites, text bubbles). */
@@ -26,6 +28,9 @@ class LIBSCRATCHCPP_EXPORT Drawable
2628
int layerOrder() const;
2729
virtual void setLayerOrder(int newLayerOrder);
2830

31+
IEngine *engine() const;
32+
void setEngine(IEngine *engine);
33+
2934
private:
3035
spimpl::unique_impl_ptr<DrawablePrivate> impl;
3136
};

include/scratchcpp/target.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace libscratchcpp
1212
{
1313

14-
class IEngine;
1514
class Variable;
1615
class List;
1716
class Block;
@@ -104,9 +103,6 @@ class LIBSCRATCHCPP_EXPORT Target : public Drawable
104103
TextBubble *bubble();
105104
const TextBubble *bubble() const;
106105

107-
IEngine *engine() const;
108-
void setEngine(IEngine *engine);
109-
110106
protected:
111107
/*! Override this method to set a custom data source for blocks, assets, comments, etc. */
112108
virtual Target *dataSource() const { return nullptr; }

src/scratch/drawable.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ void Drawable::setLayerOrder(int newLayerOrder)
2323
{
2424
impl->layerOrder = newLayerOrder;
2525
}
26+
27+
/*! Returns the engine. */
28+
IEngine *Drawable::engine() const
29+
{
30+
return impl->engine;
31+
}
32+
33+
/*! Sets the engine. */
34+
void Drawable::setEngine(IEngine *engine)
35+
{
36+
impl->engine = engine;
37+
}

src/scratch/drawable_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace libscratchcpp
66
{
77

8+
class IEngine;
9+
810
struct DrawablePrivate
911
{
1012
int layerOrder = 0;
13+
IEngine *engine = nullptr;
1114
};
1215

1316
} // namespace libscratchcpp

src/scratch/target.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,10 @@ void Target::setCostumeIndex(int newCostumeIndex)
280280
impl->costumeIndex = newCostumeIndex;
281281

282282
if (isStage()) {
283-
if (impl->engine)
284-
impl->engine->requestRedraw();
283+
IEngine *eng = engine();
284+
285+
if (eng)
286+
eng->requestRedraw();
285287
}
286288
}
287289

@@ -509,9 +511,11 @@ bool Target::touchingPoint(double x, double y) const
509511
bool Target::touchingEdge() const
510512
{
511513
// https://github.com/scratchfoundation/scratch-vm/blob/8dbcc1fc8f8d8c4f1e40629fe8a388149d6dfd1c/src/sprites/rendered-target.js#L772-L785
512-
if (impl->engine) {
513-
const double stageWidth = impl->engine->stageWidth();
514-
const double stageHeight = impl->engine->stageHeight();
514+
IEngine *eng = engine();
515+
516+
if (eng) {
517+
const double stageWidth = eng->stageWidth();
518+
const double stageHeight = eng->stageHeight();
515519
Rect bounds = boundingRect();
516520

517521
if ((bounds.left() < -stageWidth / 2) || (bounds.right() > stageWidth / 2) || (bounds.top() > stageHeight / 2) || (bounds.bottom() < -stageHeight / 2))
@@ -568,15 +572,3 @@ const TextBubble *Target::bubble() const
568572
{
569573
return &impl->bubble;
570574
}
571-
572-
/*! Returns the engine. */
573-
IEngine *Target::engine() const
574-
{
575-
return impl->engine;
576-
}
577-
578-
/*! Sets the engine. */
579-
void Target::setEngine(IEngine *engine)
580-
{
581-
impl->engine = engine;
582-
}

src/scratch/target_p.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ struct TargetPrivate
2626
TargetPrivate();
2727
TargetPrivate(const TargetPrivate &) = delete;
2828

29-
IEngine *engine = nullptr;
3029
std::string name;
3130
std::vector<std::shared_ptr<Variable>> variables;
3231
std::vector<std::shared_ptr<List>> lists;

test/scratch_classes/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ add_executable(
3535
target_link_libraries(
3636
drawable_test
3737
GTest::gtest_main
38+
GTest::gmock_main
3839
scratchcpp
40+
scratchcpp_mocks
3941
)
4042

4143
gtest_discover_tests(drawable_test)

test/scratch_classes/drawable_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/drawable.h>
2+
#include <enginemock.h>
23

34
#include "../common.h"
45

@@ -23,3 +24,13 @@ TEST(DrawableTest, LayerOrder)
2324
drawable.setLayerOrder(2);
2425
ASSERT_EQ(drawable.layerOrder(), 2);
2526
}
27+
28+
TEST(TargetTest, Engine)
29+
{
30+
Drawable drawable;
31+
ASSERT_EQ(drawable.engine(), nullptr);
32+
33+
EngineMock engine;
34+
drawable.setEngine(&engine);
35+
ASSERT_EQ(drawable.engine(), &engine);
36+
}

test/scratch_classes/target_test.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,6 @@ TEST(SpriteTest, BubbleTextRedraw)
797797
target.bubble()->setText("");
798798
}
799799

800-
TEST(TargetTest, Engine)
801-
{
802-
Target target;
803-
ASSERT_EQ(target.engine(), nullptr);
804-
805-
EngineMock engine;
806-
target.setEngine(&engine);
807-
ASSERT_EQ(target.engine(), &engine);
808-
}
809-
810800
TEST(TargetTest, DataSource)
811801
{
812802
TargetMock target;

0 commit comments

Comments
 (0)