Add gfx::Border class.

This commit is contained in:
David Capello 2011-01-27 23:29:46 -03:00
parent 40df2e1d73
commit 034a943a0c
5 changed files with 137 additions and 0 deletions

View File

@ -2,6 +2,7 @@
# Copyright (C) 2001-2011 David Capello
add_library(gfx-lib
border.cpp
hsv.cpp
point.cpp
rect.cpp

44
src/gfx/border.cpp Normal file
View File

@ -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;
}

43
src/gfx/border.h Normal file
View File

@ -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

View File

@ -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

View File

@ -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;