diff --git a/src/gfx/CMakeLists.txt b/src/gfx/CMakeLists.txt index 516afd2d6..52a8048d9 100644 --- a/src/gfx/CMakeLists.txt +++ b/src/gfx/CMakeLists.txt @@ -2,6 +2,7 @@ # Copyright (C) 2001-2011 David Capello add_library(gfx-lib + border.cpp hsv.cpp point.cpp rect.cpp diff --git a/src/gfx/border.cpp b/src/gfx/border.cpp new file mode 100644 index 000000000..bd71d2fc7 --- /dev/null +++ b/src/gfx/border.cpp @@ -0,0 +1,44 @@ +// ASE gfx library +// Copyright (C) 2001-2011 David Capello +// +// This source file is ditributed under a BSD-like license, please +// read LICENSE.txt for more information. + +#include "gfx/border.h" + +using namespace gfx; + +Border::Border() +{ + m_left = 0; + m_top = 0; + m_right = 0; + m_bottom = 0; +} + +Border::Border(int left, int top, int right, int bottom) +{ + m_left = left; + m_top = top; + m_right = right; + m_bottom = bottom; +} + +Border Border::operator-() const +{ + return Border(-m_left, -m_top, -m_right, -m_bottom); +} + +bool Border::operator==(const Border& br) const +{ + return + m_left == br.m_left && m_top == br.m_top && + m_right == br.m_right && m_bottom == br.m_bottom; +} + +bool Border::operator!=(const Border& br) const +{ + return + m_left != br.m_left || m_top != br.m_top || + m_right != br.m_right || m_bottom != br.m_bottom; +} diff --git a/src/gfx/border.h b/src/gfx/border.h new file mode 100644 index 000000000..5e45d1a69 --- /dev/null +++ b/src/gfx/border.h @@ -0,0 +1,43 @@ +// ASE gfx library +// Copyright (C) 2001-2011 David Capello +// +// This source file is ditributed under a BSD-like license, please +// read LICENSE.txt for more information. + +#ifndef GFX_BORDER_H_INCLUDED +#define GFX_BORDER_H_INCLUDED + +namespace gfx { + +class Border +{ +public: + Border(); + Border(int left, int top, int right, int bottom); + + int left() const { return m_left; }; + int top() const { return m_top; }; + int right() const { return m_right; }; + int bottom() const { return m_bottom; }; + + void left(int left) { m_left = left; } + void top(int top) { m_top = top; } + void right(int right) { m_right = right; } + void bottom(int bottom) { m_bottom = bottom; } + + Border operator-() const; + + bool operator==(const Border& br) const; + bool operator!=(const Border& br) const; + +private: + int m_left; + int m_top; + int m_right; + int m_bottom; +}; + +} // namespace gfx + +#endif + diff --git a/src/gfx/rect.cpp b/src/gfx/rect.cpp index daee747c5..2eecf9487 100644 --- a/src/gfx/rect.cpp +++ b/src/gfx/rect.cpp @@ -7,6 +7,7 @@ #include "gfx/rect.h" #include "gfx/point.h" #include "gfx/size.h" +#include "gfx/border.h" #ifdef WIN32 #define WIN32_LEAN_AND_MEAN @@ -154,6 +155,24 @@ Rect& Rect::inflate(const Size& delta) return *this; } +Rect& Rect::inflate(const Border& br) +{ + x -= br.left(); + y -= br.top(); + w += br.left() + br.right(); + h += br.top() + br.bottom(); + return *this; +} + +Rect& Rect::deflate(const Border& br) +{ + x += br.left(); + y += br.top(); + w -= br.left() + br.right(); + h -= br.top() + br.bottom(); + return *this; +} + Rect& Rect::enlarge(int unit) { x -= unit; @@ -223,6 +242,28 @@ Rect Rect::createIntersect(const Rect& rc) const return Rect(); } +const Rect& Rect::operator+=(const Border& br) +{ + inflate(br); + return *this; +} + +const Rect& Rect::operator-=(const Border& br) +{ + deflate(br); + return *this; +} + +Rect Rect::operator+(const Border& br) const +{ + return Rect(*this).inflate(br); +} + +Rect Rect::operator-(const Border& br) const +{ + return Rect(*this).deflate(br); +} + bool Rect::operator==(const Rect& rc) const { return diff --git a/src/gfx/rect.h b/src/gfx/rect.h index a87c37c67..dc4f232b9 100644 --- a/src/gfx/rect.h +++ b/src/gfx/rect.h @@ -15,6 +15,7 @@ namespace gfx { class Point; class Size; +class Border; class Rect // A rectangle. { @@ -77,6 +78,8 @@ public: Rect& offset(const Point& delta); Rect& inflate(int dw, int dh); Rect& inflate(const Size& delta); + Rect& inflate(const Border& br); + Rect& deflate(const Border& br); Rect& enlarge(int unit); Rect& shrink(int unit); @@ -97,6 +100,11 @@ public: // Returns the intersection rectangle between this and rc rectangles. Rect createIntersect(const Rect& rc) const; + const Rect& operator+=(const Border& br); + const Rect& operator-=(const Border& br); + Rect operator+(const Border& br) const; + Rect operator-(const Border& br) const; + bool operator==(const Rect& rc) const; bool operator!=(const Rect& rc) const;