Skip to content

Commit 072a37b

Browse files
authored
Merge pull request #529 from scratchcpp/touching_color_block
Implement the touching color block
2 parents ff467df + 9bc869e commit 072a37b

File tree

17 files changed

+179
-0
lines changed

17 files changed

+179
-0
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
8383

8484
/*! Used to check whether the sprite touches the given point (in Scratch coordinates). */
8585
virtual bool touchingPoint(double x, double y) const = 0;
86+
87+
/*! Used to check whether the sprite touches the given color. */
88+
virtual bool touchingColor(const Value &color) const = 0;
8689
};
8790

8891
} // namespace libscratchcpp

include/scratchcpp/istagehandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
6060

6161
/*! Used to check whether the stage touches the given point (in Scratch coordinates). */
6262
virtual bool touchingPoint(double x, double y) const = 0;
63+
64+
/*! Used to check whether the stage touches the given color. */
65+
virtual bool touchingColor(const Value &color) const = 0;
6366
};
6467

6568
} // namespace libscratchcpp

include/scratchcpp/sprite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class LIBSCRATCHCPP_EXPORT Sprite
7373
void keepInFence(double newX, double newY, double *fencedX, double *fencedY) const;
7474

7575
bool touchingPoint(double x, double y) const override;
76+
bool touchingColor(const Value &color) const override;
7677

7778
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
7879

include/scratchcpp/stage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
5252
Rect fastBoundingRect() const override;
5353

5454
bool touchingPoint(double x, double y) const override;
55+
bool touchingColor(const Value &color) const override;
5556

5657
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
5758

include/scratchcpp/target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class LIBSCRATCHCPP_EXPORT Target
9292
bool touchingSprite(Sprite *sprite) const;
9393
virtual bool touchingPoint(double x, double y) const;
9494
bool touchingEdge() const;
95+
virtual bool touchingColor(const Value &color) const;
9596

9697
double graphicsEffectValue(IGraphicsEffect *effect) const;
9798
virtual void setGraphicsEffectValue(IGraphicsEffect *effect, double value);

src/blocks/sensingblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
3131
{
3232
// Blocks
3333
engine->addCompileFunction(this, "sensing_touchingobject", &compileTouchingObject);
34+
engine->addCompileFunction(this, "sensing_touchingcolor", &compileTouchingColor);
3435
engine->addCompileFunction(this, "sensing_distanceto", &compileDistanceTo);
3536
engine->addCompileFunction(this, "sensing_askandwait", &compileAskAndWait);
3637
engine->addCompileFunction(this, "sensing_answer", &compileAnswer);
@@ -58,6 +59,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
5859

5960
// Inputs
6061
engine->addInput(this, "TOUCHINGOBJECTMENU", TOUCHINGOBJECTMENU);
62+
engine->addInput(this, "COLOR", COLOR);
6163
engine->addInput(this, "DISTANCETOMENU", DISTANCETOMENU);
6264
engine->addInput(this, "QUESTION", QUESTION);
6365
engine->addInput(this, "KEY_OPTION", KEY_OPTION);
@@ -116,6 +118,12 @@ void SensingBlocks::compileTouchingObject(Compiler *compiler)
116118
}
117119
}
118120

121+
void SensingBlocks::compileTouchingColor(Compiler *compiler)
122+
{
123+
compiler->addInput(COLOR);
124+
compiler->addFunctionCall(&touchingColor);
125+
}
126+
119127
void SensingBlocks::compileDistanceTo(Compiler *compiler)
120128
{
121129
Input *input = compiler->input(DISTANCETOMENU);
@@ -495,6 +503,12 @@ unsigned int SensingBlocks::touchingEdge(VirtualMachine *vm)
495503
return 0;
496504
}
497505

506+
unsigned int SensingBlocks::touchingColor(VirtualMachine *vm)
507+
{
508+
vm->replaceReturnValue(vm->target()->touchingColor(*vm->getInput(0, 1)), 1);
509+
return 0;
510+
}
511+
498512
unsigned int SensingBlocks::keyPressed(VirtualMachine *vm)
499513
{
500514
vm->replaceReturnValue(vm->engine()->keyPressed(vm->getInput(0, 1)->toString()), 1);

src/blocks/sensingblocks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SensingBlocks : public IBlockSection
2121
enum Inputs
2222
{
2323
TOUCHINGOBJECTMENU,
24+
COLOR,
2425
DISTANCETOMENU,
2526
QUESTION,
2627
KEY_OPTION,
@@ -61,6 +62,7 @@ class SensingBlocks : public IBlockSection
6162
void registerBlocks(IEngine *engine) override;
6263

6364
static void compileTouchingObject(Compiler *compiler);
65+
static void compileTouchingColor(Compiler *compiler);
6466
static void compileDistanceTo(Compiler *compiler);
6567
static void compileAskAndWait(Compiler *compiler);
6668
static void compileAnswer(Compiler *compiler);
@@ -90,6 +92,8 @@ class SensingBlocks : public IBlockSection
9092
static unsigned int touchingMousePointer(VirtualMachine *vm);
9193
static unsigned int touchingEdge(VirtualMachine *vm);
9294

95+
static unsigned int touchingColor(VirtualMachine *vm);
96+
9397
static unsigned int keyPressed(VirtualMachine *vm);
9498
static unsigned int mouseDown(VirtualMachine *vm);
9599
static unsigned int mouseX(VirtualMachine *vm);

src/scratch/sprite.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,15 @@ bool Sprite::touchingPoint(double x, double y) const
415415
return impl->iface->touchingPoint(x, y);
416416
}
417417

418+
/*! Overrides Target#touchingColor(). */
419+
bool Sprite::touchingColor(const Value &color) const
420+
{
421+
if (!impl->iface)
422+
return false;
423+
424+
return impl->iface->touchingColor(color);
425+
}
426+
418427
/*! Overrides Target#setGraphicsEffectValue(). */
419428
void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
420429
{

src/scratch/stage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ bool Stage::touchingPoint(double x, double y) const
158158
return impl->iface->touchingPoint(x, y);
159159
}
160160

161+
/*! Overrides Target#touchingColor(). */
162+
bool Stage::touchingColor(const Value &color) const
163+
{
164+
if (!impl->iface)
165+
return false;
166+
167+
return impl->iface->touchingColor(color);
168+
}
169+
161170
/*! Overrides Target#setGraphicsEffectValue(). */
162171
void Stage::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
163172
{

src/scratch/target.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ bool Target::touchingEdge() const
476476
return false;
477477
}
478478

479+
/*! Returns true if the Target is touching the given color (RGB triplet). */
480+
bool Target::touchingColor(const Value &color) const
481+
{
482+
return false;
483+
}
484+
479485
/*! Returns the value of the given graphics effect. */
480486
double Target::graphicsEffectValue(IGraphicsEffect *effect) const
481487
{

0 commit comments

Comments
 (0)