tibia-client/src/framework/util/size.h

127 lines
5.0 KiB
C
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2014-04-01 07:36:42 +02:00
* Copyright (c) 2010-2014 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef SIZE_H
#define SIZE_H
2011-07-13 23:12:36 +02:00
#include "point.h"
#include "../const.h"
2011-08-14 04:09:11 +02:00
template<class T>
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; }
2011-12-07 01:31:55 +01:00
void resize(T w, T h) { wd = w; ht = h; }
2011-08-14 04:09:11 +02:00
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; }
2012-01-30 01:00:12 +01:00
TSize<T> operator*(const TSize<T>& other) const { return TSize<T>((T)other.wd*wd, (T)ht*other.ht); }
TSize<T>& operator*=(const TSize<T>& other) { wd=(T)other.wd*wd; ht=(T)ht*other.ht; return *this; }
TSize<T> operator/(const TSize<T>& other) const { return TSize<T>((T)wd/other.wd, (T)ht/other.ht); }
TSize<T>& operator/=(const TSize<T>& other) { (T)wd/=other.wd; (T)ht/=other.ht; return *this; }
TSize<T> operator*(const float v) const { return TSize<T>((T)wd*v, (T)ht*v); }
TSize<T>& operator*=(const float v) { wd=(T)wd*v; ht=(T)ht*v; return *this; }
2011-08-14 04:09:11 +02:00
TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
2012-01-30 01:00:12 +01:00
TSize<T>& operator/=(const float v) { wd/=v; ht/=v; return *this; }
2011-08-14 04:09:11 +02:00
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; }
2011-08-14 04:09:11 +02:00
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; }
2013-12-01 02:34:21 +01:00
TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max<T>(wd, other.wd), std::max<T>(ht, other.ht)); }
TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min<T>(wd, other.wd), std::min<T>(ht, other.ht)); }
2011-08-14 04:09:11 +02:00
void scale(const TSize<T>& s, Fw::AspectRatioMode mode) {
if(mode == Fw::IgnoreAspectRatio || wd == 0 || ht == 0) {
wd = s.wd;
ht = s.ht;
} else {
bool useHeight;
T rw = (s.ht * wd) / ht;
if(mode == Fw::KeepAspectRatio)
useHeight = (rw <= s.wd);
else // mode == Fw::KeepAspectRatioByExpanding
useHeight = (rw >= s.wd);
if(useHeight) {
wd = rw;
ht = s.ht;
} else {
ht = (s.wd * ht)/wd;
wd = s.wd;
}
}
}
2013-01-21 00:17:56 +01:00
void scale(int w, int h, Fw::AspectRatioMode mode) { scale(TSize<T>(w, h), mode); }
2011-08-14 04:09:11 +02:00
float ratio() const { return (float)wd/ht; }
T area() const { return wd*ht; }
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;
2011-12-07 01:31:55 +01:00
size.resize(w, h);
2011-08-14 04:09:11 +02:00
return in;
}
#endif