reorganize some stuff

This commit is contained in:
Eduardo Bart
2012-01-04 23:28:29 -02:00
parent a92af44eb6
commit 30ce5e2ba9
69 changed files with 143 additions and 131 deletions

View File

@@ -1,165 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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 COLOR_H
#define COLOR_H
#include "types.h"
#include "tools.h"
#include "../const.h"
class Color
{
public:
Color() : m_rgba(0xFFFFFFFF) { }
Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : m_r(r), m_g(g), m_b(b), m_a(a) { }
Color(const Color& other) : m_rgba(other.m_rgba) { }
Color(uint32 rgba) : m_rgba(rgba) { }
uint8 a() const { return m_a; }
uint8 b() const { return m_b; }
uint8 g() const { return m_g; }
uint8 r() const { return m_r; }
uint32 rgba() const { return m_rgba; }
const uint8* rgbaPtr() const { return (const uint8*)&m_rgba; }
void setRed(int r) { m_r = r; }
void setGreen(int g) { m_g = g; }
void setBlue(int b) { m_b = b; }
void setAlpha(int a) { m_a = a; }
void setRed(float r) { setRed(int(r*255.0f)); }
void setGreen(float g) { setGreen(int(g*255.0f)); }
void setBlue(float b) { setBlue(int(b*255.0f)); }
void setAlpha(float a) { setAlpha(int(a*255.0f)); }
void setRGBA(int r, int g, int b, int a = 0xFF) { m_r = r; m_g = g; m_b = b; m_a = a; }
void setRGBA(uint32 rgba) { rgba = rgba; }
Color& operator=(const Color& other) { m_rgba = other.m_rgba; return *this; }
bool operator==(const Color& other) const { return other.m_rgba == m_rgba; }
bool operator!=(const Color& other) const { return other.m_rgba != m_rgba; }
static Color from8bit(int color) {
if(color >= 216 || color <= 0)
return Color(0, 0, 0);
int r = int(color / 36) % 6 * 51;
int g = int(color / 6) % 6 * 51;
int b = color % 6 * 51;
return Color(r, g, b);
}
private:
union {
uint32 m_rgba;
struct {
uint8 m_r;
uint8 m_g;
uint8 m_b;
uint8 m_a;
};
};
};
inline std::ostream& operator<<(std::ostream& out, const Color& color)
{
using namespace std;
out << "#" << hex << setfill('0')
<< setw(2) << (int)color.r()
<< setw(2) << (int)color.g()
<< setw(2) << (int)color.b()
<< setw(2) << (int)color.a();
out << dec << setfill(' ');
return out;
}
inline std::istream& operator>>(std::istream& in, Color& color)
{
using namespace std;
std::string tmp;
if(in.get() == '#') {
in >> tmp;
if(tmp.length() == 6 || tmp.length() == 8) {
color.setRed((uint8)Fw::hex2dec(tmp.substr(0, 2)));
color.setGreen((uint8)Fw::hex2dec(tmp.substr(2, 2)));
color.setBlue((uint8)Fw::hex2dec(tmp.substr(4, 2)));
if(tmp.length() == 8)
color.setAlpha((uint8)Fw::hex2dec(tmp.substr(6, 2)));
else
color.setAlpha(255);
} else
in.seekg(-tmp.length()-1, ios_base::cur);
} else {
in.unget();
in >> tmp;
if(tmp == "alpha") {
color = Fw::alpha;
} else if(tmp == "black") {
color = Fw::black;
} else if(tmp == "white") {
color = Fw::white;
} else if(tmp == "red") {
color = Fw::red;
} else if(tmp == "darkRed") {
color = Fw::darkRed;
} else if(tmp == "green") {
color = Fw::green;
} else if(tmp == "darkGreen") {
color = Fw::darkGreen;
} else if(tmp == "blue") {
color = Fw::blue;
} else if(tmp == "darkBlue") {
color = Fw::darkBlue;
} else if(tmp == "pink") {
color = Fw::pink;
} else if(tmp == "darkPink") {
color = Fw::darkPink;
} else if(tmp == "yellow") {
color = Fw::yellow;
} else if(tmp == "darkYellow") {
color = Fw::darkYellow;
} else if(tmp == "teal") {
color = Fw::teal;
} else if(tmp == "darkTeal") {
color = Fw::darkTeal;
} else if(tmp == "gray") {
color = Fw::gray;
} else if(tmp == "darkGray") {
color = Fw::darkGray;
} else if(tmp == "lightGray") {
color = Fw::lightGray;
} else if(tmp == "orange") {
color = Fw::orange;
} else {
in.seekg(-tmp.length(), ios_base::cur);
}
}
return in;
}
#endif

