1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.runtime
.lcdui
.gfx
;
12 import cc
.squirreljme
.runtime
.cldc
.debug
.Debugging
;
13 import javax
.microedition
.lcdui
.Font
;
14 import javax
.microedition
.lcdui
.Graphics
;
15 import javax
.microedition
.lcdui
.Image
;
16 import javax
.microedition
.lcdui
.Text
;
19 * This is a proxy version of {@link Graphics} where each instance can
20 * refer to a single graphics instance. When a draw operation occurs, the
21 * parameters of this current graphics will be set on the target.
23 * @see ProxyGraphicsTarget
26 public final class ProxyGraphics
29 /** The target graphics to draw into. */
30 protected final ProxyGraphicsTarget target
;
32 /** The current alpha color. */
33 private int _argbColor
;
35 /** The current blending mode. */
36 private int _blendingMode
;
38 /** The clip height. */
39 private int _clipHeight
;
41 /** The clip width. */
42 private int _clipWidth
;
44 /** The clip X position. */
47 /** The clip Y position. */
50 /** The current font used. */
53 /** The current stroke style. */
54 private int _strokeStyle
;
56 /** The current X translation. */
59 /** The current Y translation. */
63 * Initializes the proxy graphics with the given target.
65 * @param __target The target graphics proxy.
66 * @param __width The graphics width.
67 * @param __height The graphics height.
68 * @throws NullPointerException On null arguments.
71 public ProxyGraphics(ProxyGraphicsTarget __target
, int __width
,
73 throws NullPointerException
76 throw new NullPointerException("NARG");
78 this.target
= __target
;
79 this._clipWidth
= __width
;
80 this._clipHeight
= __height
;
88 public void clipRect(int __a
, int __b
, int __c
, int __d
)
90 throw Debugging
.todo();
98 public void copyArea(int __a
, int __b
, int __c
, int __d
, int __e
,
101 this.__graphics().copyArea(__a
, __b
, __c
, __d
, __e
, __f
, __g
);
109 public void drawArc(int __a
, int __b
, int __c
, int __d
, int __e
,
112 this.__graphics().drawArc(__a
, __b
, __c
, __d
, __e
, __f
);
120 public void drawARGB16(short[] __data
, int __off
, int __scanlen
,
121 int __x
, int __y
, int __w
, int __h
)
123 this.__graphics().drawARGB16(__data
, __off
, __scanlen
, __x
, __y
, __w
,
132 public void drawChar(char __a
, int __b
, int __c
, int __d
)
134 this.__graphics().drawChar(__a
, __b
, __c
, __d
);
142 public void drawChars(char[] __a
, int __b
, int __c
, int __d
,
145 this.__graphics().drawChars(__a
, __b
, __c
, __d
, __e
, __f
);
153 public void drawImage(Image __a
, int __b
, int __c
, int __d
)
155 this.__graphics().drawImage(__a
, __b
, __c
, __d
);
163 public void drawLine(int __a
, int __b
, int __c
, int __d
)
165 this.__graphics().drawLine(__a
, __b
, __c
, __d
);
173 public void drawRGB(int[] __a
, int __b
, int __c
, int __d
, int __e
,
174 int __f
, int __g
, boolean __h
)
176 this.__graphics().drawRGB(__a
, __b
, __c
, __d
, __e
, __f
, __g
, __h
);
184 public void drawRGB16(short[] __data
, int __off
, int __scanlen
,
185 int __x
, int __y
, int __w
, int __h
)
187 this.__graphics().drawRGB16(__data
, __off
, __scanlen
, __x
, __y
, __w
, __h
);
195 public void drawRect(int __a
, int __b
, int __c
, int __d
)
197 this.__graphics().drawRect(__a
, __b
, __c
, __d
);
205 public void drawRegion(Image __a
, int __b
, int __c
, int __d
,
206 int __e
, int __f
, int __g
, int __h
, int __i
)
208 this.__graphics().drawRegion(__a
, __b
, __c
, __d
, __e
, __f
, __g
, __h
,
217 public void drawRegion(Image __src
, int __xsrc
, int __ysrc
,
218 int __w
, int __h
, int __trans
, int __xdest
, int __ydest
, int __anch
,
219 int __wdest
, int __hdest
)
221 this.__graphics().drawRegion(__src
, __xsrc
, __ysrc
, __w
, __h
, __trans
,
222 __xdest
, __ydest
, __anch
, __wdest
, __hdest
);
230 public void drawRoundRect(int __a
, int __b
, int __c
, int __d
,
231 int __arcWidth
, int __arcHeight
)
233 this.__graphics().drawRoundRect(__a
, __b
, __c
, __d
, __arcWidth
,
242 public void drawString(String __a
, int __b
, int __c
, int __d
)
244 this.__graphics().drawString(__a
, __b
, __c
, __d
);
252 public void drawSubstring(String __a
, int __b
, int __c
, int __d
,
255 this.__graphics().drawSubstring(__a
, __b
, __c
, __d
, __e
, __f
);
263 public void drawText(Text __t
, int __x
, int __y
)
265 this.__graphics().drawText(__t
, __x
, __y
);
273 public void fillArc(int __a
, int __b
, int __c
, int __d
, int __e
,
276 this.__graphics().fillArc(__a
, __b
, __c
, __d
, __e
, __f
);
284 public void fillRect(int __x
, int __y
, int __w
, int __h
)
286 this.__graphics().fillRect(__x
, __y
, __w
, __h
);
294 public void fillRoundRect(int __a
, int __b
, int __c
, int __d
,
295 int __arcWidth
, int __arcHeight
)
297 this.__graphics().fillRoundRect(__a
, __b
, __c
, __d
, __arcWidth
,
306 public void fillTriangle(int __a
, int __b
, int __c
, int __d
,
309 this.__graphics().fillTriangle(__a
, __b
, __c
, __d
, __e
, __f
);
317 public int getAlpha()
319 return (this._argbColor
>> 24) & 0xFF;
327 public int getAlphaColor()
329 return this._argbColor
;
337 public int getBlendingMode()
339 return this._blendingMode
;
347 public int getBlueComponent()
349 return (this._argbColor
) & 0xFF;
357 public int getClipHeight()
359 return this._clipHeight
;
367 public int getClipWidth()
369 return this._clipWidth
;
377 public int getClipX()
379 return this._clipX
- this._transX
;
387 public int getClipY()
389 return this._clipY
- this._transY
;
397 public int getColor()
399 return this._argbColor
& 0xFFFFFF;
407 public int getDisplayColor(int __a
)
409 return this.__graphics().getDisplayColor(__a
);
417 public Font
getFont()
419 return this.__font();
427 public int getGrayScale()
429 return (((this._argbColor
>> 16) & 0xFF) +
430 ((this._argbColor
>> 8) & 0xFF) +
431 ((this._argbColor
) & 0xFF)) / 3;
439 public int getGreenComponent()
441 return (this._argbColor
>> 8) & 0xFF;
449 public int getRedComponent()
451 return (this._argbColor
>> 16) & 0xFF;
459 public int getStrokeStyle()
461 return this._strokeStyle
;
469 public int getTranslateX()
479 public int getTranslateY()
489 public void setAlpha(int __a
)
491 this.setAlphaColor(__a
,
492 this.getRedComponent(),
493 this.getGreenComponent(),
494 this.getBlueComponent());
502 public void setAlphaColor(int __argb
)
504 this._argbColor
= __argb
;
512 public void setAlphaColor(int __a
, int __r
, int __g
, int __b
)
514 /* {@squirreljme.error EB2y Color out of range. (Alpha; Red; Green;
516 if (__a
< 0 || __a
> 255 || __r
< 0 || __r
> 255 ||
517 __g
< 0 || __g
> 255 || __b
< 0 || __b
> 255)
518 throw new IllegalArgumentException(String
.format(
519 "EB2y %d %d %d %d", __a
, __r
, __g
, __b
));
522 this.setAlphaColor((__a
<< 24) | (__r
<< 16) | (__g
<< 8) | __b
);
530 public void setBlendingMode(int __m
)
532 /* {@squirreljme.error EB2x Invalid blending mode. (The mode)} */
533 if (__m
!= Graphics
.SRC
&& __m
!= Graphics
.SRC_OVER
)
534 throw new IllegalArgumentException("EB2x " + __m
);
537 this._blendingMode
= __m
;
545 public void setClip(int __x
, int __y
, int __w
, int __h
)
547 // Calculate the base clip coordinates
548 int startX
= __x
+ this._transX
;
549 int startY
= __y
+ this._transY
;
550 int endX
= startX
+ __w
;
551 int endY
= startY
+ __h
;
569 // Determine the bounds of all of these
570 int clipX
= Math
.max(0, startX
);
571 int clipY
= Math
.max(0, startY
);
572 int clipEndX
= Math
.max(0, endX
);
573 int clipEndY
= Math
.max(0, endY
);
578 this._clipWidth
= clipEndX
- clipX
;
579 this._clipHeight
= clipEndY
- clipY
;
587 public void setColor(int __rgb
)
589 this.setAlphaColor((this.getAlphaColor() & 0xFF_
000000) |
590 (__rgb
& 0x00_FFFFFF
));
598 public void setColor(int __r
, int __g
, int __b
)
600 this.setAlphaColor(this.getAlpha(), __r
, __g
, __b
);
608 public void setFont(Font __font
)
610 this.__graphics().setFont(__font
);
618 public void setGrayScale(int __v
)
620 this.setAlphaColor(this.getAlpha(), __v
, __v
, __v
);
629 public void setStrokeStyle(int __style
)
631 /* {@squirreljme.error EB2z Illegal stroke style.} */
632 if (__style
!= Graphics
.SOLID
&& __style
!= Graphics
.DOTTED
)
633 throw new IllegalArgumentException("EB2z");
636 this._strokeStyle
= __style
;
644 public void translate(int __x
, int __y
)
652 * Returns the font that should be used.
654 * @return The font used.
657 private Font
__font()
659 Font rv
= this._font
;
661 rv
= Font
.getDefaultFont();
666 * Initializes and returns the target graphics accordingly.
668 * @return The resultant graphics object.
671 private Graphics
__graphics()
673 // This is the graphics we are drawing into
674 Graphics target
= this.target
._target
;
676 // Pass all the adjustable parameters to the target
677 target
.setAlphaColor(this._argbColor
);
678 target
.setBlendingMode(this._blendingMode
);
679 target
.setFont(this.__font());
680 target
.setStrokeStyle(this._strokeStyle
);
682 // Translation that is needed for clipping and translation
683 int targetTransX
= target
.getTranslateX();
684 int targetTransY
= target
.getTranslateY();
686 // Set absolute clipping area
688 this._clipX
- targetTransX
, this._clipY
- targetTransY
,
689 this._clipWidth
, this._clipHeight
);
690 target
.translate(this._transX
- targetTransX
,
691 this._transY
- targetTransY
);
693 // Return the graphics we are drawing into