Skip to content

Commit 0f02a54

Browse files
authored
Merge pull request #586 from scratchcpp/bubble_layering
Implement bubble layering
2 parents 944ae5c + 6495212 commit 0f02a54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+897
-570
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ 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
58+
include/scratchcpp/textbubble.h
5759
include/scratchcpp/itimer.h
5860
include/scratchcpp/keyevent.h
5961
include/scratchcpp/rect.h

include/scratchcpp/drawable.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 IEngine;
12+
13+
class DrawablePrivate;
14+
15+
/*! \brief The Drawable class is the base class of rendered elements (stage, sprites, text bubbles). */
16+
class LIBSCRATCHCPP_EXPORT Drawable
17+
{
18+
public:
19+
Drawable();
20+
Drawable(const Drawable &) = delete;
21+
22+
/*! Returns true if this Drawable is a Target. */
23+
virtual bool isTarget() const { return false; }
24+
25+
/*! Returns true if this Drawable is a TextBubble. */
26+
virtual bool isTextBubble() const { return false; }
27+
28+
int layerOrder() const;
29+
virtual void setLayerOrder(int newLayerOrder);
30+
31+
IEngine *engine() const;
32+
virtual void setEngine(IEngine *engine);
33+
34+
private:
35+
spimpl::unique_impl_ptr<DrawablePrivate> impl;
36+
};
37+
38+
} // namespace libscratchcpp

include/scratchcpp/iengine.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class IExtension;
1818
class Broadcast;
1919
class Block;
2020
class Field;
21+
class Drawable;
2122
class Target;
2223
class Sprite;
2324
class Stage;
@@ -332,19 +333,19 @@ class LIBSCRATCHCPP_EXPORT IEngine
332333
virtual int findTarget(const std::string &targetName) const = 0;
333334

334335
/*! Moves the given sprite to the front layer. */
335-
virtual void moveSpriteToFront(Sprite *sprite) = 0;
336+
virtual void moveDrawableToFront(Drawable *drawable) = 0;
336337

337338
/*! Moves the given sprite to the back layer. */
338-
virtual void moveSpriteToBack(Sprite *sprite) = 0;
339+
virtual void moveDrawableToBack(Drawable *drawable) = 0;
339340

340341
/*! Moves the given sprite forward a number of layers. */
341-
virtual void moveSpriteForwardLayers(Sprite *sprite, int layers) = 0;
342+
virtual void moveDrawableForwardLayers(Drawable *drawable, int layers) = 0;
342343

343344
/*! Moves the given sprite backward a number of layers. */
344-
virtual void moveSpriteBackwardLayers(Sprite *sprite, int layers) = 0;
345+
virtual void moveDrawableBackwardLayers(Drawable *drawable, int layers) = 0;
345346

346347
/*! Moves the given sprite behind some other sprite. */
347-
virtual void moveSpriteBehindOther(Sprite *sprite, Sprite *other) = 0;
348+
virtual void moveDrawableBehindOther(Drawable *drawable, Drawable *other) = 0;
348349

349350
/*! Returns the Stage. */
350351
virtual Stage *stage() const = 0;

include/scratchcpp/ispritehandler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
5959
/*! Called when all graphics effects are cleared. */
6060
virtual void onGraphicsEffectsCleared() = 0;
6161

62-
/*! Called when the bubble type changes. */
63-
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;
64-
65-
/*! Called when the bubble text changes. */
66-
virtual void onBubbleTextChanged(const std::string &text) = 0;
67-
6862
/*! Used to get the current costume width. */
6963
virtual int costumeWidth() const = 0;
7064

include/scratchcpp/istagehandler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
4040
/*! Called when all graphics effects are cleared. */
4141
virtual void onGraphicsEffectsCleared() = 0;
4242

43-
/*! Called when the bubble type changes. */
44-
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;
45-
46-
/*! Called when the bubble text changes. */
47-
virtual void onBubbleTextChanged(const std::string &text) = 0;
48-
4943
/*! Used to get the current costume width. */
5044
virtual int costumeWidth() const = 0;
5145