View File

@@ -1,211 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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 MATRIX_H
#define MATRIX_H
#include <array>
#include <cstring>
#include <initializer_list>
#include <sstream>
template<int N, int M, typename T = float>
class Matrix
{
public:
Matrix() { setIdentity(); }
Matrix(const Matrix<N,M,T>& other) = default;
template<typename U>
Matrix(const std::initializer_list<U>& values) { *this = values; }
template<typename U>
Matrix(const U *values) { *this = values; }
void setIdentity();
bool isIdentity() const;
void fill(T value);
Matrix<M,N,T> transposed() const;
typename std::enable_if<N==M>::type transpose() { *this = transposed(); }
T *data() { return m[0]; }
const T *data() const { return m[0]; }
T& operator()(int row, int column) { return m[row-1][column-1]; }
T operator()(int row, int column) const { return m[row-1][column-1]; }
Matrix<N,M,T>& operator=(const Matrix<N,M,T>& other) = default;
template<typename U>
Matrix<N,M,T>& operator=(const std::initializer_list<U>& values);
template<typename U>
Matrix<N,M,T>& operator=(const U *values);
Matrix<N,M,T>& operator+=(const Matrix<N,M,T>& other);
Matrix<N,M,T>& operator-=(const Matrix<N,M,T>& other);
Matrix<N,M,T>& operator*=(T factor);
Matrix<N,M,T>& operator/=(T divisor);
bool operator==(const Matrix<N,M,T>& other) const;
bool operator!=(const Matrix<N,M,T>& other) const;
private:
Matrix(int) {} // construct without initializing identity matrix
T m[N][M];
};
template<int N, int M, typename T>
void Matrix<N,M,T>::setIdentity() {
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
if(i==j)
m[i][j] = 1.0f;
else
m[i][j] = 0.0f;
}
}
}
template<int N, int M, typename T>
bool Matrix<N,M,T>::isIdentity() const {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
if((i==j && m[i][j] != 1.0f) || (i!=j && m[i][j] != 0.0f))
return false;
return true;
}
template<int N, int M, typename T>
void Matrix<N,M,T>::fill(T value) {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] = value;
}
template<int N, int M, typename T>
Matrix<M,N,T> Matrix<N,M,T>::transposed() const {
Matrix<M,N,T> result(1);
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
result.m[j][i] = m[i][j];
return result;
}
template<int N, int M, typename T>
template<typename U>
Matrix<N,M,T>& Matrix<N,M,T>::operator=(const std::initializer_list<U>& values) {
auto it = values.begin();
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] = *(it++);
return *this;
}
template<int N, int M, typename T>
template<typename U>
Matrix<N,M,T>& Matrix<N,M,T>::operator=(const U *values) {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] = values[i*N + j];
return *this;
}
template<int N, int M, typename T>
Matrix<N,M,T>& Matrix<N,M,T>::operator+=(const Matrix<N,M,T>& other) {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] += other.m[i][j];
return *this;
}
template<int N, int M, typename T>
Matrix<N,M,T>& Matrix<N,M,T>::operator-=(const Matrix<N,M,T>& other) {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] -= other.m[i][j];
return *this;
}
template<int N, int M, typename T>
Matrix<N,M,T>& Matrix<N,M,T>::operator*=(T factor) {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] *= factor;
return *this;
}
template<int N, int M, typename T>
Matrix<N,M,T>& Matrix<N,M,T>::operator/=(T divisor) {
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
m[i][j] /= divisor;
return *this;
}
template<int N, int M, typename T>
bool Matrix<N,M,T>::operator==(const Matrix<N,M,T>& other) const
{
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
if(m[i][j] != other.m[i][j])
return false;
return true;
}
template<int N, int M, typename T>
bool Matrix<N,M,T>::operator!=(const Matrix<N,M,T>& other) const
{
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
if(m[i][j] != other.m[i][j])
return true;
return false;
}
template<int N, int M, typename T>
std::ostream& operator<<(std::ostream& out, const Matrix<N,M,T>& mat)
{
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
out << mat(i,j);
if(j+1 != M)
out << " ";
}
out << "\n";
}
return out;
}
template<int N, int M, typename T>
std::istream& operator>>(std::istream& in, Matrix<N,M,T>& mat)
{
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
in >> mat(i,j);
return in;
}
typedef Matrix<4,4> Matrix4x4;
typedef Matrix<3,3> Matrix3x3;
typedef Matrix<2,2> Matrix2x2;
typedef Matrix4x4 Matrix4;
typedef Matrix3x3 Matrix3;
typedef Matrix2x2 Matrix2;
#endif

