From 701f7f6d67659b69c105debffe33bf5c86608962 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 30 Jun 2020 09:00:20 -0300 Subject: [PATCH 1/5] Extract base CoordsRange class from MapRange Enforce MapRange to be rectangle shaped --- src/openrct2/world/Location.hpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index 734a5f6cee..64f2b53de2 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -610,12 +610,12 @@ struct TileCoordsXYZD : public TileCoordsXYZ }; /** - * Represents a rectangular range of the map using regular coordinates (32 per tile). + * Represents a range of the map using regular coordinates. */ -struct MapRange +template struct CoordsRange { - CoordsXY LeftTop; - CoordsXY RightBottom; + T LeftTop{ 0, 0 }; + T RightBottom{ 0, 0 }; int32_t GetLeft() const { @@ -634,16 +634,29 @@ struct MapRange return RightBottom.y; } - MapRange() - : MapRange(0, 0, 0, 0) - { - } - MapRange(int32_t left, int32_t top, int32_t right, int32_t bottom) + CoordsRange() = default; + CoordsRange(int32_t left, int32_t top, int32_t right, int32_t bottom) : LeftTop(left, top) , RightBottom(right, bottom) { } +}; +/** + * Represents a rectangular range of the map using regular coordinates (32 per tile). + */ + +struct MapRange : public CoordsRange +{ + using CoordsRange::CoordsRange; + MapRange(int32_t left, int32_t top, int32_t right, int32_t bottom) + : CoordsRange(left, top, right, bottom) + { + // Make sure it's a rectangle + assert(std::abs(GetLeft() - GetRight()) > 0); + assert(std::abs(GetTop() - GetBottom()) > 0); + } + MapRange Normalise() const { auto result = MapRange( From 70e245a1c36bcd40a005b89f851afe746fb92cce Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 30 Jun 2020 09:12:14 -0300 Subject: [PATCH 2/5] Add Coords constructor to Map and CoordsRange --- src/openrct2/world/Location.hpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index 64f2b53de2..773ffb5bfa 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -636,8 +636,13 @@ template struct CoordsRange CoordsRange() = default; CoordsRange(int32_t left, int32_t top, int32_t right, int32_t bottom) - : LeftTop(left, top) - , RightBottom(right, bottom) + : CoordsRange({ left, top }, { right, bottom }) + { + } + + CoordsRange(const T& leftTop, const T& rightBottom) + : LeftTop(leftTop) + , RightBottom(rightBottom) { } }; @@ -650,7 +655,12 @@ struct MapRange : public CoordsRange { using CoordsRange::CoordsRange; MapRange(int32_t left, int32_t top, int32_t right, int32_t bottom) - : CoordsRange(left, top, right, bottom) + : MapRange({ left, top }, { right, bottom }) + { + } + + MapRange(const CoordsXY& leftTop, const CoordsXY& rightBottom) + : CoordsRange(leftTop, rightBottom) { // Make sure it's a rectangle assert(std::abs(GetLeft() - GetRight()) > 0); From 5ce65eb7dceb47b54a734a5931ee90a2c96cb3a4 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 30 Jun 2020 09:26:42 -0300 Subject: [PATCH 3/5] Create ScreenLine struct --- src/openrct2/world/Location.hpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index 773ffb5bfa..af9a0b8787 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -666,7 +666,7 @@ struct MapRange : public CoordsRange assert(std::abs(GetLeft() - GetRight()) > 0); assert(std::abs(GetTop() - GetBottom()) > 0); } - + MapRange Normalise() const { auto result = MapRange( @@ -675,3 +675,17 @@ struct MapRange : public CoordsRange return result; } }; + +/** + * Represents a line on the screen + */ + +struct ScreenLine : public CoordsRange +{ + ScreenLine(const ScreenCoordsXY& leftTop, const ScreenCoordsXY& rightBottom) + : CoordsRange(leftTop, rightBottom) + { + // Make sure one of the point coords change + assert((std::abs(GetLeft() - GetRight()) > 0) || (std::abs(GetTop() - GetBottom()) > 0)); + } +}; From a86a60cbf52fcd7bfa993edb3bac30eb31fb2cb2 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 30 Jun 2020 09:32:22 -0300 Subject: [PATCH 4/5] Extract ScreenRect from Graph for reusability --- src/openrct2-ui/interface/Graph.cpp | 35 ----------------------------- src/openrct2/world/Location.hpp | 28 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/openrct2-ui/interface/Graph.cpp b/src/openrct2-ui/interface/Graph.cpp index 19e876f07d..3435b2f9db 100644 --- a/src/openrct2-ui/interface/Graph.cpp +++ b/src/openrct2-ui/interface/Graph.cpp @@ -189,41 +189,6 @@ struct FinancialTooltipInfo const money32 money{}; }; -struct ScreenRect -{ - const ScreenCoordsXY LeftTop; - const ScreenCoordsXY RightBottom; - - int32_t GetLeft() const - { - return LeftTop.x; - } - int32_t GetTop() const - { - return LeftTop.y; - } - int32_t GetRight() const - { - return RightBottom.x; - } - int32_t GetBottom() const - { - return RightBottom.y; - } - int32_t GetWidth() const - { - return RightBottom.x - LeftTop.x; - } - int32_t GetHeight() const - { - return RightBottom.y - LeftTop.y; - } - bool Contains(const ScreenCoordsXY& coords) const - { - return coords.x >= GetLeft() && coords.x <= GetRight() && coords.y >= GetTop() && coords.y <= GetBottom(); - } -}; - static constexpr auto CHART_MAX_DATA_COUNT = 64; static constexpr auto CHART_MAX_INDEX = CHART_MAX_DATA_COUNT - 1; static constexpr auto CHART_DATA_WIDTH = 6; diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index af9a0b8787..54c0a02eb1 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -689,3 +689,31 @@ struct ScreenLine : public CoordsRange assert((std::abs(GetLeft() - GetRight()) > 0) || (std::abs(GetTop() - GetBottom()) > 0)); } }; + +/** + * Represents a rectangular range on the screen + */ + +struct ScreenRect : public CoordsRange +{ + ScreenRect(const ScreenCoordsXY& leftTop, const ScreenCoordsXY& rightBottom) + : CoordsRange(leftTop, rightBottom) + { + // Make sure it's a rectangle + assert(std::abs(GetLeft() - GetRight()) > 0); + assert(std::abs(GetTop() - GetBottom()) > 0); + } + + int32_t GetWidth() const + { + return RightBottom.x - LeftTop.x; + } + int32_t GetHeight() const + { + return RightBottom.y - LeftTop.y; + } + bool Contains(const ScreenCoordsXY& coords) const + { + return coords.x >= GetLeft() && coords.x <= GetRight() && coords.y >= GetTop() && coords.y <= GetBottom(); + } +}; From b5195f77c4f5bbdd5bc57fc92b18079f0f25331a Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 30 Jun 2020 09:44:28 -0300 Subject: [PATCH 5/5] Reuse Rectangle definitions for ScreenRect and MapRange --- src/openrct2/world/Location.hpp | 46 ++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index 54c0a02eb1..a239feef95 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -647,25 +647,31 @@ template struct CoordsRange } }; +template struct RectRange : public CoordsRange +{ + using CoordsRange::CoordsRange; + + RectRange(int32_t left, int32_t top, int32_t right, int32_t bottom) + : RectRange({ left, top }, { right, bottom }) + { + } + + RectRange(const T& leftTop, const T& rightBottom) + : CoordsRange(leftTop, rightBottom) + { + // Make sure it's a rectangle + assert(std::abs(CoordsRange::GetLeft() - CoordsRange::GetRight()) > 0); + assert(std::abs(CoordsRange::GetTop() - CoordsRange::GetBottom()) > 0); + } +}; + /** * Represents a rectangular range of the map using regular coordinates (32 per tile). */ -struct MapRange : public CoordsRange +struct MapRange : public RectRange { - using CoordsRange::CoordsRange; - MapRange(int32_t left, int32_t top, int32_t right, int32_t bottom) - : MapRange({ left, top }, { right, bottom }) - { - } - - MapRange(const CoordsXY& leftTop, const CoordsXY& rightBottom) - : CoordsRange(leftTop, rightBottom) - { - // Make sure it's a rectangle - assert(std::abs(GetLeft() - GetRight()) > 0); - assert(std::abs(GetTop() - GetBottom()) > 0); - } + using RectRange::RectRange; MapRange Normalise() const { @@ -694,16 +700,10 @@ struct ScreenLine : public CoordsRange * Represents a rectangular range on the screen */ -struct ScreenRect : public CoordsRange +struct ScreenRect : public RectRange { - ScreenRect(const ScreenCoordsXY& leftTop, const ScreenCoordsXY& rightBottom) - : CoordsRange(leftTop, rightBottom) - { - // Make sure it's a rectangle - assert(std::abs(GetLeft() - GetRight()) > 0); - assert(std::abs(GetTop() - GetBottom()) > 0); - } - + using RectRange::RectRange; + int32_t GetWidth() const { return RightBottom.x - LeftTop.x;