PageRenderTime 37ms CodeModel.GetById 10ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/graphics/graphics.d

http://github.com/wilkie/djehuty
D | 561 lines | 235 code | 92 blank | 234 comment | 14 complexity | 6eaf3dfefab9959b595a4b39370f7d28 MD5 | raw file
  1/*
  2 * graphics.d
  3 *
  4 * This file contains the logic behind functions that draw to a View.
  5 *
  6 * Author: Dave Wilkinson
  7 *
  8 */
  9
 10module graphics.graphics;
 11
 12import graphics.view;
 13
 14import resource.image;
 15
 16import core.definitions;
 17import core.string;
 18import core.color;
 19
 20public import graphics.brush;
 21public import graphics.pen;
 22public import graphics.font;
 23public import graphics.region;
 24public import graphics.gradient;
 25
 26import platform.vars.view;
 27import platform.vars.brush;
 28import platform.vars.font;
 29import platform.vars.pen;
 30import platform.vars.region;
 31
 32import Scaffold = scaffold.graphics;
 33
 34// Section: Core
 35
 36// Description: This class encapsulates drawing functions for a view class.  It will draw primitives to a view canvas.
 37class Graphics {
 38
 39// Primitives
 40
 41	// Description: Draws a line from (x,y) to (x2,y2).
 42	// x: The first coordinate's x value.
 43	// y: The first coordinate's y value.
 44	// x2: The second coordinate's x value.
 45	// y2: The second coordinate's y value.
 46	void drawLine(int x, int y, int x2, int y2) {
 47		Scaffold.drawLine(_viewVars, x,y,x2,y2);
 48	}
 49
 50	// Description: Draws a rectangle defined by the top-left point: (x,y) and some size.
 51	// x: The top-left coordinate's x value.
 52	// y: The top-left coordinate's y value.
 53	// width: The width of the rectangle to draw.
 54	// height: The height of the rectangle to draw.
 55	void drawRect(int x, int y, int width, int height) {
 56		Scaffold.drawRect(_viewVars, x,y,width,height);
 57	}
 58
 59	// Description: Fills a rectangle defined by the top-left point: (x,y) and some size.
 60	// x: The top-left coordinate's x value.
 61	// y: The top-left coordinate's y value.
 62	// width: The width of the rectangle to draw.
 63	// height: The height of the rectangle to draw.
 64	void fillRect(int x, int y, int width, int height) {
 65		Scaffold.fillRect(_viewVars, x, y, width, height);
 66	}
 67
 68	// Description: Outlines a rectangle defined by the top-left point: (x,y) and some size.
 69	// x: The top-left coordinate's x value.
 70	// y: The top-left coordinate's y value.
 71	// width: The width of the rectangle to draw.
 72	// height: The height of the rectangle to draw.
 73	void strokeRect(int x, int y, int width, int height) {
 74		Scaffold.strokeRect(_viewVars, x, y, width, height);
 75	}
 76
 77	// Description: Draws an ellipse inscribed by the rectangle defined by the top-left point: (x,y) and some size.
 78	// x: The top-left coordinate's x value.
 79	// y: The top-left coordinate's y value.
 80	// width: The width of the rectangle to use to inscribe.
 81	// height: The height of the rectangle to use to inscribe.
 82	void drawOval(int x, int y, int width, int height) {
 83		Scaffold.drawOval(_viewVars, x,y,width,height);
 84	}
 85
 86	// Description: Fills an ellipse inscribed by the rectangle defined by the top-left point: (x,y) and some size.
 87	// x: The top-left coordinate's x value.
 88	// y: The top-left coordinate's y value.
 89	// width: The width of the rectangle to use to inscribe.
 90	// height: The height of the rectangle to use to inscribe.
 91	void fillOval(int x, int y, int width, int height) {
 92		Scaffold.fillOval(_viewVars, x,y,width,height);
 93	}
 94
 95	// Description: Outlines an ellipse inscribed by the rectangle defined by the top-left point: (x,y) and some size.
 96	// x: The top-left coordinate's x value.
 97	// y: The top-left coordinate's y value.
 98	// width: The width of the rectangle to use to inscribe.
 99	// height: The height of the rectangle to use to inscribe.
100	void strokeOval(int x, int y, int width, int height) {
101		Scaffold.strokeOval(_viewVars, x,y,width,height);
102	}
103
104	// Description: Draws the region given.
105	// x: The amount to translate the region in the x direction.
106	// y: The amount to translate the region in the y direction.
107	// rgn: The region to draw.
108	void drawRegion(int x, int y, Region rgn) {
109		rgn.platformDirty = false;
110		Scaffold.drawRegion(_viewVars, &rgn._pfvars, rgn.platformDirty, rgn, x, y);
111	}
112
113	// Description: Fills the region given.
114	// x: The amount to translate the region in the x direction.
115	// y: The amount to translate the region in the y direction.
116	// rgn: The region to fill.
117	void fillRegion(int x, int y, Region rgn) {
118		rgn.platformDirty = false;
119		Scaffold.fillRegion(_viewVars, &rgn._pfvars, rgn.platformDirty, rgn, x, y);
120	}
121
122	// Description: Outlines the region given.
123	// x: The amount to translate the region in the x direction.
124	// y: The amount to translate the region in the y direction.
125	// rgn: The region to stroke.
126	void strokeRegion(int x, int y, Region rgn) {
127		rgn.platformDirty = false;
128		Scaffold.strokeRegion(_viewVars, &rgn._pfvars, rgn.platformDirty, rgn, x, y);
129	}
130
131	// Description: Draws a pie wedge within the rectangular region given with the top-left point of the pie at the point (x,y). The wedge will be from the startAngle and have an arc length of the sweepAngle.
132	// x: The top-left coordinate's x value.
133	// y: The top-left coordinate's y value.
134	// width: The width of the rectangle to use to inscribe.
135	// height: The height of the rectangle to use to inscribe.
136	// startAngle: The angle to start the pie wedge.
137	// sweepAngle: The length of the arc to draw.
138	void drawPie(int x, int y, int width, int height, double startAngle, double sweepAngle) {
139		Scaffold.drawPie(_viewVars, x, y, width, height, startAngle, sweepAngle);
140	}
141
142	// Description: Outlines a pie wedge within the rectangular region given with the top-left point of the pie at the point (x,y). The wedge will be from the startAngle and have an arc length of the sweepAngle.
143	// x: The top-left coordinate's x value.
144	// y: The top-left coordinate's y value.
145	// width: The width of the rectangle to use to inscribe.
146	// height: The height of the rectangle to use to inscribe.
147	// startAngle: The angle to start the pie wedge.
148	// sweepAngle: The length of the arc to draw.
149	void strokePie(int x, int y, int width, int height, double startAngle, double sweepAngle) {
150		Scaffold.strokePie(_viewVars, x, y, width, height, startAngle, sweepAngle);
151	}
152
153	// Description: Fills a pie wedge within the rectangular region given with the top-left point of the pie at the point (x,y). The wedge will be from the startAngle and have an arc length of the sweepAngle.
154	// x: The top-left coordinate's x value.
155	// y: The top-left coordinate's y value.
156	// width: The width of the rectangle to use to inscribe.
157	// height: The height of the rectangle to use to inscribe.
158	// startAngle: The angle to start the pie wedge.
159	// sweepAngle: The length of the arc to draw.
160	void fillPie(int x, int y, int width, int height, double startAngle, double sweepAngle) {
161		Scaffold.fillPie(_viewVars, x, y, width, height, startAngle, sweepAngle);
162	}
163
164// Text
165
166	// Description: Draws the string starting with the top-left point of the text at the point (x,y).
167	// x: The x coordinate to start rendering the text.
168	// y: The y coordinate to start rendering the text.
169	// str: The string to render.
170	void drawText(int x, int y, string str) {
171		Scaffold.drawText(_viewVars, x,y,str);
172	}
173
174	// Description: Draws the string starting with the top-left point of the text at the point (x,y) and only up to the number of characters specified by length.
175	// x: The x coordinate to start rendering the text.
176	// y: The y coordinate to start rendering the text.
177	// str: The string to render.
178	// length: the length of the string.
179	void drawText(int x, int y, string str, uint length) {
180		Scaffold.drawText(_viewVars, x,y,str,length);
181	}
182
183// Clipped Text
184
185	// Description: Draws the string starting with the top-left point of the text at the point (x,y) and clips it within a rectangular region.
186	// x: The x coordinate to start rendering the text.
187	// y: The y coordinate to start rendering the text.
188	// region: The Rect object that defines the rectangular clipping region.
189	// str: The string to render.
190	void drawClippedText(int x, int y, Rect region, string str) {
191		Scaffold.drawClippedText(_viewVars, x,y,region,str);
192	}
193
194	// Description: Draws the string starting with the top-left point of the text at the point (x,y) up to the specified number of characters and clips it within a rectangular region.
195	// x: The x coordinate to start rendering the text.
196	// y: The y coordinate to start rendering the text.
197	// region: The Rect object that defines the rectangular clipping region.
198	// str: The string to render.
199	// length: the length of the string.
200	void drawClippedText(int x, int y, Rect region, string str, uint length) {
201		Scaffold.drawClippedText(_viewVars, x,y,region,str,length);
202	}
203
204// Text Measurement
205
206	// Description: Will update the Size variable with the width and height of the text as it would be rendered.
207	// str: The string to measure.
208	// sz: The Size struct to update.
209	void measureText(string str, out Size sz) {
210		Scaffold.measureText(_viewVars, str, sz);
211	}
212
213	// Description: Will update the Size variable with the width and height of the text as it would be rendered.
214	// str: The string to measure.
215	// length: The length of the string to consider.
216	// sz: The Size struct to update.
217	void measureText(string str, uint length, out Size sz) {
218		Scaffold.measureText(_viewVars, str, length, sz);
219	}
220
221// Text Colors
222
223	// Description: Will set the text background color, sometimes refered to as the highlight color.
224	// textColor: The Color structure to be used as the background color.
225	void backcolor(in Color textColor) {
226		//Scaffold.setTextBackgroundColor(_viewVars, textColor);
227	}
228
229	// Description: Will set the text foreground color.
230	// textColor: The Color structure to be used as the background color.
231	void forecolor(in Color textColor) {
232		Scaffold.setTextColor(_viewVars, textColor);
233	}
234
235// Text States
236
237	// Description: Will set the text mode to not draw the background behind the rendered text.
238	void setTextModeTransparent() {
239		Scaffold.setTextModeTransparent(_viewVars);
240	}
241
242	// Description: Will set the text mode so that it draws the background behind the rendered text using the color given by setTextBackgroundColor.
243	void setTextModeOpaque() {
244		Scaffold.setTextModeOpaque(_viewVars);
245	}
246
247// Graphics States
248
249	bool antialias() {
250		return _antialias;
251	}
252
253	void antialias(bool value) {
254		Scaffold.setAntialias(_viewVars, value);
255		_antialias = value;
256	}
257
258// Fonts
259
260	void font(ref Font font) {
261		Scaffold.setFont(_viewVars, &font._pfvars);
262	}
263
264// Brushes
265
266	// Description: Will set the fill type to that specified by the Brush object.
267	// brush: The Brush to use.
268	void brush(Brush brush) {
269		Scaffold.setBrush(_viewVars, &brush._pfvars);
270
271		if (_view._brush !is null)
272		{
273			// unattach current brush
274			_view._brush._view = null;
275		}
276		_view._brush = brush;
277
278		// set the Brush's view
279		brush._view = _view;
280	}
281
282// Pens
283
284	// Description: Will set the stroke type to that specified by the Pen object.
285	// pen: The Pen to use.
286	void pen(Pen pen) {
287		Scaffold.setPen(_viewVars, &pen._pfvars);
288
289		// set the pen in the view
290		if (_view._pen !is null) {
291			// unattach current pen
292			_view._pen._view = null;
293		}
294		_view._pen = pen;
295
296		// set the Pen's view
297		pen._view = _view;
298	}
299
300// View Interfacing
301
302	// Description: Will draw the image stored by the view object onto the current view object at the coordinate (x,y).
303	// x: The x coordinate to place the top-left corner of the image.
304	// y: The y coordinate to place the top-left corner of the image.
305	// view: The view in which to draw.
306	void drawView(int x, int y, View view) {
307		if (view is _view) {
308			// Holy recursion, Batman!
309			// XXX: Exception
310			return;
311		}
312
313		view.lock();
314
315		ViewPlatformVars* viewVarsSrc = &view._pfvars;
316
317		Scaffold.drawView(_viewVars, _view, x, y, viewVarsSrc, view);
318
319		view.unlock();
320	}
321
322	// Description: Will draw the cropped image stored by the view object onto the current view object at the coordinate (x,y).
323	// x: The x coordinate to place the top-left corner of the image.
324	// y: The y coordinate to place the top-left corner of the image.
325	// view: The view in which to draw.
326	// viewX: The x coordinate to crop the image.  Everything from this X to X + viewWidth of the view will be drawn.
327	// viewY: The y coordinate to crop the image.  Everything from this Y to Y + viewHeight of the view will be drawn.
328	void drawView(int x, int y, View view, int viewX, int viewY) {
329		if (view is _view) {
330			// Holy recursion, Batman!
331			// XXX: Exception
332			return;
333		}
334		view.lock();
335
336		ViewPlatformVars* viewVarsSrc = &view._pfvars;
337
338		Scaffold.drawView(_viewVars, _view, x, y, viewVarsSrc, view, viewX, viewY);
339
340		view.unlock();
341	}
342
343	// Description: Will draw the cropped image stored by the view object onto the current view object at the coordinate (x,y).
344	// x: The x coordinate to place the top-left corner of the image.
345	// y: The y coordinate to place the top-left corner of the image.
346	// view: The view in which to draw.
347	// viewX: The x coordinate to crop the image.  Everything from this X to X + viewWidth of the view will be drawn.
348	// viewY: The y coordinate to crop the image.  Everything from this Y to Y + viewHeight of the view will be drawn.
349	// viewWidth: The width of the region to crop from the source image.
350	// viewHeight: The height of the region to crop from the source image.
351	void drawView(int x, int y, View view, int viewX, int viewY, int viewWidth, int viewHeight) {
352		if (view is _view) {
353			// Holy recursion, Batman!
354			// XXX: Exception
355			return;
356		}
357		view.lock();
358
359		ViewPlatformVars* viewVarsSrc = &view._pfvars;
360
361		Scaffold.drawView(_viewVars, _view, x, y, viewVarsSrc, view, viewX, viewY, viewWidth, viewHeight);
362
363		view.unlock();
364	}
365
366	// Description: Will draw the image stored by the view object onto the current view object at the coordinate (x,y) at an opacity of the fraction given.
367	// x: The x coordinate to place the top-left corner of the image.
368	// y: The y coordinate to place the top-left corner of the image.
369	// view: The view in which to draw.
370	// opacity: The opacity.  1.0 is full opacity.  0.0 will result in no image, as this is full transparency.
371	void drawView(int x, int y, View view, double opacity) {
372		if (view is _view) {
373			// Holy recursion, Batman!
374			// XXX: Exception
375			return;
376		}
377		view.lock();
378
379		ViewPlatformVars* viewVarsSrc = &view._pfvars;
380
381		Scaffold.drawView(_viewVars, _view, x, y, viewVarsSrc, view, opacity);
382
383		view.unlock();
384	}
385
386	// Description: Will draw the cropped image stored by the view object onto the current view object at the coordinate (x,y) at an opacity of the fraction given.
387	// x: The x coordinate to place the top-left corner of the image.
388	// y: The y coordinate to place the top-left corner of the image.
389	// view: The view in which to draw.
390	// viewX: The x coordinate to crop the image.  Everything from this X to X + viewWidth of the view will be drawn.
391	// viewY: The y coordinate to crop the image.  Everything from this Y to Y + viewHeight of the view will be drawn.
392	// opacity: The opacity.  1.0 is full opacity.  0.0 will result in no image, as this is full transparency.
393	void drawView(int x, int y, View view, int viewX, int viewY, double opacity) {
394		if (view is _view) {
395			// Holy recursion, Batman!
396			// XXX: Exception
397			return;
398		}
399		view.lock();
400
401		ViewPlatformVars* viewVarsSrc = &view._pfvars;
402
403		Scaffold.drawView(_viewVars, _view, x, y, viewVarsSrc, view, viewX, viewY, opacity);
404
405		view.unlock();
406	}
407
408	// Description: Will draw the cropped image stored by the view object onto the current view object at the coordinate (x,y) at an opacity of the fraction given.
409	// x: The x coordinate to place the top-left corner of the image.
410	// y: The y coordinate to place the top-left corner of the image.
411	// view: The view in which to draw.
412	// viewX: The x coordinate to crop the image.  Everything from this X to X + viewWidth of the view will be drawn.
413	// viewY: The y coordinate to crop the image.  Everything from this Y to Y + viewHeight of the view will be drawn.
414	// viewWidth: The width of the region to crop from the source image.
415	// viewHeight: The height of the region to crop from the source image.
416	// opacity: The opacity.  1.0 is full opacity.  0.0 will result in no image, as this is full transparency.
417	void drawView(int x, int y, View view, int viewX, int viewY, int viewWidth, int viewHeight, double opacity) {
418		if (view is _view) {
419			// Holy recursion, Batman!
420			// XXX: Exception
421			return;
422		}
423		view.lock();
424
425		ViewPlatformVars* viewVarsSrc = &view._pfvars;
426
427		Scaffold.drawView(_viewVars, _view, x, y, viewVarsSrc, view, viewX, viewY, viewWidth, viewHeight, opacity);
428
429		view.unlock();
430	}
431
432// Image Interfacing
433
434	// Description: Will draw the image to the current view object at the coordinate (x,y).
435	// x: The x coordinate to place the top-left corner of the image.
436	// y: The y coordinate to place the top-left corner of the image.
437	// image: The image to draw.
438	void drawImage(int x, int y, Image image) {
439		ImageLock(image);
440		View v = image.view;
441		if (v !is null) {
442			drawView(x, y, v);
443		}
444		ImageUnlock(image);
445	}
446
447	// Description: Will draw the image to the current view object at the coordinate (x,y) while cropping the image at the coordinate (srcX, srcY).
448	// x: The x coordinate to place the top-left corner of the image.
449	// y: The y coordinate to place the top-left corner of the image.
450	// image: The image to draw.
451	// srcX: The x coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
452	// srcY: The y coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
453	void drawImage(int x, int y, Image image, int srcX, int srcY) {
454		ImageLock(image);
455		View v = image.view;
456		if (v !is null) {
457			drawView(x, y, v, srcX, srcY);
458		}
459		ImageUnlock(image);
460	}
461
462	// Description: Will draw the image to the current view object at the coordinate (x,y) while cropping the image at the coordinate (srcX, srcY) with the region sized by srcW and srcH.
463	// x: The x coordinate to place the top-left corner of the image.
464	// y: The y coordinate to place the top-left corner of the image.
465	// image: The image to draw.
466	// srcX: The x coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
467	// srcY: The y coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
468	// srcW: The width of the cropped region.
469	// srcH: The height of the cropped region.
470	void drawImage(int x, int y, Image image, int srcX, int srcY, int srcW, int srcH) {
471		ImageLock(image);
472		View v = image.view;
473		if (v !is null) {
474			drawView(x, y, v, srcX, srcY, srcW, srcH);
475		}
476		ImageUnlock(image);
477	}
478
479	// Description: Will draw the image to the current view object at the coordinate (x,y) at an opacity of the fraction given.
480	// x: The x coordinate to place the top-left corner of the image.
481	// y: The y coordinate to place the top-left corner of the image.
482	// image: The image to draw.
483	// opacity: The opacity.  1.0 is full opacity.  0.0 will result in no image, as this is full transparency.
484	void drawImage(int x, int y, Image image, double opacity) {
485		ImageLock(image);
486		View v = image.view;
487		if (v !is null) {
488			drawView(x, y, v, opacity);
489		}
490		ImageUnlock(image);
491	}
492
493	// Description: Will draw the image to the current view object at the coordinate (x,y) while cropping the image at the coordinate (srcX, srcY) at an opacity of the fraction given.
494	// x: The x coordinate to place the top-left corner of the image.
495	// y: The y coordinate to place the top-left corner of the image.
496	// image: The image to draw.
497	// srcX: The x coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
498	// srcY: The y coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
499	// opacity: The opacity.  1.0 is full opacity.  0.0 will result in no image, as this is full transparency.
500	void drawImage(int x, int y, Image image, int srcX, int srcY, double opacity) {
501		ImageLock(image);
502		View v = image.view;
503		if (v !is null) {
504			drawView(x, y, v, srcX, srcY, opacity);
505		}
506		ImageUnlock(image);
507	}
508
509	// Description: Will draw the image to the current view object at the coordinate (x,y) while cropping the image at the coordinate (srcX, srcY) with the region sized by srcW and srcH at an opacity of the fraction given.
510	// x: The x coordinate to place the top-left corner of the image.
511	// y: The y coordinate to place the top-left corner of the image.
512	// image: The image to draw.
513	// srcX: The x coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
514	// srcY: The y coordinate to crop the image.  The top-left corner of the render will correspond to the top-left corner of this cropped region.
515	// srcW: The width of the cropped region.
516	// srcH: The height of the cropped region.
517	// opacity: The opacity.  1.0 is full opacity.  0.0 will result in no image, as this is full transparency.
518	void drawImage(int x, int y, Image image, int srcX, int srcY, int srcW, int srcH, double opacity) {
519		ImageLock(image);
520		View v = image.view;
521		if (v !is null) {
522			drawView(x, y, v, srcX, srcY, srcW, srcH, opacity);
523		}
524		ImageUnlock(image);
525	}
526
527// Clipping
528
529	// Description: Will push the clip region to a stack preserving its state.
530	void clipSave() {
531		Scaffold.clipSave(_viewVars);
532	}
533
534	// Description: If possible, will restore the clip state most recently saved via clipSave().
535	void clipRestore() {
536		Scaffold.clipRestore(_viewVars);
537	}
538
539	// Description: Unions the current clip region with a rectangle region defined by the top-left point: (x,y) and some size.
540	// x: The top-left coordinate's x value.
541	// y: The top-left coordinate's y value.
542	// width: The width of the rectangular region to clip.
543	// height: The height of the rectangular region to clip.
544	void clipRect(int x, int y, int width, int height) {
545		Scaffold.clipRect(_viewVars, x,y,width,height);
546	}
547
548	// Description: Unions the current clip region with the region given.
549	// rgn: The region to clip.
550	void clipRegion(Region rgn) {
551		Scaffold.clipRegion(_viewVars, rgn);
552	}
553
554protected:
555
556	bool _antialias = false;
557
558	// a backward reference to the view object
559	package View _view;
560	package ViewPlatformVars* _viewVars;
561}