View File

@@ -1,101 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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 POINT_H
#define POINT_H
#include "types.h"
#include <sstream>
#include <cmath>
template<class T>
class TSize;
template<class T>
class TPoint
{
public:
TPoint() : x(0), y(0) {}
TPoint(T x, T y) : x(x), y(y) { }
TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { }
bool isNull() const { return x==0 && y==0; }
TSize<T> toSize() const { return TSize<T>(x, y); }
TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
TPoint<T> operator+(T other) const { return TPoint<T>(x + other, y + other); }
TPoint<T>& operator+=(T other) { x+=other; y+=other; return *this; }
TPoint<T> operator-(T other) const { return TPoint<T>(x - other, y - other); }
TPoint<T>& operator-=(T other) { x-=other; y-=other; return *this; }
TPoint<T> operator*(const T v) const { return TPoint<T>(x*v, y*v); }
TPoint<T>& operator*=(const T v) { x*=v; y*=v; return *this; }
TPoint<T> operator/(const T v) const { return TPoint<T>(x/v, y/v); }
TPoint<T>& operator/=(const T v) { x/=v; y/=v; return *this; }
bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
float length() const { return sqrt((float)(x*x + y*y)); }
T manhattanLength() const { return std::abs(x) + std::abs(y); }
float distanceFrom(const TPoint<T>& other) const {
return TPoint<T>(x - other.x, y - other.y).getLength();
}
T x, y;
};
typedef TPoint<int> Point;
typedef TPoint<float> PointF;
template<class T>
std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
{
out << point.x << " " << point.y;
return out;
}
template<class T>
std::istream& operator>>(std::istream& in, TPoint<T>& point)
{
in >> point.x;
in >> point.y;
return in;
}
#endif

View File

