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 marks
15
 *
16
 * @package Artichow
17
 */
18
class awMark {
19
 
20
	/**
21
	 * Circle mark
22
	 *
23
	 * @var int
24
	 */
25
	const CIRCLE = 1;
26
 
27
	/**
28
	 * Square mark
29
	 *
30
	 * @var int
31
	 */
32
	const SQUARE = 2;
33
 
34
	/**
35
	 * Triangle mark
36
	 *
37
	 * @var int
38
	 */
39
	const TRIANGLE = 3;
40
 
41
	/**
42
	 * Inverted triangle mark
43
	 *
44
	 * @var int
45
	 */
46
	const INVERTED_TRIANGLE = 4;
47
 
48
	/**
49
	 * Rhombus mark
50
	 *
51
	 * @var int
52
	 */
53
	const RHOMBUS = 5;
54
 
55
	/**
56
	 * Cross (X) mark
57
	 *
58
	 * @var int
59
	 */
60
	const CROSS = 6;
61
 
62
	/**
63
	 * Plus mark
64
	 *
65
	 * @var int
66
	 */
67
	const PLUS = 7;
68
 
69
	/**
70
	 * Image mark
71
	 *
72
	 * @var int
73
	 */
74
	const IMAGE = 8;
75
 
76
	/**
77
	 * Star mark
78
	 *
79
	 * @var int
80
	 */
81
	const STAR = 9;
82
 
83
	/**
84
	 * Paperclip mark
85
	 *
86
	 * @var int
87
	 */
88
	const PAPERCLIP = 10;
89
 
90
	/**
91
	 * Book mark
92
	 *
93
	 * @var int
94
	 */
95
	const BOOK = 11;
96
 
97
	/**
98
	 * Must marks be hidden ?
99
	 *
100
	 * @var bool
101
	 */
102
	protected $hide;
103
 
104
	/**
105
	 * Mark type
106
	 *
107
	 * @var int
108
	 */
109
	protected $type;
110
 
111
	/**
112
	 * Mark size
113
	 *
114
	 * @var int
115
	 */
116
	protected $size = 8;
117
 
118
	/**
119
	 * Fill mark
120
	 *
121
	 * @var Color, Gradient
122
	 */
123
	protected $fill;
124
 
125
	/**
126
	 * Mark image
127
	 *
128
	 * @var Image
129
	 */
130
	protected $image;
131
 
132
	/**
133
	 * To draw marks
134
	 *
135
	 * @var Driver
136
	 */
137
	protected $driver;
138
 
139
	/**
140
	 * Move position from this vector
141
	 *
142
	 * @var Point
143
	 */
144
	protected $move;
145
 
146
	/**
147
	 * Marks border
148
	 *
149
	 * @var Border
150
	 */
151
	public $border;
152
 
153
	/**
154
	 * Build the mark
155
	 */
156
	public function __construct() {
157
 
158
		$this->fill = new awColor(255, 0, 0, 0);
159
		$this->border = new awBorder;
160
		$this->border->hide();
161
 
162
		$this->move = new awPoint(0, 0);
163
 
164
	}
165
 
166
	/**
167
	 * Change mark position
168
	 *
169
	 * @param int $x Add this interval to X coord
170
	 * @param int $y Add this interval to Y coord
171
	 */
172
	public function move($x, $y) {
173
 
174
		$this->move = $this->move->move($x, $y);
175
 
176
	}
177
 
178
	/**
179
	 * Hide marks ?
180
	 *
181
	 * @param bool $hide TRUE to hide marks, FALSE otherwise
182
	 */
183
	public function hide($hide = TRUE) {
184
		$this->hide = (bool)$hide;
185
	}
186
 
187
	/**
188
	 * Show marks ?
189
	 *
190
	 * @param bool $show
191
	 */
192
	public function show($show = TRUE) {
193
		$this->hide = (bool)!$show;
194
	}
195
 
196
	/**
197
	 * Change mark type
198
	 *
199
	 * @param int $size Size in pixels
200
	 */
201
	public function setSize($size) {
202
		$this->size = (int)$size;
203
	}
204
 
205
	/**
206
	 * Change mark type
207
	 *
208
	 * @param int $type New mark type
209
	 * @param int $size Mark size (can be NULL)
210
	 */
211
	public function setType($type, $size = NULL) {
212
		$this->type = (int)$type;
213
		if($size !== NULL) {
214
			$this->setSize($size);
215
		}
216
	}
217
 
218
	/**
219
	 * Fill the mark with a color or a gradient
220
	 *
221
	 * @param mixed $fill A color or a gradient
222
	 */
223
	public function setFill($fill) {
224
		if($fill instanceof awColor or $fill instanceof awGradient) {
225
			$this->fill = $fill;
226
		}
227
	}
228
 
229
	/**
230
	 * Set an image
231
	 * Only for awMark::IMAGE type.
232
	 *
233
	 * @param Image An image
234
	 */
235
	public function setImage(awImage $image) {
236
		$this->image = $image;
237
	}
238
 
239
	/**
240
	 * Draw the mark
241
	 *
242
	 * @param awDriver $driver
243
	 * @param awPoint $point Mark center
244
	 */
245
	public function draw(awDriver $driver, awPoint $point) {
246
 
247
		// Hide marks ?
248
		if($this->hide) {
249
			return;
250
		}
251
 
252
		// Check if we can print marks
253
		if($this->type !== NULL) {
254
 
255
			$this->driver = $driver;
256
			$realPoint = $this->move->move($point->x, $point->y);
257
 
258
			switch($this->type) {
259
 
260
				case awMark::CIRCLE :
261
					$this->drawCircle($realPoint);
262
					break;
263
 
264
				case awMark::SQUARE :
265
					$this->drawSquare($realPoint);
266
					break;
267
 
268
				case awMark::TRIANGLE :
269
					$this->drawTriangle($realPoint);
270
					break;
271
 
272
				case awMark::INVERTED_TRIANGLE :
273
					$this->drawTriangle($realPoint, TRUE);
274
					break;
275
 
276
				case awMark::RHOMBUS :
277
					$this->drawRhombus($realPoint);
278
					break;
279
 
280
				case awMark::CROSS :
281
					$this->drawCross($realPoint);
282
					break;
283
 
284
				case awMark::PLUS :
285
					$this->drawCross($realPoint, TRUE);
286
					break;
287
 
288
				case awMark::IMAGE :
289
					$this->drawImage($realPoint);
290
					break;
291
 
292
				case awMark::STAR :
293
					$this->changeType('star');
294
					$this->draw($driver, $point);
295
					break;
296
 
297
				case awMark::PAPERCLIP :
298
					$this->changeType('paperclip');
299
					$this->draw($driver, $point);
300
					break;
301
 
302
				case awMark::BOOK :
303
					$this->changeType('book');
304
					$this->draw($driver, $point);
305
					break;
306
 
307
			}
308
 
309
		}
310
 
311
	}
312
 
313
	protected function changeType($image) {
314
		$this->setType(awMARK::IMAGE);
315
		$this->setImage(new awFileImage(ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.$image.'.png'));
316
	}
317
 
318
	protected function drawCircle(awPoint $point) {
319
 
320
		$this->driver->filledEllipse(
321
			$this->fill,
322
			$point,
323
			$this->size, $this->size
324
		);
325
 
326
		$this->border->ellipse(
327
			$this->driver,
328
			$point,
329
			$this->size, $this->size
330
		);
331
 
332
	}
333
 
334
	protected function drawSquare(awPoint $point) {
335
 
336
		list($x, $y) = $point->getLocation();
337
 
338
		$x1 = (int)($x - $this->size / 2);
339
		$x2 = $x1 + $this->size;
340
		$y1 = (int)($y - $this->size / 2);
341
		$y2 = $y1 + $this->size;
342
 
343
		$this->border->rectangle($this->driver, new awPoint($x1, $y1), new awPoint($x2, $y2));
344
 
345
		$size = $this->border->visible() ? 1 : 0;
346
 
347
		$this->driver->filledRectangle(
348
			$this->fill,
349
			new awLine(
350
				new awPoint($x1 + $size, $y1 + $size),
351
				new awPoint($x2 - $size, $y2 - $size)
352
			)
353
		);
354
 
355
	}
356
 
357
	protected function drawTriangle(awPoint $point, $inverted = FALSE) {
358
 
359
		list($x, $y) = $point->getLocation();
360
 
361
		$size = $this->size;
362
 
363
		$triangle = new awPolygon;
364
		// Set default style and thickness
365
		$triangle->setStyle(awPolygon::SOLID);
366
		$triangle->setThickness(1);
367
 
368
		if($inverted === TRUE) {
369
			// Bottom of the triangle
370
			$triangle->append(new awPoint($x, $y + $size / sqrt(3)));
371
 
372
			// Upper left corner
373
			$triangle->append(new awPoint($x - $size / 2, $y - $size / (2 * sqrt(3))));
374
 
375
			// Upper right corner
376
			$triangle->append(new awPoint($x + $size / 2, $y - $size / (2 * sqrt(3))));
377
		} else {
378
			// Top of the triangle
379
			$triangle->append(new awPoint($x, $y - $size / sqrt(3)));
380
 
381
			// Lower left corner
382
			$triangle->append(new awPoint($x - $size / 2, $y + $size / (2 * sqrt(3))));
383
 
384
			// Lower right corner
385
			$triangle->append(new awPoint($x + $size / 2, $y + $size / (2 * sqrt(3))));
386
		}
387
 
388
		$this->driver->filledPolygon($this->fill, $triangle);
389
 
390
		if($this->border->visible()) {
391
			$this->border->polygon($this->driver, $triangle);
392
		}
393
	}
394
 
395
	protected function drawRhombus(awPoint $point) {
396
 
397
		list($x, $y) = $point->getLocation();
398
 
399
		$rhombus = new awPolygon;
400
		// Set default style and thickness
401
		$rhombus->setStyle(awPolygon::SOLID);
402
		$rhombus->setThickness(1);
403
 
404
		// Top of the rhombus
405
		$rhombus->append(new awPoint($x, $y - $this->size / 2));
406
 
407
		// Right of the rhombus
408
		$rhombus->append(new awPoint($x + $this->size / 2, $y));
409
 
410
		// Bottom of the rhombus
411
		$rhombus->append(new awPoint($x, $y + $this->size / 2));
412
 
413
		// Left of the rhombus
414
		$rhombus->append(new awPoint($x - $this->size / 2, $y));
415
 
416
		$this->driver->filledPolygon($this->fill, $rhombus);
417
 
418
		if($this->border->visible()) {
419
			$this->border->polygon($this->driver, $rhombus);
420
		}
421
	}
422
 
423
	protected function drawCross(awPoint $point, $upright = FALSE) {
424
 
425
		list($x, $y) = $point->getLocation();
426
 
427
		if($upright === TRUE) {
428
			$x11 = (int)($x);
429
			$y11 = (int)($y - $this->size / 2);
430
			$x12 = (int)($x);
431
			$y12 = (int)($y + $this->size / 2);
432
 
433
			$y21 = (int)($y);
434
			$y22 = (int)($y);
435
		} else {
436
			$x11 = (int)($x - $this->size / 2);
437
			$y11 = (int)($y + $this->size / 2);
438
			$x12 = (int)($x + $this->size / 2);
439
			$y12 = (int)($y - $this->size / 2);
440
 
441
			$y21 = (int)($y - $this->size / 2);
442
			$y22 = (int)($y + $this->size / 2);
443
		}
444
 
445
		$x21 = (int)($x - $this->size / 2);
446
		$x22 = (int)($x + $this->size / 2);
447
 
448
		$this->driver->line(
449
			$this->fill,
450
			new awLine(
451
				new awPoint($x11, $y11),
452
				new awPoint($x12, $y12)
453
			)
454
		);
455
 
456
		$this->driver->line(
457
			$this->fill,
458
			new awLine(
459
				new awPoint($x21, $y21),
460
				new awPoint($x22, $y22)
461
			)
462
		);
463
	}
464
 
465
	protected function drawImage(awPoint $point) {
466
 
467
		if($this->image instanceof awImage) {
468
 
469
			$width = $this->image->width;
470
			$height = $this->image->height;
471
 
472
			list($x, $y) = $point->getLocation();
473
 
474
			$x1 = (int)($x - $width / 2);
475
			$x2 = $x1 + $width;
476
			$y1 = (int)($y - $width / 2);
477
			$y2 = $y1 + $height;
478
 
479
			$this->border->rectangle($this->driver, new awPoint($x1 - 1, $y1 - 1), new awPoint($x2 + 1, $y2 + 1));
480
 
481
			$this->driver->copyImage($this->image, new awPoint($x1, $y1), new awPoint($x2, $y2));
482
 
483
		}
484
 
485
	}
486
 
487
}
488
 
489
registerClass('Mark');
490
?>