Skip to content

Commit 745cc76

Browse files
committed
Add Monitor class
1 parent 13c363f commit 745cc76

File tree

8 files changed

+417
-0
lines changed

8 files changed

+417
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ target_sources(scratchcpp
5252
include/scratchcpp/rect.h
5353
include/scratchcpp/igraphicseffect.h
5454
include/scratchcpp/comment.h
55+
include/scratchcpp/monitor.h
5556
)
5657

5758
add_library(zip SHARED

include/scratchcpp/monitor.h

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

src/scratch/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ target_sources(scratchcpp
5151
comment.cpp
5252
comment_p.cpp
5353
comment_p.h
54+
monitor.cpp
55+
monitor_p.cpp
56+
monitor_p.h
5457
)

src/scratch/monitor.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/monitor.h>
4+
#include <scratchcpp/block.h>
5+
#include <scratchcpp/sprite.h>
6+
7+
#include "monitor_p.h"
8+
9+
using namespace libscratchcpp;
10+
11+
/*! Constructs Monitor. */
12+
Monitor::Monitor(const std::string &id, const std::string &opcode) :
13+
Entity(id),
14+
impl(spimpl::make_unique_impl<MonitorPrivate>(opcode))
15+
{
16+
}
17+
18+
/*! Returns the monitor's mode. */
19+
Monitor::Mode Monitor::mode() const
20+
{
21+
return impl->mode;
22+
}
23+
24+
/*! Sets the monitor's mode. */
25+
void Monitor::setMode(Mode mode)
26+
{
27+
impl->mode = mode;
28+
}
29+
30+
/*! Returns the block used to get the monitor's value. */
31+
std::shared_ptr<Block> Monitor::block() const
32+
{
33+
return impl->block;
34+
}
35+
36+
/*! Convenience method which calls block()->target(). */
37+
Sprite *Monitor::sprite() const
38+
{
39+
return dynamic_cast<Sprite *>(impl->block->target());
40+
}
41+
42+
/*! Convenience method which calls block()->setTarget(). */
43+
void Monitor::setSprite(Sprite *sprite)
44+
{
45+
impl->block->setTarget(sprite);
46+
}
47+
48+
/*! Convenience method which calls block()->opcode(). */
49+
const std::string &Monitor::opcode() const
50+
{
51+
return impl->block->opcode();
52+
}
53+
54+
/*! Returns the monitor's width. */
55+
unsigned int Monitor::width() const
56+
{
57+
return impl->width;
58+
}
59+
60+
/*! Sets the monitor's width. */
61+
void Monitor::setWidth(unsigned int width)
62+
{
63+
impl->width = width;
64+
}
65+
66+
/*! Returns the monitor's height. */
67+
unsigned int Monitor::height() const
68+
{
69+
return impl->height;
70+
}
71+
72+
/*! Sets the monitor's height. */
73+
void Monitor::setHeight(unsigned int height)
74+
{
75+
impl->height = height;
76+
}
77+
78+
/*! Returns the monitor's x-coordinate. */
79+
int Monitor::x() const
80+
{
81+
return impl->x;
82+
}
83+
84+
/*! Sets the monitor's x-coordinate. */
85+
void Monitor::setX(int x)
86+
{
87+
impl->x = x;
88+
}
89+
90+
/*! Returns the monitor's y-coordinate. */
91+
int Monitor::y() const
92+
{
93+
return impl->y;
94+
}
95+
96+
/*! Sets the monitor's y-coordinate. */
97+
void Monitor::setY(int y)
98+
{
99+
impl->y = y;
100+
}
101+
102+
/*! Returns true if the monitor is visible. */
103+
bool Monitor::visible() const
104+
{
105+
return impl->visible;
106+
}
107+
108+
/*! Sets whether the monitor is visible or not. */
109+
void Monitor::setVisible(bool visible)
110+
{
111+
impl->visible = visible;
112+
}
113+
114+
/*! Returns the minimum value of the monitor's slider. */
115+
double Monitor::sliderMin() const
116+
{
117+
return impl->sliderMin;
118+
}
119+
120+
/*! Sets the minimum value of the monitor's slider. */
121+
void Monitor::setSliderMin(double sliderMin)
122+
{
123+
impl->sliderMin = sliderMin;
124+
}
125+
126+
/*! Returns the maximum value of the monitor's slider. */
127+
double Monitor::sliderMax() const
128+
{
129+
return impl->sliderMax;
130+
}
131+
132+
/*! Sets the maximum value of the monitor's slider. */
133+
void Monitor::setSliderMax(double sliderMax)
134+
{
135+
impl->sliderMax = sliderMax;
136+
}
137+
138+
/*! Returns true if the monitor's slider allows only integer values. */
139+
bool Monitor::discrete() const
140+
{
141+
return impl->discrete;
142+
}
143+
144+
/*! Sets whether the monitor's slider allows only integer values. */
145+
void Monitor::setDiscrete(bool discrete)
146+
{
147+
impl->discrete = discrete;
148+
}

src/scratch/monitor_p.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/block.h>
4+
5+
#include "monitor_p.h"
6+
7+
using namespace libscratchcpp;
8+
9+
MonitorPrivate::MonitorPrivate(const std::string &opcode) :
10+
block(std::make_shared<Block>("", opcode))
11+
{
12+
}

src/scratch/monitor_p.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/monitor.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class Block;
11+
12+
struct MonitorPrivate
13+
{
14+
MonitorPrivate(const std::string &opcode);
15+
MonitorPrivate(const MonitorPrivate &) = delete;
16+
17+
Monitor::Mode mode = Monitor::Mode::Default;
18+
std::shared_ptr<Block> block; // Compiler needs shared_ptr
19+
unsigned int width = 0;
20+
unsigned int height = 0;
21+
int x = 0;
22+
int y = 0;
23+
bool visible = true;
24+
double sliderMin = 0;
25+
double sliderMax = 0;
26+
bool discrete = false;
27+
};
28+
29+
} // namespace libscratchcpp

test/scratch_classes/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,17 @@ target_link_libraries(
217217
)
218218

219219
gtest_discover_tests(comment_test)
220+
221+
# monitor_test
222+
add_executable(
223+
monitor_test
224+
monitor_test.cpp
225+
)
226+
227+
target_link_libraries(
228+
monitor_test
229+
GTest::gtest_main
230+
scratchcpp
231+
)
232+
233+
gtest_discover_tests(monitor_test)

0 commit comments

Comments
 (0)