@@ -1,298 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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 RECT_H
#define RECT_H
#include "types.h"
#include <sstream>
template<class T>
class TPoint;
template<class T>
class TSize;
template<class T>
class TRect
{
public:
TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
bool isEmpty() const { return x1 > x2 || y1 > y2; }
bool isValid() const { return x1 <= x2 && y1 <= y2; }
T left() const { return x1; }
T top() const { return y1; }
T right() const { return x2; }
T bottom() const { return y2; }
T horizontalCenter() const { return x1 + (x2 - x1)/2; }
T verticalCenter() const { return y1 + (y2 - y1)/2; }
T x() const { return x1; }
T y() const { return y1; }
TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
T width() const { return x2 - x1 + 1; }
T height() const { return y2 - y1 + 1; }
TSize<T> size() const { return TSize<T>(width(), height()); }
void reset() { x1 = y1 = 0; x2 = y2 = -1; }
void clear() { x2 = x1 - 1; y2 = y1 - 1; }
void setLeft(T pos) { x1 = pos; }
void setTop(T pos) { y1 = pos; }
void setRight(T pos) { x2 = pos; }
void setBottom(T pos) { y2 = pos; }
void setX(T x) { x1 = x; }
void setY(T y) { y1 = y; }
void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
void setWidth(T width) { x2 = x1 + width - 1; }
void setHeight(T height) { y2 = y1 + height- 1; }
void resize(T width, T height) { x2 = x1 + width - 1; y2 = y1 + height - 1; }
void resize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
void addLeft(T add) { x1 -= add; }
void addTop(T add) { y1 -= add; }
void addRight(T add) { x2 += add; }
void addBottom(T add) { y2 += add; }
void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x, y1 + p.y), TPoint<T>(x2 + p.x, y2 + p.y)); }
TRect<T> expanded(T pixels) const { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
void moveCenter(const TPoint<T> &p) {
T w = x2 - x1;
T h = y2 - y1;
x1 = p.x - w/2;
y1 = p.y - h/2;
x2 = x1 + w;
y2 = y1 + h;
}
void moveHorizontalCenter(T x) {
T w = x2 - x1;
x1 = x - w/2;
x2 = x1 + w;
}
void moveVerticalCenter(T y) {
T h = y2 - y1;
y1 = y - h/2;
y2 = y1 + h;
}
bool contains(const TPoint<T> &p, bool insideOnly = false) const {
T l, r;
if(x2 < x1 - 1) {
l = x2;
r = x1;
} else {
l = x1;
r = x2;
}
if(insideOnly) {
if(p.x <= l || p.x >= r)
return false;
} else {
if(p.x < l || p.x > r)
return false;
}
T t, b;
if(y2 < y1 - 1) {
t = y2;
b = y1;
} else {
t = y1;
b = y2;
}
if(insideOnly) {
if(p.y <= t || p.y >= b)
return false;
} else {
if(p.y < t || p.y > b)
return false;
}
return true;
}
bool intersects(const TRect<T> &r) const {
if(isNull() || r.isNull())
return false;
int l1 = x1;
int r1 = x1;
if(x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if(r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
if (l1 > r2 || l2 > r1)
return false;
int t1 = y1;
int b1 = y1;
if(y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if (r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
if(t1 > b2 || t2 > b1)
return false;
return true;
}
TRect<T> united(const TRect<T> &r) const {
TRect<T> tmp;
tmp.x1 = std::min(x1, r.x1);
tmp.x2 = std::max(x2, r.x2);
tmp.y1 = std::min(y1, r.y1);
tmp.y2 = std::max(y2, r.y2);
return tmp;
}
TRect<T> intersection(const TRect<T> &r) const {
if(isNull())
return r;
if(r.isNull())
return *this;
int l1 = x1;
int r1 = x1;
if(x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if(r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
int t1 = y1;
int b1 = y1;
if(y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if(r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
TRect<T> tmp;
tmp.x1 = std::max(l1, l2);
tmp.x2 = std::min(r1, r2);
tmp.y1 = std::max(t1, t2);
tmp.y2 = std::min(b1, b2);
return tmp;
}
void bound(const TRect<T> &r) {
if(isNull() || r.isNull())
return;
if(left() < r.left())
moveLeft(r.left());
if(top() < r.top())
moveTop(r.top());
if(bottom() > r.bottom())
moveBottom(r.bottom());
if(right() > r.right())
moveRight(r.right());
}
TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
TRect<T>& operator|=(const TRect<T>& other) { *this = united(other); return *this; }
TRect<T>& operator&=(const TRect<T>& other) { *this = intersection(other); return *this; }
private:
T x1, y1, x2, y2;
};
typedef TRect<int> Rect;
typedef TRect<float> RectF;
template<class T>
std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
{
out << rect.left() << " " << rect.top() << " " << rect.width() << " " << rect.height();
return out;
}
template<class T>
std::istream& operator>>(std::istream& in, TRect<T>& rect)
{
T x, y , w, h;
in >> x >> y >> w >> h;
rect.setRect(x,y,w,h);
return in;
}
#endif

View File

@@ -1,122 +0,0 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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
#include "point.h"
#include "../const.h"
template<class T>
class TSize
{
public:
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 resize(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, 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;
}
}
}
void scale(int w, int h, Fw::AspectRatioMode mode) { scale(TSize<T>(w, h)); }
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;
template<class T>
std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
{
out << size.width() << " " << size.height();
return out;
}
template<class T>
std::istream& operator>>(std::istream& in, TSize<T>& size)
{
T w, h;
in >> w >> h;
size.resize(w, h);
return in;
}
#endif

View File

@@ -76,6 +76,11 @@ std::string mkstr(const T&... args) {
return buf.str();
}
template<typename... T>
void throwException(const T&... args) {
throw Exception(Fw::mkstr(args...));
}
// used by dumper
struct dump_util {
~dump_util() { std::cout << std::endl; }
@@ -270,9 +275,16 @@ std::vector<T> split(const std::string& str, const std::string& separators = " "
return results;
}
template<typename... T>
void throwException(const T&... args) {
throw Exception(Fw::mkstr(args...));
inline std::string resolvePath(const std::string& file, std::string sourcePath) {
if(boost::starts_with(file, "/"))
return file;
if(!boost::ends_with(sourcePath, "/")) {
std::size_t slashPos = sourcePath.find_last_of("/");
if(slashPos == std::string::npos)
throwException("invalid source path '", sourcePath, "' for file '", file, "'");
sourcePath = sourcePath.substr(0, slashPos + 1);
}
return sourcePath + file;
}
template<typename T>