11// SPDX-License-Identifier: Apache-2.0
22
3+ #include < scratchcpp/iengine.h>
4+ #include < scratchcpp/compiler.h>
5+ #include < scratchcpp/compilerconstant.h>
6+ #include < scratchcpp/value.h>
7+ #include < scratchcpp/input.h>
8+ #include < scratchcpp/sprite.h>
9+ #include < scratchcpp/stringptr.h>
10+ #include < scratchcpp/string_functions.h>
11+ #include < utf8.h>
12+
313#include " sensingblocks.h"
414
515using namespace libscratchcpp ;
@@ -21,4 +31,72 @@ Rgb SensingBlocks::color() const
2131
2232void SensingBlocks::registerBlocks (IEngine *engine)
2333{
34+ engine->addCompileFunction (this , " sensing_touchingobject" , &compileTouchingObject);
35+ }
36+
37+ CompilerValue *SensingBlocks::compileTouchingObject (Compiler *compiler)
38+ {
39+ IEngine *engine = compiler->engine ();
40+ Input *input = compiler->input (" TOUCHINGOBJECTMENU" );
41+
42+ if (input->pointsToDropdownMenu ()) {
43+ std::string value = input->selectedMenuItem ();
44+
45+ if (value == " _mouse_" )
46+ return compiler->addTargetFunctionCall (" sensing_touching_mouse" , Compiler::StaticType::Bool);
47+ else if (value == " _edge_" )
48+ return compiler->addTargetFunctionCall (" sensing_touching_edge" , Compiler::StaticType::Bool);
49+ else if (value != " _stage_" ) {
50+ Target *target = engine->targetAt (engine->findTarget (value));
51+
52+ if (target && !target->isStage ()) {
53+ CompilerValue *targetPtr = compiler->addConstValue (target);
54+ return compiler->addTargetFunctionCall (" sensing_touching_sprite" , Compiler::StaticType::Bool, { Compiler::StaticType::Pointer }, { targetPtr });
55+ }
56+ }
57+ } else {
58+ CompilerValue *object = compiler->addInput (input);
59+ return compiler->addTargetFunctionCall (" sensing_touchingobject" , Compiler::StaticType::Bool, { Compiler::StaticType::String }, { object });
60+ }
61+
62+ return compiler->addConstValue (false );
63+ }
64+
65+ extern " C" bool sensing_touching_mouse (Target *target)
66+ {
67+ IEngine *engine = target->engine ();
68+ return target->touchingPoint (engine->mouseX (), engine->mouseY ());
69+ }
70+
71+ extern " C" bool sensing_touching_edge (Target *target)
72+ {
73+ return target->touchingEdge ();
74+ }
75+
76+ extern " C" bool sensing_touching_sprite (Target *target, Sprite *sprite)
77+ {
78+ return target->touchingSprite (sprite);
79+ }
80+
81+ extern " C" bool sensing_touchingobject (Target *target, const StringPtr *object)
82+ {
83+ static const StringPtr MOUSE_STR (" _mouse_" );
84+ static const StringPtr EDGE_STR (" _edge_" );
85+ static const StringPtr STAGE_STR (" _stage_" );
86+
87+ if (string_compare_case_sensitive (object, &MOUSE_STR) == 0 )
88+ return sensing_touching_mouse (target);
89+ else if (string_compare_case_sensitive (object, &EDGE_STR) == 0 )
90+ return sensing_touching_edge (target);
91+ else if (string_compare_case_sensitive (object, &STAGE_STR) != 0 ) {
92+ IEngine *engine = target->engine ();
93+ // TODO: Use UTF-16 in engine
94+ std::string u8name = utf8::utf16to8 (std::u16string (object->data ));
95+ Target *objTarget = engine->targetAt (engine->findTarget (u8name));
96+
97+ if (objTarget)
98+ return sensing_touching_sprite (target, static_cast <Sprite *>(objTarget));
99+ }
100+
101+ return false ;
24102}
0 commit comments