Skip to content

Commit 703f1fc

Browse files
committed
Add sensing monitor name functions
1 parent a53d908 commit 703f1fc

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <scratchcpp/stage.h>
1111
#include <scratchcpp/costume.h>
1212
#include <scratchcpp/variable.h>
13+
#include <scratchcpp/block.h>
1314
#include "sensingblocks.h"
1415

1516
#include "../engine/internal/clock.h"
@@ -38,6 +39,14 @@ void SensingBlocks::registerBlocks(IEngine *engine)
3839
engine->addCompileFunction(this, "sensing_current", &compileCurrent);
3940
engine->addCompileFunction(this, "sensing_dayssince2000", &compileDaysSince2000);
4041

42+
// Monitor names
43+
engine->addMonitorNameFunction(this, "sensing_mousedown", &mouseDownMonitorName);
44+
engine->addMonitorNameFunction(this, "sensing_mousex", &mouseXMonitorName);
45+
engine->addMonitorNameFunction(this, "sensing_mousey", &mouseYMonitorName);
46+
engine->addMonitorNameFunction(this, "sensing_timer", &timerMonitorName);
47+
engine->addMonitorNameFunction(this, "sensing_current", &currentMonitorName);
48+
engine->addMonitorNameFunction(this, "sensing_dayssince2000", &daysSince2000MonitorName);
49+
4150
// Inputs
4251
engine->addInput(this, "DISTANCETOMENU", DISTANCETOMENU);
4352
engine->addInput(this, "KEY_OPTION", KEY_OPTION);
@@ -310,6 +319,83 @@ void SensingBlocks::compileDaysSince2000(Compiler *compiler)
310319
compiler->addFunctionCall(&daysSince2000);
311320
}
312321

322+
const std::string &SensingBlocks::mouseDownMonitorName(Block *block)
323+
{
324+
static const std::string name = "mouse down?";
325+
return name;
326+
}
327+
328+
const std::string &SensingBlocks::mouseXMonitorName(Block *block)
329+
{
330+
static const std::string name = "mouse x";
331+
return name;
332+
}
333+
334+
const std::string &SensingBlocks::mouseYMonitorName(Block *block)
335+
{
336+
static const std::string name = "mouse y";
337+
return name;
338+
}
339+
340+
const std::string &SensingBlocks::timerMonitorName(Block *block)
341+
{
342+
static const std::string name = "timer";
343+
return name;
344+
}
345+
346+
const std::string &SensingBlocks::currentMonitorName(Block *block)
347+
{
348+
int id = block->findFieldById(CURRENTMENU)->specialValueId();
349+
350+
switch (id) {
351+
case YEAR: {
352+
static const std::string name = "year";
353+
return name;
354+
}
355+
356+
case MONTH: {
357+
static const std::string name = "month";
358+
return name;
359+
}
360+
361+
case DATE: {
362+
static const std::string name = "date";
363+
return name;
364+
}
365+
366+
case DAYOFWEEK: {
367+
static const std::string name = "day of week";
368+
return name;
369+
}
370+
371+
case HOUR: {
372+
static const std::string name = "hour";
373+
return name;
374+
}
375+
376+
case MINUTE: {
377+
static const std::string name = "minute";
378+
return name;
379+
}
380+
381+
case SECOND: {
382+
static const std::string name = "second";
383+
return name;
384+
}
385+
386+
default: {
387+
static const std::string name = "";
388+
return name;
389+
}
390+
}
391+
}
392+
393+
const std::string &SensingBlocks::daysSince2000MonitorName(Block *block)
394+
{
395+
static const std::string name = "days since 2000";
396+
return name;
397+
}
398+
313399
unsigned int SensingBlocks::keyPressed(VirtualMachine *vm)
314400
{
315401
vm->replaceReturnValue(vm->engine()->keyPressed(vm->getInput(0, 1)->toString()), 1);

src/blocks/sensingblocks.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ class SensingBlocks : public IBlockSection
6464
static void compileCurrent(Compiler *compiler);
6565
static void compileDaysSince2000(Compiler *compiler);
6666

67+
static const std::string &mouseDownMonitorName(Block *block);
68+
static const std::string &mouseXMonitorName(Block *block);
69+
static const std::string &mouseYMonitorName(Block *block);
70+
static const std::string &timerMonitorName(Block *block);
71+
static const std::string &currentMonitorName(Block *block);
72+
static const std::string &daysSince2000MonitorName(Block *block);
73+
6774
static unsigned int keyPressed(VirtualMachine *vm);
6875
static unsigned int mouseDown(VirtualMachine *vm);
6976
static unsigned int mouseX(VirtualMachine *vm);

test/blocks/sensing_blocks_test.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
117117
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_current", &SensingBlocks::compileCurrent));
118118
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_dayssince2000", &SensingBlocks::compileDaysSince2000));
119119

120+
// Monitor names
121+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "sensing_mousedown", &SensingBlocks::mouseDownMonitorName));
122+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "sensing_mousex", &SensingBlocks::mouseXMonitorName));
123+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "sensing_mousey", &SensingBlocks::mouseYMonitorName));
124+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "sensing_timer", &SensingBlocks::timerMonitorName));
125+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "sensing_current", &SensingBlocks::currentMonitorName));
126+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "sensing_dayssince2000", &SensingBlocks::daysSince2000MonitorName));
127+
120128
// Inputs
121129
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DISTANCETOMENU", SensingBlocks::DISTANCETOMENU));
122130
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "KEY_OPTION", SensingBlocks::KEY_OPTION));
@@ -377,6 +385,11 @@ TEST_F(SensingBlocksTest, MouseDown)
377385
ASSERT_TRUE(compiler.constValues().empty());
378386
}
379387

