skins2: add comparison for two rect
[vlc.git] / modules / gui / skins2 / utils / position.hpp
bloba02b3d9c2d48491bd0930e2cdbd9f8122653ce07
1 /*****************************************************************************
2 * position.hpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id$
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef POSITION_HPP
26 #define POSITION_HPP
28 #include "variable.hpp"
29 #include "observer.hpp"
30 #include "pointer.hpp"
33 /// Interface for rectangular objects
34 class Box
36 public:
37 virtual ~Box() { }
39 /// Get the size of the box
40 virtual int getWidth() const = 0;
41 virtual int getHeight() const = 0;
45 /// Interface for rectangular objects with a position
46 class GenericRect: public Box
48 public:
49 virtual int getLeft() const = 0;
50 virtual int getTop() const = 0;
54 /// Characterization of a rectangle
55 class SkinsRect: public GenericRect
57 public:
58 SkinsRect( int left, int top, int right, int bottom );
60 virtual int getLeft() const { return m_left; }
61 virtual int getTop() const { return m_top; }
62 virtual int getRight() const { return m_right; }
63 virtual int getBottom() const { return m_bottom; }
64 virtual int getWidth() const { return m_right - m_left; }
65 virtual int getHeight() const { return m_bottom - m_top; }
67 private:
68 int m_left;
69 int m_top;
70 int m_right;
71 int m_bottom;
75 /// Relative position of a rectangle in a box
76 /**
77 * Note: Even if the object is tied to its direct container rectangle, the
78 * coordinates returned by getLeft(), getTop(), getRight() and getBottom()
79 * are not relative to the direct container (which is usually a panel or
80 * the layout) but to the root container (i.e. the layout).
82 class Position: public GenericRect
84 public:
85 /// Type for reference edge/corner
86 enum Ref_t
88 /// Coordinates are relative to the upper left corner
89 kLeftTop,
90 /// Coordinates are relative to the upper right corner
91 kRightTop,
92 /// Coordinates are relative to the lower left corner
93 kLeftBottom,
94 /// Coordinates are relative to the lower right corner
95 kRightBottom
98 /// Create a new position relative to the given box
99 Position( int left, int top, int right, int bottom,
100 const GenericRect &rRect,
101 Ref_t refLeftTop, Ref_t refRightBottom,
102 bool xKeepRatio, bool yKeepRatio );
104 ~Position() { }
106 /// Get the position relative to the left top corner of the box
107 virtual int getLeft() const;
108 virtual int getTop() const;
109 int getRight() const;
110 int getBottom() const;
111 /// Get the size of the rectangle
112 virtual int getWidth() const;
113 virtual int getHeight() const;
114 /// Get the reference corners
115 Ref_t getRefLeftTop() const { return m_refLeftTop; }
116 Ref_t getRefRightBottom() const { return m_refRighBottom; }
118 private:
119 /// Position and reference edge/corner
120 int m_left;
121 int m_top;
122 int m_right;
123 int m_bottom;
124 const GenericRect &m_rRect;
125 Ref_t m_refLeftTop;
126 Ref_t m_refRighBottom;
127 /// "Keep ratio" mode
128 bool m_xKeepRatio;
129 bool m_yKeepRatio;
130 /// Initial width ratio (usually between 0 and 1)
131 double m_xRatio;
132 /// Initial height ratio (usually between 0 and 1)
133 double m_yRatio;
136 typedef CountedPtr<Position> PositionPtr;
139 /// Variable implementing the Box interface
140 class VarBox: public Variable, public Box, public Subject<VarBox>
142 public:
143 VarBox( intf_thread_t *pIntf, int width = 0, int height = 0 );
145 virtual ~VarBox() { }
147 /// Get the variable type
148 virtual const string &getType() const { return m_type; }
150 /// Get the size of the box
151 virtual int getWidth() const;
152 virtual int getHeight() const;
154 /// Change the size of the box
155 void setSize( int width, int height );
157 private:
158 /// Variable type
159 static const string m_type;
160 /// Size
161 int m_width, m_height;
165 class rect
167 public:
168 rect( int v_x = 0, int v_y = 0, int v_width = 0, int v_height = 0 )
169 : x( v_x ), y( v_y ), width( v_width ), height( v_height ) { }
170 ~rect() { }
171 int x;
172 int y;
173 int width;
174 int height;
176 // rect2 fully included in rect1
177 static bool isIncluded( const rect& rect2, const rect& rect1 )
179 int x1 = rect1.x;
180 int y1 = rect1.y;
181 int w1 = rect1.width;
182 int h1 = rect1.height;
184 int x2 = rect2.x;
185 int y2 = rect2.y;
186 int w2 = rect2.width;
187 int h2 = rect2.height;
189 return x2 >= x1 && x2 + w2 <= x1 + w1
190 && y2 >= y1 && y2 + h2 <= y1 + h1;
193 static bool areDisjunct( const rect& rect2, const rect& rect1 )
195 int x1 = rect1.x;
196 int y1 = rect1.y;
197 int w1 = rect1.width;
198 int h1 = rect1.height;
200 int x2 = rect2.x;
201 int y2 = rect2.y;
202 int w2 = rect2.width;
203 int h2 = rect2.height;
205 return y2 + h2 -1 < y1 // rect2 above rect1
206 || y2 > y1 + h1 - 1 // rect2 under rect1
207 || x2 > x1 + w1 -1 // rect2 right of rect1
208 || x2 + w2 - 1 < x1; // rect2 left of rect1
211 static bool intersect( const rect& rect1, const rect& rect2, rect* pRect )
213 int x1 = rect1.x;
214 int y1 = rect1.y;
215 int w1 = rect1.width;
216 int h1 = rect1.height;
218 int x2 = rect2.x;
219 int y2 = rect2.y;
220 int w2 = rect2.width;
221 int h2 = rect2.height;
223 if( areDisjunct( rect1, rect2 ) )
224 return false;
225 else
227 int left = max( x1, x2 );
228 int right = min( x1 + w1 - 1, x2 + w2 - 1 );
229 int top = max( y1, y2 );
230 int bottom = min( y1 + h1 - 1, y2 + h2 -1 );
231 pRect->x = left;
232 pRect->y = top;
233 pRect->width = right - left + 1;
234 pRect->height = bottom - top + 1;
236 return pRect->width > 0 && pRect->height > 0;
240 static bool join( const rect& rect1, const rect& rect2, rect* pRect )
242 int x1 = rect1.x;
243 int y1 = rect1.y;
244 int w1 = rect1.width;
245 int h1 = rect1.height;
247 int x2 = rect2.x;
248 int y2 = rect2.y;
249 int w2 = rect2.width;
250 int h2 = rect2.height;
252 int left = min( x1, x2 );
253 int right = max( x1 + w1 - 1, x2 + w2 - 1 );
254 int top = min( y1, y2 );
255 int bottom = max( y1 + h1 - 1, y2 + h2 -1 );
256 pRect->x = left;
257 pRect->y = top;
258 pRect->width = right - left + 1;
259 pRect->height = bottom - top + 1;
261 return pRect->width > 0 && pRect->height > 0;
263 static int min( int x, int y ) { return x < y ? x : y; }
264 static int max( int x, int y ) { return x < y ? y : x; }
266 bool operator==( const rect& other ) const
268 return x == other.x &&
269 y == other.y &&
270 width == other.width &&
271 height == other.height;
275 #endif