Skip to content

Commit 86bfa7b

Browse files
authored
Merge pull request #415 from scratchcpp/monitor_api
Add API for monitors
2 parents c1004e9 + 400ac54 commit 86bfa7b

27 files changed

+1588
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ target_sources(scratchcpp
5252
include/scratchcpp/rect.h
5353
include/scratchcpp/igraphicseffect.h
5454
include/scratchcpp/comment.h
55+
include/scratchcpp/monitor.h
56+
include/scratchcpp/imonitorhandler.h
5557
)
5658

5759
add_library(zip SHARED

include/scratchcpp/block.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
2727

2828
void compile(Compiler *compiler);
2929

30-
std::string opcode() const;
30+
const std::string &opcode() const;
3131

3232
std::shared_ptr<Block> next() const;
33-
std::string nextId() const;
33+
const std::string &nextId() const;
3434
void setNext(std::shared_ptr<Block> block);
3535
void setNextId(const std::string &nextId);
3636

3737
std::shared_ptr<Block> parent() const;
38-
std::string parentId() const;
38+
const std::string &parentId() const;
3939
void setParent(std::shared_ptr<Block> block);
4040
void setParentId(const std::string &id);
4141

@@ -57,7 +57,7 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
5757
bool topLevel() const;
5858

5959
std::shared_ptr<Comment> comment() const;
60-
std::string commentId() const;
60+
const std::string &commentId() const;
6161
void setComment(std::shared_ptr<Comment> comment);
6262
void setCommentId(const std::string &commentId);
6363

include/scratchcpp/iengine.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class List;
2323
class Script;
2424
class ITimer;
2525
class KeyEvent;
26+
class Monitor;
27+
class IMonitorHandler;
2628

2729
/*!
2830
* \brief The IEngine interface provides an API for running Scratch projects.
@@ -84,6 +86,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
8486
/*! Stops all currently playing sounds. */
8587
virtual void stopSounds() = 0;
8688

89+
/*! Updates the values of stage monitors. */
90+
virtual void updateMonitors() = 0;
91+
8792
/*! Steps all currently running threads. Use this to implement a custom event loop. */
8893
virtual void step() = 0;
8994

@@ -300,6 +305,18 @@ class LIBSCRATCHCPP_EXPORT IEngine
300305
/*! Returns the Stage. */
301306
virtual Stage *stage() const = 0;
302307

308+
/*! Returns the list of monitors. */
309+
virtual const std::vector<std::shared_ptr<Monitor>> &monitors() const = 0;
310+
311+
/*! Sets the list of monitors. */
312+
virtual void setMonitors(const std::vector<std::shared_ptr<Monitor>> &newMonitors) = 0;
313+
314+
/*! Sets the function which is called when a monitor is added. */
315+
virtual void setAddMonitorHandler(const std::function<void(Monitor *)> &handler) = 0;
316+
317+
/*! Sets the function which is called when a monitor is removed. */
318+
virtual void setRemoveMonitorHandler(const std::function<void(Monitor *, IMonitorHandler *)> &handler) = 0;
319+
303320
/*! Returns the list of extension names. */
304321
virtual const std::vector<std::string> &extensions() const = 0;
305322

include/scratchcpp/imonitorhandler.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
class Value;
12+
13+
class LIBSCRATCHCPP_EXPORT IMonitorHandler
14+
{
15+
public:
16+
virtual ~IMonitorHandler() { }
17+
18+
virtual void init(Monitor *) = 0;
19+
20+
virtual void onValueChanged(const VirtualMachine *vm) = 0;
21+
virtual void onVisibleChanged(bool visible) = 0;
22+
};
23+
24+
} // namespace libscratchcpp

include/scratchcpp/monitor.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/entity.h>
6+
7+
#include <vector>
8+
9+
namespace libscratchcpp
10+
{
11+
12+
class IMonitorHandler;
13+
class Block;
14+
class Script;
15+
class Sprite;
16+
class Value;
17+
class Rect;
18+
class MonitorPrivate;
19+
20+
/*! \brief The Monitor class represents a stage monitor. */
21+
class LIBSCRATCHCPP_EXPORT Monitor : public Entity
22+
{
23+
public:
24+
friend class Engine;
25+
26+
enum class Mode
27+
{
28+
Default,
29+
Large,
30+
Slider,
31+
List
32+
};
33+
34+
Monitor(const std::string &id, const std::string &opcode);
35+
Monitor(const Monitor &) = delete;
36+
37+
void setInterface(IMonitorHandler *iface);
38+
39+
Mode mode() const;
40+
void setMode(Mode mode);
41+
42+
std::shared_ptr<Block> block() const;
43+
44+
std::shared_ptr<Script> script() const;
45+
void setScript(std::shared_ptr<Script> script);
46+
47+
Sprite *sprite() const;
48+
void setSprite(Sprite *sprite);
49+
50+
const std::string &opcode() const;
51+
52+
void updateValue(const VirtualMachine *vm);
53+
54+
unsigned int width() const;
55+
void setWidth(unsigned int width);
56+
57+
unsigned int height() const;
58+
void setHeight(unsigned int height);
59+
60+
int x() const;
61+
void setX(int x);
62+
63+
int y() const;
64+
void setY(int y);
65+
66+
bool visible() const;
67+
void setVisible(bool visible);
68+
69+
double sliderMin() const;
70+
void setSliderMin(double sliderMin);
71+
72+
double sliderMax() const;
73+
void setSliderMax(double sliderMax);
74+
75+
bool discrete() const;
76+
void setDiscrete(bool discrete);
77+
78+
static Rect getInitialPosition(const std::vector<std::shared_ptr<Monitor>> &other, int monitorWidth, int monitorHeight);
79+
80+
private:
81+
spimpl::unique_impl_ptr<MonitorPrivate> impl;
82+
};
83+
84+
} // namespace libscratchcpp

include/scratchcpp/rect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class LIBSCRATCHCPP_EXPORT Rect
3232
double width() const;
3333
double height() const;
3434

35+
bool intersects(const Rect &rect) const;
36+
3537
private:
3638
spimpl::impl_ptr<RectPrivate> impl;
3739
};

0 commit comments

Comments
 (0)