Skip to content

Add API for monitors #415

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 20 commits into from
Jan 13, 2024
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ target_sources(scratchcpp
include/scratchcpp/rect.h
include/scratchcpp/igraphicseffect.h
include/scratchcpp/comment.h
include/scratchcpp/monitor.h
include/scratchcpp/imonitorhandler.h
)

add_library(zip SHARED
Expand Down
8 changes: 4 additions & 4 deletions include/scratchcpp/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity

void compile(Compiler *compiler);

std::string opcode() const;
const std::string &opcode() const;

std::shared_ptr<Block> next() const;
std::string nextId() const;
const std::string &nextId() const;
void setNext(std::shared_ptr<Block> block);
void setNextId(const std::string &nextId);

std::shared_ptr<Block> parent() const;
std::string parentId() const;
const std::string &parentId() const;
void setParent(std::shared_ptr<Block> block);
void setParentId(const std::string &id);

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

std::shared_ptr<Comment> comment() const;
std::string commentId() const;
const std::string &commentId() const;
void setComment(std::shared_ptr<Comment> comment);
void setCommentId(const std::string &commentId);

Expand Down
17 changes: 17 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class List;
class Script;
class ITimer;
class KeyEvent;
class Monitor;
class IMonitorHandler;

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

/*! Updates the values of stage monitors. */
virtual void updateMonitors() = 0;

/*! Steps all currently running threads. Use this to implement a custom event loop. */
virtual void step() = 0;

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

/*! Returns the list of monitors. */
virtual const std::vector<std::shared_ptr<Monitor>> &monitors() const = 0;

/*! Sets the list of monitors. */
virtual void setMonitors(const std::vector<std::shared_ptr<Monitor>> &newMonitors) = 0;

/*! Sets the function which is called when a monitor is added. */
virtual void setAddMonitorHandler(const std::function<void(Monitor *)> &handler) = 0;

/*! Sets the function which is called when a monitor is removed. */
virtual void setRemoveMonitorHandler(const std::function<void(Monitor *, IMonitorHandler *)> &handler) = 0;

/*! Returns the list of extension names. */
virtual const std::vector<std::string> &extensions() const = 0;

Expand Down
24 changes: 24 additions & 0 deletions include/scratchcpp/imonitorhandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "global.h"

namespace libscratchcpp
{

class Monitor;
class Value;

class LIBSCRATCHCPP_EXPORT IMonitorHandler
{
public:
virtual ~IMonitorHandler() { }

virtual void init(Monitor *) = 0;

virtual void onValueChanged(const VirtualMachine *vm) = 0;
virtual void onVisibleChanged(bool visible) = 0;
};

} // namespace libscratchcpp
84 changes: 84 additions & 0 deletions include/scratchcpp/monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <scratchcpp/entity.h>

#include <vector>

namespace libscratchcpp
{

class IMonitorHandler;
class Block;
class Script;
class Sprite;
class Value;
class Rect;
class MonitorPrivate;

/*! \brief The Monitor class represents a stage monitor. */
class LIBSCRATCHCPP_EXPORT Monitor : public Entity
{
public:
friend class Engine;

enum class Mode
{
Default,
Large,
Slider,
List
};

Monitor(const std::string &id, const std::string &opcode);
Monitor(const Monitor &) = delete;

void setInterface(IMonitorHandler *iface);

Mode mode() const;
void setMode(Mode mode);

std::shared_ptr<Block> block() const;

std::shared_ptr<Script> script() const;
void setScript(std::shared_ptr<Script> script);

Sprite *sprite() const;
void setSprite(Sprite *sprite);

const std::string &opcode() const;

void updateValue(const VirtualMachine *vm);

unsigned int width() const;
void setWidth(unsigned int width);

unsigned int height() const;
void setHeight(unsigned int height);

int x() const;
void setX(int x);

int y() const;
void setY(int y);

bool visible() const;
void setVisible(bool visible);

double sliderMin() const;
void setSliderMin(double sliderMin);

double sliderMax() const;
void setSliderMax(double sliderMax);

bool discrete() const;
void setDiscrete(bool discrete);

static Rect getInitialPosition(const std::vector<std::shared_ptr<Monitor>> &other, int monitorWidth, int monitorHeight);

private:
spimpl::unique_impl_ptr<MonitorPrivate> impl;
};

} // namespace libscratchcpp
2 changes: 2 additions & 0 deletions include/scratchcpp/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class LIBSCRATCHCPP_EXPORT Rect
double width() const;
double height() const;

bool intersects(const Rect &rect) const;

private:
spimpl::impl_ptr<RectPrivate> impl;
};
Expand Down
Loading