Subversion Repositories Applications.gtt

Rev

Rev 61 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 jpm 1
<?php
2
/*
3
 * This work is hereby released into the Public Domain.
4
 * To view a copy of the public domain dedication,
5
 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
6
 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
7
 *
8
 */
9
 
10
require_once dirname(__FILE__)."/../Graph.class.php";
11
 
12
 
13
/**
14
 * Draw shadows
15
 *
16
 */
17
class awShadow {
18
 
19
	/**
20
	 * Shadow on left and top sides
21
	 *
22
	 * @var int
23
	 */
24
	const LEFT_TOP = 1;
25
 
26
	/**
27
	 * Shadow on left and bottom sides
28
	 *
29
	 * @var int
30
	 */
31
	const LEFT_BOTTOM = 2;
32
 
33
 
34
	/**
35
	 * Shadow on right and top sides
36
	 *
37
	 * @var int
38
	 */
39
	const RIGHT_TOP = 3;
40
 
41
	/**
42
	 * Shadow on right and bottom sides
43
	 *
44
	 * @var int
45
	 */
46
	const RIGHT_BOTTOM = 4;
47
 
48
	/**
49
	 * In mode
50
	 *
51
	 * @var int
52
	 */
53
	const IN = 1;
54
 
55
	/**
56
	 * Out mode
57
	 *
58
	 * @var int
59
	 */
60
	const OUT = 2;
61
 
62
	/**
63
	 * Shadow size
64
	 *
65
	 * @var int
66
	 */
67
	private $size = 0;
68
 
69
	/**
70
	 * Hide shadow ?
71
	 *
72
	 * @var bool
73
	 */
74
	protected $hide = FALSE;
75
 
76
	/**
77
	 * Shadow color
78
	 *
79
	 * @var Color
80
	 */
81
	private $color;
82
 
83
	/**
84
	 * Shadow position
85
	 *
86
	 * @var int
87
	 */
88
	private $position;
89
 
90
	/**
91
	 * Smooth shadow ?
92
	 *
93
	 * @var bool
94
	 */
95
	private $smooth = FALSE;
96
 
97
	/**
98
	 * Shadow constructor
99
	 *
100
	 * @param int $position Shadow position
101
	 */
102
	public function __construct($position) {
103
		$this->setPosition($position);
104
	}
105
 
106
	/**
107
	 * Hide shadow ?
108
	 *
109
	 * @param bool $hide
110
	 */
111
	public function hide($hide = TRUE) {
112
		$this->hide = (bool)$hide;
113
	}
114
 
115
	/**
116
	 * Show shadow ?
117
	 *
118
	 * @param bool $show
119
	 */
120
	public function show($show = TRUE) {
121
		$this->hide = (bool)!$show;
122
	}
123
 
124
	/**
125
	 * Change shadow size
126
	 *
127
	 * @param int $size
128
	 * @param bool $smooth Smooth the shadow (facultative argument)
129
	 */
130
	public function setSize($size, $smooth = NULL) {
131
		$this->size = (int)$size;
132
		if($smooth !== NULL) {
133
			$this->smooth($smooth);
134
		}
135
	}
136
 
137
	/**
138
	 * Change shadow color
139
	 *
140
	 * @param awColor $color
141
	 */
142
	public function setColor(awColor $color) {
143
		$this->color = $color;
144
	}
145
 
146
	/**
147
	 * Change shadow position
148
	 *
149
	 * @param int $position
150
	 */
151
	public function setPosition($position) {
152
		$this->position = (int)$position;
153
	}
154
 
155
	/**
156
	 * Smooth shadow ?
157
	 *
158
	 * @param bool $smooth
159
	 */
160
	public function smooth($smooth) {
161
		$this->smooth = (bool)$smooth;
162
	}
163
 
164
	/**
165
	 * Get the space taken by the shadow
166
	 *
167
	 * @return Side
168
	 */
169
	public function getSpace() {
170
 
171
		return new awSide(
172
			($this->position === awShadow::LEFT_TOP or $this->position === awShadow::LEFT_BOTTOM) ? $this->size : 0,
173
			($this->position === awShadow::RIGHT_TOP or $this->position === awShadow::RIGHT_BOTTOM) ? $this->size : 0,
174
			($this->position === awShadow::LEFT_TOP or $this->position === awShadow::RIGHT_TOP) ? $this->size : 0,
175
			($this->position === awShadow::LEFT_BOTTOM or $this->position === awShadow::RIGHT_BOTTOM) ? $this->size : 0
176
		);
177
 
178
	}
179
 
180
	/**
181
	 * Draw shadow
182
	 *
183
	 * @param awDriver $driver
184
	 * @param awPoint $p1 Top-left point
185
	 * @param awPoint $p2 Right-bottom point
186
	 * @param int Drawing mode
187
	 */
188
	public function draw(awDriver $driver, awPoint $p1, awPoint $p2, $mode) {
189
 
190
		if($this->hide) {
191
			return;
192
		}
193
 
194
		if($this->size <= 0) {
195
			return;
196
		}
197
 
198
		$driver = clone $driver;
199
 
200
		$color = ($this->color instanceof awColor) ? $this->color : new awColor(125, 125, 125);
201
 
202
		switch($this->position) {
203
 
204
			case awShadow::RIGHT_BOTTOM :
205
 
206
				if($mode === awShadow::OUT) {
207
					$t1 = $p1->move(0, 0);
208
					$t2 = $p2->move($this->size + 1, $this->size + 1);
209
				} else { // PHP 4 compatibility
210
					$t1 = $p1->move(0, 0);
211
					$t2 = $p2->move(0, 0);
212
				}
213
 
214
				$width = $t2->x - $t1->x;
215
				$height = $t2->y - $t1->y;
216
 
217
				$driver->setAbsPosition($t1->x + $driver->x, $t1->y + $driver->y);
218
 
219
				$driver->filledRectangle(
220
					$color,
221
					new awLine(
222
						new awPoint($width - $this->size, $this->size),
223
						new awPoint($width - 1, $height - 1)
224
					)
225
				);
226
 
227
				$driver->filledRectangle(
228
					$color,
229
					new awLine(
230
						new awPoint($this->size, $height - $this->size),
231
						new awPoint($width - $this->size - 1, $height - 1)
232
					)
233
				);
234
 
235
				$this->smoothPast($driver, $color, $width, $height);
236
 
237
				break;
238
 
239
			case awShadow::LEFT_TOP :
240
 
241
				if($mode === awShadow::OUT) {
242
					$t1 = $p1->move(- $this->size, - $this->size);
243
					$t2 = $p2->move(0, 0);
244
				} else { // PHP 4 compatibility
245
					$t1 = $p1->move(0, 0);
246
					$t2 = $p2->move(0, 0);
247
				}
248
 
249
				$width = $t2->x - $t1->x;
250
				$height = $t2->y - $t1->y;
251
 
252
				$driver->setAbsPosition($t1->x + $driver->x, $t1->y + $driver->y);
253
 
254
				$height = max($height + 1, $this->size);
255
 
256
				$driver->filledRectangle(
257
					$color,
258
					new awLine(
259
						new awPoint(0, 0),
260
						new awPoint($this->size - 1, $height - $this->size - 1)
261
					)
262
				);
263
 
264
				$driver->filledRectangle(
265
					$color,
266
					new awLine(
267
						new awPoint($this->size, 0),
268
						new awPoint($width - $this->size - 1, $this->size - 1)
269
					)
270
				);
271
 
272
				$this->smoothPast($driver, $color, $width, $height);
273
 
274
				break;
275
 
276
			case awShadow::RIGHT_TOP :
277
 
278
				if($mode === awShadow::OUT) {
279
					$t1 = $p1->move(0, - $this->size);
280
					$t2 = $p2->move($this->size + 1, 0);
281
				} else { // PHP 4 compatibility
282
					$t1 = $p1->move(0, 0);
283
					$t2 = $p2->move(0, 0);
284
				}
285
 
286
				$width = $t2->x - $t1->x;
287
				$height = $t2->y - $t1->y;
288
 
289
				$driver->setAbsPosition($t1->x + $driver->x, $t1->y + $driver->y);
290
 
291
				$height = max($height + 1, $this->size);
292
 
293
				$driver->filledRectangle(
294
					$color,
295
					new awLine(
296
						new awPoint($width - $this->size, 0),
297
						new awPoint($width - 1, $height - $this->size - 1)
298
					)
299
				);
300
 
301
				$driver->filledRectangle(
302
					$color,
303
					new awLine(
304
						new awPoint($this->size, 0),
305
						new awPoint($width - $this->size - 1, $this->size - 1)
306
					)
307
				);
308
 
309
				$this->smoothFuture($driver, $color, $width, $height);
310
 
311
				break;
312
 
313
			case awShadow::LEFT_BOTTOM :
314
 
315
				if($mode === awShadow::OUT) {
316
					$t1 = $p1->move(- $this->size, 0);
317
					$t2 = $p2->move(0, $this->size + 1);
318
				} else { // PHP 4 compatibility
319
					$t1 = $p1->move(0, 0);
320
					$t2 = $p2->move(0, 0);
321
				}
322
 
323
				$width = $t2->x - $t1->x;
324
				$height = $t2->y - $t1->y;
325
 
326
				$driver->setAbsPosition($t1->x + $driver->x, $t1->y + $driver->y);
327
 
328
				$driver->filledRectangle(
329
					$color,
330
					new awLine(
331
						new awPoint(0, $this->size),
332
						new awPoint($this->size - 1, $height - 1)
333
					)
334
				);
335
 
336
				$driver->filledRectangle(
337
					$color,
338
					new awLine(
339
						new awPoint($this->size, $height - $this->size),
340
						new awPoint($width - $this->size - 1, $height - 1)
341
					)
342
				);
343
 
344
				$this->smoothFuture($driver, $color, $width, $height);
345
 
346
				break;
347
 
348
		}
349
 
350
	}
351
 
352
	private function smoothPast(awDriver $driver, awColor $color, $width, $height) {
353
 
354
		if($this->smooth) {
355
 
356
			for($i = 0; $i < $this->size; $i++) {
357
				for($j = 0; $j <= $i; $j++) {
358
					$driver->point(
359
						$color,
360
						new awPoint($i, $j + $height - $this->size)
361
					);
362
				}
363
			}
364
 
365
			for($i = 0; $i < $this->size; $i++) {
366
				for($j = 0; $j <= $i; $j++) {
367
					$driver->point(
368
						$color,
369
						new awPoint($width - $this->size + $j, $i)
370
					);
371
				}
372
			}
373
 
374
		}
375
 
376
	}
377
 
378
	private function smoothFuture(awDriver $driver, awColor $color, $width, $height) {
379
 
380
		if($this->smooth) {
381
 
382
			for($i = 0; $i < $this->size; $i++) {
383
				for($j = 0; $j <= $i; $j++) {
384
					$driver->point(
385
						$color,
386
						new awPoint($i, $this->size - $j - 1)
387
					);
388
				}
389
			}
390
 
391
			for($i = 0; $i < $this->size; $i++) {
392
				for($j = 0; $j <= $i; $j++) {
393
					$driver->point(
394
						$color,
395
						new awPoint($width - $this->size + $j, $height - $i - 1)
396
					);
397
				}
398
			}
399
 
400
		}
401
	}
402
 
403
}
404
 
405
registerClass('Shadow');
406
?>