include/scratchcpp/sprite.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ class LIBSCRATCHCPP_EXPORT Sprite
8888

8989
void clearGraphicsEffects() override;
9090

91-
virtual void setBubbleType(Target::BubbleType type) override;
92-
virtual void setBubbleText(const std::string &text) override;
93-
9491
private:
9592
Target *dataSource() const override;
9693
bool touchingClones(const std::vector<Sprite *> &clones) const override;

include/scratchcpp/stage.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
6262

6363
void clearGraphicsEffects() override;
6464

65-
virtual void setBubbleType(Target::BubbleType type) override;
66-
virtual void setBubbleText(const std::string &text) override;
67-
6865
private:
6966
bool touchingClones(const std::vector<Sprite *> &clones) const override;
7067

include/scratchcpp/target.h

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,34 @@
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

1211
namespace libscratchcpp
1312
{
1413

15-
class IEngine;
1614
class Variable;
1715
class List;
1816
class Block;
1917
class Comment;
2018
class Costume;
2119
class Sound;
2220
class Sprite;
21+
class TextBubble;
2322
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:
30-
enum class BubbleType
31-
{
32-
Say,
33-
Think
34-
};
35-
3629
Target();
3730
Target(const Target &) = delete;
3831
virtual ~Target() { }
3932

33+
bool isTarget() const override final;
34+
4035
/*! Returns true if this Target is the stage. */
4136
virtual bool isStage() const { return false; }
4237

@@ -83,9 +78,6 @@ class LIBSCRATCHCPP_EXPORT Target
8378
std::shared_ptr<Sound> soundAt(int index) const;
8479
int findSound(const std::string &soundName) const;
8580

86-
int layerOrder() const;
87-
virtual void setLayerOrder(int newLayerOrder);
88-
8981
double volume() const;
9082
void setVolume(double newVolume);
9183

@@ -108,14 +100,10 @@ class LIBSCRATCHCPP_EXPORT Target
108100

109101
virtual void clearGraphicsEffects();
110102

111-
BubbleType bubbleType() const;
112-
virtual void setBubbleType(BubbleType type);
113-
114-
const std::string &bubbleText() const;
115-
virtual void setBubbleText(const std::string &text);
103+
TextBubble *bubble();
104+
const TextBubble *bubble() const;
116105

117-
IEngine *engine() const;
118-
void setEngine(IEngine *engine);
106+
void setEngine(IEngine *engine) override final;
119107

120108
protected:
121109
/*! Override this method to set a custom data source for blocks, assets, comments, etc. */

include/scratchcpp/textbubble.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "drawable.h"
6+
#include "signal.h"
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class TextBubblePrivate;
12+
13+
/*! \brief The TextBubble class represents a text bubble created using say or think block. */
14+
class TextBubble : public Drawable
15+
{
16+
public:
17+
enum class Type
18+
{
19+
Say,
20+
Think
21+
};
22+
23+
TextBubble();
24+
TextBubble(const TextBubble &) = delete;
25+
26+
bool isTextBubble() const override final;
27+
28+
Type type() const;
29+
virtual void setType(Type type);
30+
sigslot::signal<Type> &typeChanged() const;
31+
32+
const std::string &text() const;
33+
virtual void setText(const std::string &text);
34+
sigslot::signal<const std::string &> &textChanged() const;
35+
36+
private:
37+
spimpl::unique_impl_ptr<TextBubblePrivate> impl;
38+
};
39+
40+
} // namespace libscratchcpp

src/blocks/looksblocks.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void LooksBlocks::onInit(IEngine *engine)
115115
const auto &targets = engine->targets();
116116

117117
for (auto target : targets) {
118-
target->setBubbleText("");
118+
target->bubble()->setText("");
119119
target->clearGraphicsEffects();
120120
}
121121
});
@@ -520,13 +520,13 @@ bool LooksBlocks::wait(VirtualMachine *vm)
520520
}
521521
}
522522

