11// SPDX-License-Identifier: Apache-2.0
22
3+ #include < scratchcpp/iengine.h>
4+ #include < scratchcpp/dev/compiler.h>
5+ #include < scratchcpp/block.h>
6+ #include < scratchcpp/field.h>
7+ #include < scratchcpp/broadcast.h>
8+ #include < scratchcpp/dev/executioncontext.h>
9+ #include < scratchcpp/thread.h>
10+ #include < scratchcpp/dev/compilerconstant.h>
11+
312#include " eventblocks.h"
413
514using namespace libscratchcpp ;
@@ -16,4 +25,105 @@ std::string libscratchcpp::EventBlocks::description() const
1625
1726void EventBlocks::registerBlocks (IEngine *engine)
1827{
28+ engine->addCompileFunction (this , " event_whentouchingobject" , &compileWhenTouchingObject);
29+ engine->addCompileFunction (this , " event_whenflagclicked" , &compileWhenFlagClicked);
30+ engine->addCompileFunction (this , " event_whenthisspriteclicked" , &compileWhenThisSpriteClicked);
31+ engine->addCompileFunction (this , " event_whenstageclicked" , &compileWhenStageClicked);
32+ engine->addCompileFunction (this , " event_whenbroadcastreceived" , &compileWhenBroadcastReceived);
33+ engine->addCompileFunction (this , " event_whenbackdropswitchesto" , &compileWhenBackdropSwitchesTo);
34+ engine->addCompileFunction (this , " event_whengreaterthan" , &compileWhenGreaterThan);
35+ engine->addCompileFunction (this , " event_broadcast" , &compileBroadcast);
36+ engine->addCompileFunction (this , " event_broadcastandwait" , &compileBroadcastAndWait);
37+ engine->addCompileFunction (this , " event_whenkeypressed" , &compileWhenKeyPressed);
38+ }
39+
40+ CompilerValue *EventBlocks::compileWhenTouchingObject (Compiler *compiler)
41+ {
42+ compiler->engine ()->addWhenTouchingObjectScript (compiler->block ());
43+ return nullptr ;
44+ }
45+
46+ CompilerValue *EventBlocks::compileWhenFlagClicked (Compiler *compiler)
47+ {
48+ compiler->engine ()->addGreenFlagScript (compiler->block ());
49+ return nullptr ;
50+ }
51+
52+ CompilerValue *EventBlocks::compileWhenThisSpriteClicked (Compiler *compiler)
53+ {
54+ compiler->engine ()->addTargetClickScript (compiler->block ());
55+ return nullptr ;
56+ }
57+
58+ CompilerValue *EventBlocks::compileWhenStageClicked (Compiler *compiler)
59+ {
60+ compiler->engine ()->addTargetClickScript (compiler->block ());
61+ return nullptr ;
62+ }
63+
64+ CompilerValue *EventBlocks::compileWhenBroadcastReceived (Compiler *compiler)
65+ {
66+ auto block = compiler->block ();
67+ Field *field = compiler->field (" BROADCAST_OPTION" );
68+
69+ if (field) {
70+ auto broadcast = std::static_pointer_cast<Broadcast>(field->valuePtr ());
71+ compiler->engine ()->addBroadcastScript (block, field, broadcast.get ());
72+ }
73+
74+ return nullptr ;
75+ }
76+
77+ CompilerValue *EventBlocks::compileWhenBackdropSwitchesTo (Compiler *compiler)
78+ {
79+ auto block = compiler->block ();
80+ Field *field = compiler->field (" BACKDROP" );
81+
82+ if (field)
83+ compiler->engine ()->addBackdropChangeScript (block, field);
84+
85+ return nullptr ;
86+ }
87+
88+ CompilerValue *EventBlocks::compileWhenGreaterThan (Compiler *compiler)
89+ {
90+ compiler->engine ()->addWhenGreaterThanScript (compiler->block ());
91+ return nullptr ;
92+ }
93+
94+ CompilerValue *EventBlocks::compileBroadcast (Compiler *compiler)
95+ {
96+ auto input = compiler->addInput (" BROADCAST_INPUT" );
97+ auto wait = compiler->addConstValue (false );
98+ compiler->addFunctionCallWithCtx (" event_broadcast" , Compiler::StaticType::Void, { Compiler::StaticType::String, Compiler::StaticType::Bool }, { input, wait });
99+ return nullptr ;
100+ }
101+
102+ CompilerValue *EventBlocks::compileBroadcastAndWait (Compiler *compiler)
103+ {
104+ auto input = compiler->addInput (" BROADCAST_INPUT" );
105+ auto wait = compiler->addConstValue (true );
106+ compiler->addFunctionCallWithCtx (" event_broadcast" , Compiler::StaticType::Void, { Compiler::StaticType::String, Compiler::StaticType::Bool }, { input, wait });
107+ return nullptr ;
108+ }
109+
110+ CompilerValue *EventBlocks::compileWhenKeyPressed (Compiler *compiler)
111+ {
112+ auto block = compiler->block ();
113+ Field *field = compiler->field (" KEY_OPTION" );
114+
115+ if (field)
116+ compiler->engine ()->addKeyPressScript (block, field);
117+
118+ return nullptr ;
119+ }
120+
121+ extern " C" void event_broadcast (ExecutionContext *ctx, const char *name, bool wait)
122+ {
123+ Thread *thread = ctx->thread ();
124+ IEngine *engine = thread->engine ();
125+ std::vector<int > broadcasts = engine->findBroadcasts (name);
126+
127+ for (int index : broadcasts)
128+ engine->broadcast (index, thread, wait);
19129}
0 commit comments