@@ -30,6 +30,7 @@ std::string SensingBlocks::name() const
3030void SensingBlocks::registerBlocks (IEngine *engine)
3131{
3232 // Blocks
33+ engine->addCompileFunction (this , " sensing_touchingobject" , &compileTouchingObject);
3334 engine->addCompileFunction (this , " sensing_distanceto" , &compileDistanceTo);
3435 engine->addCompileFunction (this , " sensing_askandwait" , &compileAskAndWait);
3536 engine->addCompileFunction (this , " sensing_answer" , &compileAnswer);
@@ -56,6 +57,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
5657 engine->addMonitorNameFunction (this , " sensing_dayssince2000" , &daysSince2000MonitorName);
5758
5859 // Inputs
60+ engine->addInput (this , " TOUCHINGOBJECTMENU" , TOUCHINGOBJECTMENU);
5961 engine->addInput (this , " DISTANCETOMENU" , DISTANCETOMENU);
6062 engine->addInput (this , " QUESTION" , QUESTION);
6163 engine->addInput (this , " KEY_OPTION" , KEY_OPTION);
@@ -91,6 +93,29 @@ void SensingBlocks::registerBlocks(IEngine *engine)
9193 engine->questionAnswered ().connect (&onAnswer);
9294}
9395
96+ void SensingBlocks::compileTouchingObject (Compiler *compiler)
97+ {
98+ Input *input = compiler->input (TOUCHINGOBJECTMENU);
99+
100+ if (input->type () != Input::Type::ObscuredShadow) {
101+ assert (input->pointsToDropdownMenu ());
102+ std::string value = input->selectedMenuItem ();
103+
104+ if (value == " _mouse_" )
105+ compiler->addFunctionCall (&touchingMousePointer);
106+ else if (value == " _edge_" )
107+ compiler->addFunctionCall (&touchingEdge);
108+ else {
109+ int index = compiler->engine ()->findTarget (value);
110+ compiler->addConstValue (index);
111+ compiler->addFunctionCall (&touchingObjectByIndex);
112+ }
113+ } else {
114+ compiler->addInput (input);
115+ compiler->addFunctionCall (&touchingObject);
116+ }
117+ }
118+
94119void SensingBlocks::compileDistanceTo (Compiler *compiler)
95120{
96121 Input *input = compiler->input (DISTANCETOMENU);
@@ -435,6 +460,41 @@ const std::string &SensingBlocks::daysSince2000MonitorName(Block *block)
435460 return name;
436461}
437462
463+ unsigned int SensingBlocks::touchingObject (VirtualMachine *vm)
464+ {
465+ std::string value = vm->getInput (0 , 1 )->toString ();
466+
467+ if (value == " _mouse_" )
468+ vm->replaceReturnValue (vm->target ()->touchingPoint (vm->engine ()->mouseX (), vm->engine ()->mouseY ()), 1 );
469+ else if (value == " _edge_" )
470+ vm->replaceReturnValue (vm->target ()->touchingEdge (), 1 );
471+ else {
472+ Target *target = vm->engine ()->targetAt (vm->engine ()->findTarget (value));
473+ vm->replaceReturnValue (touchingObjectCommon (vm->target (), target), 1 );
474+ }
475+
476+ return 0 ;
477+ }
478+
479+ unsigned int SensingBlocks::touchingObjectByIndex (VirtualMachine *vm)
480+ {
481+ Target *target = vm->engine ()->targetAt (vm->getInput (0 , 1 )->toInt ());
482+ vm->replaceReturnValue (touchingObjectCommon (vm->target (), target), 1 );
483+ return 0 ;
484+ }
485+
486+ unsigned int SensingBlocks::touchingMousePointer (VirtualMachine *vm)
487+ {
488+ vm->addReturnValue (vm->target ()->touchingPoint (vm->engine ()->mouseX (), vm->engine ()->mouseY ()));
489+ return 0 ;
490+ }
491+
492+ unsigned int SensingBlocks::touchingEdge (VirtualMachine *vm)
493+ {
494+ vm->addReturnValue (vm->target ()->touchingEdge ());
495+ return 0 ;
496+ }
497+
438498unsigned int SensingBlocks::keyPressed (VirtualMachine *vm)
439499{
440500 vm->replaceReturnValue (vm->engine ()->keyPressed (vm->getInput (0 , 1 )->toString ()), 1 );
@@ -921,6 +981,14 @@ unsigned int SensingBlocks::daysSince2000(VirtualMachine *vm)
921981 return 0 ;
922982}
923983
984+ bool SensingBlocks::touchingObjectCommon (Target *source, Target *target)
985+ {
986+ if (source && target && !target->isStage ())
987+ return source->touchingSprite (static_cast <Sprite *>(target));
988+
989+ return false ;
990+ }
991+
924992void SensingBlocks::enqueueAsk (const std::string &question, VirtualMachine *vm)
925993{
926994 // https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L117-L119
0 commit comments