Skip to content

Implement set drag mode block #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/blocks/sensingblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
engine->addCompileFunction(this, "sensing_mousedown", &compileMouseDown);
engine->addCompileFunction(this, "sensing_mousex", &compileMouseX);
engine->addCompileFunction(this, "sensing_mousey", &compileMouseY);
engine->addCompileFunction(this, "sensing_setdragmode", &compileSetDragMode);
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
engine->addCompileFunction(this, "sensing_current", &compileCurrent);
Expand All @@ -39,6 +40,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)

// Fields
engine->addField(this, "CURRENTMENU", CURRENTMENU);
engine->addField(this, "DRAG_MODE", DRAG_MODE);

// Field values
engine->addFieldValue(this, "YEAR", YEAR);
Expand All @@ -48,6 +50,8 @@ void SensingBlocks::registerBlocks(IEngine *engine)
engine->addFieldValue(this, "HOUR", HOUR);
engine->addFieldValue(this, "MINUTE", MINUTE);
engine->addFieldValue(this, "SECOND", SECOND);
engine->addFieldValue(this, "draggable", Draggable);
engine->addFieldValue(this, "not draggable", NotDraggable);
}

void SensingBlocks::compileDistanceTo(Compiler *compiler)
Expand Down Expand Up @@ -92,6 +96,24 @@ void SensingBlocks::compileMouseY(Compiler *compiler)
compiler->addFunctionCall(&mouseY);
}

void SensingBlocks::compileSetDragMode(Compiler *compiler)
{
int option = compiler->field(DRAG_MODE)->specialValueId();

switch (option) {
case Draggable:
compiler->addFunctionCall(&setDraggableMode);
break;

case NotDraggable:
compiler->addFunctionCall(&setNotDraggableMode);
break;

default:
break;
}
}

void SensingBlocks::compileTimer(Compiler *compiler)
{
compiler->addFunctionCall(&timer);
Expand Down Expand Up @@ -169,6 +191,22 @@ unsigned int SensingBlocks::mouseY(VirtualMachine *vm)
return 0;
}

unsigned int SensingBlocks::setDraggableMode(VirtualMachine *vm)
{
if (Sprite *sprite = dynamic_cast<Sprite *>(vm->target()))
sprite->setDraggable(true);

return 0;
}

unsigned int SensingBlocks::setNotDraggableMode(VirtualMachine *vm)
{
if (Sprite *sprite = dynamic_cast<Sprite *>(vm->target()))
sprite->setDraggable(false);

return 0;
}

unsigned int SensingBlocks::distanceTo(VirtualMachine *vm)
{
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
Expand Down
11 changes: 9 additions & 2 deletions src/blocks/sensingblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class SensingBlocks : public IBlockSection

enum Fields
{
CURRENTMENU
CURRENTMENU,
DRAG_MODE
};

enum FieldValues
Expand All @@ -31,7 +32,9 @@ class SensingBlocks : public IBlockSection
DAYOFWEEK,
HOUR,
MINUTE,
SECOND
SECOND,
Draggable,
NotDraggable
};

std::string name() const override;
Expand All @@ -43,6 +46,7 @@ class SensingBlocks : public IBlockSection
static void compileMouseDown(Compiler *compiler);
static void compileMouseX(Compiler *compiler);
static void compileMouseY(Compiler *compiler);
static void compileSetDragMode(Compiler *compiler);
static void compileTimer(Compiler *compiler);
static void compileResetTimer(Compiler *compiler);
static void compileCurrent(Compiler *compiler);
Expand All @@ -53,6 +57,9 @@ class SensingBlocks : public IBlockSection
static unsigned int mouseX(VirtualMachine *vm);
static unsigned int mouseY(VirtualMachine *vm);

static unsigned int setDraggableMode(VirtualMachine *vm);
static unsigned int setNotDraggableMode(VirtualMachine *vm);

static unsigned int distanceTo(VirtualMachine *vm);
static unsigned int distanceToByIndex(VirtualMachine *vm);
static unsigned int distanceToMousePointer(VirtualMachine *vm);
Expand Down
58 changes: 58 additions & 0 deletions test/blocks/sensing_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_mousedown", &SensingBlocks::compileMouseDown));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_mousex", &SensingBlocks::compileMouseX));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_mousey", &SensingBlocks::compileMouseY));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_setdragmode", &SensingBlocks::compileSetDragMode));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_timer", &SensingBlocks::compileTimer));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_resettimer", &SensingBlocks::compileResetTimer));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_current", &SensingBlocks::compileCurrent));
Expand All @@ -113,6 +114,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)

// Fields
EXPECT_CALL(m_engineMock, addField(m_section.get(), "CURRENTMENU", SensingBlocks::CURRENTMENU));
EXPECT_CALL(m_engineMock, addField(m_section.get(), "DRAG_MODE", SensingBlocks::DRAG_MODE));

// Field values
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "YEAR", SensingBlocks::YEAR));
Expand All @@ -122,6 +124,8 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "HOUR", SensingBlocks::HOUR));
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "MINUTE", SensingBlocks::MINUTE));
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "SECOND", SensingBlocks::SECOND));
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "draggable", SensingBlocks::Draggable));
EXPECT_CALL(m_engineMock, addFieldValue(m_section.get(), "not draggable", SensingBlocks::NotDraggable));

m_section->registerBlocks(&m_engineMock);
}
Expand Down Expand Up @@ -465,6 +469,60 @@ TEST_F(SensingBlocksTest, MouseYImpl)
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), -95.564);
}

TEST_F(SensingBlocksTest, SetDragMode)
{
Compiler compiler(&m_engineMock);

// set drag mode [draggable]
auto block1 = std::make_shared<Block>("a", "sensing_setdragmode");
addDropdownField(block1, "DRAG_MODE", SensingBlocks::DRAG_MODE, "draggable", SensingBlocks::Draggable);

// set drag mode [not draggable]
auto block2 = std::make_shared<Block>("b", "sensing_setdragmode");
addDropdownField(block2, "DRAG_MODE", SensingBlocks::DRAG_MODE, "not draggable", SensingBlocks::NotDraggable);

compiler.init();

EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::setDraggableMode)).WillOnce(Return(0));
compiler.setBlock(block1);
SensingBlocks::compileSetDragMode(&compiler);

EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::setNotDraggableMode)).WillOnce(Return(1));
compiler.setBlock(block2);
SensingBlocks::compileSetDragMode(&compiler);

compiler.end();

ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_EXEC, 1, vm::OP_HALT }));
ASSERT_TRUE(compiler.constValues().empty());
}

TEST_F(SensingBlocksTest, SetDragModeImpl)
{
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_EXEC, 1, vm::OP_HALT };
static BlockFunc functions[] = { &SensingBlocks::setDraggableMode, &SensingBlocks::setNotDraggableMode };

Sprite sprite;
sprite.setDraggable(false);

VirtualMachine vm(&sprite, &m_engineMock, nullptr);
vm.setFunctions(functions);

vm.setBytecode(bytecode1);
vm.run();

ASSERT_EQ(vm.registerCount(), 0);
ASSERT_TRUE(sprite.draggable());

vm.reset();
vm.setBytecode(bytecode2);
vm.run();

ASSERT_EQ(vm.registerCount(), 0);
ASSERT_FALSE(sprite.draggable());
}

TEST_F(SensingBlocksTest, Timer)
{
Compiler compiler(&m_engineMock);
Expand Down