Skip to content

Commit 14c3d56

Browse files
authored
Merge pull request #570 from scratchcpp/sensing_colositouchingcolor
Implement sensing_coloristouchingcolor block
2 parents eea2f63 + 4d57b32 commit 14c3d56

File tree

17 files changed

+145
-0
lines changed

17 files changed

+145
-0
lines changed

include/scratchcpp/ispritehandler.h

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

9393
/*! Used to check whether the sprite touches the given color. */
9494
virtual bool touchingColor(const Value &color) const = 0;
95+
96+
/*! Used to check whether the mask part of the sprite touches the given color. */
97+
virtual bool touchingColor(const Value &color, const Value &mask) const = 0;
9598
};
9699

97100
} // namespace libscratchcpp

include/scratchcpp/istagehandler.h

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

7070
/*! Used to check whether the stage touches the given color. */
7171
virtual bool touchingColor(const Value &color) const = 0;
72+
73+
/*! Used to check whether the mask part of the stage touches the given color. */
74+
virtual bool touchingColor(const Value &color, const Value &mask) const = 0;
7275
};
7376

7477
} // namespace libscratchcpp

include/scratchcpp/sprite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class LIBSCRATCHCPP_EXPORT Sprite
8282

8383
bool touchingPoint(double x, double y) const override;
8484
bool touchingColor(const Value &color) const override;
85+
bool touchingColor(const Value &color, const Value &mask) const override;
8586

8687
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
8788

include/scratchcpp/stage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
5656

5757
bool touchingPoint(double x, double y) const override;
5858
bool touchingColor(const Value &color) const override;
59+
bool touchingColor(const Value &color, const Value &mask) const override;
5960

6061
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
6162

include/scratchcpp/target.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class LIBSCRATCHCPP_EXPORT Target
101101
virtual bool touchingPoint(double x, double y) const;
102102
bool touchingEdge() const;
103103
virtual bool touchingColor(const Value &color) const;
104+
virtual bool touchingColor(const Value &color, const Value &mask) const;
104105

105106
double graphicsEffectValue(IGraphicsEffect *effect) const;
106107
virtual void setGraphicsEffectValue(IGraphicsEffect *effect, double value);

src/blocks/sensingblocks.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
3232
// Blocks
3333
engine->addCompileFunction(this, "sensing_touchingobject", &compileTouchingObject);
3434
engine->addCompileFunction(this, "sensing_touchingcolor", &compileTouchingColor);
35+
engine->addCompileFunction(this, "sensing_coloristouchingcolor", &compileColorIsTouchingColor);
3536
engine->addCompileFunction(this, "sensing_distanceto", &compileDistanceTo);
3637
engine->addCompileFunction(this, "sensing_askandwait", &compileAskAndWait);
3738
engine->addCompileFunction(this, "sensing_answer", &compileAnswer);
@@ -60,6 +61,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
6061
// Inputs
6162
engine->addInput(this, "TOUCHINGOBJECTMENU", TOUCHINGOBJECTMENU);
6263
engine->addInput(this, "COLOR", COLOR);
64+
engine->addInput(this, "COLOR2", COLOR2);
6365
engine->addInput(this, "DISTANCETOMENU", DISTANCETOMENU);
6466
engine->addInput(this, "QUESTION", QUESTION);
6567
engine->addInput(this, "KEY_OPTION", KEY_OPTION);
@@ -139,6 +141,13 @@ void SensingBlocks::compileTouchingColor(Compiler *compiler)
139141
compiler->addFunctionCall(&touchingColor);
140142
}
141143

144+
void SensingBlocks::compileColorIsTouchingColor(Compiler *compiler)
145+
{
146+
compiler->addInput(COLOR2); // target color
147+
compiler->addInput(COLOR); // mask color
148+
compiler->addFunctionCall(&colorIsTouchingColor);
149+
}
150+
142151
void SensingBlocks::compileDistanceTo(Compiler *compiler)
143152
{
144153
Input *input = compiler->input(DISTANCETOMENU);
@@ -525,6 +534,12 @@ unsigned int SensingBlocks::touchingColor(VirtualMachine *vm)
525534
return 0;
526535
}
527536

537+
unsigned int SensingBlocks::colorIsTouchingColor(VirtualMachine *vm)
538+
{
539+
vm->replaceReturnValue(vm->target()->touchingColor(*vm->getInput(0, 2), *vm->getInput(1, 2)), 2);
540+
return 1;
541+
}
542+
528543
unsigned int SensingBlocks::keyPressed(VirtualMachine *vm)
529544
{
530545
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
@@ -22,6 +22,7 @@ class SensingBlocks : public IBlockSection
2222
{
2323
TOUCHINGOBJECTMENU,
2424
COLOR,
25+
COLOR2,
2526
DISTANCETOMENU,
2627
QUESTION,
2728
KEY_OPTION,
@@ -64,6 +65,7 @@ class SensingBlocks : public IBlockSection
6465

6566
static void compileTouchingObject(Compiler *compiler);
6667
static void compileTouchingColor(Compiler *compiler);
68+
static void compileColorIsTouchingColor(Compiler *compiler);
6769
static void compileDistanceTo(Compiler *compiler);
6870
static void compileAskAndWait(Compiler *compiler);
6971
static void compileAnswer(Compiler *compiler);
@@ -95,6 +97,8 @@ class SensingBlocks : public IBlockSection
9597

9698
static unsigned int touchingColor(VirtualMachine *vm);
9799

100+
static unsigned int colorIsTouchingColor(VirtualMachine *vm);
101+
98102
static unsigned int keyPressed(VirtualMachine *vm);
99103
static unsigned int mouseDown(VirtualMachine *vm);
100104
static unsigned int mouseX(VirtualMachine *vm);

src/scratch/sprite.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ bool Sprite::touchingColor(const Value &color) const
503503
return impl->iface->touchingColor(color);
504504
}
505505

506+
/*! Overrides Target#touchingColor(). */
507+
bool Sprite::touchingColor(const Value &color, const Value &mask) const
508+
{
509+
if (!impl->iface)
510+
return false;
511+
512+
return impl->iface->touchingColor(color, mask);
513+
}
514+
506515
/*! Overrides Target#setGraphicsEffectValue(). */
507516
void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
508517
{

src/scratch/stage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ bool Stage::touchingColor(const Value &color) const
185185
return impl->iface->touchingColor(color);
186186
}
187187

188+
/*! Overrides Target#touchingColor(). */
189+
bool Stage::touchingColor(const Value &color, const Value &mask) const
190+
{
191+
if (!impl->iface)
192+
return false;
193+
194+
return impl->iface->touchingColor(color, mask);
195+
}
196+
188197
/*! Overrides Target#setGraphicsEffectValue(). */
189198
void Stage::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
190199
{

src/scratch/target.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,12 @@ bool Target::touchingColor(const Value &color) const
520520
return false;
521521
}
522522

523+
/*! Returns true if the mask part of the Target is touching the given color (RGB triplet). */
524+
bool Target::touchingColor(const Value &color, const Value &mask) const
525+
{
526+
return false;
527+
}
528+
523529
/*! Returns the value of the given graphics effect. */
524530
double Target::graphicsEffectValue(IGraphicsEffect *effect) const
525531
{

0 commit comments

Comments
 (0)