From 8ab1923abe46d0aed62aa4918d7edc1a8d1f041e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 20 Jul 2019 16:46:33 +0200 Subject: [PATCH] LibDraw: Add orientation-based offset/size manipulation helpers. These are useful when doing widgets that can be switched between vertical and horizontal mode, such as GSlider. The idea is that instead of using "x" and "y" directly, you use the "primary" and "secondary" offset/size for the Orientation you're configured in. --- Libraries/LibDraw/Point.h | 27 +++++++++++++++++++++++ Libraries/LibDraw/Rect.h | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/Libraries/LibDraw/Point.h b/Libraries/LibDraw/Point.h index 0f3f65beb8..19936d2bc6 100644 --- a/Libraries/LibDraw/Point.h +++ b/Libraries/LibDraw/Point.h @@ -2,6 +2,7 @@ #include #include +#include class Rect; struct WSAPI_Point; @@ -68,6 +69,32 @@ public: bool is_null() const { return !m_x && !m_y; } + int primary_offset_for_orientation(Orientation orientation) const + { + return orientation == Orientation::Vertical ? y() : x(); + } + + void set_primary_offset_for_orientation(Orientation orientation, int value) + { + if (orientation == Orientation::Vertical) + set_y(value); + else + set_x(value); + } + + int secondary_offset_for_orientation(Orientation orientation) const + { + return orientation == Orientation::Vertical ? x() : y(); + } + + void set_secondary_offset_for_orientation(Orientation orientation, int value) + { + if (orientation == Orientation::Vertical) + set_x(value); + else + set_y(value); + } + private: int m_x { 0 }; int m_y { 0 }; diff --git a/Libraries/LibDraw/Rect.h b/Libraries/LibDraw/Rect.h index c3ae82de1c..af81bbc294 100644 --- a/Libraries/LibDraw/Rect.h +++ b/Libraries/LibDraw/Rect.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -132,6 +133,51 @@ public: && bottom() >= other.bottom(); } + int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); } + void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); } + int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); } + void set_secondary_offset_for_orientation(Orientation orientation, int value) { m_location.set_secondary_offset_for_orientation(orientation, value); } + + int primary_size_for_orientation(Orientation orientation) const + { + return orientation == Orientation::Vertical ? height() : width(); + } + + void set_primary_size_for_orientation(Orientation orientation, int value) + { + if (orientation == Orientation::Vertical) + set_height(value); + else + set_width(value); + } + + int secondary_size_for_orientation(Orientation orientation) const + { + return orientation == Orientation::Vertical ? width() : height(); + } + + void set_secondary_size_for_orientation(Orientation orientation, int value) + { + if (orientation == Orientation::Vertical) + set_width(value); + else + set_height(value); + } + + int first_edge_for_orientation(Orientation orientation) const + { + if (orientation == Orientation::Vertical) + return top(); + return left(); + } + + int last_edge_for_orientation(Orientation orientation) const + { + if (orientation == Orientation::Vertical) + return bottom(); + return right(); + } + int left() const { return x(); } int right() const { return x() + width() - 1; } int top() const { return y(); }