Indentations break the feed.
[SquirrelJME.git] / modules / midp-lcdui / src / main / java / cc / squirreljme / runtime / lcdui / gfx / ProxyGraphics.java
blob0e99cf5f51f68035b9032f8c2135e0df12451737
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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;
18 /**
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
24 * @since 2022/02/25
26 public final class ProxyGraphics
27 extends Graphics
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. */
45 private int _clipX;
47 /** The clip Y position. */
48 private int _clipY;
50 /** The current font used. */
51 private Font _font;
53 /** The current stroke style. */
54 private int _strokeStyle;
56 /** The current X translation. */
57 private int _transX;
59 /** The current Y translation. */
60 private int _transY;
62 /**
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.
69 * @since 2022/02/25
71 public ProxyGraphics(ProxyGraphicsTarget __target, int __width,
72 int __height)
73 throws NullPointerException
75 if (__target == null)
76 throw new NullPointerException("NARG");
78 this.target = __target;
79 this._clipWidth = __width;
80 this._clipHeight = __height;
83 /**
84 * {@inheritDoc}
85 * @since 2022/02/25
87 @Override
88 public void clipRect(int __a, int __b, int __c, int __d)
90 throw Debugging.todo();
93 /**
94 * {@inheritDoc}
95 * @since 2022/02/25
97 @Override
98 public void copyArea(int __a, int __b, int __c, int __d, int __e,
99 int __f, int __g)
101 this.__graphics().copyArea(__a, __b, __c, __d, __e, __f, __g);
105 * {@inheritDoc}
106 * @since 2022/02/25
108 @Override
109 public void drawArc(int __a, int __b, int __c, int __d, int __e,
110 int __f)
112 this.__graphics().drawArc(__a, __b, __c, __d, __e, __f);
116 * {@inheritDoc}
117 * @since 2022/02/25
119 @Override
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,
124 __h);
128 * {@inheritDoc}
129 * @since 2022/02/25
131 @Override
132 public void drawChar(char __a, int __b, int __c, int __d)
134 this.__graphics().drawChar(__a, __b, __c, __d);
138 * {@inheritDoc}
139 * @since 2022/02/25
141 @Override
142 public void drawChars(char[] __a, int __b, int __c, int __d,
143 int __e, int __f)
145 this.__graphics().drawChars(__a, __b, __c, __d, __e, __f);
149 * {@inheritDoc}
150 * @since 2022/02/25
152 @Override
153 public void drawImage(Image __a, int __b, int __c, int __d)
155 this.__graphics().drawImage(__a, __b, __c, __d);
159 * {@inheritDoc}
160 * @since 2022/02/25
162 @Override
163 public void drawLine(int __a, int __b, int __c, int __d)
165 this.__graphics().drawLine(__a, __b, __c, __d);
169 * {@inheritDoc}
170 * @since 2022/02/25
172 @Override
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);
180 * {@inheritDoc}
181 * @since 2022/02/25
183 @Override
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);
191 * {@inheritDoc}
192 * @since 2022/02/25
194 @Override
195 public void drawRect(int __a, int __b, int __c, int __d)
197 this.__graphics().drawRect(__a, __b, __c, __d);
201 * {@inheritDoc}
202 * @since 2022/02/25
204 @Override
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,
209 __i);
213 * {@inheritDoc}
214 * @since 2022/02/25
216 @Override
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);
226 * {@inheritDoc}
227 * @since 2022/02/25
229 @Override
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,
234 __arcHeight);
238 * {@inheritDoc}
239 * @since 2022/02/25
241 @Override
242 public void drawString(String __a, int __b, int __c, int __d)
244 this.__graphics().drawString(__a, __b, __c, __d);
248 * {@inheritDoc}
249 * @since 2022/02/25
251 @Override
252 public void drawSubstring(String __a, int __b, int __c, int __d,
253 int __e, int __f)
255 this.__graphics().drawSubstring(__a, __b, __c, __d, __e, __f);
259 * {@inheritDoc}
260 * @since 2022/02/25
262 @Override
263 public void drawText(Text __t, int __x, int __y)
265 this.__graphics().drawText(__t, __x, __y);
269 * {@inheritDoc}
270 * @since 2022/02/25
272 @Override
273 public void fillArc(int __a, int __b, int __c, int __d, int __e,
274 int __f)
276 this.__graphics().fillArc(__a, __b, __c, __d, __e, __f);
280 * {@inheritDoc}
281 * @since 2022/02/25
283 @Override
284 public void fillRect(int __x, int __y, int __w, int __h)
286 this.__graphics().fillRect(__x, __y, __w, __h);
290 * {@inheritDoc}
291 * @since 2022/02/25
293 @Override
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,
298 __arcHeight);
302 * {@inheritDoc}
303 * @since 2022/02/25
305 @Override
306 public void fillTriangle(int __a, int __b, int __c, int __d,
307 int __e, int __f)
309 this.__graphics().fillTriangle(__a, __b, __c, __d, __e, __f);
313 * {@inheritDoc}
314 * @since 2022/02/25
316 @Override
317 public int getAlpha()
319 return (this._argbColor >> 24) & 0xFF;
323 * {@inheritDoc}
324 * @since 2017/02/10
326 @Override
327 public int getAlphaColor()
329 return this._argbColor;
333 * {@inheritDoc}
334 * @since 2022/02/25
336 @Override
337 public int getBlendingMode()
339 return this._blendingMode;
343 * {@inheritDoc}
344 * @since 2022/02/25
346 @Override
347 public int getBlueComponent()
349 return (this._argbColor) & 0xFF;
353 * {@inheritDoc}
354 * @since 2022/02/25
356 @Override
357 public int getClipHeight()
359 return this._clipHeight;
363 * {@inheritDoc}
364 * @since 2022/02/25
366 @Override
367 public int getClipWidth()
369 return this._clipWidth;
373 * {@inheritDoc}
374 * @since 2022/02/25
376 @Override
377 public int getClipX()
379 return this._clipX - this._transX;
383 * {@inheritDoc}
384 * @since 2022/02/25
386 @Override
387 public int getClipY()
389 return this._clipY - this._transY;
393 * {@inheritDoc}
394 * @since 2022/02/25
396 @Override
397 public int getColor()
399 return this._argbColor & 0xFFFFFF;
403 * {@inheritDoc}
404 * @since 2022/02/25
406 @Override
407 public int getDisplayColor(int __a)
409 return this.__graphics().getDisplayColor(__a);
413 * {@inheritDoc}
414 * @since 2022/02/25
416 @Override
417 public Font getFont()
419 return this.__font();
423 * {@inheritDoc}
424 * @since 2022/02/25
426 @Override
427 public int getGrayScale()
429 return (((this._argbColor >> 16) & 0xFF) +
430 ((this._argbColor >> 8) & 0xFF) +
431 ((this._argbColor) & 0xFF)) / 3;
435 * {@inheritDoc}
436 * @since 2022/02/25
438 @Override
439 public int getGreenComponent()
441 return (this._argbColor >> 8) & 0xFF;
445 * {@inheritDoc}
446 * @since 2022/02/25
448 @Override
449 public int getRedComponent()
451 return (this._argbColor >> 16) & 0xFF;
455 * {@inheritDoc}
456 * @since 2022/02/25
458 @Override
459 public int getStrokeStyle()
461 return this._strokeStyle;
465 * {@inheritDoc}
466 * @since 2022/02/25
468 @Override
469 public int getTranslateX()
471 return this._transX;
475 * {@inheritDoc}
476 * @since 2022/02/25
478 @Override
479 public int getTranslateY()
481 return this._transY;
485 * {@inheritDoc}
486 * @since 2022/02/25
488 @Override
489 public void setAlpha(int __a)
491 this.setAlphaColor(__a,
492 this.getRedComponent(),
493 this.getGreenComponent(),
494 this.getBlueComponent());
498 * {@inheritDoc}
499 * @since 2022/02/25
501 @Override
502 public void setAlphaColor(int __argb)
504 this._argbColor = __argb;
508 * {@inheritDoc}
509 * @since 2022/02/25
511 @Override
512 public void setAlphaColor(int __a, int __r, int __g, int __b)
514 /* {@squirreljme.error EB2y Color out of range. (Alpha; Red; Green;
515 Blue)} */
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));
521 // Set
522 this.setAlphaColor((__a << 24) | (__r << 16) | (__g << 8) | __b);
526 * {@inheritDoc}
527 * @since 2022/02/25
529 @Override
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);
536 // Cache locally
537 this._blendingMode = __m;
541 * {@inheritDoc}
542 * @since 2022/02/25
544 @Override
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;
553 // Normalize X
554 if (endX < startX)
556 int temp = endX;
557 endX = startX;
558 startX = temp;
561 // Normalize Y
562 if (endY < startY)
564 int temp = endY;
565 endY = startY;
566 startY = temp;
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);
575 // Record internally
576 this._clipX = clipX;
577 this._clipY = clipY;
578 this._clipWidth = clipEndX - clipX;
579 this._clipHeight = clipEndY - clipY;
583 * {@inheritDoc}
584 * @since 2022/02/25
586 @Override
587 public void setColor(int __rgb)
589 this.setAlphaColor((this.getAlphaColor() & 0xFF_000000) |
590 (__rgb & 0x00_FFFFFF));
594 * {@inheritDoc}
595 * @since 2022/02/25
597 @Override
598 public void setColor(int __r, int __g, int __b)
600 this.setAlphaColor(this.getAlpha(), __r, __g, __b);
604 * {@inheritDoc}
605 * @since 2022/02/25
607 @Override
608 public void setFont(Font __font)
610 this.__graphics().setFont(__font);
614 * {@inheritDoc}
615 * @since 2022/02/25
617 @Override
618 public void setGrayScale(int __v)
620 this.setAlphaColor(this.getAlpha(), __v, __v, __v);
624 * {@inheritDoc}
625 * @since 2022/02/25
626 * @param __style
628 @Override
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");
635 // Set
636 this._strokeStyle = __style;
640 * {@inheritDoc}
641 * @since 2022/02/25
643 @Override
644 public void translate(int __x, int __y)
646 // Cache locally
647 this._transX += __x;
648 this._transY += __y;
652 * Returns the font that should be used.
654 * @return The font used.
655 * @since 2022/02/25
657 private Font __font()
659 Font rv = this._font;
660 if (rv == null)
661 rv = Font.getDefaultFont();
662 return rv;
666 * Initializes and returns the target graphics accordingly.
668 * @return The resultant graphics object.
669 * @since 2022/02/25
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
687 target.setClip(
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
694 return target;