Skip to content

Commit 16121d3

Browse files
committed
Add Rect class
1 parent f24ce30 commit 16121d3

File tree

10 files changed

+256
-0
lines changed

10 files changed

+256
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ target_sources(scratchcpp
5050
include/scratchcpp/keyevent.h
5151
include/scratchcpp/iimageformat.h
5252
include/scratchcpp/iimageformatfactory.h
53+
include/scratchcpp/rect.h
5354
)
5455

5556
add_library(zip SHARED

include/scratchcpp/rect.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "global.h"
6+
#include "spimpl.h"
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class RectPrivate;
12+
13+
/*! The Rect class represents a rectangle. */
14+
class LIBSCRATCHCPP_EXPORT Rect
15+
{
16+
public:
17+
Rect(double left, double top, double right, double bottom);
18+
Rect();
19+
20+
double left() const;
21+
void setLeft(double left);
22+
23+
double top() const;
24+
void setTop(double top);
25+
26+
double right() const;
27+
void setRight(double right);
28+
29+
double bottom() const;
30+
void setBottom(double bottom);
31+
32+
double width() const;
33+
double height() const;
34+
35+
private:
36+
spimpl::impl_ptr<RectPrivate> impl;
37+
};
38+
39+
} // namespace libscratchcpp

include/scratchcpp/sprite.h

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

1010
class ISpriteHandler;
11+
class Rect;
1112
class SpritePrivate;
1213

1314
/*! \brief The Sprite class represents a Scratch sprite. */
@@ -62,6 +63,8 @@ class LIBSCRATCHCPP_EXPORT Sprite : public Target
6263
void setRotationStyle(const std::string &newRotationStyle);
6364
void setRotationStyle(const char *newRotationStyle);
6465

66+
Rect boundingRect() const;
67+
6568
private:
6669
Target *dataSource() const override;
6770

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ target_sources(scratchcpp
66
scratchconfiguration.cpp
77
scratchconfiguration_p.cpp
88
scratchconfiguration_p.h
9+
rect.cpp
10+
rect_p.cpp
11+
rect_p.h
912
)
1013

1114
add_subdirectory(blocks)

src/rect.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/rect.h>
4+
#include <cmath>
5+
#include "rect_p.h"
6+
7+
using namespace libscratchcpp;
8+
9+
/*! Constructs Rect. */
10+
Rect::Rect(double left, double top, double right, double bottom) :
11+
impl(spimpl::make_impl<RectPrivate>(left, top, right, bottom))
12+
{
13+
}
14+
15+
/*! \copydoc Rect() */
16+
Rect::Rect() :
17+
impl(spimpl::make_impl<RectPrivate>())
18+
{
19+
}
20+
21+
/*! Returns the x-coordinate of the left edge. */
22+
double Rect::left() const
23+
{
24+
return impl->left;
25+
}
26+
27+
/*! Sets the x-coordinate of the left edge. */
28+
void Rect::setLeft(double left)
29+
{
30+
impl->left = left;
31+
}
32+
33+
/*! Returns the y-coordinate of the top edge. */
34+
double Rect::top() const
35+
{
36+
return impl->top;
37+
}
38+
39+
/*! Sets the y-coordinate of the top edge. */
40+
void Rect::setTop(double top)
41+
{
42+
impl->top = top;
43+
}
44+
45+
/*! Returns the x-coordinate of the right edge. */
46+
double Rect::right() const
47+
{
48+
return impl->right;
49+
}
50+
51+
/*! Sets the x-coordinate of the right edge. */
52+
void Rect::setRight(double right)
53+
{
54+
impl->right = right;
55+
}
56+
57+
/*! Returns the y-coordinate of the bottom edge. */
58+
double Rect::bottom() const
59+
{
60+
return impl->bottom;
61+
}
62+
63+
/*! Sets the y-coordinate of the bottom edge. */
64+
void Rect::setBottom(double bottom)
65+
{
66+
impl->bottom = bottom;
67+
}
68+
69+
/*! Returns the width of the rectangle. */
70+
double Rect::width() const
71+
{
72+
return std::abs(impl->right - impl->left);
73+
}
74+
75+
/*! Returns the height of the rectangle. */
76+
double Rect::height() const
77+
{
78+
return std::abs(impl->top - impl->bottom);
79+
}

