Skip to content

Commit 9ea8a1f

Browse files
committed
Implement sensing_touchingobject block
1 parent 35bb084 commit 9ea8a1f

File tree

4 files changed

+437
-1
lines changed

4 files changed

+437
-1
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
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

515
using namespace libscratchcpp;
@@ -21,4 +31,72 @@ Rgb SensingBlocks::color() const
2131

2232
void 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
}

src/blocks/sensingblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class SensingBlocks : public IExtension
1515
Rgb color() const override;
1616

1717
void registerBlocks(IEngine *engine) override;
18+
19+
private:
20+
static CompilerValue *compileTouchingObject(Compiler *compiler);
1821
};
1922

2023
} // namespace libscratchcpp

test/blocks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ if (LIBSCRATCHCPP_ENABLE_SENSING_BLOCKS)
114114
GTest::gmock_main
115115
scratchcpp
116116
scratchcpp_mocks
117+
block_test_deps
117118
)
118119

119120
gtest_discover_tests(sensing_blocks_test)

0 commit comments

Comments
 (0)