2010-11-25 00:36:15 +01:00
|
|
|
#ifndef SIZE_H
|
|
|
|
#define SIZE_H
|
|
|
|
|
2011-07-13 23:12:36 +02:00
|
|
|
#include "point.h"
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
enum ESizeScaleMode {
|
|
|
|
IGNORE_ASPECT_RATIO,
|
|
|
|
KEEP_ASPECT_RATIO,
|
|
|
|
KEEP_ASPECT_RATIO_BY_EXPANDING
|
|
|
|
};
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<class T>
|
2010-11-25 00:36:15 +01:00
|
|
|
class TSize
|
|
|
|
{
|
|
|
|
public:
|
2011-08-14 04:09:11 +02:00
|
|
|
TSize() : wd(-1), ht(-1) {};
|
|
|
|
TSize(T width, T height) : wd(width), ht(height) { };
|
|
|
|
TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
|
|
|
|
|
|
|
|
TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
|
|
|
|
|
|
|
|
bool isNull() const { return wd==0 && ht==0; }
|
|
|
|
bool isEmpty() const { return wd<1 || ht<1; }
|
|
|
|
bool isValid() const { return wd>=0 && ht>=0; }
|
|
|
|
|
|
|
|
int width() const { return wd; }
|
|
|
|
int height() const { return ht; }
|
|
|
|
|
|
|
|
void setSize(T w, T h) { wd = w; ht = h; }
|
|
|
|
void setWidth(T w) { wd = w; }
|
|
|
|
void setHeight(T h) { ht = h; }
|
|
|
|
|
|
|
|
TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
|
|
|
|
TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
|
|
|
|
TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
|
|
|
|
TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
|
|
|
|
TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
|
|
|
|
TSize<T> operator*(const float v) const { return TSize<T>((T)v*wd, (T)ht*v); }
|
|
|
|
TSize<T>& operator*=(const float v) { wd=(T)v*wd; ht=(T)ht*v; return *this; }
|
|
|
|
TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
|
|
|
|
TSize<T>& operator/=(const float v) { (T)wd/=v; (T)ht/=v; return *this; }
|
|
|
|
|
|
|
|
bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
|
|
|
|
bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
|
|
|
|
bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
|
|
|
|
bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
|
|
|
|
|
|
|
|
TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
|
|
|
|
bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
|
|
|
|
bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
|
|
|
|
|
|
|
|
TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
|
|
|
|
TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
|
|
|
|
|
|
|
|
void scale(const TSize<T>& s, ESizeScaleMode mode) {
|
2010-11-25 00:36:15 +01:00
|
|
|
if(mode == IGNORE_ASPECT_RATIO || wd == 0 || ht == 0) {
|
|
|
|
wd = s.wd;
|
|
|
|
ht = s.ht;
|
|
|
|
} else {
|
|
|
|
bool useHeight;
|
|
|
|
T rw = (s.ht * wd) / ht;
|
|
|
|
|
|
|
|
if(mode == KEEP_ASPECT_RATIO)
|
|
|
|
useHeight = (rw <= s.wd);
|
|
|
|
else // mode == KEEP_ASPECT_RATIO_BY_EXPANDING
|
|
|
|
useHeight = (rw >= s.wd);
|
|
|
|
|
|
|
|
if(useHeight) {
|
|
|
|
wd = rw;
|
|
|
|
ht = s.ht;
|
|
|
|
} else {
|
|
|
|
ht = (s.wd * ht)/wd;
|
|
|
|
wd = s.wd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
void scale(int w, int h, ESizeScaleMode mode) { scale(TSize<T>(w, h)); }
|
2010-11-25 00:36:15 +01:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
float ratio() const { return (float)wd/ht; }
|
|
|
|
T area() const { return wd*ht; }
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
T wd, ht;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef TSize<int> Size;
|
|
|
|
typedef TSize<float> SizeF;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<class T>
|
|
|
|
std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
|
2011-04-10 22:40:44 +02:00
|
|
|
{
|
2011-08-14 04:09:11 +02:00
|
|
|
out << size.width() << " " << size.height();
|
2011-07-13 23:12:36 +02:00
|
|
|
return out;
|
2011-04-10 22:40:44 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
template<class T>
|
|
|
|
std::istream& operator>>(std::istream& in, TSize<T>& size)
|
|
|
|
{
|
|
|
|
T w, h;
|
|
|
|
in >> w >> h;
|
|
|
|
size.setSize(w, h);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
#endif
|