55#include < scratchcpp/field.h>
66#include < scratchcpp/block.h>
77#include < scratchcpp/variable.h>
8+ #include < scratchcpp/stage.h>
9+ #include < scratchcpp/monitor.h>
810
911#include " variableblocks.h"
1012
@@ -21,6 +23,8 @@ void VariableBlocks::registerBlocks(IEngine *engine)
2123 engine->addCompileFunction (this , " data_variable" , &compileVariable);
2224 engine->addCompileFunction (this , " data_setvariableto" , &compileSetVariable);
2325 engine->addCompileFunction (this , " data_changevariableby" , &compileChangeVariableBy);
26+ engine->addCompileFunction (this , " data_showvariable" , &compileShowVariable);
27+ engine->addCompileFunction (this , " data_hidevariable" , &compileHideVariable);
2428
2529 // Monitor names
2630 engine->addMonitorNameFunction (this , " data_variable" , &variableMonitorName);
@@ -53,6 +57,84 @@ void VariableBlocks::compileChangeVariableBy(Compiler *compiler)
5357 compiler->addInstruction (vm::OP_CHANGE_VAR, { compiler->variableIndex (compiler->field (VARIABLE)->valuePtr ()) });
5458}
5559
60+ void VariableBlocks::compileShowVariable (Compiler *compiler)
61+ {
62+ Field *field = compiler->field (VARIABLE);
63+ assert (field);
64+ Variable *var = static_cast <Variable *>(field->valuePtr ().get ());
65+ assert (var);
66+
67+ compiler->addConstValue (var->id ());
68+
69+ if (var->target () == static_cast <Target *>(compiler->engine ()->stage ()))
70+ compiler->addFunctionCall (&showGlobalVariable);
71+ else
72+ compiler->addFunctionCall (&showVariable);
73+ }
74+
75+ void VariableBlocks::compileHideVariable (Compiler *compiler)
76+ {
77+ Field *field = compiler->field (VARIABLE);
78+ assert (field);
79+ Variable *var = static_cast <Variable *>(field->valuePtr ().get ());
80+ assert (var);
81+
82+ compiler->addConstValue (var->id ());
83+
84+ if (var->target () == static_cast <Target *>(compiler->engine ()->stage ()))
85+ compiler->addFunctionCall (&hideGlobalVariable);
86+ else
87+ compiler->addFunctionCall (&hideVariable);
88+ }
89+
90+ void VariableBlocks::setVarVisible (std::shared_ptr<Variable> var, bool visible)
91+ {
92+ if (var) {
93+ assert (var->monitor ());
94+ var->monitor ()->setVisible (visible);
95+ }
96+ }
97+
98+ unsigned int VariableBlocks::showGlobalVariable (VirtualMachine *vm)
99+ {
100+ if (Stage *target = vm->engine ()->stage ()) {
101+ int index = target->findVariableById (vm->getInput (0 , 1 )->toString ());
102+ setVarVisible (target->variableAt (index), true );
103+ }
104+
105+ return 1 ;
106+ }
107+
108+ unsigned int VariableBlocks::showVariable (VirtualMachine *vm)
109+ {
110+ if (Target *target = vm->target ()) {
111+ int index = target->findVariableById (vm->getInput (0 , 1 )->toString ());
112+ setVarVisible (target->variableAt (index), true );
113+ }
114+
115+ return 1 ;
116+ }
117+
118+ unsigned int VariableBlocks::hideGlobalVariable (VirtualMachine *vm)
119+ {
120+ if (Stage *target = vm->engine ()->stage ()) {
121+ int index = target->findVariableById (vm->getInput (0 , 1 )->toString ());
122+ setVarVisible (target->variableAt (index), false );
123+ }
124+
125+ return 1 ;
126+ }
127+
128+ unsigned int VariableBlocks::hideVariable (VirtualMachine *vm)
129+ {
130+ if (Target *target = vm->target ()) {
131+ int index = target->findVariableById (vm->getInput (0 , 1 )->toString ());
132+ setVarVisible (target->variableAt (index), false );
133+ }
134+
135+ return 1 ;
136+ }
137+
56138const std::string &VariableBlocks::variableMonitorName (Block *block)
57139{
58140 Variable *var = dynamic_cast <Variable *>(block->findFieldById (VARIABLE)->valuePtr ().get ());
0 commit comments