388+
TEST_F(SensingBlocksTest, MouseDownMonitorName)
389+
{
390+
ASSERT_EQ(SensingBlocks::mouseDownMonitorName(nullptr), "mouse down?");
391+
}
392+
380393
TEST_F(SensingBlocksTest, MouseDownImpl)
381394
{
382395
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
@@ -418,6 +431,11 @@ TEST_F(SensingBlocksTest, MouseX)
418431
ASSERT_TRUE(compiler.constValues().empty());
419432
}
420433

434+
TEST_F(SensingBlocksTest, MouseXMonitorName)
435+
{
436+
ASSERT_EQ(SensingBlocks::mouseXMonitorName(nullptr), "mouse x");
437+
}
438+
421439
TEST_F(SensingBlocksTest, MouseXImpl)
422440
{
423441
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
@@ -459,6 +477,11 @@ TEST_F(SensingBlocksTest, MouseY)
459477
ASSERT_TRUE(compiler.constValues().empty());
460478
}
461479

480+
TEST_F(SensingBlocksTest, MouseYMonitorName)
481+
{
482+
ASSERT_EQ(SensingBlocks::mouseYMonitorName(nullptr), "mouse y");
483+
}
484+
462485
TEST_F(SensingBlocksTest, MouseYImpl)
463486
{
464487
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
@@ -552,6 +575,11 @@ TEST_F(SensingBlocksTest, Timer)
552575
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
553576
}
554577

578+
TEST_F(SensingBlocksTest, TimerMonitorName)
579+
{
580+
ASSERT_EQ(SensingBlocks::timerMonitorName(nullptr), "timer");
581+
}
582+
555583
TEST_F(SensingBlocksTest, TimerImpl)
556584
{
557585
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
@@ -1386,6 +1414,38 @@ TEST_F(SensingBlocksTest, Current)
13861414
std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_EXEC, 1, vm::OP_EXEC, 2, vm::OP_EXEC, 3, vm::OP_EXEC, 4, vm::OP_EXEC, 5, vm::OP_EXEC, 6, vm::OP_HALT }));
13871415
}
13881416

1417+
TEST_F(SensingBlocksTest, CurrentMonitorName)
1418+
{
1419+
// current [year]
1420+
auto block1 = createSensingCurrentBlock("a", "YEAR", SensingBlocks::YEAR);
1421+
1422+
// current [month]
1423+
auto block2 = createSensingCurrentBlock("b", "MONTH", SensingBlocks::MONTH);
1424+
1425+
// current [date]
1426+
auto block3 = createSensingCurrentBlock("c", "DATE", SensingBlocks::DATE);
1427+
1428+
// current [day of week]
1429+
auto block4 = createSensingCurrentBlock("d", "DAYOFWEEK", SensingBlocks::DAYOFWEEK);
1430+
1431+
// current [hour]
1432+
auto block5 = createSensingCurrentBlock("e", "HOUR", SensingBlocks::HOUR);
1433+
1434+
// current [minute]
1435+
auto block6 = createSensingCurrentBlock("f", "MINUTE", SensingBlocks::MINUTE);
1436+
1437+
// current [second]
1438+
auto block7 = createSensingCurrentBlock("g", "SECOND", SensingBlocks::SECOND);
1439+
1440+
ASSERT_EQ(SensingBlocks::currentMonitorName(block1.get()), "year");
1441+
ASSERT_EQ(SensingBlocks::currentMonitorName(block2.get()), "month");
1442+
ASSERT_EQ(SensingBlocks::currentMonitorName(block3.get()), "date");
1443+
ASSERT_EQ(SensingBlocks::currentMonitorName(block4.get()), "day of week");
1444+
ASSERT_EQ(SensingBlocks::currentMonitorName(block5.get()), "hour");
1445+
ASSERT_EQ(SensingBlocks::currentMonitorName(block6.get()), "minute");
1446+
ASSERT_EQ(SensingBlocks::currentMonitorName(block7.get()), "second");
1447+
}
1448+
13891449
TEST_F(SensingBlocksTest, CurrentYear)
13901450
{
13911451
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
@@ -1521,6 +1581,11 @@ TEST_F(SensingBlocksTest, DaysSince2000)
15211581
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
15221582
}
15231583

1584+
TEST_F(SensingBlocksTest, DaysSince2000MonitorName)
1585+
{
1586+
ASSERT_EQ(SensingBlocks::daysSince2000MonitorName(nullptr), "days since 2000");
1587+
}
1588+
15241589
TEST_F(SensingBlocksTest, DaysSince2000Impl)
15251590
{
15261591
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };

0 commit comments

Comments
 (0)