Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ target_sources(scratchcpp
include/scratchcpp/block.h
include/scratchcpp/istagehandler.h
include/scratchcpp/ispritehandler.h
include/scratchcpp/drawable.h
include/scratchcpp/target.h
include/scratchcpp/stage.h
include/scratchcpp/sprite.h
include/scratchcpp/textbubble.h
include/scratchcpp/itimer.h
include/scratchcpp/keyevent.h
include/scratchcpp/rect.h
Expand Down
38 changes: 38 additions & 0 deletions include/scratchcpp/drawable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "global.h"
#include "spimpl.h"

namespace libscratchcpp
{

class IEngine;

class DrawablePrivate;

/*! \brief The Drawable class is the base class of rendered elements (stage, sprites, text bubbles). */
class LIBSCRATCHCPP_EXPORT Drawable
{
public:
Drawable();
Drawable(const Drawable &) = delete;

/*! Returns true if this Drawable is a Target. */
virtual bool isTarget() const { return false; }

/*! Returns true if this Drawable is a TextBubble. */
virtual bool isTextBubble() const { return false; }

int layerOrder() const;
virtual void setLayerOrder(int newLayerOrder);

IEngine *engine() const;
virtual void setEngine(IEngine *engine);

private:
spimpl::unique_impl_ptr<DrawablePrivate> impl;
};

} // namespace libscratchcpp
11 changes: 6 additions & 5 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class IExtension;
class Broadcast;
class Block;
class Field;
class Drawable;
class Target;
class Sprite;
class Stage;
Expand Down Expand Up @@ -332,19 +333,19 @@ class LIBSCRATCHCPP_EXPORT IEngine
virtual int findTarget(const std::string &targetName) const = 0;

/*! Moves the given sprite to the front layer. */
virtual void moveSpriteToFront(Sprite *sprite) = 0;
virtual void moveDrawableToFront(Drawable *drawable) = 0;

/*! Moves the given sprite to the back layer. */
virtual void moveSpriteToBack(Sprite *sprite) = 0;
virtual void moveDrawableToBack(Drawable *drawable) = 0;

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

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

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

/*! Returns the Stage. */
virtual Stage *stage() const = 0;
Expand Down
6 changes: 0 additions & 6 deletions include/scratchcpp/ispritehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
/*! Called when all graphics effects are cleared. */
virtual void onGraphicsEffectsCleared() = 0;

/*! Called when the bubble type changes. */
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;

/*! Called when the bubble text changes. */
virtual void onBubbleTextChanged(const std::string &text) = 0;

/*! Used to get the current costume width. */
virtual int costumeWidth() const = 0;

Expand Down
6 changes: 0 additions & 6 deletions include/scratchcpp/istagehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
/*! Called when all graphics effects are cleared. */
virtual void onGraphicsEffectsCleared() = 0;

/*! Called when the bubble type changes. */
virtual void onBubbleTypeChanged(Target::BubbleType type) = 0;

/*! Called when the bubble text changes. */
virtual void onBubbleTextChanged(const std::string &text) = 0;

/*! Used to get the current costume width. */
virtual int costumeWidth() const = 0;

Expand Down
3 changes: 0 additions & 3 deletions include/scratchcpp/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ class LIBSCRATCHCPP_EXPORT Sprite

void clearGraphicsEffects() override;

virtual void setBubbleType(Target::BubbleType type) override;
virtual void setBubbleText(const std::string &text) override;

private:
Target *dataSource() const override;
bool touchingClones(const std::vector<Sprite *> &clones) const override;
Expand Down
3 changes: 0 additions & 3 deletions include/scratchcpp/stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target

void clearGraphicsEffects() override;

virtual void setBubbleType(Target::BubbleType type) override;
virtual void setBubbleText(const std::string &text) override;

private:
bool touchingClones(const std::vector<Sprite *> &clones) const override;

Expand Down
28 changes: 8 additions & 20 deletions include/scratchcpp/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,34 @@

#include <vector>

#include "global.h"
#include "spimpl.h"
#include "drawable.h"
#include "rect.h"
#include "sound.h"

namespace libscratchcpp
{

class IEngine;
class Variable;
class List;
class Block;
class Comment;
class Costume;
class Sound;
class Sprite;
class TextBubble;
class IGraphicsEffect;
class TargetPrivate;

/*! \brief The Target class is the Stage or a Sprite. */
class LIBSCRATCHCPP_EXPORT Target
class LIBSCRATCHCPP_EXPORT Target : public Drawable
{
public:
enum class BubbleType
{
Say,
Think
};

Target();
Target(const Target &) = delete;
virtual ~Target() { }

bool isTarget() const override final;

/*! Returns true if this Target is the stage. */
virtual bool isStage() const { return false; }

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

int layerOrder() const;
virtual void setLayerOrder(int newLayerOrder);

double volume() const;
void setVolume(double newVolume);

Expand All @@ -108,14 +100,10 @@ class LIBSCRATCHCPP_EXPORT Target

virtual void clearGraphicsEffects();

BubbleType bubbleType() const;
virtual void setBubbleType(BubbleType type);

const std::string &bubbleText() const;
virtual void setBubbleText(const std::string &text);
TextBubble *bubble();
const TextBubble *bubble() const;

IEngine *engine() const;
void setEngine(IEngine *engine);
void setEngine(IEngine *engine) override final;

protected:
/*! Override this method to set a custom data source for blocks, assets, comments, etc. */
Expand Down
40 changes: 40 additions & 0 deletions include/scratchcpp/textbubble.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "drawable.h"
#include "signal.h"

namespace libscratchcpp
{

class TextBubblePrivate;

/*! \brief The TextBubble class represents a text bubble created using say or think block. */
class TextBubble : public Drawable
{
public:
enum class Type
{
Say,
Think
};

TextBubble();
TextBubble(const TextBubble &) = delete;

bool isTextBubble() const override final;

Type type() const;
virtual void setType(Type type);
sigslot::signal<Type> &typeChanged() const;

const std::string &text() const;
virtual void setText(const std::string &text);
sigslot::signal<const std::string &> &textChanged() const;

private:
spimpl::unique_impl_ptr<TextBubblePrivate> impl;
};

} // namespace libscratchcpp
26 changes: 13 additions & 13 deletions src/blocks/looksblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void LooksBlocks::onInit(IEngine *engine)
const auto &targets = engine->targets();

for (auto target : targets) {
target->setBubbleText("");
target->bubble()->setText("");
target->clearGraphicsEffects();
}
});
Expand Down Expand Up @@ -520,13 +520,13 @@ bool LooksBlocks::wait(VirtualMachine *vm)
}
}

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

