mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-11-16 00:12:51 +00:00
Codechange: Add some Rect helpers.
Shrink/Expand/Translate are marked nodiscard as they provide a new Rect
rather than modifying the original Rect.
(cherry picked from commit d8e01f8aa5
)
This commit is contained in:
parent
bbd84815b8
commit
cabb4b08d9
@ -49,6 +49,112 @@ struct Rect {
|
||||
int top;
|
||||
int right;
|
||||
int bottom;
|
||||
|
||||
/**
|
||||
* Get width of Rect.
|
||||
* @return width of Rect.
|
||||
*/
|
||||
inline int Width() const { return this->right - this->left + 1; }
|
||||
|
||||
/**
|
||||
* Get height of Rect.
|
||||
* @return height of Rect.
|
||||
*/
|
||||
inline int Height() const { return this->bottom - this->top + 1; }
|
||||
|
||||
/**
|
||||
* Copy and shrink Rect by s pixels.
|
||||
* @param s number of pixels to remove from each side of Rect.
|
||||
* @return the new smaller Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect Shrink(int s) const
|
||||
{
|
||||
return {this->left + s, this->top + s, this->right - s, this->bottom - s};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy and shrink Rect by h horizontal and v vertical pixels.
|
||||
* @param h number of pixels to remove from left and right sides.
|
||||
* @param v number of pixels to remove from top and bottom sides.
|
||||
* @return the new smaller Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect Shrink(int h, int v) const
|
||||
{
|
||||
return {this->left + h, this->top + v, this->right - h, this->bottom - v};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy and shrink Rect by pixels.
|
||||
* @param left number of pixels to remove from left side.
|
||||
* @param top number of pixels to remove from top side.
|
||||
* @param right number of pixels to remove from right side.
|
||||
* @param bottom number of pixels to remove from bottom side.
|
||||
* @return the new smaller Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const
|
||||
{
|
||||
return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy and expand Rect by s pixels.
|
||||
* @param s number of pixels to add to each side of Rect.
|
||||
* @return the new larger Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect Expand(int s) const
|
||||
{
|
||||
return this->Shrink(-s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy and translate Rect by x,y pixels.
|
||||
* @param x number of pixels to move horizontally.
|
||||
* @param y number of pixels to move vertically.
|
||||
* @return the new translated Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect Translate(int x, int y) const
|
||||
{
|
||||
return {this->left + x, this->top + y, this->right + x, this->bottom + y};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Rect and set its width.
|
||||
* @param width width in pixels for new Rect.
|
||||
* @param end if set, set width at end of Rect, i.e. on right.
|
||||
* @return the new resized Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect WithWidth(int width, bool end) const
|
||||
{
|
||||
return end
|
||||
? Rect {this->right - width + 1, this->top, this->right, this->bottom}
|
||||
: Rect {this->left, this->top, this->left + width - 1, this->bottom};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Rect and indent it from its position.
|
||||
* @param indent offset in pixels for new Rect.
|
||||
* @param end if set, set indent at end of Rect, i.e. on right.
|
||||
* @return the new resized Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect Indent(int indent, bool end) const
|
||||
{
|
||||
return end
|
||||
? Rect {this->left, this->top, this->right - indent, this->bottom}
|
||||
: Rect {this->left + indent, this->top, this->right, this->bottom};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Rect and set its height.
|
||||
* @param width height in pixels for new Rect.
|
||||
* @param end if set, set height at end of Rect, i.e. at bottom.
|
||||
* @return the new resized Rect.
|
||||
*/
|
||||
[[nodiscard]] inline Rect WithHeight(int height, bool end = false) const
|
||||
{
|
||||
return end
|
||||
? Rect {this->left, this->bottom - height + 1, this->right, this->bottom}
|
||||
: Rect {this->left, this->top, this->right, this->top + height - 1};
|
||||
}
|
||||
};
|
||||
|
||||
struct Rect16 {
|
||||
|
Loading…
Reference in New Issue
Block a user