Skip to content

Commit 00abb3a

Browse files
committed
Add Drawable class
1 parent 944ae5c commit 00abb3a

File tree

12 files changed

+104
-27
lines changed

12 files changed

+104
-27
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ target_sources(scratchcpp
5151
include/scratchcpp/block.h
5252
include/scratchcpp/istagehandler.h
5353
include/scratchcpp/ispritehandler.h
54+
include/scratchcpp/drawable.h
5455
include/scratchcpp/target.h
5556
include/scratchcpp/stage.h
5657
include/scratchcpp/sprite.h

include/scratchcpp/drawable.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "global.h"
6+
#include "spimpl.h"
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class DrawablePrivate;
12+
13+
/*! \brief The Drawable class is the base class of rendered elements (stage, sprites, text bubbles). */
14+
class LIBSCRATCHCPP_EXPORT Drawable
15+
{
16+
public:
17+
Drawable();
18+
Drawable(const Drawable &) = delete;
19+
20+
int layerOrder() const;
21+
virtual void setLayerOrder(int newLayerOrder);
22+
23+
private:
24+
spimpl::unique_impl_ptr<DrawablePrivate> impl;
25+
};
26+
27+
} // namespace libscratchcpp

include/scratchcpp/target.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
#include <vector>
66

7-
#include "global.h"
8-
#include "spimpl.h"
7+
#include "drawable.h"
98
#include "rect.h"
109
#include "sound.h"
1110

@@ -24,7 +23,7 @@ class IGraphicsEffect;
2423
class TargetPrivate;
2524

2625
/*! \brief The Target class is the Stage or a Sprite. */
27-
class LIBSCRATCHCPP_EXPORT Target
26+
class LIBSCRATCHCPP_EXPORT Target : public Drawable
2827
{
2928
public:
3029
enum class BubbleType
@@ -83,9 +82,6 @@ class LIBSCRATCHCPP_EXPORT Target
8382
std::shared_ptr<Sound> soundAt(int index) const;
8483
int findSound(const std::string &soundName) const;
8584

86-
int layerOrder() const;
87-
virtual void setLayerOrder(int newLayerOrder);
88-
8985
double volume() const;
9086
void setVolume(double newVolume);
9187

src/scratch/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ target_sources(scratchcpp
22
PRIVATE
33
value_functions.cpp
44
value_functions_p.h
5+
drawable.cpp
6+
drawable_p.cpp
7+
drawable_p.h
58
target.cpp
69
target_p.cpp
710
target_p.h

src/scratch/drawable.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/drawable.h>
4+
5+
#include "drawable_p.h"
6+
7+
using namespace libscratchcpp;
8+
9+
/*! Constructs Drawable. */
10+
Drawable::Drawable() :
11+
impl(spimpl::make_unique_impl<DrawablePrivate>())
12+
{
13+
}
14+
15+
/*! Returns the layer number. */
16+
int Drawable::layerOrder() const
17+
{
18+
return impl->layerOrder;
19+
}
20+
21+
/*! Sets the layer number. */
22+
void Drawable::setLayerOrder(int newLayerOrder)
23+
{
24+
impl->layerOrder = newLayerOrder;
25+
}

src/scratch/drawable_p.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "drawable_p.h"
4+
5+
using namespace libscratchcpp;

src/scratch/drawable_p.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
namespace libscratchcpp
6+
{
7+
8+
struct DrawablePrivate
9+
{
10+
int layerOrder = 0;
11+
};
12+
13+
} // namespace libscratchcpp

src/scratch/target.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static std::unordered_map<Sound::Effect, std::pair<double, double>> SOUND_EFFECT
2424

2525
/*! Constructs target. */
2626
Target::Target() :
27+
Drawable(),
2728
impl(spimpl::make_unique_impl<TargetPrivate>())
2829
{
2930
}
@@ -394,18 +395,6 @@ int Target::findSound(const std::string &soundName) const
394395
return it - impl->sounds.begin();
395396
}
396397

397-
/*! Returns the layer number. */
398-
int Target::layerOrder() const
399-
{
400-
return impl->layerOrder;
401-
}
402-
403-
/*! Sets the layer number. */
404-
void Target::setLayerOrder(int newLayerOrder)
405-
{
406-
impl->layerOrder = newLayerOrder;
407-
}
408-
409398
/*! Returns the volume. */
410399
double Target::volume() const
411400
{

src/scratch/target_p.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ struct TargetPrivate
3434
int costumeIndex = -1;
3535
std::vector<std::shared_ptr<Costume>> costumes;
3636
std::vector<std::shared_ptr<Sound>> sounds;
37-
int layerOrder = 0;
3837
double volume = 100;
3938
std::unordered_map<Sound::Effect, double> soundEffects;
4039
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;

test/scratch_classes/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ target_link_libraries(
2626

2727
gtest_discover_tests(list_test)
2828

29+
# drawable_test
30+
add_executable(
31+
drawable_test
32+
drawable_test.cpp
33+
)
34+
35+
target_link_libraries(
36+
drawable_test
37+
GTest::gtest_main
38+
scratchcpp
39+
)
40+
41+
gtest_discover_tests(drawable_test)
42+
2943
# sprite_test
3044
add_executable(
3145
sprite_test

0 commit comments

Comments
 (0)