523-
void LooksBlocks::showBubble(VirtualMachine *vm, Target::BubbleType type, const std::string &text)
523+
void LooksBlocks::showBubble(VirtualMachine *vm, TextBubble::Type type, const std::string &text)
524524
{
525525
Target *target = vm->target();
526526

527527
if (target) {
528-
target->setBubbleType(type);
529-
target->setBubbleText(text);
528+
target->bubble()->setType(type);
529+
target->bubble()->setText(text);
530530
m_waitingBubbles.erase(target);
531531
}
532532
}
@@ -536,7 +536,7 @@ void LooksBlocks::hideBubble(Target *target)
536536
if (!target)
537537
return;
538538

539-
target->setBubbleText("");
539+
target->bubble()->setText("");
540540
m_waitingBubbles.erase(target);
541541
}
542542

@@ -545,7 +545,7 @@ unsigned int LooksBlocks::startSayForSecs(VirtualMachine *vm)
545545
Target *target = vm->target();
546546

547547
if (target) {
548-
showBubble(vm, Target::BubbleType::Say, vm->getInput(0, 2)->toString());
548+
showBubble(vm, TextBubble::Type::Say, vm->getInput(0, 2)->toString());
549549
m_waitingBubbles[target] = vm;
550550
startWait(vm, vm->getInput(1, 2)->toDouble());
551551
}
@@ -572,7 +572,7 @@ unsigned int LooksBlocks::sayForSecs(VirtualMachine *vm)
572572

573573
unsigned int LooksBlocks::say(VirtualMachine *vm)
574574
{
575-
showBubble(vm, Target::BubbleType::Say, vm->getInput(0, 1)->toString());
575+
showBubble(vm, TextBubble::Type::Say, vm->getInput(0, 1)->toString());
576576
return 1;
577577
}
578578

@@ -581,7 +581,7 @@ unsigned int LooksBlocks::startThinkForSecs(VirtualMachine *vm)
581581
Target *target = vm->target();
582582

583583
if (target) {
584-
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 2)->toString());
584+
showBubble(vm, TextBubble::Type::Think, vm->getInput(0, 2)->toString());
585585
m_waitingBubbles[target] = vm;
586586
startWait(vm, vm->getInput(1, 2)->toDouble());
587587
}
@@ -596,7 +596,7 @@ unsigned int LooksBlocks::thinkForSecs(VirtualMachine *vm)
596596

597597
unsigned int LooksBlocks::think(VirtualMachine *vm)
598598
{
599-
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 1)->toString());
599+
showBubble(vm, TextBubble::Type::Think, vm->getInput(0, 1)->toString());
600600
return 1;
601601
}
602602

@@ -1015,7 +1015,7 @@ unsigned int LooksBlocks::goToFront(VirtualMachine *vm)
10151015
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
10161016

10171017
if (sprite)
1018-
vm->engine()->moveSpriteToFront(sprite);
1018+
vm->engine()->moveDrawableToFront(sprite);
10191019

10201020
return 0;
10211021
}
@@ -1025,7 +1025,7 @@ unsigned int LooksBlocks::goToBack(VirtualMachine *vm)
10251025
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
10261026

10271027
if (sprite)
1028-
vm->engine()->moveSpriteToBack(sprite);
1028+
vm->engine()->moveDrawableToBack(sprite);
10291029

10301030
return 0;
10311031
}
@@ -1035,7 +1035,7 @@ unsigned int LooksBlocks::goForwardLayers(VirtualMachine *vm)
10351035
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
10361036

10371037
if (sprite)
1038-
vm->engine()->moveSpriteForwardLayers(sprite, vm->getInput(0, 1)->toInt());
1038+
vm->engine()->moveDrawableForwardLayers(sprite, vm->getInput(0, 1)->toInt());
10391039

10401040
return 1;
10411041
}
@@ -1045,7 +1045,7 @@ unsigned int LooksBlocks::goBackwardLayers(VirtualMachine *vm)
10451045
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
10461046

10471047
if (sprite)
1048-
vm->engine()->moveSpriteBackwardLayers(sprite, vm->getInput(0, 1)->toInt());
1048+
vm->engine()->moveDrawableBackwardLayers(sprite, vm->getInput(0, 1)->toInt());
10491049

10501050
return 1;
10511051
}

0 commit comments

Comments
 (0)