src/rect_p.cpp

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+
#include "rect_p.h"
4+
5+
namespace libscratchcpp
6+
{
7+
8+
RectPrivate::RectPrivate(double left, double top, double right, double bottom) :
9+
left(left),
10+
top(top),
11+
right(right),
12+
bottom(bottom)
13+
{
14+
}
15+
16+
RectPrivate::RectPrivate()
17+
{
18+
}
19+
20+
} // namespace libscratchcpp

src/rect_p.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
namespace libscratchcpp
6+
{
7+
8+
struct RectPrivate
9+
{
10+
RectPrivate(double left, double top, double right, double bottom);
11+
RectPrivate();
12+
13+
double left = 0;
14+
double top = 0;
15+
double right = 0;
16+
double bottom = 0;
17+
};
18+
19+
} // namespace libscratchcpp

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ add_subdirectory(clock)
3232
add_subdirectory(timer)
3333
add_subdirectory(randomgenerator)
3434
add_subdirectory(imageformats)
35+
add_subdirectory(rect)

test/rect/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(
2+
rect_test
3+
rect_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
rect_test
8+
GTest::gtest_main
9+
scratchcpp
10+
)
11+
12+
gtest_discover_tests(rect_test)

test/rect/rect_test.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <scratchcpp/rect.h>
2+
#include <cmath>
3+
4+
#include "../common.h"
5+
6+
using namespace libscratchcpp;
7+
8+
TEST(RectTest, Constructors)
9+
{
10+
{
11+
Rect rect;
12+
ASSERT_EQ(rect.left(), 0);
13+
ASSERT_EQ(rect.top(), 0);
14+
ASSERT_EQ(rect.right(), 0);
15+
ASSERT_EQ(rect.bottom(), 0);
16+
ASSERT_EQ(rect.width(), 0);
17+
ASSERT_EQ(rect.height(), 0);
18+
}
19+
20+
{
21+
Rect rect(8.4, 150.78, 145.89, -179.99);
22+
ASSERT_EQ(rect.left(), 8.4);
23+
ASSERT_EQ(rect.top(), 150.78);
24+
ASSERT_EQ(rect.right(), 145.89);
25+
ASSERT_EQ(rect.bottom(), -179.99);
26+
ASSERT_EQ(std::round(rect.width() * 100) / 100, 137.49);
27+
ASSERT_EQ(rect.height(), 330.77);
28+
}
29+
}
30+
31+
TEST(RectTest, Left)
32+
{
33+
Rect rect;
34+
35+
rect.setLeft(-78.05);
36+
ASSERT_EQ(rect.left(), -78.05);
37+
}
38+
39+
TEST(RectTest, Top)
40+
{
41+
Rect rect;
42+
43+
rect.setTop(-22.89);
44+
ASSERT_EQ(rect.top(), -22.89);
45+
}
46+
47+
TEST(RectTest, Right)
48+
{
49+
Rect rect;
50+
51+
rect.setRight(100.512);
52+
ASSERT_EQ(rect.right(), 100.512);
53+
}
54+
55+
TEST(RectTest, Bottom)
56+
{
57+
Rect rect;
58+
59+
rect.setBottom(-58.162);
60+
ASSERT_EQ(rect.bottom(), -58.162);
61+
}
62+
63+
TEST(RectTest, Width)
64+
{
65+
Rect rect;
66+
67+
rect.setLeft(-78.05);
68+
rect.setRight(100.512);
69+
ASSERT_EQ(rect.width(), 178.562);
70+
}
71+
72+
TEST(RectTest, Height)
73+
{
74+
Rect rect;
75+
76+
rect.setTop(-22.89);
77+
rect.setBottom(-58.162);
78+
ASSERT_EQ(rect.height(), 35.272);
79+
}

0 commit comments

Comments
 (0)