if (target) {
target->setBubbleType(type);
target->setBubbleText(text);
target->bubble()->setType(type);
target->bubble()->setText(text);
m_waitingBubbles.erase(target);
}
}
Expand All @@ -536,7 +536,7 @@ void LooksBlocks::hideBubble(Target *target)
if (!target)
return;

target->setBubbleText("");
target->bubble()->setText("");
m_waitingBubbles.erase(target);
}

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

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

unsigned int LooksBlocks::say(VirtualMachine *vm)
{
showBubble(vm, Target::BubbleType::Say, vm->getInput(0, 1)->toString());
showBubble(vm, TextBubble::Type::Say, vm->getInput(0, 1)->toString());
return 1;
}

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

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

unsigned int LooksBlocks::think(VirtualMachine *vm)
{
showBubble(vm, Target::BubbleType::Think, vm->getInput(0, 1)->toString());
showBubble(vm, TextBubble::Type::Think, vm->getInput(0, 1)->toString());
return 1;
}

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

if (sprite)
vm->engine()->moveSpriteToFront(sprite);
vm->engine()->moveDrawableToFront(sprite);

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

if (sprite)
vm->engine()->moveSpriteToBack(sprite);
vm->engine()->moveDrawableToBack(sprite);

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

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

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

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

return 1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/looksblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <scratchcpp/iextension.h>
#include <scratchcpp/target.h>
#include <scratchcpp/textbubble.h>
#include <vector>
#include <unordered_map>
#include <chrono>
Expand Down Expand Up @@ -91,7 +92,7 @@ class LooksBlocks : public IExtension

static void startWait(VirtualMachine *vm, double secs);
static bool wait(VirtualMachine *vm);
static void showBubble(VirtualMachine *vm, Target::BubbleType type, const std::string &text);
static void showBubble(VirtualMachine *vm, TextBubble::Type type, const std::string &text);
static void hideBubble(Target *target);

static unsigned int startSayForSecs(VirtualMachine *vm);
Expand Down
Loading
Loading