/* * location.h * BoE * * Created by Celtic Minstrel on 20/04/09. * */ #ifndef BOE_LOCATION_H #define BOE_LOCATION_H #include #include #include struct rectangle; struct location { int x,y; location(); location(int x, int y); template location(sf::Vector2 other) : location(other.x, other.y) {} bool in(rectangle r); template operator typename sf::template Vector2() { return sf::Vector2(x,y); } }; struct loc_compare { bool operator()(location a, location b) const; }; class rectangle_size_delegate { friend struct rectangle; rectangle& forRect; int rectangle::* b1; int rectangle::* b2; rectangle_size_delegate(rectangle& rect, int rectangle::* first, int rectangle::* last); public: operator int(); rectangle_size_delegate& operator=(int val); rectangle_size_delegate& operator+=(int val); rectangle_size_delegate& operator-=(int val); rectangle_size_delegate& operator*=(int val); rectangle_size_delegate& operator/=(int val); }; struct rectangle { int top, left, bottom, right; rectangle(); rectangle(location tl, location br); rectangle(int t, int l, int b, int r); explicit rectangle(const sf::Texture& texture); explicit rectangle(const sf::RenderTarget& texture); template rectangle(sf::Rect other) : rectangle(other.top, other.left, other.top + other.height, other.left + other.width) {} template rectangle(sf::Vector2 size) : rectangle(0, 0, size.y, size.x) {} rectangle_size_delegate width(); rectangle_size_delegate height(); location centre(); location topLeft(); location topRight(); location bottomLeft(); location bottomRight(); bool contains(location p); bool contains(int x, int y); void offset(int h, int v); void offset(location diff); template void offset(sf::Vector2 diff) {offset(diff.x,diff.y);} void inset(int dh, int dv); rectangle& operator&=(rectangle other); template operator typename sf::template Rect() { return sf::Rect(left, top, width(), height()); } }; struct info_rect_t : public rectangle { std::string descr; info_rect_t(int t, int l, int b, int r, const std::string& s) : rectangle(t,l,b,r), descr(s) {} info_rect_t(const rectangle& r) : rectangle(r) {} // Declaring one constructor suppresses all implicit constructors, so declare them explicitly info_rect_t() = default; info_rect_t(const info_rect_t& other) = default; // Ditto for assignment operator info_rect_t& operator=(const info_rect_t& other) = default; }; struct sign_loc_t : public location { std::string text; sign_loc_t(int x, int y, const std::string& s) : location(x,y), text(s) {} sign_loc_t(const location& loc) : location(loc) {} // Declaring one constructor suppresses all implicit constructors, so declare them explicitly sign_loc_t() = default; sign_loc_t(const sign_loc_t& other) = default; // Ditto for assignment operator sign_loc_t& operator=(const sign_loc_t& other) = default; }; struct spec_loc_t : public location { long spec; spec_loc_t(int x, int y, long spec) : location(x,y), spec(spec) {} spec_loc_t(const location& loc) : location(loc) {} spec_loc_t& operator=(const location& loc) { *this = spec_loc_t(loc); return *this; } // Declaring one constructor suppresses all implicit constructors, so declare them explicitly spec_loc_t() = default; spec_loc_t(const spec_loc_t& other) = default; // Ditto for assignment operator spec_loc_t& operator=(const spec_loc_t& other) = default; }; bool operator == (location p1,location p2); bool operator != (location p1,location p2); bool operator == (rectangle r1, rectangle r2); bool operator != (rectangle r1, rectangle r2); // TODO: This isn't a union, because it returns a rectangle. rectangle operator&(rectangle one, rectangle two); rectangle rectunion(rectangle one, rectangle two); short dist(location p1,location p2); short vdist(location p1,location p2); location loc(int a, int b); location loc(); rectangle rect(); rectangle rect(location tl, location br); rectangle rect(int top, int left, int bottom, int right); std::ostream& operator<< (std::ostream& out, location l); std::ostream& operator<< (std::ostream& out, spec_loc_t l); std::ostream& operator<< (std::ostream& out, sign_loc_t l); std::ostream& operator<< (std::ostream& out, rectangle r); std::ostream& operator<< (std::ostream& out, info_rect_t r); #endif