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 GNU General Public License v3+, or later.
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
,
233 this.__graphics().drawRoundRect(__a
, __b
, __c
, __d
, __e
, __f
);
241 public void drawString(String __a
, int __b
, int __c
, int __d
)
243 this.__graphics().drawString(__a
, __b
, __c
, __d
);
251 public void drawSubstring(String __a
, int __b
, int __c
, int __d
,
254 this.__graphics().drawSubstring(__a
, __b
, __c
, __d
, __e
, __f
);
262 public void drawText(Text __t
, int __x
, int __y
)
264 this.__graphics().drawText(__t
, __x
, __y
);
272 public void fillArc(int __a
, int __b
, int __c
, int __d
, int __e
,
275 this.__graphics().fillArc(__a
, __b
, __c
, __d
, __e
, __f
);
283 public void fillRect(int __x
, int __y
, int __w
, int __h
)
285 this.__graphics().fillRect(__x
, __y
, __w
, __h
);
293 public void fillRoundRect(int __a
, int __b
, int __c
, int __d
,
296 this.__graphics().fillRoundRect(__a
, __b
, __c
, __d
, __e
, __f
);
304 public void fillTriangle(int __a
, int __b
, int __c
, int __d
,
307 this.__graphics().fillTriangle(__a
, __b
, __c
, __d
, __e
, __f
);
315 public int getAlpha()
317 return (this._argbColor
>> 24) & 0xFF;
325 public int getAlphaColor()
327 return this._argbColor
;
335 public int getBlendingMode()
337 return this._blendingMode
;
345 public int getBlueComponent()
347 return (this._argbColor
) & 0xFF;
355 public int getClipHeight()
357 return this._clipHeight
;
365 public int getClipWidth()
367 return this._clipWidth
;
375 public int getClipX()
377 return this._clipX
- this._transX
;
385 public int getClipY()
387 return this._clipY
- this._transY
;
395 public int getColor()
397 return this._argbColor
& 0xFFFFFF;
405 public int getDisplayColor(int __a
)
407 return this.__graphics().getDisplayColor(__a
);
415 public Font
getFont()
417 return this.__font();
425 public int getGrayScale()
427 return (((this._argbColor
>> 16) & 0xFF) +
428 ((this._argbColor
>> 8) & 0xFF) +
429 ((this._argbColor
) & 0xFF)) / 3;
437 public int getGreenComponent()
439 return (this._argbColor
>> 8) & 0xFF;
447 public int getRedComponent()
449 return (this._argbColor
>> 16) & 0xFF;
457 public int getStrokeStyle()
459 return this._strokeStyle
;
467 public int getTranslateX()
477 public int getTranslateY()
487 public void setAlpha(int __a
)
489 this.setAlphaColor(__a
,
490 this.getRedComponent(),
491 this.getGreenComponent(),
492 this.getBlueComponent());
500 public void setAlphaColor(int __argb
)
502 this._argbColor
= __argb
;
510 public void setAlphaColor(int __a
, int __r
, int __g
, int __b
)
512 // {@squirreljme.error EB2y Color out of range. (Alpha; Red; Green;
514 if (__a
< 0 || __a
> 255 || __r
< 0 || __r
> 255 ||
515 __g
< 0 || __g
> 255 || __b
< 0 || __b
> 255)
516 throw new IllegalArgumentException(String
.format(
517 "EB2y %d %d %d %d", __a
, __r
, __g
, __b
));
520 this.setAlphaColor((__a
<< 24) | (__r
<< 16) | (__g
<< 8) | __b
);
528 public void setBlendingMode(int __m
)
530 // {@squirreljme.error EB2x Invalid blending mode. (The mode)}
531 if (__m
!= Graphics
.SRC
&& __m
!= Graphics
.SRC_OVER
)
532 throw new IllegalArgumentException("EB2x " + __m
);
535 this._blendingMode
= __m
;
543 public void setClip(int __x
, int __y
, int __w
, int __h
)
545 // Calculate the base clip coordinates
546 int startX
= __x
+ this._transX
;
547 int startY
= __y
+ this._transY
;
548 int endX
= startX
+ __w
;
549 int endY
= startY
+ __h
;
567 // Determine the bounds of all of these
568 int clipX
= Math
.max(0, startX
);
569 int clipY
= Math
.max(0, startY
);
570 int clipEndX
= Math
.max(0, endX
);
571 int clipEndY
= Math
.max(0, endY
);
576 this._clipWidth
= clipEndX
- clipX
;
577 this._clipHeight
= clipEndY
- clipY
;
585 public void setColor(int __rgb
)
587 this.setAlphaColor((this.getAlphaColor() & 0xFF_
000000) |
588 (__rgb
& 0x00_FFFFFF
));
596 public void setColor(int __r
, int __g
, int __b
)
598 this.setAlphaColor(this.getAlpha(), __r
, __g
, __b
);
606 public void setFont(Font __font
)
608 this.__graphics().setFont(__font
);
616 public void setGrayScale(int __v
)
618 this.setAlphaColor(this.getAlpha(), __v
, __v
, __v
);
627 public void setStrokeStyle(int __style
)
629 // {@squirreljme.error EB2z Illegal stroke style.}
630 if (__style
!= Graphics
.SOLID
&& __style
!= Graphics
.DOTTED
)
631 throw new IllegalArgumentException("EB2z");
634 this._strokeStyle
= __style
;
642 public void translate(int __x
, int __y
)
650 * Returns the font that should be used.
652 * @return The font used.
655 private Font
__font()
657 Font rv
= this._font
;
659 rv
= Font
.getDefaultFont();
664 * Initializes and returns the target graphics accordingly.
666 * @return The resultant graphics object.
669 private Graphics
__graphics()
671 // This is the graphics we are drawing into
672 Graphics target
= this.target
._target
;
674 // Pass all the adjustable parameters to the target
675 target
.setAlphaColor(this._argbColor
);
676 target
.setBlendingMode(this._blendingMode
);
677 target
.setFont(this.__font());
678 target
.setStrokeStyle(this._strokeStyle
);
680 // Translation that is needed for clipping and translation
681 int targetTransX
= target
.getTranslateX();
682 int targetTransY
= target
.getTranslateY();
684 // Set absolute clipping area
686 this._clipX
- targetTransX
, this._clipY
- targetTransY
,
687 this._clipWidth
, this._clipHeight
);
688 target
.translate(this._transX
- targetTransX
,
689 this._transY
- targetTransY
);
691 // Return the graphics we are drawing into