Skip to content

Commit 9936b16

Browse files
committed
Add IMonitorHandler interface
1 parent 8b9544a commit 9936b16

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ target_sources(scratchcpp
5353
include/scratchcpp/igraphicseffect.h
5454
include/scratchcpp/comment.h
5555
include/scratchcpp/monitor.h
56+
include/scratchcpp/imonitorhandler.h
5657
)
5758

5859
add_library(zip SHARED
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "global.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class Monitor;
11+
12+
class LIBSCRATCHCPP_EXPORT IMonitorHandler
13+
{
14+
public:
15+
virtual ~IMonitorHandler() { }
16+
17+
virtual void init(Monitor *) = 0;
18+
};
19+
20+
} // namespace libscratchcpp

include/scratchcpp/monitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace libscratchcpp
1010
{
1111

12+
class IMonitorHandler;
1213
class Block;
1314
class Sprite;
1415
class Rect;
@@ -29,6 +30,8 @@ class LIBSCRATCHCPP_EXPORT Monitor : public Entity
2930
Monitor(const std::string &id, const std::string &opcode);
3031
Monitor(const Monitor &) = delete;
3132

33+
void setInterface(IMonitorHandler *iface);
34+
3235
Mode mode() const;
3336
void setMode(Mode mode);
3437

src/scratch/monitor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scratchcpp/block.h>
55
#include <scratchcpp/sprite.h>
66
#include <scratchcpp/rect.h>
7+
#include <scratchcpp/imonitorhandler.h>
78

89
#include "monitor_p.h"
910
#include "engine/internal/randomgenerator.h"
@@ -22,6 +23,15 @@ Monitor::Monitor(const std::string &id, const std::string &opcode) :
2223
{
2324
}
2425

26+
/*! Sets the monitor interface. */
27+
void Monitor::setInterface(IMonitorHandler *iface)
28+
{
29+
impl->iface = iface;
30+
31+
if (iface)
32+
iface->init(this);
33+
}
34+
2535
/*! Returns the monitor's mode. */
2636
Monitor::Mode Monitor::mode() const
2737
{

src/scratch/monitor_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct MonitorPrivate
1515
MonitorPrivate(const std::string &opcode);
1616
MonitorPrivate(const MonitorPrivate &) = delete;
1717

18+
IMonitorHandler *iface = nullptr;
1819
Monitor::Mode mode = Monitor::Mode::Default;
1920
std::shared_ptr<Block> block; // Compiler needs shared_ptr
2021
unsigned int width = 0;

test/mocks/monitorhandlermock.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <scratchcpp/imonitorhandler.h>
4+
#include <gmock/gmock.h>
5+
6+
using namespace libscratchcpp;
7+
8+
class MonitorHandlerMock : public IMonitorHandler
9+
{
10+
public:
11+
MOCK_METHOD(void, init, (Monitor *), (override));
12+
};

test/scratch_classes/monitor_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/sprite.h>
44
#include <scratchcpp/rect.h>
55
#include <scratch/monitor_p.h>
6+
#include <monitorhandlermock.h>
67
#include <randomgeneratormock.h>
78

89
#include "../common.h"
@@ -33,6 +34,15 @@ TEST(MonitorTest, Id)
3334
ASSERT_EQ(monitor.id(), "def");
3435
}
3536

37+
TEST(MonitorTest, Interface)
38+
{
39+
Monitor monitor("", "");
40+
MonitorHandlerMock iface;
41+
42+
EXPECT_CALL(iface, init(&monitor));
43+
monitor.setInterface(&iface);
44+
}
45+
3646
TEST(MonitorTest, Mode)
3747
{
3848
Monitor monitor("", "");

0 commit comments

Comments
 (0)