| 1 |
// Copyright (C) 2007 Christopher E. Miller |
|---|
| 2 |
// See the included license.txt for license details. |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
/// |
|---|
| 6 |
module dfl.drawing; |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
import dfl.internal.gtk; |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
/// X and Y coordinate. |
|---|
| 13 |
struct Point // docmain |
|---|
| 14 |
{ |
|---|
| 15 |
union |
|---|
| 16 |
{ |
|---|
| 17 |
struct |
|---|
| 18 |
{ |
|---|
| 19 |
gint x; |
|---|
| 20 |
gint y; |
|---|
| 21 |
} |
|---|
| 22 |
GdkPoint point; // package |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
/// Construct a new Point. |
|---|
| 27 |
static Point opCall(int x, int y) |
|---|
| 28 |
{ |
|---|
| 29 |
Point pt; |
|---|
| 30 |
pt.x = x; |
|---|
| 31 |
pt.y = y; |
|---|
| 32 |
return pt; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
/// ditto |
|---|
| 36 |
static Point opCall() |
|---|
| 37 |
{ |
|---|
| 38 |
Point pt; |
|---|
| 39 |
return pt; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
/// |
|---|
| 44 |
int opEquals(Point pt) |
|---|
| 45 |
{ |
|---|
| 46 |
return x == pt.x && y == pt.y; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
/// |
|---|
| 51 |
Point opAdd(Size sz) |
|---|
| 52 |
{ |
|---|
| 53 |
Point result; |
|---|
| 54 |
result.x = x + sz.width; |
|---|
| 55 |
result.y = y + sz.height; |
|---|
| 56 |
return result; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
/// |
|---|
| 61 |
Point opSub(Size sz) |
|---|
| 62 |
{ |
|---|
| 63 |
Point result; |
|---|
| 64 |
result.x = x - sz.width; |
|---|
| 65 |
result.y = y - sz.height; |
|---|
| 66 |
return result; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
/// |
|---|
| 71 |
void opAddAssign(Size sz) |
|---|
| 72 |
{ |
|---|
| 73 |
x += sz.width; |
|---|
| 74 |
y += sz.height; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
/// |
|---|
| 79 |
void opSubAssign(Size sz) |
|---|
| 80 |
{ |
|---|
| 81 |
x -= sz.width; |
|---|
| 82 |
y -= sz.height; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
/// |
|---|
| 87 |
Point opNeg() |
|---|
| 88 |
{ |
|---|
| 89 |
return Point(-x, -y); |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
/// Width and height. |
|---|
| 95 |
struct Size // docmain |
|---|
| 96 |
{ |
|---|
| 97 |
int width; |
|---|
| 98 |
int height; |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
/// Construct a new Size. |
|---|
| 102 |
static Size opCall(int width, int height) |
|---|
| 103 |
{ |
|---|
| 104 |
Size sz; |
|---|
| 105 |
sz.width = width; |
|---|
| 106 |
sz.height = height; |
|---|
| 107 |
return sz; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
/// ditto |
|---|
| 111 |
static Size opCall() |
|---|
| 112 |
{ |
|---|
| 113 |
Size sz; |
|---|
| 114 |
return sz; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
/// |
|---|
| 119 |
int opEquals(Size sz) |
|---|
| 120 |
{ |
|---|
| 121 |
return width == sz.width && height == sz.height; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
/// |
|---|
| 126 |
Size opAdd(Size sz) |
|---|
| 127 |
{ |
|---|
| 128 |
Size result; |
|---|
| 129 |
result.width = width + sz.width; |
|---|
| 130 |
result.height = height + sz.height; |
|---|
| 131 |
return result; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
/// |
|---|
| 136 |
Size opSub(Size sz) |
|---|
| 137 |
{ |
|---|
| 138 |
Size result; |
|---|
| 139 |
result.width = width - sz.width; |
|---|
| 140 |
result.height = height - sz.height; |
|---|
| 141 |
return result; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
/// |
|---|
| 146 |
void opAddAssign(Size sz) |
|---|
| 147 |
{ |
|---|
| 148 |
width += sz.width; |
|---|
| 149 |
height += sz.height; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
/// |
|---|
| 154 |
void opSubAssign(Size sz) |
|---|
| 155 |
{ |
|---|
| 156 |
width -= sz.width; |
|---|
| 157 |
height -= sz.height; |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
/// X, Y, width and height rectangle dimensions. |
|---|
| 163 |
struct Rect // docmain |
|---|
| 164 |
{ |
|---|
| 165 |
gint x, y, width, height; |
|---|
| 166 |
|
|---|
| 167 |
// Used internally. |
|---|
| 168 |
void getRect(GdkRectangle* r) // package |
|---|
| 169 |
{ |
|---|
| 170 |
*r = *cast(GdkRectangle*)this; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
/// |
|---|
| 175 |
Point location() // getter |
|---|
| 176 |
{ |
|---|
| 177 |
return Point(x, y); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
/// ditto |
|---|
| 181 |
void location(Point pt) // setter |
|---|
| 182 |
{ |
|---|
| 183 |
x = pt.x; |
|---|
| 184 |
y = pt.y; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
/// |
|---|
| 189 |
Size size() //getter |
|---|
| 190 |
{ |
|---|
| 191 |
return Size(width, height); |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
/// ditto |
|---|
| 195 |
void size(Size sz) // setter |
|---|
| 196 |
{ |
|---|
| 197 |
width = sz.width; |
|---|
| 198 |
height = sz.height; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
/// |
|---|
| 203 |
int right() // getter |
|---|
| 204 |
{ |
|---|
| 205 |
return x + width; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
/// |
|---|
| 210 |
int bottom() // getter |
|---|
| 211 |
{ |
|---|
| 212 |
return y + height; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
/// Construct a new Rect. |
|---|
| 217 |
static Rect opCall(int x, int y, int width, int height) |
|---|
| 218 |
{ |
|---|
| 219 |
Rect r; |
|---|
| 220 |
r.x = x; |
|---|
| 221 |
r.y = y; |
|---|
| 222 |
r.width = width; |
|---|
| 223 |
r.height = height; |
|---|
| 224 |
return r; |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
/// ditto |
|---|
| 228 |
static Rect opCall(Point location, Size size) |
|---|
| 229 |
{ |
|---|
| 230 |
Rect r; |
|---|
| 231 |
r.x = location.x; |
|---|
| 232 |
r.y = location.y; |
|---|
| 233 |
r.width = size.width; |
|---|
| 234 |
r.height = size.height; |
|---|
| 235 |
return r; |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
/// ditto |
|---|
| 239 |
static Rect opCall() |
|---|
| 240 |
{ |
|---|
| 241 |
Rect r; |
|---|
| 242 |
return r; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
// Used internally. |
|---|
| 247 |
static Rect opCall(GdkRectangle* rect) // package |
|---|
| 248 |
{ |
|---|
| 249 |
Rect r; |
|---|
| 250 |
r = *cast(Rect*)rect; |
|---|
| 251 |
return r; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
/// Construct a new Rect from left, top, right and bottom values. |
|---|
| 256 |
static Rect fromLTRB(int left, int top, int right, int bottom) |
|---|
| 257 |
{ |
|---|
| 258 |
Rect r; |
|---|
| 259 |
r.x = left; |
|---|
| 260 |
r.y = top; |
|---|
| 261 |
r.width = right - left; |
|---|
| 262 |
r.height = bottom - top; |
|---|
| 263 |
return r; |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
/// |
|---|
| 268 |
int opEquals(Rect r) |
|---|
| 269 |
{ |
|---|
| 270 |
return x == r.x && y == r.y && |
|---|
| 271 |
width == r.width && height == r.height; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
/// |
|---|
| 276 |
bool contains(int c_x, int c_y) |
|---|
| 277 |
{ |
|---|
| 278 |
if(c_x >= x && c_y >= y) |
|---|
| 279 |
{ |
|---|
| 280 |
if(c_x <= right && c_y <= bottom) |
|---|
| 281 |
return true; |
|---|
| 282 |
} |
|---|
| 283 |
return false; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
/// ditto |
|---|
| 287 |
bool contains(Point pos) |
|---|
| 288 |
{ |
|---|
| 289 |
return contains(pos.x, pos.y); |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
/// ditto |
|---|
| 293 |
// Contained entirely within -this-. |
|---|
| 294 |
bool contains(Rect r) |
|---|
| 295 |
{ |
|---|
| 296 |
if(r.x >= x && r.y >= y) |
|---|
| 297 |
{ |
|---|
| 298 |
if(r.right <= right && r.bottom <= bottom) |
|---|
| 299 |
return true; |
|---|
| 300 |
} |
|---|
| 301 |
return false; |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
/// |
|---|
| 306 |
void inflate(int i_width, int i_height) |
|---|
| 307 |
{ |
|---|
| 308 |
x -= i_width; |
|---|
| 309 |
width += i_width * 2; |
|---|
| 310 |
y -= i_height; |
|---|
| 311 |
height += i_height * 2; |
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
/// ditto |
|---|
| 315 |
void inflate(Size insz) |
|---|
| 316 |
{ |
|---|
| 317 |
inflate(insz.width, insz.height); |
|---|
| 318 |
} |
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
/// |
|---|
| 322 |
// Just tests if there's an intersection. |
|---|
| 323 |
bool intersectsWith(Rect r) |
|---|
| 324 |
{ |
|---|
| 325 |
if(r.right >= x && r.bottom >= y) |
|---|
| 326 |
{ |
|---|
| 327 |
if(r.y <= bottom && r.x <= right) |
|---|
| 328 |
return true; |
|---|
| 329 |
} |
|---|
| 330 |
return false; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
/// |
|---|
| 335 |
void offset(int x, int y) |
|---|
| 336 |
{ |
|---|
| 337 |
this.x += x; |
|---|
| 338 |
this.y += y; |
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
/// ditto |
|---|
| 342 |
void offset(Point pt) |
|---|
| 343 |
{ |
|---|
| 344 |
//return offset(pt.x, pt.y); |
|---|
| 345 |
this.x += pt.x; |
|---|
| 346 |
this.y += pt.y; |
|---|
| 347 |
} |
|---|
| 348 |
} |
|---|